Repository navigation
Support direct reading from text index as a hint for functions like and equals - #88550
Conversation
|
Workflow [PR], commit [405dd22] Summary: ❌
|
86d4180 to
cd23c8e
Compare
george-larionov
left a comment
There was a problem hiding this comment.
I ran out of time, I will finish the review tomorrow.
george-larionov
left a comment
There was a problem hiding this comment.
Overall it looks good to me, it is a nice optimization. Just a few comments and suggestions.
| return std::nullopt; | ||
| /// Do not optimize if there are multiple text indexes set for the same expression. | ||
| /// It is abgious which index to use. However, we allow to use several indexes for different expressions. | ||
| /// for example, we can use indexes both for mapKeys(m) and mapValues(m) in one function m['key'] = 'value'. |
There was a problem hiding this comment.
Can you add a test for this case?
| return std::nullopt; | ||
|
|
||
| if (function_node.children.size() != 2) | ||
| if (!WhichDataType(function_node.result_type).isUInt8()) |
There was a problem hiding this comment.
in what situation would this be hit? Can we test for it?
There was a problem hiding this comment.
@george-larionov The initial test was because the first version of the optimization was implemented only for hasToken and searchAll|Any which had an specific prototype and the check was replacing function nodes with other function nodes with similar prototypes.
I suppose that the new test is to force a stop here in case some other functions are added in the future; because the new version replaces the function with an UInt column.
There was a problem hiding this comment.
It's an early exit for functions that cannot be used in the text index to avoid further heavy analysis of them.
There was a problem hiding this comment.
Please add add a brief comment in the source code.
Ergus
left a comment
There was a problem hiding this comment.
Overall LGTM but:
I added a few questions; the most concerning one for me is the probabilistic approach to estimate cardinality because I find it too heuristic for a 100% accuracy; and normal language doesn't follow a uniform words distribution. But I am pretty sure that I could be missing something there.
Some changes will be needed to accomplish with the changes for empty matches merged in #89757
There are also some conflicts with master that should be fixed before merging.
An external contributed PR #89977 should be updated if this merges before because there are conflicts
| return &dag.addFunction(node->function_base, new_children, ""); | ||
| } | ||
|
|
||
| return node; |
There was a problem hiding this comment.
In the DAG there is only one list of node objects and the rest are pointers to the nodes in that list.
In that case if we iterate over the list and replace node info in place it is just simpler and faster. Don't need to store and search a ReplacementMap, don't need to iterate recursively over all the DAG and don't need to update any reference in a children because memory address won't change and the list entries are unique.
Did we have an issue with that approach?
Because the "and" replacement implemented bellow I think is also possible with the "old" method.
There was a problem hiding this comment.
Technically, in-place modification was incorrect because we didn't update the result_name of the parent nodes; however, we should do this. It didn't cause bugs, but left a DAG in an inconsistent state. Also, I'd like to avoid modifying private fields of ActionsDAG inplace, and use public methods like addInput and addFunction. Iterating a DAG one extra time is not a big performance issue.
| return std::nullopt; | ||
|
|
||
| if (function_node.children.size() != 2) | ||
| if (!WhichDataType(function_node.result_type).isUInt8()) |
There was a problem hiding this comment.
@george-larionov The initial test was because the first version of the optimization was implemented only for hasToken and searchAll|Any which had an specific prototype and the check was replacing function nodes with other function nodes with similar prototypes.
I suppose that the new test is to force a stop here in case some other functions are added in the future; because the new version replaces the function with an UInt column.
| const String new_filter_column_name = filter_column_node.result_name; | ||
| auto new_filter_column_name = result.filter_node->result_name; | ||
| filter_node->step = std::make_unique<FilterStep>(read_from_merge_tree_step->getOutputHeader(), filter_dag.clone(), new_filter_column_name, removes_filter_column); | ||
| } |
There was a problem hiding this comment.
There is another PR: #89977 That adds support for PREWHERE and will conflict with these changes.
There was a problem hiding this comment.
@CurtizJ I (and likely users too) lost the overview
- which function,
- with which tokenizer,
is (not) supported by direct read or by by direct read as a hint. Could you please update https://clickhouse.com/docs/engines/table-engines/mergetree-family/invertedindexes#direct-read as well? While at it, please also document (user-level docs) how the hint mechanism works.
| return std::nullopt; | ||
|
|
||
| if (function_node.children.size() != 2) | ||
| if (!WhichDataType(function_node.result_type).isUInt8()) |
There was a problem hiding this comment.
Please add add a brief comment in the source code.
|
I'll add documentation in a separate PR. The documentation of inverted indexes requires some other corrections, though. |
|
Failed tests are flaky according to the cidb. |
|
Is this optimization expected to work for LowCardinality columns? I'm unable to leverage it in this setup (fiddle): However, using an ordinary String column works: |
|
@EmeraldShift I opened a bug - #90778 |
|
@CurtizJ, unfortunately, direct reading from the skip index contains a bug: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=95063&sha=d2a17cd12b4ee95370796d65ae52e9f889f13672&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 Either we will fix it, or we will revert the feature entirely. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved filtering performance for predicates with functions such as
like,equals,has, and others by leveraging an additional preliminary filter built from the text index. This optimization is enabled via thequery_plan_text_index_add_hintsetting. Improved usage of the text index for columns ofMapdata type.