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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to