Skip to content

Commit d9669ab

Browse files
committed
Define type checker for grok and item operators
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent e3a491d commit d9669ab

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ private static SqlOperandTypeChecker extractTypeCheckerFromUDF(
251251
return (udfOperandMetadata == null) ? null : udfOperandMetadata.getInnerTypeChecker();
252252
}
253253

254+
/**
255+
* Wrap a SqlOperator into a FunctionImp with a composite type checker.
256+
*
257+
* @param operator the SqlOperator to wrap
258+
* @param typeChecker the CompositeOperandTypeChecker to use for type checking
259+
* @param checkCompositionType if true, the type checker will check whether the composition type
260+
* of the type checker is OR.
261+
* @return a FunctionImp that resolves to the operator and has the specified type checker
262+
*/
254263
private static FunctionImp wrapWithCompositeTypeChecker(
255264
SqlOperator operator,
256265
CompositeOperandTypeChecker typeChecker,
@@ -388,7 +397,6 @@ void populate() {
388397
registerOperator(IS_NULL, SqlStdOperatorTable.IS_NULL);
389398
registerOperator(IFNULL, SqlStdOperatorTable.COALESCE);
390399
registerOperator(COALESCE, SqlStdOperatorTable.COALESCE);
391-
registerOperator(INTERNAL_ITEM, SqlStdOperatorTable.ITEM);
392400

393401
// Register library operator
394402
registerOperator(REGEXP, SqlLibraryOperators.REGEXP);
@@ -544,6 +552,17 @@ void populate() {
544552
(CompositeOperandTypeChecker)
545553
OperandTypes.STRING_INTEGER.or(OperandTypes.STRING_INTEGER_INTEGER),
546554
false));
555+
// SqlStdOperatorTable.ITEM.getOperandTypeChecker() checks only the first operand instead of
556+
// all operands. Therefore, we wrap it with a custom CompositeOperandTypeChecker to check both
557+
// operands.
558+
register(
559+
INTERNAL_ITEM,
560+
wrapWithCompositeTypeChecker(
561+
SqlStdOperatorTable.ITEM,
562+
(CompositeOperandTypeChecker)
563+
OperandTypes.family(SqlTypeFamily.ARRAY, SqlTypeFamily.INTEGER)
564+
.or(OperandTypes.family(SqlTypeFamily.MAP, SqlTypeFamily.ANY)),
565+
false));
547566
register(
548567
LOG,
549568
createFunctionImpWithTypeChecker(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import org.apache.calcite.sql.type.SqlTypeName;
2323
import org.apache.calcite.sql.type.SqlTypeUtil;
2424
import org.apache.commons.lang3.tuple.Pair;
25+
import org.opensearch.sql.calcite.utils.PPLOperandTypes;
2526
import org.opensearch.sql.data.model.ExprValue;
2627
import org.opensearch.sql.expression.DSL;
2728
import org.opensearch.sql.expression.LiteralExpression;
2829
import org.opensearch.sql.expression.function.ImplementorUDF;
30+
import org.opensearch.sql.expression.function.UDFOperandMetadata;
2931
import org.opensearch.sql.expression.parse.GrokExpression;
3032

3133
public final class GrokFunction extends ImplementorUDF {
@@ -44,6 +46,11 @@ public SqlReturnTypeInference getReturnTypeInference() {
4446
false));
4547
}
4648

49+
@Override
50+
public UDFOperandMetadata getOperandMetadata() {
51+
return PPLOperandTypes.STRING_STRING;
52+
}
53+
4754
public static class GrokImplementor implements NotNullImplementor {
4855
@Override
4956
public Expression implement(

0 commit comments

Comments
 (0)