forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Antalya 26.3 Hybrid: added support for segment pruning #1788
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
mkmkme
wants to merge
1
commit into
antalya-26.3
Choose a base branch
from
mkmkme/antalya-26.3/hybrid-segment-pruning
base: antalya-26.3
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
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,117 @@ | ||
| #include <Storages/HybridSegmentPruner.h> | ||
|
|
||
| #include <Core/Range.h> | ||
| #include <DataTypes/IDataType.h> | ||
| #include <Interpreters/Context.h> | ||
| #include <Interpreters/ExpressionAnalyzer.h> | ||
| #include <Interpreters/TreeRewriter.h> | ||
| #include <Parsers/ASTExpressionList.h> | ||
| #include <Parsers/ASTFunction.h> | ||
| #include <Parsers/ASTIdentifier.h> | ||
| #include <Storages/ColumnsDescription.h> | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| ASTPtr makeIdentityKeyAST(const Names & column_names) | ||
| { | ||
| auto key_ast = make_intrusive<ASTFunction>(); | ||
| key_ast->name = "tuple"; | ||
| key_ast->arguments = make_intrusive<ASTExpressionList>(); | ||
| key_ast->children.push_back(key_ast->arguments); | ||
| for (const auto & name : column_names) | ||
| key_ast->arguments->children.push_back(make_intrusive<ASTIdentifier>(name)); | ||
| return key_ast; | ||
| } | ||
|
|
||
| NamesAndTypesList filterComparable(const NamesAndTypesList & in) | ||
| { | ||
| NamesAndTypesList out; | ||
| for (const auto & c : in) | ||
| if (c.type && c.type->isComparable()) | ||
| out.push_back(c); | ||
| return out; | ||
| } | ||
|
|
||
| KeyDescription buildIdentityKey(const NamesAndTypesList & comparable_cols, ContextPtr context) | ||
| { | ||
| Names names; | ||
| names.reserve(comparable_cols.size()); | ||
| for (const auto & c : comparable_cols) | ||
| names.push_back(c.name); | ||
| return KeyDescription::getKeyFromAST( | ||
| makeIdentityKeyAST(names), | ||
| ColumnsDescription{comparable_cols}, | ||
| context); | ||
| } | ||
|
|
||
| NamesAndTypesList namesAndTypesFromKey(const KeyDescription & key) | ||
| { | ||
| NamesAndTypesList out; | ||
| for (size_t i = 0; i < key.column_names.size(); ++i) | ||
| out.emplace_back(key.column_names[i], key.data_types[i]); | ||
| return out; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| HybridSegmentPruner::HybridSegmentPruner( | ||
| const ActionsDAGWithInversionPushDown & filter_dag, | ||
| const NamesAndTypesList & hybrid_columns, | ||
| ContextPtr context_) | ||
| : identity_key(buildIdentityKey(filterComparable(hybrid_columns), context_)) | ||
| , user_condition(filter_dag, context_, | ||
| identity_key.column_names, identity_key.expression, | ||
| /*single_point=*/ false) | ||
| , context(std::move(context_)) | ||
| { | ||
| useless = identity_key.column_names.empty() || user_condition.alwaysUnknownOrTrue(); | ||
| } | ||
|
|
||
| bool HybridSegmentPruner::canBePruned(const ASTPtr & substituted_segment_predicate) const | ||
| { | ||
| if (useless || !substituted_segment_predicate) | ||
| return false; | ||
|
|
||
| auto segment_ast = substituted_segment_predicate->clone(); | ||
| auto sample = namesAndTypesFromKey(identity_key); | ||
| auto syntax_result = TreeRewriter(context).analyze(segment_ast, sample); | ||
| auto segment_dag = ExpressionAnalyzer(segment_ast, syntax_result, context).getActionsDAG(true); | ||
| ActionsDAGWithInversionPushDown segment_filter(segment_dag.getOutputs().at(0), context); | ||
|
|
||
| KeyCondition segment_condition( | ||
| segment_filter, context, | ||
| identity_key.column_names, identity_key.expression, | ||
| /*single_point=*/ false); | ||
|
|
||
| Hyperrectangle rect; | ||
| rect.reserve(identity_key.column_names.size()); | ||
|
|
||
| for (size_t i = 0; i < identity_key.column_names.size(); ++i) | ||
| { | ||
| Ranges col_ranges; | ||
| if (!segment_condition.extractPlainRangesForColumn(i, col_ranges)) | ||
| { | ||
| rect.push_back(Range::createWholeUniverse()); | ||
| continue; | ||
| } | ||
|
|
||
| if (col_ranges.empty()) | ||
| return true; | ||
|
|
||
| if (col_ranges.size() != 1) | ||
| { | ||
| rect.push_back(Range::createWholeUniverse()); | ||
| continue; | ||
| } | ||
|
|
||
| rect.push_back(col_ranges.front()); | ||
| } | ||
|
|
||
| return !user_condition.checkInHyperrectangle(rect, identity_key.data_types).can_be_true; | ||
| } | ||
|
|
||
| } | ||
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,47 @@ | ||
| #pragma once | ||
|
|
||
| #include <Core/NamesAndTypes.h> | ||
| #include <Interpreters/Context_fwd.h> | ||
| #include <Parsers/IAST_fwd.h> | ||
| #include <Storages/KeyDescription.h> | ||
| #include <Storages/MergeTree/KeyCondition.h> | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
||
| /// Hybrid-segment pruner, modeled after PartitionPruner / Iceberg::ManifestFilesPruner / | ||
| /// Paimon::PartitionPruner. | ||
| /// | ||
| /// Build one KeyCondition over the user filter (PREWHERE+WHERE represented as an | ||
| /// ActionsDAG) using all comparable Hybrid columns as the key. For each segment, build | ||
| /// a second KeyCondition from its (already watermark-substituted) predicate AST and | ||
| /// use `KeyCondition::extractPlainRangesForColumn` to obtain a Hyperrectangle (fail-open | ||
| /// to whole-universe per column when extraction is ambiguous). Then ask | ||
| /// `KeyCondition::checkInHyperrectangle(rect, types).can_be_true`. The segment can be | ||
| /// pruned iff the answer is false. | ||
| /// | ||
| /// canBePruned() returns true only when (user_filter AND segment_predicate) is provably | ||
| /// empty. It returns false in all other cases — unsupported segment shapes, missing user | ||
| /// filter, exceptions — so the caller falls back to scanning the segment normally. | ||
| class HybridSegmentPruner | ||
| { | ||
| public: | ||
| HybridSegmentPruner( | ||
| const ActionsDAGWithInversionPushDown & filter_dag, | ||
| const NamesAndTypesList & hybrid_columns, | ||
| ContextPtr context); | ||
|
|
||
| bool canBePruned(const ASTPtr & substituted_segment_predicate) const; | ||
|
|
||
| /// True if the user filter is unrecognizable / always-true on the Hybrid key columns: | ||
| /// no segment can ever be pruned, so callers can short-circuit. | ||
| bool isUseless() const { return useless; } | ||
|
|
||
| private: | ||
| KeyDescription identity_key; | ||
| KeyCondition user_condition; | ||
| ContextPtr context; | ||
| bool useless = false; | ||
| }; | ||
|
|
||
| } |
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
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.
Why is it that when there are no ranges you can actually prune the segment?