Skip to content

Commit 67d9ea6

Browse files
committed
Define overrides for atan function to allow conditional sql call rewriting (1579/2015)
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent d81fc44 commit 67d9ea6

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/validate/PplConvertletTable.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import org.apache.calcite.rel.type.RelDataType;
1111
import org.apache.calcite.rex.RexCall;
12+
import org.apache.calcite.sql.SqlBasicCall;
1213
import org.apache.calcite.sql.SqlCall;
1314
import org.apache.calcite.sql.SqlFunction;
1415
import org.apache.calcite.sql.SqlOperator;
@@ -37,6 +38,14 @@ private PplConvertletTable() {
3738
registerOperator(SqlStdOperatorTable.LESS_THAN, ipConvertlet(PPLBuiltinOperators.LESS_IP));
3839
registerOperator(
3940
SqlStdOperatorTable.LESS_THAN_OR_EQUAL, ipConvertlet(PPLBuiltinOperators.LTE_IP));
41+
// There is no implementation for PPLBuiltinOperators.ATAN. It needs to be replaced to
42+
// SqlStdOperatorTable.ATAN when converted to RelNode
43+
registerOperator(
44+
PPLBuiltinOperators.ATAN,
45+
(cx, call) -> {
46+
((SqlBasicCall) call).setOperator(SqlStdOperatorTable.ATAN);
47+
return StandardConvertletTable.INSTANCE.convertCall(cx, call);
48+
});
4049
}
4150

4251
@Override

core/src/main/java/org/opensearch/sql/expression/function/PPLBuiltinOperators.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@
2323
import org.apache.calcite.linq4j.tree.Expression;
2424
import org.apache.calcite.rex.RexCall;
2525
import org.apache.calcite.sql.SqlAggFunction;
26+
import org.apache.calcite.sql.SqlBasicCall;
27+
import org.apache.calcite.sql.SqlCall;
2628
import org.apache.calcite.sql.SqlFunction;
29+
import org.apache.calcite.sql.SqlFunctionCategory;
2730
import org.apache.calcite.sql.SqlKind;
31+
import org.apache.calcite.sql.SqlNode;
32+
import org.apache.calcite.sql.SqlOperator;
33+
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
34+
import org.apache.calcite.sql.type.OperandTypes;
2835
import org.apache.calcite.sql.type.ReturnTypes;
2936
import org.apache.calcite.sql.type.SqlTypeTransforms;
3037
import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
38+
import org.apache.calcite.sql.validate.SqlValidator;
3139
import org.apache.calcite.util.BuiltInMethod;
3240
import org.opensearch.sql.calcite.udf.udaf.FirstAggFunction;
3341
import org.opensearch.sql.calcite.udf.udaf.LastAggFunction;
@@ -477,6 +485,25 @@ public class PPLBuiltinOperators extends ReflectiveSqlOperatorTable {
477485
public static final SqlFunction ENHANCED_COALESCE =
478486
new EnhancedCoalesceFunction().toUDF("COALESCE");
479487

488+
public static final SqlFunction ATAN =
489+
new SqlFunction(
490+
"ATAN",
491+
SqlKind.OTHER_FUNCTION,
492+
ReturnTypes.DOUBLE_NULLABLE,
493+
null,
494+
OperandTypes.NUMERIC_OPTIONAL_NUMERIC,
495+
SqlFunctionCategory.NUMERIC) {
496+
@Override
497+
public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
498+
SqlOperator op =
499+
call.getOperandList().size() == 2
500+
? SqlStdOperatorTable.ATAN2
501+
: SqlStdOperatorTable.ATAN;
502+
((SqlBasicCall) call).setOperator(op);
503+
return call;
504+
}
505+
};
506+
480507
/**
481508
* Returns the PPL specific operator table, creating it if necessary.
482509
*

core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ void populate() {
784784
registerOperator(ABS, SqlStdOperatorTable.ABS);
785785
registerOperator(ACOS, SqlStdOperatorTable.ACOS);
786786
registerOperator(ASIN, SqlStdOperatorTable.ASIN);
787-
registerOperator(ATAN, SqlStdOperatorTable.ATAN);
787+
registerOperator(ATAN, PPLBuiltinOperators.ATAN);
788788
registerOperator(ATAN2, SqlStdOperatorTable.ATAN2);
789789
// TODO, workaround to support sequence CompositeOperandTypeChecker.
790790
registerOperator(

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteArrayFunctionIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ public void testArrayWithMix() {
6565
TEST_INDEX_BANK)));
6666

6767
verifyErrorMessageContains(
68-
e,
69-
"Cannot resolve function: ARRAY, arguments: [INTEGER,BOOLEAN], caused by: fail to create"
70-
+ " array with fixed type");
68+
e, "Cannot infer return type for array; operand types: [INTEGER, BOOLEAN]");
7169
}
7270

7371
@Test

0 commit comments

Comments
 (0)