Skip to content

Commit

Permalink
Merge pull request #55394 from ClickHouse/cherrypick/23.3/b619d07baf2…
Browse files Browse the repository at this point in the history
…9ce4ae252cb2ada4f1510c68b41b0

Cherry pick #55353 to 23.3: Fix trash optimization (up to a certain extent)
  • Loading branch information
robot-ch-test-poll3 committed Oct 9, 2023
2 parents c37ee3d + b619d07 commit b0cb202
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/Interpreters/LogicalExpressionsOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,41 @@ inline ASTs & getFunctionOperands(const ASTFunction * or_function)

bool LogicalExpressionsOptimizer::isLowCardinalityEqualityChain(const std::vector<ASTFunction *> & functions) const
{
if (functions.size() > 1)
if (functions.size() <= 1)
return false;

if (!functions[0])
return false;

/// Check if the identifier has LowCardinality type.
auto & first_operands = getFunctionOperands(functions.at(0));

if (first_operands.empty())
return false;

if (!first_operands[0])
return false;

const auto * identifier = first_operands.at(0)->as<ASTIdentifier>();
if (!identifier)
return false;

auto pos = IdentifierSemantic::getMembership(*identifier);
if (!pos)
pos = IdentifierSemantic::chooseTableColumnMatch(*identifier, tables_with_columns, true);

if (!pos)
return false;

if (*pos >= tables_with_columns.size())
return false;

if (auto data_type_and_name = tables_with_columns.at(*pos).columns.tryGetByName(identifier->shortName()))
{
/// Check if identifier is LowCardinality type
auto & first_operands = getFunctionOperands(functions[0]);
const auto * identifier = first_operands[0]->as<ASTIdentifier>();
if (identifier)
{
auto pos = IdentifierSemantic::getMembership(*identifier);
if (!pos)
pos = IdentifierSemantic::chooseTableColumnMatch(*identifier, tables_with_columns, true);
if (pos)
{
if (auto data_type_and_name = tables_with_columns[*pos].columns.tryGetByName(identifier->shortName()))
{
if (typeid_cast<const DataTypeLowCardinality *>(data_type_and_name->type.get()))
return true;
}
}
}
if (typeid_cast<const DataTypeLowCardinality *>(data_type_and_name->type.get()))
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
3 changes: 3 additions & 0 deletions tests/queries/0_stateless/02893_trash_optimization.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM merge('system', '^one$') AS one
WHERE (one.dummy = 0) OR (one.dummy = 1);

0 comments on commit b0cb202

Please sign in to comment.