Skip to content

Commit

Permalink
Merge pull request ClickHouse#44716 from ClickHouse/vdimir/merge_engi…
Browse files Browse the repository at this point in the history
…ne_nullable
  • Loading branch information
vdimir committed Jan 9, 2023
2 parents 9af70c1 + 2ff0d7e commit 75e9a45
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Storages/StorageMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,43 @@ bool StorageMerge::canMoveConditionsToPrewhere() const
/// If new table that matches regexp for current storage and doesn't support PREWHERE
/// will appear after this check and before calling "read" method, the optimized query may fail.
/// Since it's quite rare case, we just ignore this possibility.
const auto & table_doesnt_support_prewhere = getFirstTable([](const auto & table) { return !table->canMoveConditionsToPrewhere(); });
bool can_move = (table_doesnt_support_prewhere == nullptr);

return getFirstTable([](const auto & table) { return !table->canMoveConditionsToPrewhere(); }) == nullptr;
if (!can_move)
return false;

if (!getInMemoryMetadataPtr())
return false;

std::unordered_map<std::string, const IDataType *> column_types;
for (const auto & name_type : getInMemoryMetadataPtr()->getColumns().getAll())
{
column_types.emplace(name_type.name, name_type.type.get());
}

/// Check that all tables have the same column types, otherwise prewhere will fail
forEachTable([&](const StoragePtr & table)
{
const auto & metadata_ptr = table->getInMemoryMetadataPtr();
if (!metadata_ptr)
can_move = false;

if (!can_move)
return;

for (const auto & column : metadata_ptr->getColumns().getAll())
{
const auto * src_type = column_types[column.name];
if (src_type && !src_type->equals(*column.type))
{
can_move = false;
return;
}
}
});

return can_move;
}

bool StorageMerge::mayBenefitFromIndexForIn(const ASTPtr & left_in_operand, ContextPtr query_context, const StorageMetadataPtr & /*metadata_snapshot*/) const
Expand Down
Empty file.
18 changes: 18 additions & 0 deletions tests/queries/0_stateless/02518_merge_engine_nullable_43324.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

DROP TABLE IF EXISTS foo;
DROP TABLE IF EXISTS foo__fuzz_0;
DROP TABLE IF EXISTS foo_merge;

CREATE TABLE foo (`Id` Int32, `Val` Int32) ENGINE = MergeTree ORDER BY Id;
CREATE TABLE foo__fuzz_0 (`Id` Int64, `Val` Nullable(Int32)) ENGINE = MergeTree ORDER BY Id;

INSERT INTO foo SELECT number, number % 5 FROM numbers(10);
INSERT INTO foo__fuzz_0 SELECT number, number % 5 FROM numbers(10);

CREATE TABLE merge1 AS foo ENGINE = Merge(currentDatabase(), '^foo');
CREATE TABLE merge2 (`Id` Int32, `Val` Int32) ENGINE = Merge(currentDatabase(), '^foo');
CREATE TABLE merge3 (`Id` Int32, `Val` Int32) ENGINE = Merge(currentDatabase(), '^foo__fuzz_0');

SELECT * FROM merge1 WHERE Val = 3 AND Val = 1;
SELECT * FROM merge2 WHERE Val = 3 AND Val = 1;
SELECT * FROM merge3 WHERE Val = 3 AND Val = 1;

0 comments on commit 75e9a45

Please sign in to comment.