diff --git a/dbms/src/Interpreters/InterpreterCreateQuery.cpp b/dbms/src/Interpreters/InterpreterCreateQuery.cpp index 8b213119439..8045ee3419c 100644 --- a/dbms/src/Interpreters/InterpreterCreateQuery.cpp +++ b/dbms/src/Interpreters/InterpreterCreateQuery.cpp @@ -545,10 +545,12 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) // If this is a stub ATTACH query, read the query definition from the database if (create.attach && !create.storage && !create.columns_list) { + bool if_not_exists = create.if_not_exists; // Table SQL definition is available even if the table is detached auto query = context.getDatabase(create.database)->getCreateTableQuery(context, create.table); create = query->as(); // Copy the saved create query, but use ATTACH instead of CREATE create.attach = true; + create.if_not_exists = if_not_exists; } String current_database = context.getCurrentDatabase(); diff --git a/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.reference b/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.reference @@ -0,0 +1 @@ +1 diff --git a/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.sql b/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.sql new file mode 100644 index 00000000000..e9411577d56 --- /dev/null +++ b/dbms/tests/queries/0_stateless/01073_attach_if_not_exists.sql @@ -0,0 +1,6 @@ +CREATE TABLE t (a Int) ENGINE = Log; +ATTACH TABLE t; -- { serverError 57 } +ATTACH TABLE IF NOT EXISTS t; +DETACH TABLE t; +ATTACH TABLE IF NOT EXISTS t; +EXISTS TABLE t;