-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-40885][table] Support LATERAL SNAPSHOT join in the Table API #29368
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
fhueske
wants to merge
3
commits into
apache:master
Choose a base branch
from
confluentinc:fhueske-FLINK-40885-Add-LSJ-to-TableAPI
base: master
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -284,7 +284,17 @@ private AggCall getAggCall(Expression aggregateExpression) { | |
| @Override | ||
| public RelNode visit(JoinQueryOperation join) { | ||
| final Set<CorrelationId> corSet; | ||
| if (join.isCorrelated()) { | ||
| // Check if this is a LATERAL SNAPSHOT join. The SNAPSHOT function never references the | ||
| // outer row. Emit a plain LogicalJoin instead of a Correlate, so that | ||
| // LogicalJoinToLateralSnapshotJoinRule matches. | ||
| final QueryOperation right = join.getChildren().get(1); | ||
| final boolean isLateralSnapshotJoin = | ||
| right instanceof CorrelatedFunctionQueryOperation | ||
| && isSnapshot( | ||
| ((CorrelatedFunctionQueryOperation) right) | ||
| .getResolvedFunction() | ||
| .getDefinition()); | ||
| if (join.isCorrelated() && !isLateralSnapshotJoin) { | ||
| corSet = Collections.singleton(relBuilder.peek().getCluster().createCorrel()); | ||
| } else { | ||
| corSet = Collections.emptySet(); | ||
|
|
@@ -352,44 +362,11 @@ public RelNode visit(FunctionQueryOperation functionTable) { | |
| .map( | ||
| resolvedArg -> { | ||
| if (resolvedArg instanceof TableReferenceExpression) { | ||
| final TableReferenceExpression tableRef = | ||
| (TableReferenceExpression) resolvedArg; | ||
| final LogicalType tableArgType = | ||
| tableRef.getOutputDataType().getLogicalType(); | ||
| final RelDataType rowType = | ||
| typeFactory.buildRelNodeRowType( | ||
| (RowType) tableArgType); | ||
| final int[] partitionKeys; | ||
| final int[] orderKeys; | ||
| final SortOrder[] sortOrders; | ||
| if (tableRef.getQueryOperation() | ||
| instanceof PartitionQueryOperation) { | ||
| final PartitionQueryOperation partitionOperation = | ||
| (PartitionQueryOperation) | ||
| tableRef.getQueryOperation(); | ||
| partitionKeys = | ||
| partitionOperation.getPartitionKeys(); | ||
| orderKeys = partitionOperation.getOrderKeys(); | ||
| final SortDirection[] directions = | ||
| partitionOperation.getOrderDirections(); | ||
| sortOrders = new SortOrder[directions.length]; | ||
| for (int i = 0; i < directions.length; i++) { | ||
| sortOrders[i] = | ||
| SortOrder.fromSortDirection( | ||
| directions[i]); | ||
| } | ||
| } else { | ||
| partitionKeys = new int[0]; | ||
| orderKeys = new int[0]; | ||
| sortOrders = new SortOrder[0]; | ||
| } | ||
| final RexTableArgCall tableArgCall = | ||
| new RexTableArgCall( | ||
| rowType, | ||
| buildTableArgCall( | ||
| (TableReferenceExpression) resolvedArg, | ||
| inputStack.size(), | ||
| partitionKeys, | ||
| orderKeys, | ||
| sortOrders); | ||
| typeFactory); | ||
| inputStack.add(relBuilder.build()); | ||
| return tableArgCall; | ||
| } | ||
|
|
@@ -400,22 +377,9 @@ public RelNode visit(FunctionQueryOperation functionTable) { | |
| // relBuilder.build() works in LIFO fashion, this restores the original input order | ||
| Collections.reverse(inputStack); | ||
|
|
||
| final BridgingSqlFunction sqlFunction = | ||
| BridgingSqlFunction.of(relBuilder.getCluster(), contextFunction); | ||
|
|
||
| final RexNode call = | ||
| relBuilder | ||
| .getRexBuilder() | ||
| .makeCall(outputRelDataType, sqlFunction, rexNodeArgs); | ||
| final RelNode functionScan = | ||
| LogicalTableFunctionScan.create( | ||
| relBuilder.getCluster(), | ||
| inputStack, | ||
| call, | ||
| null, | ||
| outputRelDataType, | ||
| Collections.emptySet()); | ||
| relBuilder.push(functionScan); | ||
| relBuilder.push( | ||
| createTableFunctionScan( | ||
| contextFunction, rexNodeArgs, inputStack, outputRelDataType)); | ||
| return relBuilder.build(); | ||
| } | ||
|
|
||
|
|
@@ -430,6 +394,11 @@ public RelNode visit(PartitionQueryOperation partition) { | |
| public RelNode visit(CorrelatedFunctionQueryOperation correlatedFunction) { | ||
| final ContextResolvedFunction contextFunction = | ||
| correlatedFunction.getResolvedFunction(); | ||
| // SNAPSHOT is currently the only PTF valid in a lateral join and hence the only | ||
| // function accepting table args. | ||
| if (isSnapshot(contextFunction.getDefinition())) { | ||
| return convertCorrelatedFunctionWithTableArgs(correlatedFunction); | ||
| } | ||
| final List<RexNode> parameters = convertToRexNodes(correlatedFunction.getArguments()); | ||
|
|
||
| final FunctionDefinition functionDefinition = contextFunction.getDefinition(); | ||
|
|
@@ -455,6 +424,87 @@ public RelNode visit(CorrelatedFunctionQueryOperation correlatedFunction) { | |
| return relBuilder.build(); | ||
| } | ||
|
|
||
| private RelNode convertCorrelatedFunctionWithTableArgs( | ||
| CorrelatedFunctionQueryOperation correlatedFunction) { | ||
| final FlinkTypeFactory typeFactory = ShortcutUtils.unwrapTypeFactory(relBuilder); | ||
| final List<RelNode> inputs = new ArrayList<>(); | ||
| final List<RexNode> args = new ArrayList<>(); | ||
| for (ResolvedExpression arg : correlatedFunction.getArguments()) { | ||
| if (arg instanceof TableReferenceExpression) { | ||
| final TableReferenceExpression tableRef = (TableReferenceExpression) arg; | ||
| args.add(buildTableArgCall(tableRef, inputs.size(), typeFactory)); | ||
| inputs.add(tableRef.getQueryOperation().accept(QueryOperationConverter.this)); | ||
| } else { | ||
| args.add(convertExprToRexNode(arg)); | ||
| } | ||
| } | ||
| // Derive the output row type like the FROM-clause PTF path | ||
| // (FunctionQueryOperation#getOutputDataType). | ||
| final RelDataType outputType = | ||
| typeFactory.buildRelNodeRowType( | ||
| (RowType) | ||
| DataTypeUtils.fromResolvedSchemaPreservingTimeAttributes( | ||
| correlatedFunction.getResolvedSchema()) | ||
| .getLogicalType()); | ||
| return createTableFunctionScan( | ||
| correlatedFunction.getResolvedFunction(), args, inputs, outputType); | ||
| } | ||
|
|
||
| private boolean isSnapshot(FunctionDefinition definition) { | ||
|
Contributor
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. there is existing |
||
| // SNAPSHOT is currently the only PTF valid as the build side of a lateral join; the | ||
| // lateral-join-specific handling in this converter keys off this single predicate. | ||
| return definition.equals(BuiltInFunctionDefinitions.SNAPSHOT); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a {@link TableReferenceExpression} table argument into a {@link | ||
| * RexTableArgCall}, carrying PARTITION BY / ORDER BY keys when the referenced operation is | ||
| * a {@link PartitionQueryOperation}. | ||
| */ | ||
| private RexTableArgCall buildTableArgCall( | ||
| TableReferenceExpression tableRef, int inputIndex, FlinkTypeFactory typeFactory) { | ||
| final RelDataType rowType = | ||
| typeFactory.buildRelNodeRowType( | ||
| (RowType) tableRef.getOutputDataType().getLogicalType()); | ||
| final int[] partitionKeys; | ||
| final int[] orderKeys; | ||
| final SortOrder[] sortOrders; | ||
| if (tableRef.getQueryOperation() instanceof PartitionQueryOperation) { | ||
| final PartitionQueryOperation partitionOperation = | ||
| (PartitionQueryOperation) tableRef.getQueryOperation(); | ||
| partitionKeys = partitionOperation.getPartitionKeys(); | ||
| orderKeys = partitionOperation.getOrderKeys(); | ||
| final SortDirection[] directions = partitionOperation.getOrderDirections(); | ||
| sortOrders = new SortOrder[directions.length]; | ||
| for (int i = 0; i < directions.length; i++) { | ||
| sortOrders[i] = SortOrder.fromSortDirection(directions[i]); | ||
| } | ||
| } else { | ||
| partitionKeys = new int[0]; | ||
| orderKeys = new int[0]; | ||
| sortOrders = new SortOrder[0]; | ||
| } | ||
| return new RexTableArgCall(rowType, inputIndex, partitionKeys, orderKeys, sortOrders); | ||
| } | ||
|
|
||
| /** Builds a {@link LogicalTableFunctionScan} for a function call with table arguments. */ | ||
| private RelNode createTableFunctionScan( | ||
| ContextResolvedFunction function, | ||
| List<RexNode> args, | ||
| List<RelNode> inputs, | ||
| RelDataType outputType) { | ||
| final BridgingSqlFunction sqlFunction = | ||
| BridgingSqlFunction.of(relBuilder.getCluster(), function); | ||
| final RexNode call = relBuilder.getRexBuilder().makeCall(outputType, sqlFunction, args); | ||
| return LogicalTableFunctionScan.create( | ||
| relBuilder.getCluster(), | ||
| inputs, | ||
| call, | ||
| null, | ||
| outputType, | ||
| Collections.emptySet()); | ||
| } | ||
|
|
||
| private RelNode convertLegacyTableFunction( | ||
| CorrelatedFunctionQueryOperation correlatedFunction, | ||
| TableFunctionDefinition functionDefinition, | ||
|
|
||
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.
?