MaterializedMySQL: Add support of double quoted comments

This commit is contained in:
Val Doroshchuk 2023-07-20 12:24:52 +02:00 committed by Valentyn Doroshchuk WX1158589
parent 2b29e3dc83
commit d16d444943
5 changed files with 90 additions and 1 deletions

View File

@ -1900,6 +1900,39 @@ bool ParserSubstitution::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
}
bool ParserMySQLComment::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
if (pos->type != TokenType::QuotedIdentifier && pos->type != TokenType::StringLiteral)
return false;
String s;
ReadBufferFromMemory in(pos->begin, pos->size());
try
{
if (pos->type == TokenType::StringLiteral)
readQuotedStringWithSQLStyle(s, in);
else
readDoubleQuotedStringWithSQLStyle(s, in);
}
catch (const Exception &)
{
expected.add(pos, "string literal or double quoted string");
return false;
}
if (in.count() != pos->size())
{
expected.add(pos, "string literal or double quoted string");
return false;
}
auto literal = std::make_shared<ASTLiteral>(s);
literal->begin = pos;
literal->end = ++pos;
node = literal;
return true;
}
bool ParserMySQLGlobalVariable::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
if (pos->type != TokenType::DoubleAt)

View File

@ -367,6 +367,21 @@ protected:
};
/** MySQL comment:
* CREATE TABLE t (
* i INT PRIMARY KEY,
* first_name VARCHAR(255) COMMENT 'FIRST_NAME',
* last_name VARCHAR(255) COMMENT "LAST_NAME"
* )
*/
class ParserMySQLComment : public IParserBase
{
protected:
const char * getName() const override { return "MySQL comment parser"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
/** MySQL-style global variable: @@var
*/
class ParserMySQLGlobalVariable : public IParserBase

View File

@ -50,7 +50,7 @@ static inline bool parseColumnDeclareOptions(IParser::Pos & pos, ASTPtr & node,
OptionDescribe("PRIMARY KEY", "primary_key", std::make_unique<ParserAlwaysTrue>()),
OptionDescribe("UNIQUE", "unique_key", std::make_unique<ParserAlwaysTrue>()),
OptionDescribe("KEY", "primary_key", std::make_unique<ParserAlwaysTrue>()),
OptionDescribe("COMMENT", "comment", std::make_unique<ParserStringLiteral>()),
OptionDescribe("COMMENT", "comment", std::make_unique<ParserMySQLComment>()),
OptionDescribe("CHARACTER SET", "charset_name", std::make_unique<ParserCharsetOrCollateName>()),
OptionDescribe("CHARSET", "charset", std::make_unique<ParserCharsetOrCollateName>()),
OptionDescribe("COLLATE", "collate", std::make_unique<ParserCharsetOrCollateName>()),

View File

@ -1617,6 +1617,41 @@ def materialized_with_column_comments_test(clickhouse_node, mysql_node, service_
mysql_node.query("DROP DATABASE materialized_with_column_comments_test")
def double_quoted_comment(clickhouse_node, mysql_node, service_name):
db = "comment_db"
mysql_node.query(f"DROP DATABASE IF EXISTS {db}")
clickhouse_node.query(f"DROP DATABASE IF EXISTS {db}")
mysql_node.query(f"CREATE DATABASE {db}")
mysql_node.query(
f'CREATE TABLE {db}.t1 (i INT PRIMARY KEY, id VARCHAR(255) COMMENT "ID")'
)
mysql_node.query(
f"CREATE TABLE {db}.t2 (i INT PRIMARY KEY, id VARCHAR(255) COMMENT 'ID')"
)
clickhouse_node.query(
f"CREATE DATABASE {db} ENGINE = MaterializedMySQL('{service_name}:3306', '{db}', 'root', 'clickhouse')"
)
check_query(
clickhouse_node,
f"SHOW TABLES FROM {db} FORMAT TSV",
"t1\nt2\n",
)
# incremental
mysql_node.query(
f'CREATE TABLE {db}.t3 (i INT PRIMARY KEY, id VARCHAR(255) COMMENT "ID")'
)
mysql_node.query(
f"CREATE TABLE {db}.t4 (i INT PRIMARY KEY, id VARCHAR(255) COMMENT 'ID')"
)
check_query(
clickhouse_node, f"SHOW TABLES FROM {db} FORMAT TSV", "t1\nt2\nt3\nt4\n"
)
clickhouse_node.query(f"DROP DATABASE IF EXISTS {db}")
mysql_node.query(f"DROP DATABASE IF EXISTS {db}")
def materialized_with_enum8_test(clickhouse_node, mysql_node, service_name):
mysql_node.query("DROP DATABASE IF EXISTS materialized_with_enum8_test")
clickhouse_node.query("DROP DATABASE IF EXISTS materialized_with_enum8_test")

View File

@ -416,6 +416,12 @@ def test_materialized_with_column_comments(
)
def test_double_quoted_comment(started_cluster, started_mysql_8_0, clickhouse_node):
materialized_with_ddl.double_quoted_comment(
clickhouse_node, started_mysql_8_0, "mysql80"
)
def test_materialized_with_enum(
started_cluster, started_mysql_8_0, started_mysql_5_7, clickhouse_node
):