|
15 | 15 | import org.apache.calcite.rex.RexLiteral; |
16 | 16 | import org.apache.calcite.rex.RexNode; |
17 | 17 | import org.apache.calcite.sql.SqlIntervalQualifier; |
| 18 | +import org.apache.calcite.sql.fun.SqlLibraryOperators; |
18 | 19 | import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
19 | 20 | import org.apache.calcite.sql.parser.SqlParserPos; |
20 | 21 | import org.apache.calcite.sql.type.SqlTypeName; |
|
28 | 29 | import org.opensearch.sql.expression.function.PPLBuiltinOperators; |
29 | 30 |
|
30 | 31 | public class ExtendedRexBuilder extends RexBuilder { |
| 32 | + /** |
| 33 | + * Formats double values in PPL using a non-scientific notation, displaying up to 16 digits after |
| 34 | + * the decimal point. This formatting is consistent with PPL V2. |
| 35 | + */ |
| 36 | + private static final String DOUBLE_FORMAT = "0.0###############"; |
| 37 | + |
| 38 | + private final RexLiteral doubleFormat; |
31 | 39 |
|
32 | 40 | public ExtendedRexBuilder(RexBuilder rexBuilder) { |
33 | 41 | super(rexBuilder.getTypeFactory()); |
| 42 | + doubleFormat = makeLiteral(DOUBLE_FORMAT); |
34 | 43 | } |
35 | 44 |
|
36 | 45 | public RexNode coalesce(RexNode... nodes) { |
@@ -150,6 +159,15 @@ public RexNode makeCast( |
150 | 159 | String.format(Locale.ROOT, "Cannot cast from %s to %s", argExprType, udt.name())); |
151 | 160 | }; |
152 | 161 | } |
| 162 | + // If casting an approximate numeric (e.g. double) to a character type and no format is |
| 163 | + // specified, |
| 164 | + // use the custom double format to ensure non\-scientific notation with up to 16 decimal digits. |
| 165 | + // This patch is necessary because Calcite's built-in CAST converts 0.0 to 0E0 as string. |
| 166 | + else if (SqlTypeUtil.isApproximateNumeric(exp.getType()) |
| 167 | + && SqlTypeUtil.isCharacter(type) |
| 168 | + && format.getType().getSqlTypeName() == SqlTypeName.NULL) { |
| 169 | + return makeCall(type, SqlLibraryOperators.FORMAT_NUMBER, List.of(exp, doubleFormat)); |
| 170 | + } |
153 | 171 | return super.makeCast(pos, type, exp, matchNullability, safe, format); |
154 | 172 | } |
155 | 173 | } |
0 commit comments