-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-27431 SQL Calcite: Optimize scans with filter #12600
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
alex-plekhanov
wants to merge
5
commits into
apache:master
Choose a base branch
from
alex-plekhanov:ignite-27431
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
5 commits
Select commit
Hold shift + click to select a range
96b2606
IGNITE-27431 SQL Calcite: Optimize scans with filter
alex-plekhanov 5913dbb
IGNITE-27431 Review comments fixed
alex-plekhanov 93dd527
Merge remote-tracking branch 'apache/master' into ignite-27431
alex-plekhanov 6b4ec6b
IGNITE-27431 Fix after merge
alex-plekhanov 08fcefa
IGNITE-27431 Review comments fixed
alex-plekhanov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ | |
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.ProjectNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanStorageNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanTableRowNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.SortAggregateNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.SortNode; | ||
| import org.apache.ignite.internal.processors.query.calcite.exec.rel.TableSpoolNode; | ||
|
|
@@ -116,6 +117,7 @@ | |
| import org.apache.ignite.internal.processors.query.calcite.util.Commons; | ||
| import org.apache.ignite.internal.processors.query.calcite.util.RexUtils; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import static org.apache.calcite.rel.RelDistribution.Type.HASH_DISTRIBUTED; | ||
| import static org.apache.ignite.internal.processors.query.calcite.util.TypeUtils.combinedRowType; | ||
|
|
@@ -368,10 +370,10 @@ private boolean hasExchange(RelNode rel) { | |
| ImmutableBitSet requiredColumns = rel.requiredColumns(); | ||
| List<SearchBounds> searchBounds = rel.searchBounds(); | ||
|
|
||
| RelDataType rowType = rel.getDataSourceRowType(); | ||
| RelDataType inputRowType = rel.getDataSourceRowType(); | ||
|
|
||
| Predicate<Row> filters = condition == null ? null : expressionFactory.predicate(condition, rowType); | ||
| Function<Row, Row> prj = projects == null ? null : expressionFactory.project(projects, rowType); | ||
| Predicate<Row> filters = condition == null ? null : expressionFactory.predicate(condition, inputRowType); | ||
| Function<Row, Row> prj = projects == null ? null : expressionFactory.project(projects, inputRowType); | ||
| RangeIterable<Row> ranges = searchBounds == null ? null : | ||
| expressionFactory.ranges(searchBounds, rel.collation(), tbl.getRowType(typeFactory)); | ||
|
|
||
|
|
@@ -382,7 +384,8 @@ private boolean hasExchange(RelNode rel) { | |
| if (idx != null && !tbl.isIndexRebuildInProgress()) { | ||
| Iterable<Row> rowsIter = idx.scan(ctx, grp, ranges, requiredColumns); | ||
|
|
||
| return new ScanStorageNode<>(tbl.name() + '.' + idx.name(), ctx, rowType, rowsIter, filters, prj); | ||
| return createStorageScan(tbl.name() + '.' + idx.name(), rel.getRowType(), inputRowType, | ||
| rowsIter, filters, prj, requiredColumns, rel.conditionColumns()); | ||
| } | ||
| else { | ||
| // Index was invalidated after planning, workaround through table-scan -> sort -> index spool. | ||
|
|
@@ -402,12 +405,10 @@ private boolean hasExchange(RelNode rel) { | |
| requiredColumns | ||
| ); | ||
|
|
||
| // If there are projects in the scan node - after the scan we already have target row type. | ||
| if (!spoolNodeRequired && projects != null) | ||
| rowType = rel.getRowType(); | ||
| RelDataType rowType = projNodeRequired ? rel.getRowType() : inputRowType; | ||
|
|
||
| Node<Row> node = new ScanStorageNode<>(tbl.name(), ctx, rowType, rowsIter, filterHasCorrelation ? null : filters, | ||
| projNodeRequired ? null : prj); | ||
| Node<Row> node = createStorageScan(tbl.name(), rowType, inputRowType, rowsIter, | ||
| filterHasCorrelation ? null : filters, projNodeRequired ? null : prj, requiredColumns, rel.conditionColumns()); | ||
|
|
||
| RelCollation collation = rel.collation(); | ||
|
|
||
|
|
@@ -438,7 +439,7 @@ private boolean hasExchange(RelNode rel) { | |
| remappedSearchBounds.add(searchBounds.get(i)); | ||
|
|
||
| // Collation and row type are already remapped taking into account requiredColumns. | ||
| ranges = expressionFactory.ranges(remappedSearchBounds, collation, rowType); | ||
| ranges = expressionFactory.ranges(remappedSearchBounds, collation, inputRowType); | ||
| } | ||
|
|
||
| IndexSpoolNode<Row> spoolNode = IndexSpoolNode.createTreeSpool( | ||
|
|
@@ -547,10 +548,10 @@ private boolean hasExchange(RelNode rel) { | |
|
|
||
| IgniteTable tbl = rel.getTable().unwrap(IgniteTable.class); | ||
|
|
||
| RelDataType rowType = rel.getDataSourceRowType(); | ||
| RelDataType inputRowType = rel.getDataSourceRowType(); | ||
|
|
||
| Predicate<Row> filters = condition == null ? null : expressionFactory.predicate(condition, rowType); | ||
| Function<Row, Row> prj = projects == null ? null : expressionFactory.project(projects, rowType); | ||
| Predicate<Row> filters = condition == null ? null : expressionFactory.predicate(condition, inputRowType); | ||
| Function<Row, Row> prj = projects == null ? null : expressionFactory.project(projects, inputRowType); | ||
|
|
||
| ColocationGroup grp = ctx.group(rel.sourceId()); | ||
|
|
||
|
|
@@ -559,12 +560,14 @@ private boolean hasExchange(RelNode rel) { | |
| if (idx != null && !tbl.isIndexRebuildInProgress()) { | ||
| Iterable<Row> rowsIter = idx.scan(ctx, grp, null, requiredColumns); | ||
|
|
||
| return new ScanStorageNode<>(tbl.name() + '.' + idx.name(), ctx, rowType, rowsIter, filters, prj); | ||
| return createStorageScan(tbl.name() + '.' + idx.name(), rel.getRowType(), inputRowType, | ||
| rowsIter, filters, prj, requiredColumns, rel.conditionColumns()); | ||
| } | ||
| else { | ||
| Iterable<Row> rowsIter = tbl.scan(ctx, grp, requiredColumns); | ||
|
|
||
| return new ScanStorageNode<>(tbl.name(), ctx, rowType, rowsIter, filters, prj); | ||
| return createStorageScan(tbl.name(), rel.getRowType(), inputRowType, rowsIter, filters, prj, | ||
| requiredColumns, rel.conditionColumns()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -942,4 +945,46 @@ private Node<Row> visit(RelNode rel) { | |
| public <T extends Node<Row>> T go(IgniteRel rel) { | ||
| return (T)visit(rel); | ||
| } | ||
|
|
||
| /** */ | ||
| private ScanStorageNode<Row> createStorageScan( | ||
| String storageName, | ||
| RelDataType outputRowType, | ||
| RelDataType inputRowType, | ||
| Iterable<Row> rowsIter, | ||
| @Nullable Predicate<Row> filter, | ||
| @Nullable Function<Row, Row> rowTransformer, | ||
| @Nullable ImmutableBitSet requiredColumns, | ||
| @Nullable ImmutableBitSet filterColumns | ||
| ) { | ||
| int fieldsCnt = outputRowType.getFieldCount(); | ||
|
|
||
| if (filter == null || filterColumns == null || filterColumns.cardinality() == fieldsCnt | ||
|
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. What if I do something like "select i1 from t where i2=?" Can it happen? |
||
| || !(rowsIter instanceof TableRowIterable)) | ||
| return new ScanStorageNode<>(storageName, ctx, outputRowType, rowsIter, filter, rowTransformer); | ||
|
|
||
| ImmutableBitSet reqCols = requiredColumns == null ? ImmutableBitSet.range(0, fieldsCnt) : requiredColumns; | ||
|
|
||
| int[] filterColMapping = reqCols.toArray(); | ||
| int[] otherColMapping = filterColMapping.clone(); | ||
|
|
||
| for (int i = 0; i < filterColMapping.length; i++) { | ||
| if (filterColumns.get(i)) | ||
| otherColMapping[i] = -1; | ||
| else | ||
| filterColMapping[i] = -1; | ||
| } | ||
|
|
||
| return new ScanTableRowNode<>( | ||
| storageName, | ||
| ctx, | ||
| outputRowType, | ||
| inputRowType, | ||
| (TableRowIterable<Object, Row>)rowsIter, | ||
| filter, | ||
| rowTransformer, | ||
| filterColMapping, | ||
| otherColMapping | ||
| ); | ||
| } | ||
| } | ||
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.
Suggestion: it looks bit confusing. Let's rename pameters and fields like this everywhere to something like:
rawToRelColMappingand/orrelToRawColMappingThere 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.
Here we have mapping from node row fields to table row columns. We have identical naming in IndexScan: idxFieldMapping - from index keys to row fields, fieldIdxMapping - from row fields to index keys.
Maybe just extend the javadoc?