Skip to content

Commit 4580458

Browse files
committed
Fix float literal by inserting compulsory cast
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent def7990 commit 4580458

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

core/src/main/java/org/opensearch/sql/calcite/validate/shuttles/PplRelToSqlRelShuttle.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.opensearch.sql.calcite.validate.shuttles;
77

88
import java.math.BigDecimal;
9+
import java.util.List;
910
import org.apache.calcite.avatica.util.TimeUnit;
1011
import org.apache.calcite.rel.RelNode;
1112
import org.apache.calcite.rel.RelShuttleImpl;
@@ -14,10 +15,12 @@
1415
import org.apache.calcite.rex.RexNode;
1516
import org.apache.calcite.rex.RexShuttle;
1617
import 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

Comments
 (0)