expand CTE in alter modify query

This commit is contained in:
Yakov Olkhovskiy 2024-03-02 15:28:45 +00:00
parent 0f6a1b451b
commit 3825cb3ad0
3 changed files with 28 additions and 0 deletions

View File

@ -1,3 +1,4 @@
#include <Interpreters/ApplyWithSubqueryVisitor.h>
#include <Interpreters/InterpreterAlterQuery.h>
#include <Interpreters/InterpreterFactory.h>
@ -71,11 +72,15 @@ BlockIO InterpreterAlterQuery::execute()
BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
{
ASTSelectWithUnionQuery * modify_query = nullptr;
for (auto & child : alter.command_list->children)
{
auto * command_ast = child->as<ASTAlterCommand>();
if (command_ast->sql_security)
InterpreterCreateQuery::processSQLSecurityOption(getContext(), command_ast->sql_security->as<ASTSQLSecurity &>());
else if (command_ast->type == ASTAlterCommand::MODIFY_QUERY)
modify_query = command_ast->select->as<ASTSelectWithUnionQuery>();
}
BlockIO res;
@ -123,6 +128,12 @@ BlockIO InterpreterAlterQuery::executeToTable(const ASTAlterQuery & alter)
throw Exception(ErrorCodes::TABLE_IS_READ_ONLY, "Table is read-only");
auto table_lock = table->lockForShare(getContext()->getCurrentQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
if (modify_query)
{
// Expand CTE before filling default database
ApplyWithSubqueryVisitor().visit(*modify_query);
}
/// Add default database to table identifiers that we can encounter in e.g. default expressions, mutation expression, etc.
AddDefaultDatabaseVisitor visitor(getContext(), table_id.getDatabaseName());
ASTPtr command_list_ptr = alter.command_list->ptr();

View File

@ -0,0 +1,2 @@
CREATE MATERIALIZED VIEW default.mv_03002 TO default.table_03002\n(\n `ts` DateTime\n)\nAS SELECT ts\nFROM default.table_03002
CREATE MATERIALIZED VIEW default.mv_03002 TO default.table_03002\n(\n `ts` DateTime\n)\nAS WITH MY_CTE AS\n (\n SELECT ts\n FROM default.table_03002\n )\nSELECT *\nFROM\nMY_CTE

View File

@ -0,0 +1,15 @@
CREATE TABLE table_03002 (ts DateTime, event_type String) ENGINE = MergeTree ORDER BY (event_type, ts);
CREATE MATERIALIZED VIEW mv_03002 TO table_03002 AS SELECT ts FROM table_03002;
SHOW CREATE TABLE mv_03002;
ALTER TABLE mv_03002 MODIFY QUERY
WITH MY_CTE AS (SELECT ts FROM table_03002)
SELECT * FROM MY_CTE;
SHOW CREATE TABLE mv_03002;
DROP TABLE mv_03002;
DROP TABLE table_03002;