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

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

Reply via email to