66package org .opensearch .sql .calcite .validate .shuttles ;
77
88import java .math .BigDecimal ;
9+ import java .util .List ;
910import org .apache .calcite .avatica .util .TimeUnit ;
1011import org .apache .calcite .rel .RelNode ;
1112import org .apache .calcite .rel .RelShuttleImpl ;
1415import org .apache .calcite .rex .RexNode ;
1516import org .apache .calcite .rex .RexShuttle ;
1617import org .apache .calcite .sql .SqlIntervalQualifier ;
18+ import org .apache .calcite .sql .fun .SqlLibraryOperators ;
19+ import org .apache .calcite .sql .type .SqlTypeName ;
1720
1821/**
1922 * A RelShuttle that recursively visits all RelNodes and their RexNode expressions to fix interval
20- * literals before/after SQL conversion.
23+ * literals and float literal before/after SQL conversion.
2124 *
2225 * <p>This shuttle extends RelShuttleImpl to ensure it visits the entire RelNode tree recursively,
2326 * applying the interval literal fixes at each node.
@@ -28,8 +31,23 @@ public class PplRelToSqlRelShuttle extends RelShuttleImpl {
2831 public PplRelToSqlRelShuttle (RexBuilder rexBuilder , boolean forward ) {
2932 this .rexShuttle =
3033 new RexShuttle () {
34+ /**
35+ * This visitor fixes: 1. float literal: when converting logical plan to sql node, float
36+ * information is missing. All floats will be treated as double. A compulsory cast is
37+ * inserted here to ensure a cast presents in the generated SQL 2. interval literal: we
38+ * create and read the interval literal in a different way that how Calcite originally
39+ * expected it to be.
40+ */
3141 @ Override
3242 public RexNode visitLiteral (RexLiteral literal ) {
43+ // 1. Fix float literal
44+ SqlTypeName literalType = literal .getType ().getSqlTypeName ();
45+ if (SqlTypeName .REAL .equals (literalType ) || SqlTypeName .FLOAT .equals (literalType )) {
46+ return rexBuilder .makeCall (
47+ literal .getType (), SqlLibraryOperators .SAFE_CAST , List .of (literal ));
48+ }
49+
50+ // 2. Fix interval literal
3351 SqlIntervalQualifier qualifier = literal .getType ().getIntervalQualifier ();
3452 if (qualifier == null ) {
3553 return literal ;
0 commit comments