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 backquoting in dictionary ddl #9734

Merged
merged 5 commits into from
Mar 19, 2020
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
15 changes: 7 additions & 8 deletions dbms/src/Dictionaries/getDictionaryConfigurationFromAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ namespace
using NamesToTypeNames = std::unordered_map<std::string, std::string>;
/// Get value from field and convert it to string.
/// Also remove quotes from strings.
String getUnescapedFieldString(const Field & field)
String getFieldAsString(const Field & field)
{
String string = applyVisitor(FieldVisitorToString(), field);
if (!string.empty() && string.front() == '\'' && string.back() == '\'')
return string.substr(1, string.size() - 2);
return string;
if (field.getType() == Field::Types::Which::String)
return field.get<String>();
return applyVisitor(FieldVisitorToString(), field);
}


Expand Down Expand Up @@ -183,7 +182,7 @@ void buildSingleAttribute(
AutoPtr<Element> null_value_element(doc->createElement("null_value"));
String null_value_str;
if (dict_attr->default_value)
null_value_str = getUnescapedFieldString(dict_attr->default_value->as<ASTLiteral>()->value);
null_value_str = getFieldAsString(dict_attr->default_value->as<ASTLiteral>()->value);
AutoPtr<Text> null_value(doc->createTextNode(null_value_str));
null_value_element->appendChild(null_value);
attribute_element->appendChild(null_value_element);
Expand All @@ -197,7 +196,7 @@ void buildSingleAttribute(
if (const auto * literal = dict_attr->expression->as<ASTLiteral>();
literal && literal->value.getType() == Field::Types::String)
{
expression_str = getUnescapedFieldString(literal->value);
expression_str = getFieldAsString(literal->value);
}
else
expression_str = queryToString(dict_attr->expression);
Expand Down Expand Up @@ -346,7 +345,7 @@ void buildConfigurationFromFunctionWithKeyValueArguments(
}
else if (auto literal = pair->second->as<const ASTLiteral>(); literal)
{
AutoPtr<Text> value(doc->createTextNode(getUnescapedFieldString(literal->value)));
AutoPtr<Text> value(doc->createTextNode(getFieldAsString(literal->value)));
current_xml_element->appendChild(value);
}
else if (auto list = pair->second->as<const ASTExpressionList>(); list)
Expand Down
29 changes: 29 additions & 0 deletions dbms/tests/integration/test_dictionaries_ddl/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,32 @@ def test_file_dictionary_restrictions(started_cluster):
node3.query("SELECT dictGetString('test.restricted_file_dictionary', 'value', toUInt64(1))")
except QueryRuntimeException as ex:
assert 'is not inside' in str(ex)


def test_dictionary_with_where(started_cluster):
mysql_conn = create_mysql_conn("root", "clickhouse", "localhost", 3308)
execute_mysql_query(mysql_conn, "CREATE DATABASE IF NOT EXISTS clickhouse")
execute_mysql_query(mysql_conn, "CREATE TABLE clickhouse.special_table (key_field1 int, value1 text, PRIMARY KEY (key_field1))")
execute_mysql_query(mysql_conn, "INSERT INTO clickhouse.special_table VALUES (1, 'abcabc'), (2, 'qweqwe')")

node1.query("""
CREATE DICTIONARY default.special_dict (
key_field1 Int32,
value1 String DEFAULT 'xxx'
)
PRIMARY KEY key_field1
SOURCE(MYSQL(
USER 'root'
PASSWORD 'clickhouse'
DB 'clickhouse'
TABLE 'special_table'
REPLICA(PRIORITY 1 HOST 'mysql1' PORT 3306)
WHERE 'value1 = \\'qweqwe\\' OR value1 = \\'\\\\u3232\\''
))
LAYOUT(FLAT())
LIFETIME(MIN 1 MAX 3)
""")

node1.query("SYSTEM RELOAD DICTIONARY default.special_dict")

assert node1.query("SELECT dictGetString('default.special_dict', 'value1', toUInt64(2))") == 'qweqwe\n'