Repository navigation
fix the bug that LogicalExpressionOptimizerPass lost logical type of … - #66344
Conversation
|
Please, add a test. |
|
This is an automated comment for commit 82e13d7 with description of existing statuses. It's updated for the latest CI running ✅ Click here to open a full report in a separate page Successful checks
|
done |
|
failures are unrelated:
|
|
|
CI passed and can this pr be merged bro ? @novikd : ) |
| } | ||
|
|
||
| auto rhs_node = std::make_shared<ConstantNode>(std::move(args)); | ||
| auto rhs_node = std::make_shared<ConstantNode>(std::move(args), std::make_shared<DataTypeTuple>(std::move(tuple_element_types))); |
There was a problem hiding this comment.
Doesn't it also require adding a conversation of tuple elements to IN first argument type? I'm not sure if the semantics is correct right now.
There was a problem hiding this comment.
I'm not quite sure what this question is about, but regarding this fix - the issue is that DB::Field doesn't have DateTime types (replacing them by some Decimal I believe), so ConstantNode created without explicit type argument will derive types from Field's in args
|
Hi @chloro-pn — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
…rite convertNotEqualsChainToNotIn built the right-hand tuple as ConstantNode(Tuple), so the element types were re-derived from the Field values by FieldToDataType. That collapses DateTime to UInt32, DateTime64 to a plain Decimal64, an Enum to its underlying integer and so on, and the rewritten notIn then compares against different values than the notEquals chain it replaced. Two observed effects, both with optimize_min_inequality_conjunction_chain_length reached: d Date, ts DateTime d != parseDateTimeBestEffort(...) AND ... -- matched every row ts != toDate(...) AND ... -- matched every row The results were silently wrong: with the rewrite disabled the same queries return one row, with it enabled they return all four. tryReplaceOrEqualsChainWithIn already passes an explicit DataTypeTuple built from ConstantNode::getResultType() for this exact reason (the OR -> IN direction had the same bug, fixed in ClickHouse#66344). Do the same here. Found by the automated review of ClickHouse#111924.
The equals chain has passed the resolved constant types to the set since ClickHouse#66344, but preserving the types is not the whole requirement: the set still converts each element to the expression's type while the comparison is evaluated in the wider of the two. d Date d = toDateTime('2020-01-01 12:00:00') OR ... -- matched 3 rows, should match none Every row differs from a non-midnight constant, so the chain selects nothing, but each constant truncates onto a row and the set matches it. Same mechanism and same fix as the notEquals direction; a NULL constant is excluded before the chain is collected, so `k = 1 OR k = NULL` is unaffected.
…rite convertNotEqualsChainToNotIn built the right-hand tuple as ConstantNode(Tuple), so the element types were re-derived from the Field values by FieldToDataType. That collapses DateTime to UInt32, DateTime64 to a plain Decimal64, an Enum to its underlying integer and so on, and the rewritten notIn then compares against different values than the notEquals chain it replaced. Two observed effects, both with optimize_min_inequality_conjunction_chain_length reached: d Date, ts DateTime d != parseDateTimeBestEffort(...) AND ... -- matched every row ts != toDate(...) AND ... -- matched every row The results were silently wrong: with the rewrite disabled the same queries return one row, with it enabled they return all four. tryReplaceOrEqualsChainWithIn already passes an explicit DataTypeTuple built from ConstantNode::getResultType() for this exact reason (the OR -> IN direction had the same bug, fixed in ClickHouse#66344). Do the same here. Found by the automated review of ClickHouse#111924.
The equals chain has passed the resolved constant types to the set since ClickHouse#66344, but preserving the types is not the whole requirement: the set still converts each element to the expression's type while the comparison is evaluated in the wider of the two. d Date d = toDateTime('2020-01-01 12:00:00') OR ... -- matched 3 rows, should match none Every row differs from a non-midnight constant, so the chain selects nothing, but each constant truncates onto a row and the set matches it. Same mechanism and same fix as the notEquals direction; a NULL constant is excluded before the chain is collected, so `k = 1 OR k = NULL` is unaffected.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
fix the bug that LogicalExpressionOptimizerPass lost logical type of constant. closes #64487
Documentation entry for user-facing changes
CI Settings (Only check the boxes if you know what you are doing):