-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: remap query virtual columns to clustered base table columns when equivalent #19659
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
clintropolis
wants to merge
6
commits into
apache:master
Choose a base branch
from
clintropolis:clustered-vc-equivalence
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
6 commits
Select commit
Hold shift + click to select a range
f7de9b6
feat: remap query virtual columns to clustered base table columns whe…
clintropolis b76b28a
more test
clintropolis 98c1abc
Merge remote-tracking branch 'upstream/master' into clustered-vc-equi…
clintropolis da07e9a
Merge remote-tracking branch 'upstream/master' into clustered-vc-equi…
clintropolis 3d678dc
better
clintropolis 19267a7
fixes
clintropolis 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 |
|---|---|---|
|
|
@@ -21,11 +21,17 @@ | |
|
|
||
| import org.apache.druid.query.filter.Filter; | ||
| import org.apache.druid.segment.CursorBuildSpec; | ||
| import org.apache.druid.segment.VirtualColumn; | ||
| import org.apache.druid.segment.VirtualColumns; | ||
| import org.apache.druid.segment.filter.FalseFilter; | ||
| import org.apache.druid.segment.filter.TrueFilter; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
|
|
@@ -39,14 +45,17 @@ public final class ClusterGroupQueryPlan | |
| { | ||
| private final List<TableClusterGroupSpec> survivingGroups; | ||
| private final Function<TableClusterGroupSpec, Filter> rewriter; | ||
| private final Map<String, String> virtualColumnRemap; | ||
|
|
||
| ClusterGroupQueryPlan( | ||
| List<TableClusterGroupSpec> survivingGroups, | ||
| Function<TableClusterGroupSpec, Filter> rewriter | ||
| Function<TableClusterGroupSpec, Filter> rewriter, | ||
| Map<String, String> virtualColumnRemap | ||
| ) | ||
| { | ||
| this.survivingGroups = survivingGroups; | ||
| this.rewriter = rewriter; | ||
| this.virtualColumnRemap = virtualColumnRemap; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -58,6 +67,16 @@ public List<TableClusterGroupSpec> survivingGroups() | |
| return survivingGroups; | ||
| } | ||
|
|
||
| /** | ||
| * Query-level mapping of {@code queryVirtualColumnOutputName -> materializedColumnName} for query virtual columns | ||
| * that are equivalent to a clustering column or a non-clustering materialized column in the clustered base table. | ||
| * Empty when no query virtual column has a materialized equivalent (or there are no query virtual columns). | ||
| */ | ||
| public Map<String, String> virtualColumnRemap() | ||
| { | ||
| return virtualColumnRemap; | ||
| } | ||
|
|
||
| /** | ||
| * Per-group rewrite of the query's filter against {@code group}'s constant clustering tuple: clustering-column | ||
| * leaves collapse to {@link TrueFilter} / {@link FalseFilter} and fold through AND / OR / NOT, so the rewritten | ||
|
|
@@ -76,21 +95,60 @@ public Filter rewriteFor(TableClusterGroupSpec group) | |
|
|
||
| /** | ||
| * Rebuild {@code spec} for {@code group}'s per-group cursor by swapping in this plan's per-group filter rewrite | ||
| * (see {@link #rewriteFor}). Returns {@code spec} unchanged when there is no filter, or when the rewrite is | ||
| * identical to the original (no clustering leaves folded), so the common no-op case allocates nothing. Shared by | ||
| * the {@link org.apache.druid.segment.QueryableIndexCursorFactory} (historical) and | ||
| * {@link org.apache.druid.segment.incremental.IncrementalIndexCursorFactory} (realtime) clustered dispatch. | ||
| * (see {@link #rewriteFor}) and, when {@link #virtualColumnRemap()} is non-empty, substituting any query virtual | ||
| * columns that are equivalent to a materialized column. | ||
| * <p> | ||
| * When there is a remap, the matched query virtual columns (the remap keys) are dropped from the spec's virtual | ||
| * columns, and the per-group filter's required columns are rewritten via the remap so that non-clustering-VC filter | ||
| * leaves point at the materialized physical column (clustering-VC leaves were already folded to TRUE / FALSE by the | ||
| * per-group rewrite walk, so they won't reference the dropped virtual columns). The grouping / select / aggregator | ||
| * references are served instead by the {@link org.apache.druid.segment.RemapColumnSelectorFactory} the cursor | ||
| * factory wraps around the {@link ClusteringColumnSelectorFactory}. | ||
| */ | ||
| public CursorBuildSpec rebuildCursorBuildSpec(CursorBuildSpec spec, TableClusterGroupSpec group) | ||
| { | ||
| if (spec.getFilter() == null) { | ||
| return spec; | ||
| if (virtualColumnRemap.isEmpty()) { | ||
| if (spec.getFilter() == null) { | ||
| return spec; | ||
| } | ||
| final Filter rewritten = rewriteFor(group); | ||
| if (rewritten == spec.getFilter()) { | ||
| return spec; | ||
| } | ||
| return CursorBuildSpec.builder(spec).setFilter(rewritten).build(); | ||
| } | ||
| final Filter rewritten = rewriteFor(group); | ||
| if (rewritten == spec.getFilter()) { | ||
| return spec; | ||
|
|
||
| // Drop the remapped query virtual columns from the per-group spec; the materialized columns they map to are | ||
| // either the per-group constant clustering column or a per-group physical column, both served directly by the | ||
| // ClusteringColumnSelectorFactory (the cursor factory additionally wraps it with a RemapColumnSelectorFactory so | ||
| // the original query VC names resolve to the materialized columns). | ||
| final List<VirtualColumn> prunedVcs = new ArrayList<>(); | ||
|
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. naming seems backwards. aren't these actually whare are not being pruned out of the query? |
||
| for (VirtualColumn vc : spec.getVirtualColumns().getVirtualColumns()) { | ||
| if (!virtualColumnRemap.containsKey(vc.getOutputName())) { | ||
| prunedVcs.add(vc); | ||
| } | ||
| } | ||
| return CursorBuildSpec.builder(spec).setFilter(rewritten).build(); | ||
|
|
||
| // Per-group filter rewrite first (folds clustering leaves), then remap any surviving non-clustering-VC leaves to | ||
| // the materialized physical column. | ||
| Filter rewritten = spec.getFilter() == null ? null : rewriteFor(group); | ||
| if (rewritten != null) { | ||
| rewritten = Projections.rewriteFilterRequiredColumns(rewritten, virtualColumnRemap); | ||
| } | ||
|
|
||
| final CursorBuildSpec.CursorBuildSpecBuilder builder = CursorBuildSpec.builder(spec) | ||
| .setFilter(rewritten) | ||
| .setVirtualColumns(VirtualColumns.create(prunedVcs)); | ||
|
|
||
| // The dropped query VCs now read their materialized target columns, so add them | ||
| final Set<String> physicalColumns = spec.getPhysicalColumns(); | ||
| if (physicalColumns != null) { | ||
| final Set<String> withTargets = new HashSet<>(physicalColumns); | ||
| withTargets.addAll(virtualColumnRemap.values()); | ||
| builder.setPhysicalColumns(withTargets); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
| } | ||
|
|
||
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 is a not entirely true.
walkClusterGroupFilteronly folds Equality, In, and Null. Everything else falls through unchanged. This will result in wrong results when something like a Range filter on a query vc with equivalent clustering column survives the walk.Repro wrong empty results: