trino: Proper truncation missing in casts to varchar(x)

presto:default> select cast(bigint '12345678' as varchar(2));
 _col0
-------
 12

but …

presto:default> select cast(bigint '12345678' as varchar(2)) = '12';
 _col0
-------
 false

This is because https://github.com/prestosql/presto/blob/c74b775e103a7a239aafef1b7ca15fe6370495fe/presto-main/src/main/java/io/prestosql/type/BigintOperators.java#L270-L276 should consider x (@LiteralParameter("x") long x) and truncate the result to make sure no more than x characters are returned.

Same probably applies to casts from other types to varchar.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 16 (15 by maintainers)

Most upvoted comments

Is someone already working on this? I would like to give it a try…

I thought changing 1.0 to 1 would be losing precision

I think you can think of any floating point as having infinite number of trailing zeros. In this context you won’t lose any precision.

@ankitdixit each case needs to be looked at separately.

  • in some cases truncation should be applied
  • in some cases exception should be raised
  • I imagine, in some cases the outcome depends on actual value being converted (truncation or exception)

If you want to work on this, I would propose to raise a separate PR for each case being fixed. (all integer types {{tinyint,smallint,integer,bigint}} should be considered as one case; same for floating point: {{real,double}})