Skip to content

Commit 682eb9d

Browse files
committed
applied recommended changes
Signed-off-by: Asif Bashar <[email protected]>
1 parent 5cbedcc commit 682eb9d

File tree

5 files changed

+17
-59
lines changed

5 files changed

+17
-59
lines changed

core/src/main/java/org/opensearch/sql/calcite/CalciteRexNodeVisitor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,19 @@ public RexNode visitFunction(Function node, CalcitePlanContext context) {
417417
context.setInCoalesceFunction(false);
418418
}
419419
}
420-
420+
if (node.getFuncName().equalsIgnoreCase("tostring") && args.size() == 1) {
421+
RelDataType targetType =
422+
context.relBuilder.getTypeFactory().createSqlType(SqlTypeName.VARCHAR);
423+
// Get the source expression from arguments
424+
RexNode sourceExpression = arguments.get(0);
425+
// Create cast RexNode using ExtendedRexBuilder
426+
return context.rexBuilder.makeCast(targetType, sourceExpression);
427+
}
421428
RexNode resolvedNode =
422429
PPLFuncImpTable.INSTANCE.resolve(
423430
context.rexBuilder, node.getFuncName(), arguments.toArray(new RexNode[0]));
424431
if (resolvedNode != null) {
432+
425433
return resolvedNode;
426434
}
427435
throw new IllegalArgumentException("Unsupported operator: " + node.getFuncName());

core/src/main/java/org/opensearch/sql/expression/function/udf/ToStringFunction.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.calcite.linq4j.tree.Expressions;
1919
import org.apache.calcite.rex.RexCall;
2020
import org.apache.calcite.runtime.SqlFunctions;
21+
import org.apache.calcite.sql.*;
2122
import org.apache.calcite.sql.type.SqlReturnTypeInference;
2223
import org.opensearch.sql.calcite.utils.PPLOperandTypes;
2324
import org.opensearch.sql.calcite.utils.PPLReturnTypes;
@@ -65,17 +66,12 @@ public static class ToStringImplementor implements NotNullImplementor {
6566
public Expression implement(
6667
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
6768
Expression fieldValue = translatedOperands.get(0);
69+
6870
if (translatedOperands.size() > 1) {
6971
Expression format = translatedOperands.get(1);
7072
return Expressions.call(ToStringFunction.class, "toString", fieldValue, format);
7173
} else {
72-
// autoboxes to Boolean
73-
74-
if (!fieldValue.getType().getTypeName().equals("Boolean")) {
75-
return Expressions.call(ToStringFunction.class, "toString", fieldValue);
76-
} else {
77-
return Expressions.call(ToStringFunction.class, "toString", fieldValue);
78-
}
74+
return Expressions.call(ToStringFunction.class, "toString", fieldValue);
7975
}
8076
}
8177
}

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,9 @@ evalFunctionCall
848848
;
849849

850850

851-
// cast, tostring function
851+
// cast function
852852
dataTypeFunctionCall
853853
: CAST LT_PRTHS logicalExpression AS convertedDataType RT_PRTHS
854-
| TOSTRING LT_PRTHS functionArgs RT_PRTHS
855854
;
856855

857856

@@ -1219,6 +1218,7 @@ systemFunctionName
12191218
textFunctionName
12201219
: SUBSTR
12211220
| SUBSTRING
1221+
| TOSTRING
12221222
| TRIM
12231223
| LTRIM
12241224
| RTRIM

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.IntStream;
2121
import java.util.stream.Stream;
22-
import org.antlr.v4.runtime.CommonTokenStream;
2322
import org.antlr.v4.runtime.ParserRuleContext;
2423
import org.antlr.v4.runtime.RuleContext;
2524
import org.antlr.v4.runtime.tree.ParseTree;
@@ -30,10 +29,8 @@
3029
import org.opensearch.sql.ast.expression.subquery.ScalarSubquery;
3130
import org.opensearch.sql.ast.tree.Trendline;
3231
import org.opensearch.sql.calcite.plan.OpenSearchConstants;
33-
import org.opensearch.sql.common.antlr.CaseInsensitiveCharStream;
3432
import org.opensearch.sql.common.antlr.SyntaxCheckException;
3533
import org.opensearch.sql.common.utils.StringUtils;
36-
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLLexer;
3734
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser;
3835
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.BinaryArithmeticContext;
3936
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.BooleanLiteralContext;
@@ -414,52 +411,10 @@ private Function buildFunction(
414411
functionName, args.stream().map(this::visitFunctionArg).collect(Collectors.toList()));
415412
}
416413

417-
public DataTypeFunctionCallContext createDataTypeFunctionCallContext(String castExpression) {
418-
// Create a case-insensitive character stream from the input
419-
CaseInsensitiveCharStream charStream = new CaseInsensitiveCharStream(castExpression);
420-
421-
// Create lexer and parser
422-
OpenSearchPPLLexer lexer = new OpenSearchPPLLexer(charStream);
423-
CommonTokenStream tokens = new CommonTokenStream(lexer);
424-
OpenSearchPPLParser parser = new OpenSearchPPLParser(tokens);
425-
426-
// Parse the expression - cast is part of evalFunctionCall
427-
DataTypeFunctionCallContext evalContext = parser.dataTypeFunctionCall();
428-
return evalContext;
429-
}
430-
431414
/** Cast function. */
432415
@Override
433416
public UnresolvedExpression visitDataTypeFunctionCall(DataTypeFunctionCallContext ctx) {
434-
if (ctx.functionArgs() != null) {
435-
436-
ParseTree rootNode = ctx.getChild(0);
437-
String functionName = rootNode.getText();
438-
final String mappedName =
439-
FUNCTION_NAME_MAPPING.getOrDefault(functionName.toLowerCase(Locale.ROOT), functionName);
440-
System.out.println(mappedName);
441-
if (mappedName != null && mappedName.equals("tostring")) {
442-
if (ctx.functionArgs().functionArg().size() == 1) {
443-
List<OpenSearchPPLParser.FunctionArgContext> functionArgs =
444-
ctx.functionArgs().functionArg();
445-
446-
String castExpresstion =
447-
String.format("cast( %s as String)", functionArgs.getFirst().getText());
448-
DataTypeFunctionCallContext toStringDataTypeConversionContext =
449-
this.createDataTypeFunctionCallContext(castExpresstion);
450-
return new Cast(
451-
visit(toStringDataTypeConversionContext.logicalExpression()),
452-
visit(toStringDataTypeConversionContext.convertedDataType()));
453-
//
454-
} else {
455-
return buildFunction(mappedName, ctx.functionArgs().functionArg());
456-
}
457-
} else {
458-
return new Cast(visit(ctx.logicalExpression()), visit(ctx.convertedDataType()));
459-
}
460-
} else {
461-
return new Cast(visit(ctx.logicalExpression()), visit(ctx.convertedDataType()));
462-
}
417+
return new Cast(visit(ctx.logicalExpression()), visit(ctx.convertedDataType()));
463418
}
464419

465420
@Override

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLStringFunctionTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testToStringFormatNotSpecified() {
5555
+ " fields string_value, cast_value";
5656
RelNode root = getRelNode(ppl);
5757
String expectedLogical =
58-
"LogicalProject(string_value=[SAFE_CAST($3)], cast_value=[SAFE_CAST($3)])\n"
58+
"LogicalProject(string_value=[CAST($3):VARCHAR NOT NULL], cast_value=[SAFE_CAST($3)])\n"
5959
+ " LogicalTableScan(table=[[scott, EMP]])\n";
6060
String expectedResult =
6161
"string_value=7902; cast_value=7902\n"
@@ -76,8 +76,7 @@ public void testToStringFormatNotSpecified() {
7676
verifyResult(root, expectedResult);
7777

7878
String expectedSparkSql =
79-
"SELECT SAFE_CAST(`MGR` AS STRING) `string_value`, SAFE_CAST(`MGR` AS STRING)"
80-
+ " `cast_value`\n"
79+
"SELECT CAST(`MGR` AS STRING) `string_value`, SAFE_CAST(`MGR` AS STRING) `cast_value`\n"
8180
+ "FROM `scott`.`EMP`";
8281
verifyPPLToSparkSQL(root, expectedSparkSql);
8382
}

0 commit comments

Comments
 (0)