mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-21 17:20:50 +00:00
dbms: allow specifying expressions for dictionary attributes [#METR-16432]
This commit is contained in:
parent
ff4fb2784a
commit
7f38ad390e
@ -20,10 +20,12 @@ const auto max_connections = 16;
|
||||
class ClickHouseDictionarySource final : public IDictionarySource
|
||||
{
|
||||
public:
|
||||
ClickHouseDictionarySource(const Poco::Util::AbstractConfiguration & config,
|
||||
ClickHouseDictionarySource(const DictionaryStructure & dict_struct,
|
||||
const Poco::Util::AbstractConfiguration & config,
|
||||
const std::string & config_prefix,
|
||||
Block & sample_block, Context & context)
|
||||
: host{config.getString(config_prefix + ".host")},
|
||||
: dict_struct{dict_struct},
|
||||
host{config.getString(config_prefix + ".host")},
|
||||
port(config.getInt(config_prefix + ".port")),
|
||||
user{config.getString(config_prefix + ".user", "")},
|
||||
password{config.getString(config_prefix + ".password", "")},
|
||||
@ -41,7 +43,8 @@ public:
|
||||
|
||||
/// copy-constructor is provided in order to support cloneability
|
||||
ClickHouseDictionarySource(const ClickHouseDictionarySource & other)
|
||||
: host{other.host}, port{other.port}, user{other.user}, password{other.password},
|
||||
: dict_struct{other.dict_struct},
|
||||
host{other.host}, port{other.port}, user{other.user}, password{other.password},
|
||||
db{other.db}, table{other.table},
|
||||
where{other.where},
|
||||
sample_block{other.sample_block}, context(other.context),
|
||||
@ -90,14 +93,19 @@ private:
|
||||
WriteBufferFromString out{query};
|
||||
writeString("SELECT ", out);
|
||||
|
||||
auto first = true;
|
||||
for (const auto idx : ext::range(0, sample_block.columns()))
|
||||
{
|
||||
if (!first)
|
||||
writeString(", ", out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
|
||||
writeString(sample_block.getByPosition(idx).name, out);
|
||||
first = false;
|
||||
for (const auto & attr : dict_struct.attributes)
|
||||
{
|
||||
writeString(", ", out);
|
||||
|
||||
if (!attr.expression.empty())
|
||||
{
|
||||
writeString(attr.expression, out);
|
||||
writeString(" AS ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(attr.name, out);
|
||||
}
|
||||
|
||||
writeString(" FROM ", out);
|
||||
@ -128,17 +136,21 @@ private:
|
||||
WriteBufferFromString out{query};
|
||||
writeString("SELECT ", out);
|
||||
|
||||
auto first = true;
|
||||
for (const auto idx : ext::range(0, sample_block.columns()))
|
||||
{
|
||||
if (!first)
|
||||
writeString(", ", out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
|
||||
writeString(sample_block.getByPosition(idx).name, out);
|
||||
first = false;
|
||||
for (const auto & attr : dict_struct.attributes)
|
||||
{
|
||||
writeString(", ", out);
|
||||
|
||||
if (!attr.expression.empty())
|
||||
{
|
||||
writeString(attr.expression, out);
|
||||
writeString(" AS ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(attr.name, out);
|
||||
}
|
||||
|
||||
const auto & id_column_name = sample_block.getByPosition(0).name;
|
||||
writeString(" FROM ", out);
|
||||
if (!db.empty())
|
||||
{
|
||||
@ -155,10 +167,10 @@ private:
|
||||
writeString(" AND ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(id_column_name, out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
writeString(" IN (", out);
|
||||
|
||||
first = true;
|
||||
auto first = true;
|
||||
for (const auto id : ids)
|
||||
{
|
||||
if (!first)
|
||||
@ -174,6 +186,7 @@ private:
|
||||
return query;
|
||||
}
|
||||
|
||||
const DictionaryStructure dict_struct;
|
||||
const std::string host;
|
||||
const UInt16 port;
|
||||
const std::string user;
|
||||
|
@ -64,11 +64,11 @@ public:
|
||||
}
|
||||
else if ("mysql" == source_type)
|
||||
{
|
||||
return std::make_unique<MySQLDictionarySource>(config, config_prefix + ".mysql", sample_block);
|
||||
return std::make_unique<MySQLDictionarySource>(dict_struct, config, config_prefix + ".mysql", sample_block);
|
||||
}
|
||||
else if ("clickhouse" == source_type)
|
||||
{
|
||||
return std::make_unique<ClickHouseDictionarySource>(config, config_prefix + ".clickhouse",
|
||||
return std::make_unique<ClickHouseDictionarySource>(dict_struct, config, config_prefix + ".clickhouse",
|
||||
sample_block, context);
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,11 @@ class MySQLDictionarySource final : public IDictionarySource
|
||||
static const auto max_block_size = 8192;
|
||||
|
||||
public:
|
||||
MySQLDictionarySource(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
||||
MySQLDictionarySource(const DictionaryStructure & dict_struct,
|
||||
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
||||
Block & sample_block)
|
||||
: db{config.getString(config_prefix + ".db", "")},
|
||||
: dict_struct{dict_struct},
|
||||
db{config.getString(config_prefix + ".db", "")},
|
||||
table{config.getString(config_prefix + ".table")},
|
||||
where{config.getString(config_prefix + ".where", "")},
|
||||
sample_block{sample_block},
|
||||
@ -29,7 +31,8 @@ public:
|
||||
|
||||
/// copy-constructor is provided in order to support cloneability
|
||||
MySQLDictionarySource(const MySQLDictionarySource & other)
|
||||
: db{other.db},
|
||||
: dict_struct{other.dict_struct},
|
||||
db{other.db},
|
||||
table{other.table},
|
||||
where{other.where},
|
||||
sample_block{other.sample_block},
|
||||
@ -101,14 +104,19 @@ private:
|
||||
WriteBufferFromString out{query};
|
||||
writeString("SELECT ", out);
|
||||
|
||||
auto first = true;
|
||||
for (const auto idx : ext::range(0, sample_block.columns()))
|
||||
{
|
||||
if (!first)
|
||||
writeString(", ", out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
|
||||
writeString(sample_block.getByPosition(idx).name, out);
|
||||
first = false;
|
||||
for (const auto & attr : dict_struct.attributes)
|
||||
{
|
||||
writeString(", ", out);
|
||||
|
||||
if (!attr.expression.empty())
|
||||
{
|
||||
writeString(attr.expression, out);
|
||||
writeString(" AS ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(attr.name, out);
|
||||
}
|
||||
|
||||
writeString(" FROM ", out);
|
||||
@ -139,17 +147,21 @@ private:
|
||||
WriteBufferFromString out{query};
|
||||
writeString("SELECT ", out);
|
||||
|
||||
auto first = true;
|
||||
for (const auto idx : ext::range(0, sample_block.columns()))
|
||||
{
|
||||
if (!first)
|
||||
writeString(", ", out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
|
||||
writeString(sample_block.getByPosition(idx).name, out);
|
||||
first = false;
|
||||
for (const auto & attr : dict_struct.attributes)
|
||||
{
|
||||
writeString(", ", out);
|
||||
|
||||
if (!attr.expression.empty())
|
||||
{
|
||||
writeString(attr.expression, out);
|
||||
writeString(" AS ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(attr.name, out);
|
||||
}
|
||||
|
||||
const auto & id_column_name = sample_block.getByPosition(0).name;
|
||||
writeString(" FROM ", out);
|
||||
if (!db.empty())
|
||||
{
|
||||
@ -166,10 +178,10 @@ private:
|
||||
writeString(" AND ", out);
|
||||
}
|
||||
|
||||
writeProbablyBackQuotedString(id_column_name, out);
|
||||
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
||||
writeString(" IN (", out);
|
||||
|
||||
first = true;
|
||||
auto first = true;
|
||||
for (const auto id : ids)
|
||||
{
|
||||
if (!first)
|
||||
@ -185,6 +197,7 @@ private:
|
||||
return query;
|
||||
}
|
||||
|
||||
const DictionaryStructure dict_struct;
|
||||
const std::string db;
|
||||
const std::string table;
|
||||
const std::string where;
|
||||
|
Loading…
Reference in New Issue
Block a user