ISSUES-10056 reused evaluateConstantExpressionOrIdentifierAsLiteral

This commit is contained in:
zhang2014 2020-04-08 13:41:11 +08:00
parent aa0fcf4088
commit 9eb96b87db
2 changed files with 22 additions and 26 deletions

View File

@ -16,6 +16,7 @@
#if USE_MYSQL #if USE_MYSQL
#include <Databases/DatabaseMySQL.h> #include <Databases/DatabaseMySQL.h>
#include <Interpreters/evaluateConstantExpression.h>
#endif #endif
@ -51,7 +52,7 @@ DatabasePtr DatabaseFactory::get(
} }
template <typename ValueType> template <typename ValueType>
static inline ValueType getLiteralValue(const ASTPtr & ast, const String & engine_name) static inline ValueType safeGetLiteralValue(const ASTPtr &ast, const String &engine_name)
{ {
if (!ast || !ast->as<ASTLiteral>()) if (!ast || !ast->as<ASTLiteral>())
throw Exception("Database engine " + engine_name + " requested literal argument.", ErrorCodes::BAD_ARGUMENTS); throw Exception("Database engine " + engine_name + " requested literal argument.", ErrorCodes::BAD_ARGUMENTS);
@ -59,19 +60,6 @@ static inline ValueType getLiteralValue(const ASTPtr & ast, const String & engin
return ast->as<ASTLiteral>()->value.safeGet<ValueType>(); return ast->as<ASTLiteral>()->value.safeGet<ValueType>();
} }
[[maybe_unused]] static inline String getIdentifierOrStringLiteral(const ASTPtr & ast, const String & engine_name)
{
if (ast)
{
if (const auto & literal = ast->as<ASTLiteral>())
return literal->value.safeGet<String>();
else if (const auto & identifier = ast->as<ASTIdentifier>())
return identifier->name;
}
throw Exception("Database engine " + engine_name + " requested literal or identifier argument.", ErrorCodes::BAD_ARGUMENTS);
}
DatabasePtr DatabaseFactory::getImpl( DatabasePtr DatabaseFactory::getImpl(
const String & database_name, const String & metadata_path, const ASTStorage * engine_define, Context & context) const String & database_name, const String & metadata_path, const ASTStorage * engine_define, Context & context)
{ {
@ -103,11 +91,13 @@ DatabasePtr DatabaseFactory::getImpl(
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
const auto & arguments = engine->arguments->children; ASTs & arguments = engine->arguments->children;
const auto & host_name_and_port = getLiteralValue<String>(arguments[0], "MySQL"); arguments[1] = evaluateConstantExpressionOrIdentifierAsLiteral(arguments[1], context);
const auto & database_name_in_mysql = getIdentifierOrStringLiteral(arguments[1], "MySQL");
const auto & mysql_user_name = getLiteralValue<String>(arguments[2], "MySQL"); const auto & host_name_and_port = safeGetLiteralValue<String>(arguments[0], "MySQL");
const auto & mysql_user_password = getLiteralValue<String>(arguments[3], "MySQL"); const auto & database_name_in_mysql = safeGetLiteralValue<String>(arguments[1], "MySQL");
const auto & mysql_user_name = safeGetLiteralValue<String>(arguments[2], "MySQL");
const auto & mysql_user_password = safeGetLiteralValue<String>(arguments[3], "MySQL");
try try
{ {
@ -138,7 +128,7 @@ DatabasePtr DatabaseFactory::getImpl(
const auto & arguments = engine->arguments->children; const auto & arguments = engine->arguments->children;
const auto cache_expiration_time_seconds = getLiteralValue<UInt64>(arguments[0], "Lazy"); const auto cache_expiration_time_seconds = safeGetLiteralValue<UInt64>(arguments[0], "Lazy");
return std::make_shared<DatabaseLazy>(database_name, metadata_path, cache_expiration_time_seconds, context); return std::make_shared<DatabaseLazy>(database_name, metadata_path, cache_expiration_time_seconds, context);
} }

View File

@ -5,6 +5,7 @@ import pymysql.cursors
import pytest import pytest
from helpers.cluster import ClickHouseCluster from helpers.cluster import ClickHouseCluster
from helpers.client import QueryRuntimeException
cluster = ClickHouseCluster(__file__) cluster = ClickHouseCluster(__file__)
clickhouse_node = cluster.add_instance('node1', main_configs=['configs/remote_servers.xml'], with_mysql=True) clickhouse_node = cluster.add_instance('node1', main_configs=['configs/remote_servers.xml'], with_mysql=True)
@ -116,12 +117,17 @@ def test_clickhouse_join_for_mysql_database(started_cluster):
clickhouse_node.query("CREATE TABLE default.t1_remote_mysql AS mysql('mysql1:3306','test','t1_mysql_local','root','clickhouse')") clickhouse_node.query("CREATE TABLE default.t1_remote_mysql AS mysql('mysql1:3306','test','t1_mysql_local','root','clickhouse')")
clickhouse_node.query("CREATE TABLE default.t2_remote_mysql AS mysql('mysql1:3306','test','t2_mysql_local','root','clickhouse')") clickhouse_node.query("CREATE TABLE default.t2_remote_mysql AS mysql('mysql1:3306','test','t2_mysql_local','root','clickhouse')")
assert clickhouse_node.query("SELECT s.pays " assert clickhouse_node.query("SELECT s.pays "
"FROM default.t1_remote_mysql AS s " "FROM default.t1_remote_mysql AS s "
"LEFT JOIN default.t1_remote_mysql AS s_ref " "LEFT JOIN default.t1_remote_mysql AS s_ref "
"ON (s_ref.opco = s.opco AND s_ref.service = s.service)") == '' "ON (s_ref.opco = s.opco AND s_ref.service = s.service)") == ''
mysql_node.query("DROP DATABASE test") mysql_node.query("DROP DATABASE test")
def test_bad_arguments_for_mysql_database_engine(started_cluster): def test_bad_arguments_for_mysql_database_engine(started_cluster):
assert clickhouse_node.query( with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', '127.0.0.1', port=3308)) as mysql_node:
"CREATE TABLE default.t1_remote_mysql AS mysql('mysql1:3306', 'test', 't1_mysql_local', root, 'clickhouse')").find( with pytest.raises(QueryRuntimeException) as exception:
'Database engine MySQL requested literal argument.') != -1 mysql_node.query("CREATE DATABASE IF NOT EXISTS test_bad_arguments DEFAULT CHARACTER SET 'utf8'")
clickhouse_node.query("CREATE DATABASE test_database ENGINE = MySQL('mysql1:3306', test_bad_arguments, root, 'clickhouse')")
assert 'Database engine MySQL requested literal argument.' in str(exception.value)
mysql_node.query("DROP DATABASE test_bad_arguments")