mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 16:12:01 +00:00
expand CTE in alter modify query
This commit is contained in:
parent
0f6a1b451b
commit
3825cb3ad0
@ -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();
|
||||
|
@ -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
|
15
tests/queries/0_stateless/03002_modify_query_cte.sql
Normal file
15
tests/queries/0_stateless/03002_modify_query_cte.sql
Normal 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;
|
Loading…
Reference in New Issue
Block a user