You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "zzwqqq (via GitHub)" <gi...@apache.org> on 2026/05/14 15:32:18 UTC
[PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
zzwqqq opened a new pull request, #4942:
URL: https://github.com/apache/calcite/pull/4942
<!--
Thanks for sending a pull request! Here are some critical tips for you:
1. READ THE GUIDE FIRST: https://calcite.apache.org/develop/#contributing
*For significant contributions, please discuss on the dev mailing list or Jira BEFORE coding.*
2. JIRA IS USUALLY MANDATORY: Ensure you have created an issue on the Calcite Jira:
https://issues.apache.org/jira/projects/CALCITE/issues
*Check existing issues first to avoid duplicates.*
*Note: A Jira is NOT required for typos and cosmetic changes (i.e., changes that are neither bugs nor features).*
3. TIMING: Strongly recommended to create the Jira BEFORE you start writing code (e.g., a day or so before posting a PR).
This gives others a chance to weigh in on your specification.
4. CRITICAL CONSISTENCY RULE
The following three items MUST match exactly in wording and meaning:
(A) The Jira Issue Title
(B) This Pull Request Title
(C) Your Git Commit Message
Format: [CALCITE-XXXX] <Description>
Example: [CALCITE-0000] Add IF NOT EXISTS clause to CREATE TABLE
Guidelines for a good Title:
- Illustrate using SQL keywords (in ALL-CAPS) rather than Java method names.
- Focus on the specification and user experience, not the implementation.
- Make it clear whether it is a bug or a feature.
- Use words like "should" to indicate desired behavior vs. current behavior (e.g., "Validator should not close model file").
5. REPRODUCTION: If fixing a bug, please provide a concise SQL example or test case to reproduce the issue for a faster review.
6. TESTING: Ensure `./gradlew build` passes and appropriate tests are added/updated.
-->
## Jira Link
[CALCITE-7529](https://issues.apache.org/jira/browse/CALCITE-7529)
## Changes Proposed
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR.
-->
Fix constant reduction of high-precision `TIME` and `TIMESTAMP` literals.
`RexExecutorImpl` evaluates constants using generated Java code, where `TIME` and `TIMESTAMP` use millisecond-based runtime values. This can drop digits beyond milliseconds when a custom `RelDataTypeSystem` allows precision greater than 3.
This PR avoids that loss by keeping high-precision temporal literal casts as Rex literals when possible, and by leaving existing `RexLiteral`s unchanged during reduction.
Added regression tests for `TIME(6)`, `TIMESTAMP(6)`, existing temporal literals, and `TIME(6)` to `VARCHAR`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3283113469
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -928,32 +924,26 @@ public RexNode makeCast(
case TIME:
final SqlTimeLiteral timeLiteral =
SqlParserUtil.parseTimeLiteral(value, pos);
- if (!isSubMillisecondLiteral(timeLiteral, value)) {
+ if (timeLiteral.getPrec() <= 0
Review Comment:
I understand what 0 means, but negative precisions are less clear.
There's "PRECISION_NOT_SPECIFIED", but I'd rather see an explicit check for that if that's what you are looking for.
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -969,54 +959,31 @@ public RexNode makeCast(
return temporalLiteral;
}
- private static @Nullable String formatTemporalLiteral(RexLiteral literal) {
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ final String value;
switch (literal.getType().getSqlTypeName()) {
case TIME:
final TimeString time = literal.getValueAs(TimeString.class);
- return time == null || !hasSubMillisecondPrecision(time)
- ? null
- : time.toString(precision(literal.getType()));
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(literal.getType().getPrecision());
+ break;
case TIMESTAMP:
final TimestampString timestamp = literal.getValueAs(TimestampString.class);
- return timestamp == null || !hasSubMillisecondPrecision(timestamp)
- ? null
- : timestamp.toString(precision(literal.getType()));
+ if (timestamp == null || literal.getType().getPrecision() <= 3) {
Review Comment:
Why is 3 hardwired here?
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -969,54 +959,31 @@ public RexNode makeCast(
return temporalLiteral;
}
- private static @Nullable String formatTemporalLiteral(RexLiteral literal) {
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ final String value;
switch (literal.getType().getSqlTypeName()) {
case TIME:
final TimeString time = literal.getValueAs(TimeString.class);
- return time == null || !hasSubMillisecondPrecision(time)
- ? null
- : time.toString(precision(literal.getType()));
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(literal.getType().getPrecision());
+ break;
case TIMESTAMP:
final TimestampString timestamp = literal.getValueAs(TimestampString.class);
- return timestamp == null || !hasSubMillisecondPrecision(timestamp)
- ? null
- : timestamp.toString(precision(literal.getType()));
+ if (timestamp == null || literal.getType().getPrecision() <= 3) {
+ return null;
+ }
+ value = timestamp.toString(literal.getType().getPrecision());
+ break;
default:
return null;
}
- }
-
- private static boolean hasSubMillisecondPrecision(TimeString time) {
- return hasFractionPrecisionBeyond(time.toString(), 3);
- }
-
- private static boolean hasSubMillisecondPrecision(TimestampString timestamp) {
- return hasFractionPrecisionBeyond(timestamp.toString(), 3);
- }
-
- private static boolean hasFractionPrecisionBeyond(String value, int precision) {
- final int dot = value.indexOf('.');
- return dot >= 0 && value.length() - dot - 1 > precision;
- }
-
- private static boolean isSubMillisecondLiteral(
- SqlAbstractDateTimeLiteral literal,
- String value) {
- return literal.getPrec() > 3 && literal.toFormattedString().equals(value);
- }
-
- private static int precision(RelDataType type) {
- return type.getPrecision() < 0 ? 0 : type.getPrecision();
- }
-
- private static boolean fitsInCharacterType(RelDataType type, String value) {
- switch (type.getSqlTypeName()) {
- case CHAR:
- case VARCHAR:
- return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0;
- default:
- return false;
- }
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
Review Comment:
please add a comment explaining what happens here
##########
core/src/test/java/org/apache/calcite/rex/RexExecutorTest.java:
##########
@@ -160,6 +164,171 @@ protected void check(final Action action) {
});
}
+ @Test void testReduceTimeCastWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexNode cast =
+ rexBuilder.makeCast(
+ rexBuilder.getTypeFactory().createSqlType(SqlTypeName.TIME, 6),
+ rexBuilder.makeLiteral("12:34:56.123456"));
+
+ final RexNode reduced = reduce(rexBuilder, executor, cast).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(
+ ((RexLiteral) reduced).getValueAs(TimeString.class).toString(6),
+ equalTo("12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimestampCastWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexNode cast =
+ rexBuilder.makeCast(
+ rexBuilder.getTypeFactory().createSqlType(SqlTypeName.TIMESTAMP, 6),
+ rexBuilder.makeLiteral("2020-01-01 12:34:56.123456"));
+
+ final RexNode reduced = reduce(rexBuilder, executor, cast).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(
+ ((RexLiteral) reduced).getValueAs(TimestampString.class).toString(6),
+ equalTo("2020-01-01 12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceLiteralWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexLiteral timeLiteral =
+ rexBuilder.makeTimeLiteral(new TimeString("12:34:56.123456"), 6);
+ final RexLiteral timestampLiteral =
+ rexBuilder.makeTimestampLiteral(
+ new TimestampString("2020-01-01 12:34:56.123456"), 6);
+ final RexNode expression =
+ rexBuilder.makeCall(SqlStdOperatorTable.PLUS,
+ rexBuilder.makeExactLiteral(BigDecimal.TEN),
+ rexBuilder.makeExactLiteral(BigDecimal.ONE));
+
+ final List<RexNode> reducedValues =
+ reduce(rexBuilder, executor, timeLiteral, expression, timestampLiteral);
+
+ assertThat(reducedValues, hasSize(3));
+ assertThat(
+ ((RexLiteral) reducedValues.get(0)).getValueAs(TimeString.class).toString(6),
+ equalTo("12:34:56.123456"));
+ assertThat(((RexLiteral) reducedValues.get(1)).getValue2(), equalTo(11L));
+ assertThat(
+ ((RexLiteral) reducedValues.get(2)).getValueAs(TimestampString.class)
+ .toString(6),
+ equalTo("2020-01-01 12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 6),
+ rexBuilder.makeLiteral("12:34:56.123456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithMilliseconds() {
+ check((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 3),
+ rexBuilder.makeLiteral("12:34:56.987654"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.987"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithZeroMilliseconds() {
+ check((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 3),
+ rexBuilder.makeLiteral("12:34:56.000456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.000"));
+ });
+ }
+
+ @Test void testReduceTimestampToVarcharWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTimestamp =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 6),
+ rexBuilder.makeLiteral("2020-01-01 12:34:56.000456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTimestamp);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("2020-01-01 12:34:56.000456"));
+ });
+ }
+
+ private static void checkHighPrecision(Action action) {
+ action.check(new RexBuilder(highPrecisionTemporalTypeFactory()), executor());
+ }
+
+ private static List<RexNode> reduce(RexBuilder rexBuilder,
+ RexExecutorImpl executor, RexNode... nodes) {
+ final List<RexNode> reducedValues = new ArrayList<>();
+ executor.reduce(rexBuilder, ImmutableList.copyOf(nodes), reducedValues);
+ return reducedValues;
+ }
+
+ private static RelDataTypeFactory highPrecisionTemporalTypeFactory() {
+ return new JavaTypeFactoryImpl(
Review Comment:
Can you run tests with multiple precisions, checking for precisions 1..9 for example in a loop?
##########
core/src/main/java/org/apache/calcite/rex/RexExecutorImpl.java:
##########
@@ -136,14 +137,32 @@ public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> e
@Override public void reduce(RexBuilder rexBuilder, List<RexNode> constExps,
List<RexNode> reducedValues) {
assert reducedValues.isEmpty();
+ final List<RexNode> exps = new ArrayList<>();
Review Comment:
This deserves a comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3300922141
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -958,6 +958,11 @@ public RexNode makeCast(
return temporalLiteral;
}
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal type is not supported, the literal value is
+ * unavailable, or the formatted temporal value does not fit in the target
Review Comment:
Thanks, I removed the caller behavior from the JavaDoc and made the null-return cases more specific.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3295979029
##########
core/src/main/java/org/apache/calcite/util/BuiltInMethod.java:
##########
@@ -784,8 +779,12 @@ public enum BuiltInMethod {
String.class, int.class),
UNIX_DATE_TO_STRING(DateTimeUtils.class, "unixDateToString", int.class),
UNIX_TIME_TO_STRING(DateTimeUtils.class, "unixTimeToString", int.class),
+ UNIX_TIME_TO_STRING_WITH_PRECISION(DateTimeUtils.class, "unixTimeToString",
Review Comment:
Right. I ran into this while debugging `CalciteSqlOperatorTest.testConcatWSFunc`. This is still limited by the runtime representation; a lot of the enumerable path uses milliseconds / Calendar internally, so this only preserves up to 3 fractional digits today.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3299959398
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -958,6 +958,11 @@ public RexNode makeCast(
return temporalLiteral;
}
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal type is not supported, the literal value is
+ * unavailable, or the formatted temporal value does not fit in the target
Review Comment:
Please do not document what the caller does, it's not the job of the callee. Other callers may be added in the future.
What is "value is unavailable"?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292904162
##########
core/src/test/java/org/apache/calcite/rex/RexExecutorTest.java:
##########
@@ -160,6 +164,171 @@ protected void check(final Action action) {
});
}
+ @Test void testReduceTimeCastWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexNode cast =
+ rexBuilder.makeCast(
+ rexBuilder.getTypeFactory().createSqlType(SqlTypeName.TIME, 6),
+ rexBuilder.makeLiteral("12:34:56.123456"));
+
+ final RexNode reduced = reduce(rexBuilder, executor, cast).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(
+ ((RexLiteral) reduced).getValueAs(TimeString.class).toString(6),
+ equalTo("12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimestampCastWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexNode cast =
+ rexBuilder.makeCast(
+ rexBuilder.getTypeFactory().createSqlType(SqlTypeName.TIMESTAMP, 6),
+ rexBuilder.makeLiteral("2020-01-01 12:34:56.123456"));
+
+ final RexNode reduced = reduce(rexBuilder, executor, cast).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(
+ ((RexLiteral) reduced).getValueAs(TimestampString.class).toString(6),
+ equalTo("2020-01-01 12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceLiteralWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RexLiteral timeLiteral =
+ rexBuilder.makeTimeLiteral(new TimeString("12:34:56.123456"), 6);
+ final RexLiteral timestampLiteral =
+ rexBuilder.makeTimestampLiteral(
+ new TimestampString("2020-01-01 12:34:56.123456"), 6);
+ final RexNode expression =
+ rexBuilder.makeCall(SqlStdOperatorTable.PLUS,
+ rexBuilder.makeExactLiteral(BigDecimal.TEN),
+ rexBuilder.makeExactLiteral(BigDecimal.ONE));
+
+ final List<RexNode> reducedValues =
+ reduce(rexBuilder, executor, timeLiteral, expression, timestampLiteral);
+
+ assertThat(reducedValues, hasSize(3));
+ assertThat(
+ ((RexLiteral) reducedValues.get(0)).getValueAs(TimeString.class).toString(6),
+ equalTo("12:34:56.123456"));
+ assertThat(((RexLiteral) reducedValues.get(1)).getValue2(), equalTo(11L));
+ assertThat(
+ ((RexLiteral) reducedValues.get(2)).getValueAs(TimestampString.class)
+ .toString(6),
+ equalTo("2020-01-01 12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 6),
+ rexBuilder.makeLiteral("12:34:56.123456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.123456"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithMilliseconds() {
+ check((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 3),
+ rexBuilder.makeLiteral("12:34:56.987654"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.987"));
+ });
+ }
+
+ @Test void testReduceTimeToVarcharWithZeroMilliseconds() {
+ check((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTime =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIME, 3),
+ rexBuilder.makeLiteral("12:34:56.000456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTime);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("12:34:56.000"));
+ });
+ }
+
+ @Test void testReduceTimestampToVarcharWithMicroseconds() {
+ checkHighPrecision((rexBuilder, executor) -> {
+ final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
+ final RexNode castToTimestamp =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 6),
+ rexBuilder.makeLiteral("2020-01-01 12:34:56.000456"));
+ final RexNode castToVarchar =
+ rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.VARCHAR, 30),
+ castToTimestamp);
+
+ final RexNode reduced = reduce(rexBuilder, executor, castToVarchar).get(0);
+
+ assertThat(reduced, instanceOf(RexLiteral.class));
+ assertThat(((RexLiteral) reduced).getValueAs(String.class),
+ equalTo("2020-01-01 12:34:56.000456"));
+ });
+ }
+
+ private static void checkHighPrecision(Action action) {
+ action.check(new RexBuilder(highPrecisionTemporalTypeFactory()), executor());
+ }
+
+ private static List<RexNode> reduce(RexBuilder rexBuilder,
+ RexExecutorImpl executor, RexNode... nodes) {
+ final List<RexNode> reducedValues = new ArrayList<>();
+ executor.reduce(rexBuilder, ImmutableList.copyOf(nodes), reducedValues);
+ return reducedValues;
+ }
+
+ private static RelDataTypeFactory highPrecisionTemporalTypeFactory() {
+ return new JavaTypeFactoryImpl(
Review Comment:
Expanded these to run through precisions 1..9. I did the same for the temporal-to-VARCHAR cases
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3295979604
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Replace the CAST with a character literal only if the formatted temporal
+ // value fits in the target type; otherwise leave the CAST unchanged.
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
+ ? makeLiteral(value, type, true)
+ : null;
+ }
+
+ private static boolean isExactFractionalSecondLiteral(
Review Comment:
Added a short JavaDoc. This checks that the parsed literal has explicit fractional-second precision and still formats back to the original string.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3245844160
##########
core/src/main/java/org/apache/calcite/rex/RexExecutorImpl.java:
##########
@@ -136,14 +137,32 @@ public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> e
@Override public void reduce(RexBuilder rexBuilder, List<RexNode> constExps,
List<RexNode> reducedValues) {
assert reducedValues.isEmpty();
+ final List<RexNode> exps = new ArrayList<>();
Review Comment:
what happens here? you are only reducing expressions which are already literals?
This seems to be some kind of regression - other constant expressions are not reduced?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3295981622
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
Review Comment:
Added JavaDoc here, including the null-return cases where the caller keeps the regular CAST.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4599352868
> I think the commits can be squashed so we can merge
Thanks. I've squashed the commits.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3306993951
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,124 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal is not a TIME or TIMESTAMP, the literal
+ * cannot be read as the corresponding temporal string value, or the formatted
+ * temporal value does not fit in the target character type. */
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
Review Comment:
I don't understand why this function is called `makeCast`, I don't see any cast operator being created in this function
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,124 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal is not a TIME or TIMESTAMP, the literal
+ * cannot be read as the corresponding temporal string value, or the formatted
+ * temporal value does not fit in the target character type. */
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Only create a character literal if the formatted temporal value fits in
+ // the target type.
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
+ ? makeLiteral(value, type, true)
+ : null;
+ }
+
+ /** Returns whether a parsed TIME or TIMESTAMP literal has explicit
+ * fractional-second precision and preserves the original character value
Review Comment:
with a precision high enough to preserve the original value exactly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292948712
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -969,54 +959,31 @@ public RexNode makeCast(
return temporalLiteral;
}
- private static @Nullable String formatTemporalLiteral(RexLiteral literal) {
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ final String value;
switch (literal.getType().getSqlTypeName()) {
case TIME:
final TimeString time = literal.getValueAs(TimeString.class);
- return time == null || !hasSubMillisecondPrecision(time)
- ? null
- : time.toString(precision(literal.getType()));
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(literal.getType().getPrecision());
+ break;
case TIMESTAMP:
final TimestampString timestamp = literal.getValueAs(TimestampString.class);
- return timestamp == null || !hasSubMillisecondPrecision(timestamp)
- ? null
- : timestamp.toString(precision(literal.getType()));
+ if (timestamp == null || literal.getType().getPrecision() <= 3) {
+ return null;
+ }
+ value = timestamp.toString(literal.getType().getPrecision());
+ break;
default:
return null;
}
- }
-
- private static boolean hasSubMillisecondPrecision(TimeString time) {
- return hasFractionPrecisionBeyond(time.toString(), 3);
- }
-
- private static boolean hasSubMillisecondPrecision(TimestampString timestamp) {
- return hasFractionPrecisionBeyond(timestamp.toString(), 3);
- }
-
- private static boolean hasFractionPrecisionBeyond(String value, int precision) {
- final int dot = value.indexOf('.');
- return dot >= 0 && value.length() - dot - 1 > precision;
- }
-
- private static boolean isSubMillisecondLiteral(
- SqlAbstractDateTimeLiteral literal,
- String value) {
- return literal.getPrec() > 3 && literal.toFormattedString().equals(value);
- }
-
- private static int precision(RelDataType type) {
- return type.getPrecision() < 0 ? 0 : type.getPrecision();
- }
-
- private static boolean fitsInCharacterType(RelDataType type, String value) {
- switch (type.getSqlTypeName()) {
- case CHAR:
- case VARCHAR:
- return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0;
- default:
- return false;
- }
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
Review Comment:
Added a note here about when this can replace the CAST with a literal.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4456409836
## [](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [88.7% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_duplicated_lines_density&view=list)
<!-- sqra-placement-anchor -->
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292905496
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -928,32 +924,26 @@ public RexNode makeCast(
case TIME:
final SqlTimeLiteral timeLiteral =
SqlParserUtil.parseTimeLiteral(value, pos);
- if (!isSubMillisecondLiteral(timeLiteral, value)) {
+ if (timeLiteral.getPrec() <= 0
Review Comment:
Switched this to check PRECISION_NOT_SPECIFIED explicitly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292904997
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -969,54 +959,31 @@ public RexNode makeCast(
return temporalLiteral;
}
- private static @Nullable String formatTemporalLiteral(RexLiteral literal) {
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ final String value;
switch (literal.getType().getSqlTypeName()) {
case TIME:
final TimeString time = literal.getValueAs(TimeString.class);
- return time == null || !hasSubMillisecondPrecision(time)
- ? null
- : time.toString(precision(literal.getType()));
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(literal.getType().getPrecision());
+ break;
case TIMESTAMP:
final TimestampString timestamp = literal.getValueAs(TimestampString.class);
- return timestamp == null || !hasSubMillisecondPrecision(timestamp)
- ? null
- : timestamp.toString(precision(literal.getType()));
+ if (timestamp == null || literal.getType().getPrecision() <= 3) {
Review Comment:
that 3 was just the millisecond cutoff from the old version. Removed it and now format using the literal type's precision.
I also checked PostgreSQL: `concat_ws(',', timestamp '2024-07-06 12:15:48.678')` keeps `.678`(https://onecompiler.com/postgresql/44q5pytxk). After updating the expectation, `CalciteSqlOperatorTest.testConcatWSFunc` exposed that the VALUES-based path was still dropping it, so I updated the generated TIME/TIMESTAMP-to-character cast to pass through the source precision.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4595457095
I think the comments can be squashed so we can merge
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3246167931
##########
core/src/main/java/org/apache/calcite/rex/RexExecutorImpl.java:
##########
@@ -136,14 +137,32 @@ public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> e
@Override public void reduce(RexBuilder rexBuilder, List<RexNode> constExps,
List<RexNode> reducedValues) {
assert reducedValues.isEmpty();
+ final List<RexNode> exps = new ArrayList<>();
Review Comment:
I'm copying literals through unchanged, and compiling/reducing only non-literal constant expressions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3295983557
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Replace the CAST with a character literal only if the formatted temporal
Review Comment:
I reworded this to describe what the helper does directly, without referring to the surrounding CAST.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4604450034
## [](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [96.2% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_duplicated_lines_density&view=list)
<!-- sqra-placement-anchor -->
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3308059192
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,124 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal is not a TIME or TIMESTAMP, the literal
+ * cannot be read as the corresponding temporal string value, or the formatted
+ * temporal value does not fit in the target character type. */
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
Review Comment:
Right, I named it to describe what it actually returns.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4550993836
Let's wait for the 1.43 release to merge this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3295460430
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
Review Comment:
This needs JavaDoc, particularly because it returns null on many paths.
You would not expect that result from the function name.
##########
core/src/main/java/org/apache/calcite/util/BuiltInMethod.java:
##########
@@ -784,8 +779,12 @@ public enum BuiltInMethod {
String.class, int.class),
UNIX_DATE_TO_STRING(DateTimeUtils.class, "unixDateToString", int.class),
UNIX_TIME_TO_STRING(DateTimeUtils.class, "unixTimeToString", int.class),
+ UNIX_TIME_TO_STRING_WITH_PRECISION(DateTimeUtils.class, "unixTimeToString",
Review Comment:
Both these methods assume that the actual data value is expressed as an integer number of milliseconds, so they won't work for precisions higher than 3. Perhaps this is not worsening the problem, but it looks to me like it will need to be fixed at some point again.
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Replace the CAST with a character literal only if the formatted temporal
Review Comment:
I don't see any cast in this function, so this comment is misleading.
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,116 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Replace the CAST with a character literal only if the formatted temporal
+ // value fits in the target type; otherwise leave the CAST unchanged.
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
+ ? makeLiteral(value, type, true)
+ : null;
+ }
+
+ private static boolean isExactFractionalSecondLiteral(
Review Comment:
can you document what this function is supposed to do?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu merged PR #4942:
URL: https://github.com/apache/calcite/pull/4942
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292904997
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -969,54 +959,31 @@ public RexNode makeCast(
return temporalLiteral;
}
- private static @Nullable String formatTemporalLiteral(RexLiteral literal) {
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ final String value;
switch (literal.getType().getSqlTypeName()) {
case TIME:
final TimeString time = literal.getValueAs(TimeString.class);
- return time == null || !hasSubMillisecondPrecision(time)
- ? null
- : time.toString(precision(literal.getType()));
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(literal.getType().getPrecision());
+ break;
case TIMESTAMP:
final TimestampString timestamp = literal.getValueAs(TimestampString.class);
- return timestamp == null || !hasSubMillisecondPrecision(timestamp)
- ? null
- : timestamp.toString(precision(literal.getType()));
+ if (timestamp == null || literal.getType().getPrecision() <= 3) {
Review Comment:
that 3 was just the millisecond cutoff from the old version. Removed it and now format using the literal type's precision.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3292906020
##########
core/src/main/java/org/apache/calcite/rex/RexExecutorImpl.java:
##########
@@ -136,14 +137,32 @@ public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> e
@Override public void reduce(RexBuilder rexBuilder, List<RexNode> constExps,
List<RexNode> reducedValues) {
assert reducedValues.isEmpty();
+ final List<RexNode> exps = new ArrayList<>();
Review Comment:
Done. I added a comment explaining that RexLiteral inputs are already reduced, so reduce keeps them as Rex values instead of round-tripping them through generated code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] RexExecutor constant reduction loses sub-millisecond precision for TIME/TIMESTAMP literals [calcite]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #4942:
URL: https://github.com/apache/calcite/pull/4942#issuecomment-4507001633
## [](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4942&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4942&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [93.8% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4942&metric=new_duplicated_lines_density&view=list)
<!-- sqra-placement-anchor -->
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4942)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [CALCITE-7529] Casts between literals and TIME/TIMESTAMP can lose precision beyond milliseconds [calcite]
Posted by "zzwqqq (via GitHub)" <gi...@apache.org>.
zzwqqq commented on code in PR #4942:
URL: https://github.com/apache/calcite/pull/4942#discussion_r3308060851
##########
core/src/main/java/org/apache/calcite/rex/RexBuilder.java:
##########
@@ -877,6 +887,124 @@ public RexNode makeCast(
return makeAbstractCast(pos, type, exp, safe, format);
}
+ private @Nullable RexNode makeCastForTemporalLiteral(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ if (!format.isNull()) {
+ return null;
+ }
+ if (SqlTypeUtil.isCharacter(literal.getType())) {
+ return makeCastFromCharacterLiteralToTemporal(
+ pos, type, literal, matchNullability, safe, format);
+ }
+ if (SqlTypeUtil.isCharacter(type)) {
+ return makeCastFromTemporalLiteralToCharacter(type, literal);
+ }
+ return null;
+ }
+
+ private @Nullable RexNode makeCastFromCharacterLiteralToTemporal(
+ SqlParserPos pos,
+ RelDataType type,
+ RexLiteral literal,
+ boolean matchNullability,
+ boolean safe,
+ RexLiteral format) {
+ final NlsString nlsString = literal.getValueAs(NlsString.class);
+ if (nlsString == null) {
+ return null;
+ }
+ final String value = nlsString.getValue().trim();
+ final RexNode temporalLiteral;
+ try {
+ switch (type.getSqlTypeName()) {
+ case TIME:
+ final SqlTimeLiteral timeLiteral =
+ SqlParserUtil.parseTimeLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timeLiteral, value)) {
+ return null;
+ }
+ final TimeString time =
+ requireNonNull(timeLiteral.getValueAs(TimeString.class),
+ "timeLiteral.getValueAs(TimeString.class)");
+ temporalLiteral = makeTimeLiteral(time, type.getPrecision());
+ break;
+ case TIMESTAMP:
+ final SqlTimestampLiteral timestampLiteral =
+ SqlParserUtil.parseTimestampLiteral(value, pos);
+ if (!isExactFractionalSecondLiteral(timestampLiteral, value)) {
+ return null;
+ }
+ final TimestampString timestamp =
+ requireNonNull(timestampLiteral.getValueAs(TimestampString.class),
+ "timestampLiteral.getValueAs(TimestampString.class)");
+ temporalLiteral = makeTimestampLiteral(timestamp, type.getPrecision());
+ break;
+ default:
+ return null;
+ }
+ } catch (RuntimeException e) {
+ return safe ? makeNullLiteral(type) : null;
+ }
+ if (type.isNullable()
+ && !temporalLiteral.getType().isNullable()
+ && matchNullability) {
+ return makeAbstractCast(pos, type, temporalLiteral, safe, format);
+ }
+ return temporalLiteral;
+ }
+
+ /** Converts a TIME or TIMESTAMP literal to a character literal.
+ *
+ * <p>Returns null if the literal is not a TIME or TIMESTAMP, the literal
+ * cannot be read as the corresponding temporal string value, or the formatted
+ * temporal value does not fit in the target character type. */
+ private @Nullable RexNode makeCastFromTemporalLiteralToCharacter(
+ RelDataType type,
+ RexLiteral literal) {
+ // Format temporal literals directly so they do not go through generated
+ // code, whose TIME/TIMESTAMP runtime values have millisecond precision.
+ final String value;
+ final int precision = literal.getType().getPrecision();
+ switch (literal.getType().getSqlTypeName()) {
+ case TIME:
+ final TimeString time = literal.getValueAs(TimeString.class);
+ if (time == null) {
+ return null;
+ }
+ value = time.toString(precision);
+ break;
+ case TIMESTAMP:
+ final TimestampString timestamp = literal.getValueAs(TimestampString.class);
+ if (timestamp == null) {
+ return null;
+ }
+ value = timestamp.toString(precision);
+ break;
+ default:
+ return null;
+ }
+ // Only create a character literal if the formatted temporal value fits in
+ // the target type.
+ return SqlTypeUtil.comparePrecision(type.getPrecision(), value.length()) >= 0
+ ? makeLiteral(value, type, true)
+ : null;
+ }
+
+ /** Returns whether a parsed TIME or TIMESTAMP literal has explicit
+ * fractional-second precision and preserves the original character value
Review Comment:
Thanks, I updated the doc with that wording
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org