Skip to content
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

Fix ClickHouse-sourced dictionaries with an explicit query #56236

Merged
merged 1 commit into from Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Dictionaries/ExternalQueryBuilder.cpp
Expand Up @@ -396,18 +396,20 @@ std::string ExternalQueryBuilder::composeLoadKeysQuery(
}
else
{
writeString(query, out);

auto condition_position = query.find(CONDITION_PLACEHOLDER_TO_REPLACE_VALUE);
if (condition_position == std::string::npos)
{
writeString(" WHERE ", out);
writeString("SELECT * FROM (", out);
writeString(query, out);
writeString(") WHERE ", out);
composeKeysCondition(key_columns, requested_rows, method, partition_key_prefix, out);
writeString(";", out);

return out.str();
}

writeString(query, out);

WriteBufferFromOwnString condition_value_buffer;
composeKeysCondition(key_columns, requested_rows, method, partition_key_prefix, condition_value_buffer);
const auto & condition_value = condition_value_buffer.str();
Expand Down
@@ -0,0 +1 @@
b
20 changes: 20 additions & 0 deletions tests/queries/0_stateless/02907_clickhouse_dictionary_bug.sh
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Tags: zookeeper

CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh

${CLICKHOUSE_CLIENT} -n -q "
DROP DICTIONARY IF EXISTS 02907_dictionary;
DROP TABLE IF EXISTS 02907_table;

CREATE TABLE 02907_table (A String, B String) ENGINE=Memory AS SELECT 'a', 'b';
CREATE DICTIONARY 02907_dictionary(A String, B String) PRIMARY KEY A
SOURCE(CLICKHOUSE(QUERY \$\$ SELECT A, B FROM ${CLICKHOUSE_DATABASE}.02907_table ORDER BY A DESC LIMIT 1 BY A \$\$))
LAYOUT(complex_key_direct());

SELECT dictGet('02907_dictionary','B','a');

DROP DICTIONARY 02907_dictionary;
DROP TABLE 02907_table;"