-
Notifications
You must be signed in to change notification settings - Fork 141
[SQL] JSON_* and VARIANT_* functions for filtering and transformations #6686
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
Open
mihaibudiu
wants to merge
1
commit into
feldera:main
Choose a base branch
from
mihaibudiu:issue6685
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
84 changes: 84 additions & 0 deletions
84
...in/java/org/dbsp/sqlCompiler/compiler/frontend/calciteCompiler/VariantFilterFunction.java
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package org.dbsp.sqlCompiler.compiler.frontend.calciteCompiler; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.sql.SqlCallBinding; | ||
| import org.apache.calcite.sql.SqlFunctionCategory; | ||
| import org.apache.calcite.sql.SqlOperandCountRange; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.type.FunctionSqlType; | ||
| import org.apache.calcite.sql.type.OperandTypes; | ||
| import org.apache.calcite.sql.type.SqlOperandCountRanges; | ||
| import org.apache.calcite.sql.type.SqlOperandTypeChecker; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.apache.calcite.sql.type.SqlTypeUtil; | ||
|
|
||
| /** Calcite-level implementation of the VARIANT_FILTER and VARIANT_DEEP_FILTER functions. | ||
| * Both take (variant, (label, value) -> predicate) and return a VARIANT with | ||
| * the items of the input variant for which the predicate is true. */ | ||
| class VariantFilterFunction extends CustomFunctions.NonOptimizedFunction { | ||
| private VariantFilterFunction(String name, SqlTypeName labelTypeName, String documentation) { | ||
| super(name, | ||
| FILTER_INFERENCE, | ||
| makeChecker(name, labelTypeName), | ||
| SqlFunctionCategory.USER_DEFINED_FUNCTION, | ||
| documentation, FunctionDocumentation.NO_FILE); | ||
| } | ||
|
|
||
| /** Always nullable: a dropped non-map variant produces NULL */ | ||
| static final SqlReturnTypeInference FILTER_INFERENCE = opBinding -> { | ||
| RelDataTypeFactory typeFactory = opBinding.getTypeFactory(); | ||
| return typeFactory.createTypeWithNullability( | ||
| typeFactory.createSqlType(SqlTypeName.VARIANT), true); | ||
| }; | ||
|
|
||
| /** Checks (VARIANT, (labelTypeName, VARIANT) -> BOOLEAN) operands */ | ||
| static SqlOperandTypeChecker makeChecker(String name, SqlTypeName labelTypeName) { | ||
| String signature = name + "(<VARIANT>, <FUNCTION(" + | ||
| labelTypeName + ", VARIANT)-><BOOLEAN>>)"; | ||
| return new SqlOperandTypeChecker() { | ||
| @Override | ||
| public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { | ||
| if (!OperandTypes.VARIANT.checkSingleOperandType( | ||
| callBinding, callBinding.operand(0), 0, throwOnFailure)) | ||
| return false; | ||
|
|
||
| RelDataTypeFactory typeFactory = callBinding.getTypeFactory(); | ||
| // The label is NULL when the variant does not hold a map | ||
| RelDataType labelType = typeFactory.createTypeWithNullability( | ||
| typeFactory.createSqlType(labelTypeName), true); | ||
| RelDataType valueType = typeFactory.createSqlType(SqlTypeName.VARIANT); | ||
| GenericLambdaTypeChecker lambdaChecker = | ||
| new GenericLambdaTypeChecker(signature, labelType, valueType); | ||
| if (!lambdaChecker.checkSingleOperandType( | ||
| callBinding, callBinding.operand(1), 1, throwOnFailure)) | ||
| return false; | ||
|
|
||
| RelDataType functionType = SqlTypeUtil.deriveType(callBinding, callBinding.operand(1)); | ||
| if (!(functionType instanceof FunctionSqlType fType) | ||
| || fType.getReturnType().getSqlTypeName() != SqlTypeName.BOOLEAN) { | ||
| if (throwOnFailure) | ||
| throw callBinding.newValidationSignatureError(); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public SqlOperandCountRange getOperandCountRange() { | ||
| return SqlOperandCountRanges.of(2); | ||
| } | ||
|
|
||
| @Override | ||
| public String getAllowedSignatures(SqlOperator op, String opName) { | ||
| return signature; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| static final VariantFilterFunction INSTANCE = new VariantFilterFunction( | ||
| "VARIANT_FILTER", SqlTypeName.VARIANT, "json#variant_filter"); | ||
| static final VariantFilterFunction DEEP = new VariantFilterFunction( | ||
| "VARIANT_DEEP_FILTER", SqlTypeName.VARCHAR, "json#variant_deep_filter"); | ||
| } |
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.
This removes all standard Calcite
JSON_*functions (JSON_VALUE, JSON_QUERY, JSON_EXISTS, JSON_ARRAY, JSON_OBJECT, JSON_DEPTH, JSON_LENGTH, JSON_PRETTY, JSON_REMOVE, etc.) — not just the ones that conflict with the new Feldera functions. The direct conflict isJSON_KEYSandJSON_OBJECT_KEYS, but the rest are collateral. Were any of the removed functions previously usable in Feldera SQL? If so, this is a breaking change worth documenting. If not (because Feldera uses VARIANT instead of string-based JSON), then this is fine — but a comment saying "Feldera replaces all standard JSON functions with VARIANT-based equivalents" would make the intent clearer for future readers.