generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Add compare_ip operator udfs #3821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
qianheng-aws
merged 18 commits into
opensearch-project:main
from
ishaoxy:compare-ip-operator
Jul 11, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5d8c62c
ip_compare operator added
ishaoxy 3008d75
only type checker issue left
ishaoxy 851f40e
fix by modifying ip.sqlTypeName from OTHER to NULL in type checker
ishaoxy e4f619e
fix less
ishaoxy 73bec7b
modify the CalcitePPLFunctionTypeTest text
ishaoxy 2f9c5b4
allow CalciteIPComparisonIT in CalciteNoPushdownIT
ishaoxy 40d80a4
Modify the signature description in udf
ishaoxy a4916b2
fix some typing errors
ishaoxy f755494
modify the udfs for better style
ishaoxy 0acc8f7
Make IpComparisonOperators an inner enum of CompareIPFunction
yuancu e587271
Merge pull request #2 from yuancu/refactor-ip-compare
ishaoxy fb8e969
modify registerOperator
ishaoxy 999fdf7
modify registerOperator
ishaoxy 5151350
add type checker for cidr
ishaoxy ea1414c
Merge pull request #3 from ishaoxy/add-typechecker-cidr
ishaoxy e2a124f
add javadoc
ishaoxy 62a6084
move switch case to the implement method
ishaoxy eedd8c9
Merge remote-tracking branch 'upstream/main' into compare-ip-operator
ishaoxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -458,7 +458,10 @@ functionName, getActualSignature(argTypes), e.getMessage()), | |
| } | ||
| StringJoiner allowedSignatures = new StringJoiner(","); | ||
| for (var implement : implementList) { | ||
| allowedSignatures.add(implement.getKey().typeChecker().getAllowedSignatures()); | ||
| String signature = implement.getKey().typeChecker().getAllowedSignatures(); | ||
| if (!signature.isEmpty()) { | ||
| allowedSignatures.add(signature); | ||
| } | ||
| } | ||
| throw new ExpressionEvaluationException( | ||
| String.format( | ||
|
|
@@ -481,40 +484,70 @@ private abstract static class AbstractBuilder { | |
| /** Maps an operator to an implementation. */ | ||
| abstract void register(BuiltinFunctionName functionName, FunctionImp functionImp); | ||
|
|
||
| void registerOperator(BuiltinFunctionName functionName, SqlOperator operator) { | ||
| SqlOperandTypeChecker typeChecker; | ||
| if (operator instanceof SqlUserDefinedFunction udfOperator) { | ||
| typeChecker = extractTypeCheckerFromUDF(udfOperator); | ||
| } else { | ||
| typeChecker = operator.getOperandTypeChecker(); | ||
| } | ||
| /** | ||
| * Register one or multiple operators under a single function name. This allows function | ||
| * overloading based on operand types. | ||
| * | ||
| * <p>When a function is called, the system will try each registered operator in sequence, | ||
| * checking if the provided arguments match the operator's type requirements. The first operator | ||
| * whose type checker accepts the arguments will be used to execute the function. | ||
| * | ||
| * @param functionName the built-in function name under which to register the operators | ||
| * @param operators the operators to associate with this function name, tried in sequence until | ||
| * one matches the argument types during resolution | ||
| */ | ||
| public void registerOperator(BuiltinFunctionName functionName, SqlOperator... operators) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a javadoc for this method to explain what are the operators for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
| for (SqlOperator operator : operators) { | ||
| SqlOperandTypeChecker typeChecker; | ||
| if (operator instanceof SqlUserDefinedFunction udfOperator) { | ||
| typeChecker = extractTypeCheckerFromUDF(udfOperator); | ||
| } else { | ||
| typeChecker = operator.getOperandTypeChecker(); | ||
| } | ||
|
|
||
| // Only the composite operand type checker for UDFs are concerned here. | ||
| if (operator instanceof SqlUserDefinedFunction | ||
| && typeChecker instanceof CompositeOperandTypeChecker compositeTypeChecker) { | ||
| // UDFs implement their own composite type checkers, which always use OR logic for argument | ||
| // types. Verifying the composition type would require accessing a protected field in | ||
| // CompositeOperandTypeChecker. If access to this field is not allowed, type checking will | ||
| // be skipped, so we avoid checking the composition type here. | ||
| register(functionName, wrapWithCompositeTypeChecker(operator, compositeTypeChecker, false)); | ||
| } else if (typeChecker instanceof ImplicitCastOperandTypeChecker implicitCastTypeChecker) { | ||
| register(functionName, wrapWithImplicitCastTypeChecker(operator, implicitCastTypeChecker)); | ||
| } else if (typeChecker instanceof CompositeOperandTypeChecker compositeTypeChecker) { | ||
| // If compositeTypeChecker contains operand checkers other than family type checkers or | ||
| // other than OR compositions, the function with be registered with a null type checker, | ||
| // which means the function will not be type checked. | ||
| register(functionName, wrapWithCompositeTypeChecker(operator, compositeTypeChecker, true)); | ||
| } else if (typeChecker instanceof SameOperandTypeChecker comparableTypeChecker) { | ||
| // Comparison operators like EQUAL, GREATER_THAN, LESS_THAN, etc. | ||
| // SameOperandTypeCheckers like COALESCE, IFNULL, etc. | ||
| register(functionName, wrapWithComparableTypeChecker(operator, comparableTypeChecker)); | ||
| } else { | ||
| logger.info( | ||
| "Cannot create type checker for function: {}. Will skip its type checking", | ||
| functionName); | ||
| register( | ||
| functionName, | ||
| (RexBuilder builder, RexNode... node) -> builder.makeCall(operator, node)); | ||
| // Only the composite operand type checker for UDFs are concerned here. | ||
| if (operator instanceof SqlUserDefinedFunction | ||
| && typeChecker instanceof CompositeOperandTypeChecker compositeTypeChecker) { | ||
| // UDFs implement their own composite type checkers, which always use OR logic for | ||
| // argument | ||
| // types. Verifying the composition type would require accessing a protected field in | ||
| // CompositeOperandTypeChecker. If access to this field is not allowed, type checking will | ||
| // be skipped, so we avoid checking the composition type here. | ||
| register( | ||
| functionName, wrapWithCompositeTypeChecker(operator, compositeTypeChecker, false)); | ||
| } else if (typeChecker instanceof ImplicitCastOperandTypeChecker implicitCastTypeChecker) { | ||
| register( | ||
| functionName, wrapWithImplicitCastTypeChecker(operator, implicitCastTypeChecker)); | ||
| } else if (typeChecker instanceof CompositeOperandTypeChecker compositeTypeChecker) { | ||
| // If compositeTypeChecker contains operand checkers other than family type checkers or | ||
| // other than OR compositions, the function with be registered with a null type checker, | ||
| // which means the function will not be type checked. | ||
| register( | ||
| functionName, wrapWithCompositeTypeChecker(operator, compositeTypeChecker, true)); | ||
| } else if (typeChecker instanceof SameOperandTypeChecker comparableTypeChecker) { | ||
| // Comparison operators like EQUAL, GREATER_THAN, LESS_THAN, etc. | ||
| // SameOperandTypeCheckers like COALESCE, IFNULL, etc. | ||
| register(functionName, wrapWithComparableTypeChecker(operator, comparableTypeChecker)); | ||
| } else if (typeChecker instanceof UDFOperandMetadata.IPOperandMetadata) { | ||
| register( | ||
| functionName, | ||
| createFunctionImpWithTypeChecker( | ||
| (builder, arg1, arg2) -> builder.makeCall(operator, arg1, arg2), | ||
| new PPLTypeChecker.PPLIPCompareTypeChecker())); | ||
| } else if (typeChecker instanceof UDFOperandMetadata.CidrOperandMetadata) { | ||
| register( | ||
| functionName, | ||
| createFunctionImpWithTypeChecker( | ||
| (builder, arg1, arg2) -> builder.makeCall(operator, arg1, arg2), | ||
| new PPLTypeChecker.PPLCidrTypeChecker())); | ||
| } else { | ||
| logger.info( | ||
| "Cannot create type checker for function: {}. Will skip its type checking", | ||
| functionName); | ||
| register( | ||
| functionName, | ||
| (RexBuilder builder, RexNode... node) -> builder.makeCall(operator, node)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -622,16 +655,18 @@ public PPLTypeChecker getTypeChecker() { | |
| } | ||
|
|
||
| void populate() { | ||
| // register operators for comparison | ||
| registerOperator(NOTEQUAL, PPLBuiltinOperators.NOT_EQUALS_IP, SqlStdOperatorTable.NOT_EQUALS); | ||
| registerOperator(EQUAL, PPLBuiltinOperators.EQUALS_IP, SqlStdOperatorTable.EQUALS); | ||
| registerOperator(GREATER, PPLBuiltinOperators.GREATER_IP, SqlStdOperatorTable.GREATER_THAN); | ||
| registerOperator(GTE, PPLBuiltinOperators.GTE_IP, SqlStdOperatorTable.GREATER_THAN_OR_EQUAL); | ||
| registerOperator(LESS, PPLBuiltinOperators.LESS_IP, SqlStdOperatorTable.LESS_THAN); | ||
| registerOperator(LTE, PPLBuiltinOperators.LTE_IP, SqlStdOperatorTable.LESS_THAN_OR_EQUAL); | ||
|
|
||
| // Register std operator | ||
| registerOperator(AND, SqlStdOperatorTable.AND); | ||
| registerOperator(OR, SqlStdOperatorTable.OR); | ||
| registerOperator(NOT, SqlStdOperatorTable.NOT); | ||
| registerOperator(NOTEQUAL, SqlStdOperatorTable.NOT_EQUALS); | ||
| registerOperator(EQUAL, SqlStdOperatorTable.EQUALS); | ||
| registerOperator(GREATER, SqlStdOperatorTable.GREATER_THAN); | ||
| registerOperator(GTE, SqlStdOperatorTable.GREATER_THAN_OR_EQUAL); | ||
| registerOperator(LESS, SqlStdOperatorTable.LESS_THAN); | ||
| registerOperator(LTE, SqlStdOperatorTable.LESS_THAN_OR_EQUAL); | ||
| registerOperator(ADD, SqlStdOperatorTable.PLUS); | ||
| registerOperator(SUBTRACT, SqlStdOperatorTable.MINUS); | ||
| registerOperator(MULTIPLY, SqlStdOperatorTable.MULTIPLY); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update the PR description to align changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reminding me.