Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redshift connector cannot read timestamp columns in >v341 #6607

Closed
grantatspothero opened this issue Jan 14, 2021 · 5 comments
Closed

Redshift connector cannot read timestamp columns in >v341 #6607

grantatspothero opened this issue Jan 14, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@grantatspothero
Copy link
Contributor

Querying columns of type timestamp fails in trino versions >v345.

--created is a timestamp column in redshift
select created 
from redshift.schema.table

with the stacktrace:

io.prestosql.spi.PrestoException: Could not serialize column 'created' of type 'timestamp(3)' at position 1:1
	...
Caused by: java.lang.IllegalArgumentException: Expected 0s for digits beyond precision 3: epochMicros = 1545228016441221
	at io.prestosql.spi.type.SqlTimestamp.newInstance(SqlTimestamp.java:51)
	at io.prestosql.spi.type.ShortTimestampType.getObjectValue(ShortTimestampType.java:125)
	at io.prestosql.server.protocol.QueryResultRows.getRowValues(QueryResultRows.java:176)
	... 44 more

I think this is because the redshift connector uses the basejdbc connector, which maps the timestamp redshift type to the TIMESTAMP_MILLIS trino timestamp type. However, the redshift timestamp type has 6 digits of precision (not the 3 trino is expecting).

case Types.TIMESTAMP:
// TODO default to `timestampColumnMapping`
return Optional.of(timestampColumnMappingUsingSqlTimestamp(TIMESTAMP_MILLIS));

https://docs.aws.amazon.com/redshift/latest/dg/r_Datetime_types.html#r_Datetime_types-timestamp

This started causing queries to fail in v346 when this PR was introduced: #5742 Previously SqlTimestamp. newInstance would round the timestamp when the precision did not match the expected precision, now it explicitly fails.

I think the fix should just be overriding toColumnMapping in the redshift client to handle timestamps with 6 digits of precision.

See this thread for context:
https://trinodb.slack.com/archives/CGB0QHWSW/p1610645172011400

@findepi findepi added the bug Something isn't working label Jan 15, 2021
@findepi findepi changed the title Redshift connector cannot read timestamp columns in >v345 Redshift connector cannot read timestamp columns in >v341 Jan 15, 2021
@findepi
Copy link
Member

findepi commented Jan 15, 2021

There is lack of rounding in the default column mapping being returned here

return Optional.of(timestampColumnMappingUsingSqlTimestamp(TIMESTAMP_MILLIS));

which is

public static long toPrestoTimestamp(TimestampType timestampType, LocalDateTime localDateTime)
{
long precision = timestampType.getPrecision();
checkArgument(precision <= MAX_SHORT_PRECISION, "Precision is out of range: %s", precision);
Instant instant = localDateTime.atZone(UTC).toInstant();
return instant.getEpochSecond() * MICROSECONDS_PER_SECOND + roundDiv(instant.getNano(), NANOSECONDS_PER_MICROSECOND);
}

The code went in 342.
In 346 we probably added validation (didn't check exact), but the behavior in 342-345 was silently incorrect leading to incorrect query results.

@grantatspothero
Copy link
Contributor Author

@findepi when you say "silently leading to incorrect query results" do you mean "silently rounding the precision of the timestamp" or do you mean "completely wrong timestamp values". Just trying to grasp the severity of the bug.

@findepi
Copy link
Member

findepi commented Jan 15, 2021

@grantatspothero for just SELECT queries it was returning correct results
but if the value was compared with something, or aggregated, it could return incorrect ones.
Of course, i am not saying this is the common case for timestamps, i am just pointing out #5742 is not to the root cause.

@tooptoop4
Copy link
Contributor

@ebyhr
Copy link
Member

ebyhr commented Dec 12, 2022

Closing as #15365

@ebyhr ebyhr closed this as completed Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

4 participants