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

Validate time with time zone well-formedness #9280

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import static io.trino.spi.type.TimeType.MAX_PRECISION;
import static io.trino.spi.type.Timestamps.MINUTES_PER_HOUR;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_HOUR;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MINUTE;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_SECOND;
import static io.trino.spi.type.Timestamps.POWERS_OF_TEN;
import static io.trino.spi.type.Timestamps.SECONDS_PER_MINUTE;
import static io.trino.spi.type.Timestamps.rescale;
import static java.lang.Math.abs;
import static java.lang.String.format;

Expand All @@ -35,6 +37,20 @@ public final class SqlTimeWithTimeZone

public static SqlTimeWithTimeZone newInstance(int precision, long picoseconds, int offsetMinutes)
{
if (precision < 0 || precision > 12) {
findepi marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Invalid precision: " + precision);
}
if (rescale(rescale(picoseconds, 12, precision), precision, 12) != picoseconds) {
throw new IllegalArgumentException(format("picoseconds contains data beyond specified precision (%s): %s", precision, picoseconds));
}
if (picoseconds < 0 || picoseconds >= PICOSECONDS_PER_DAY) {
throw new IllegalArgumentException("picoseconds is out of range: " + picoseconds);
}
// TIME WITH TIME ZONE's valid offsets are [-14:00, 14:00]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we enforce it already (just in a more ugly way during query processing). Or will some queries which used to work may start failing now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I checked literal form prior, but that's not enough. Filed #9288.

if (offsetMinutes < -14 * 60 || offsetMinutes > 14 * 60) {
throw new IllegalArgumentException("offsetMinutes is out of range: " + offsetMinutes);
}

return new SqlTimeWithTimeZone(precision, picoseconds, offsetMinutes);
}

Expand Down