-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7618] Add filter pushdown support to the file adapter's CSV table implementation #5048
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
Diveyam-Mishra
wants to merge
3
commits into
apache:main
Choose a base branch
from
Diveyam-Mishra:CALCITE-7618
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
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
85 changes: 85 additions & 0 deletions
85
file/src/main/java/org/apache/calcite/adapter/file/CsvFilterTableScanRule.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,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.calcite.adapter.file; | ||
|
|
||
| import org.apache.calcite.plan.RelOptRuleCall; | ||
| import org.apache.calcite.plan.RelRule; | ||
| import org.apache.calcite.rel.logical.LogicalFilter; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.rex.RexUtil; | ||
|
|
||
| import org.immutables.value.Value; | ||
|
|
||
| /** | ||
| * Planner rule that pushes filter predicates into a | ||
| * {@link CsvTableScan}. | ||
| * | ||
| * <p>Any predicate expressible as a {@link org.apache.calcite.rex.RexNode} | ||
| * (including AND, OR, NOT, IS NULL, comparisons, LIKE, etc.) can be pushed | ||
| * down. The condition is compiled at plan time via | ||
| * {@link org.apache.calcite.adapter.enumerable.RexToLixTranslator} into a | ||
| * Java {@link org.apache.calcite.linq4j.function.Predicate1} and applied | ||
| * directly on the enumerable produced by the scan, so no rows that fail the | ||
| * predicate are ever materialised. | ||
| * | ||
| * @see FileRules#FILTER_SCAN | ||
| */ | ||
| @Value.Enclosing | ||
| public class CsvFilterTableScanRule | ||
| extends RelRule<CsvFilterTableScanRule.Config> { | ||
|
|
||
| /** Creates a CsvFilterTableScanRule. */ | ||
| protected CsvFilterTableScanRule(Config config) { | ||
| super(config); | ||
| } | ||
|
|
||
| @Override public void onMatch(RelOptRuleCall call) { | ||
| final LogicalFilter filter = call.rel(0); | ||
| final CsvTableScan scan = call.rel(1); | ||
|
|
||
| // Compose a conjunction of the existing condition and the new one. | ||
| final RexNode newCondition; | ||
| if (scan.condition == null) { | ||
| newCondition = filter.getCondition(); | ||
| } else { | ||
| newCondition = | ||
| RexUtil.composeConjunction(scan.getCluster().getRexBuilder(), | ||
| java.util.Arrays.asList(scan.condition, filter.getCondition())); | ||
| } | ||
|
|
||
| // Build a new scan that carries the pushed-down filter condition. | ||
| final CsvTableScan newScan = | ||
| new CsvTableScan(scan.getCluster(), scan.getTable(), scan.csvTable, | ||
| scan.fields, newCondition); | ||
|
|
||
| call.transformTo(newScan); | ||
| } | ||
|
|
||
| /** Rule configuration. */ | ||
| @Value.Immutable(singleton = false) | ||
| public interface Config extends RelRule.Config { | ||
| Config DEFAULT = ImmutableCsvFilterTableScanRule.Config.builder() | ||
| .withOperandSupplier(b0 -> | ||
| b0.operand(LogicalFilter.class).oneInput(b1 -> | ||
| b1.operand(CsvTableScan.class).noInputs())) | ||
| .build(); | ||
|
|
||
| @Override default CsvFilterTableScanRule toRule() { | ||
| return new CsvFilterTableScanRule(this); | ||
| } | ||
| } | ||
| } |
141 changes: 141 additions & 0 deletions
141
file/src/main/java/org/apache/calcite/adapter/file/CsvProjectFilterTableScanRule.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,141 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.calcite.adapter.file; | ||
|
|
||
| import org.apache.calcite.plan.RelOptRuleCall; | ||
| import org.apache.calcite.plan.RelRule; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.logical.LogicalFilter; | ||
| import org.apache.calcite.rel.logical.LogicalProject; | ||
| import org.apache.calcite.rex.RexInputRef; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.rex.RexUtil; | ||
|
|
||
| import org.immutables.value.Value; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Planner rule that matches a {@link LogicalProject} on a {@link LogicalFilter} | ||
| * on a {@link CsvTableScan}, and pushes filter predicates into the scan. | ||
| * | ||
| * @see FileRules#PROJECT_FILTER_SCAN | ||
| */ | ||
| @Value.Enclosing | ||
| public class CsvProjectFilterTableScanRule | ||
| extends RelRule<CsvProjectFilterTableScanRule.Config> { | ||
|
|
||
| /** Creates a CsvProjectFilterTableScanRule. */ | ||
| protected CsvProjectFilterTableScanRule(Config config) { | ||
| super(config); | ||
| } | ||
|
|
||
| @Override public void onMatch(RelOptRuleCall call) { | ||
| final LogicalProject project = call.rel(0); | ||
| final LogicalFilter filter = call.rel(1); | ||
| final CsvTableScan scan = call.rel(2); | ||
|
|
||
| // Find all input fields referenced by the project expressions | ||
| final java.util.Set<Integer> projectInputFields = new java.util.HashSet<>(); | ||
| for (RexNode proj : project.getProjects()) { | ||
| proj.accept(new org.apache.calcite.rex.RexVisitorImpl<Void>(true) { | ||
| @Override public Void visitInputRef(RexInputRef inputRef) { | ||
| projectInputFields.add(inputRef.getIndex()); | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Find all input fields referenced by the filter condition | ||
| final java.util.Set<Integer> filterInputFields = new java.util.HashSet<>(); | ||
| filter.getCondition().accept(new org.apache.calcite.rex.RexVisitorImpl<Void>(true) { | ||
| @Override public Void visitInputRef(RexInputRef inputRef) { | ||
| filterInputFields.add(inputRef.getIndex()); | ||
| return null; | ||
| } | ||
| }); | ||
|
|
||
| // Union the projected/referenced indices | ||
| final java.util.Set<Integer> neededProjectedIndices = new java.util.TreeSet<>(); | ||
| neededProjectedIndices.addAll(projectInputFields); | ||
| neededProjectedIndices.addAll(filterInputFields); | ||
|
|
||
| // Map needed scan projected indices to full-table indices | ||
| final int[] newFields = new int[neededProjectedIndices.size()]; | ||
| int k = 0; | ||
| for (int idx : neededProjectedIndices) { | ||
| newFields[k++] = scan.fields[idx]; | ||
|
Diveyam-Mishra marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Build index map from old projected index to new index in newFields | ||
| final java.util.Map<Integer, Integer> indexMap = new java.util.HashMap<>(); | ||
| int newIdx = 0; | ||
| for (int idx : neededProjectedIndices) { | ||
| indexMap.put(idx, newIdx++); | ||
| } | ||
|
|
||
| // Create shuttle to map RexInputRef indices | ||
| final org.apache.calcite.rex.RexShuttle shuttle = new org.apache.calcite.rex.RexShuttle() { | ||
| @Override public RexNode visitInputRef(RexInputRef inputRef) { | ||
| final Integer mapped = indexMap.get(inputRef.getIndex()); | ||
| if (mapped == null) { | ||
| return inputRef; | ||
| } | ||
| return scan.getCluster().getRexBuilder().makeInputRef(inputRef.getType(), mapped); | ||
| } | ||
| }; | ||
|
|
||
| final RexNode mappedCondition = filter.getCondition().accept(shuttle); | ||
| final List<RexNode> mappedProjects = new java.util.ArrayList<>(); | ||
| for (RexNode proj : project.getProjects()) { | ||
| mappedProjects.add(proj.accept(shuttle)); | ||
| } | ||
|
|
||
| final RexNode finalCondition; | ||
| if (scan.condition == null) { | ||
| finalCondition = mappedCondition; | ||
| } else { | ||
| finalCondition = | ||
| RexUtil.composeConjunction(scan.getCluster().getRexBuilder(), | ||
| java.util.Arrays.asList(scan.condition.accept(shuttle), mappedCondition)); | ||
| } | ||
|
|
||
| final CsvTableScan newScan = | ||
| new CsvTableScan(scan.getCluster(), scan.getTable(), scan.csvTable, | ||
| newFields, finalCondition); | ||
|
|
||
| final RelNode result = | ||
| project.copy(project.getTraitSet(), newScan, mappedProjects, project.getRowType()); | ||
|
|
||
| call.transformTo(result); | ||
| } | ||
|
|
||
| /** Rule configuration. */ | ||
| @Value.Immutable(singleton = false) | ||
| public interface Config extends RelRule.Config { | ||
| Config DEFAULT = ImmutableCsvProjectFilterTableScanRule.Config.builder() | ||
| .withOperandSupplier(b0 -> | ||
|
Diveyam-Mishra marked this conversation as resolved.
|
||
| b0.operand(LogicalProject.class).oneInput(b1 -> | ||
| b1.operand(LogicalFilter.class).oneInput(b2 -> | ||
| b2.operand(CsvTableScan.class).noInputs()))) | ||
| .build(); | ||
|
|
||
| @Override default CsvProjectFilterTableScanRule toRule() { | ||
| return new CsvProjectFilterTableScanRule(this); | ||
| } | ||
| } | ||
| } | ||
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.
This javadoc seems out of date