-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7559] SqlParserUtil.parseTimeTzLiteral should reject unknown… #4986
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -371,16 +371,24 @@ public static SqlTimeTzLiteral parseTimeTzLiteral( | |
| final String timeZone = s.substring(lastSpace + 1); | ||
| final String time = s.substring(0, lastSpace); | ||
|
|
||
| final TimeZone tz = TimeZone.getTimeZone(timeZone); | ||
| if (tz != null) { | ||
| try { | ||
| ZoneId zoneId = ZoneId.of(timeZone); | ||
| TimeZone tz = TimeZone.getTimeZone(zoneId); | ||
| pt = | ||
| DateTimeUtils.parsePrecisionDateTimeLiteral(time, Format.get().time, tz, -1); | ||
| } catch (DateTimeException e) { | ||
| String message = e.getMessage(); | ||
| if (message == null) { | ||
| message = "Error parsing TIME ZONE"; | ||
| } | ||
| throw SqlUtil.newContextException(pos, | ||
| RESOURCE.illegalLiteral("TIME WITH TIME ZONE", s, message)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here. |
||
| } | ||
| } | ||
| if (pt == null) { | ||
| throw SqlUtil.newContextException(pos, | ||
| RESOURCE.illegalLiteral("TIME WITH TIME ZONE", s, | ||
| RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str())); | ||
| RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING + " zone").str())); | ||
| } | ||
| final TimeWithTimeZoneString t = TimeWithTimeZoneString.fromCalendarFields(pt.getCalendar()) | ||
| .withFraction(pt.getFraction()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import org.apache.calcite.avatica.util.TimeUnit; | ||
| import org.apache.calcite.runtime.CalciteContextException; | ||
| import org.apache.calcite.sql.SqlIntervalQualifier; | ||
| import org.apache.calcite.sql.SqlTimeTzLiteral; | ||
| import org.apache.calcite.sql.SqlTimestampTzLiteral; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -94,6 +95,33 @@ public class SqlParserUtilTest { | |
| } | ||
| } | ||
|
|
||
| @Test void testTimeWithTimeZone() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd better add jira link here which show Jira this test is trying to solve.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, please see other examples for the formatting used for JIRA test cases. |
||
| SqlParserPos pos = new SqlParserPos(2, 3); | ||
| SqlTimeTzLiteral lit = | ||
| SqlParserUtil.parseTimeTzLiteral("10:10:10 GMT", pos); | ||
| assertThat(lit, hasToString("TIME WITH TIME ZONE '10:10:10 UTC'")); | ||
|
|
||
| // Like parseTimestampTzLiteral, parseTimeTzLiteral should reject unknown | ||
| // time zones instead of silently falling back to GMT. | ||
| try { | ||
| SqlParserUtil.parseTimeTzLiteral("10:10:10 incorrect_zone", pos); | ||
| fail("Should be unreachable"); | ||
| } catch (CalciteContextException ex) { | ||
| assertThat( | ||
| ex.getMessage(), is("At line 2, column 3: Illegal TIME WITH TIME ZONE literal " | ||
| + "'10:10:10 incorrect_zone': Unknown time-zone ID: incorrect_zone")); | ||
| } | ||
|
|
||
| try { | ||
| SqlParserUtil.parseTimeTzLiteral("10:10:10", pos); | ||
| fail("Should be unreachable"); | ||
| } catch (CalciteContextException ex) { | ||
| assertThat( | ||
| ex.getMessage(), is("At line 2, column 3: Illegal TIME WITH TIME ZONE literal " | ||
| + "'10:10:10': not in format 'HH:mm:ss zone'")); | ||
| } | ||
| } | ||
|
|
||
| @Test void testMinuteToSecondIntervalToMillis() { | ||
| final SqlIntervalQualifier qualifier = | ||
| new SqlIntervalQualifier(TimeUnit.MINUTE, TimeUnit.SECOND, POSITION); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improper capitalization => "Error parsing timezone"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think this is fine, since this is how the SQL type is spelled