Changeset: 1e1674e6cc71 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/1e1674e6cc71 Modified Files: ChangeLog sql/server/rel_psm.c sql/server/rel_schema.c sql/server/sql_parser.y sql/test/Tests/create-or-replace-triggers.test sql/test/Tests/create-or-replace-views.test sql/test/Tests/function_syntax.test sql/test/Tests/procedure_syntax.test Branch: default Log Message:
Added support for IF NOT EXISTS syntax in following CREATE statements: CREATE AGGREGATE FUNCTION CREATE FILTER FUNCTION CREATE FUNCTION CREATE PROCEDURE CREATE TEMPORARY VIEW CREATE TRIGGER CREATE VIEW CREATE WINDOW FUNCTION diffs (truncated from 795 to 300 lines): diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,33 @@ # ChangeLog file for devel # This file is updated with Maddlog +* Thu Nov 26 2026 Martin van Dinther <[email protected]> +- Added support for IF NOT EXISTS syntax in following CREATE statements: + CREATE AGGREGATE FUNCTION + CREATE FILTER FUNCTION + CREATE FUNCTION + CREATE PROCEDURE + CREATE TEMPORARY VIEW + CREATE TRIGGER + CREATE VIEW + CREATE WINDOW FUNCTION + The IF NOT EXISTS clause is optional but when used must be placed before + the object name, so: + CREATE AGGREGATE FUNCTION IF NOT EXISTS name ... + CREATE FILTER FUNCTION IF NOT EXISTS name ... + CREATE FUNCTION IF NOT EXISTS name ... + CREATE PROCEDURE IF NOT EXISTS name ... + CREATE TEMPORARY VIEW IF NOT EXISTS name ... + CREATE TRIGGER IF NOT EXISTS name ... + CREATE VIEW IF NOT EXISTS name ... + CREATE WINDOW FUNCTION IF NOT EXISTS name ... + When IF NOT EXIST is used in these CREATE statements, it will no longer + report an error when the object with same name or function signature + already exists. This can be useful in scripts, where you want to + create the object only if is does not yet exists. + Note: It is not allowed to combine both CREATE OR REPLACE and IF NOT EXISTS + in these CREATE statements. + * Wed Nov 18 2026 Martin van Dinther <[email protected]> - Queries to a proto_loader('odbc:...') function with a limit-clause will now push the limit value down to the ODBC driver via ODBC call diff --git a/sql/server/rel_psm.c b/sql/server/rel_psm.c --- a/sql/server/rel_psm.c +++ b/sql/server/rel_psm.c @@ -883,7 +883,7 @@ has_generic_decimal_result(list *types) #define admin_privs(g) ((g) == USER_MONETDB || (g) == ROLE_SYSADMIN) static sql_rel * -rel_create_func(sql_query *query, dlist *qname, dlist *params, symbol *res, dlist *ext_name, dlist *body, sql_ftype type, sql_flang lang, int replace, int order_spec) +rel_create_func(sql_query *query, dlist *qname, dlist *params, symbol *res, dlist *ext_name, dlist *body, sql_ftype type, sql_flang lang, int replace, int order_spec, int if_not_exists) { mvc *sql = query->sql; const char *fname = qname_schema_object(qname); @@ -916,6 +916,9 @@ rel_create_func(sql_query *query, dlist if (create && store_readonly(sql->session->tr->store)) return sql_error(sql, 06, SQLSTATE(42000) "Schema statements cannot be executed on a readonly database."); + if (replace && if_not_exists) + return sql_error(sql, 02, SQLSTATE(42000) "Syntax error: CREATE OR REPLACE %s in combination with IF NOT EXISTS is not allowed", F); + if (union_err) return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %ss cannot return tables", F, fn); else if (res && type == F_PROC) @@ -946,6 +949,10 @@ rel_create_func(sql_query *query, dlist list_destroy(type_list); return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: name '%s' cannot be used", F, fname); } + if (if_not_exists) { + list_destroy(type_list); + return rel_psm_block(sql->sa, new_exp_list(sql->sa)); + } if (!replace && params) { char *arg_list = NULL; node *n; @@ -1301,7 +1308,7 @@ _stack_push_table(mvc *sql, const char * } static sql_rel * -create_trigger(sql_query *query, dlist *qname, int time, symbol *trigger_event, dlist *tqname, dlist *opt_ref, dlist *triggered_action, int replace) +create_trigger(sql_query *query, dlist *qname, int time, symbol *trigger_event, dlist *tqname, dlist *opt_ref, dlist *triggered_action, int replace, int if_not_exists) { mvc *sql = query->sql; const char *triggerschema = qname_schema(qname); @@ -1321,6 +1328,9 @@ create_trigger(sql_query *query, dlist * symbol *condition = triggered_action->h->next->data.sym; bool old_useviews = sql->use_views; + if (replace && if_not_exists) + return sql_error(sql, 02, SQLSTATE(42000) "Syntax error: %s in combination with IF NOT EXISTS is not allowed", base); + if (opt_ref) { dnode *dl = opt_ref->h; for ( ; dl; dl = dl->next) { @@ -1351,8 +1361,12 @@ create_trigger(sql_query *query, dlist * } if (!mvc_schema_privs(sql, ss)) return sql_error(sql, 02, SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), ss->base.name); - if (!replace && mvc_bind_trigger(sql, ss, triggername) != NULL) - return sql_error(sql, 02, SQLSTATE(42000) "%s: name '%s' already in use", base, triggername); + if (mvc_bind_trigger(sql, ss, triggername) != NULL) { + if (!replace && !if_not_exists) + return sql_error(sql, 02, SQLSTATE(42000) "%s: name '%s' already in use", base, triggername); + if (if_not_exists) + return rel_psm_block(sql->sa, new_exp_list(sql->sa)); + } switch (trigger_event->token) { case SQL_INSERT: { if (old_name) @@ -1534,10 +1548,16 @@ rel_psm(sql_query *query, symbol *s) dlist *l = s->data.lval; sql_ftype type = (sql_ftype) l->h->next->next->next->next->next->data.i_val; sql_flang lang = (sql_flang) l->h->next->next->next->next->next->next->data.i_val; - int repl = l->h->next->next->next->next->next->next->next->data.i_val; - int order_spec = l->h->next->next->next->next->next->next->next->next->data.i_val; + int replace = l->h->next->next->next->next->next->next->next->data.i_val; + int order_spec = l->h->next->next->next->next->next->next->next->next->data.i_val; + int if_not_exists = l->h->next->next->next->next->next->next->next->next->next->data.i_val; - ret = rel_create_func(query, l->h->data.lval, l->h->next->data.lval, l->h->next->next->data.sym, l->h->next->next->next->data.lval, l->h->next->next->next->next->data.lval, type, lang, repl, order_spec); + ret = rel_create_func(query, l->h->data.lval, + l->h->next->data.lval, + l->h->next->next->data.sym, + l->h->next->next->next->data.lval, + l->h->next->next->next->next->data.lval, + type, lang, replace, order_spec, if_not_exists); sql->type = Q_SCHEMA; } break; case SQL_DROP_FUNC: @@ -1585,7 +1605,14 @@ rel_psm(sql_query *query, symbol *s) dlist *l = s->data.lval; assert(l->h->next->type == type_int); - ret = create_trigger(query, l->h->data.lval, l->h->next->data.i_val, l->h->next->next->data.sym, l->h->next->next->next->data.lval, l->h->next->next->next->next->data.lval, l->h->next->next->next->next->next->data.lval, l->h->next->next->next->next->next->next->data.i_val); + ret = create_trigger(query, l->h->data.lval, + l->h->next->data.i_val, + l->h->next->next->data.sym, + l->h->next->next->next->data.lval, + l->h->next->next->next->next->data.lval, + l->h->next->next->next->next->next->data.lval, + l->h->next->next->next->next->next->next->data.i_val, /* or replace */ + l->h->next->next->next->next->next->next->next->data.i_val); /* if not exists */ sql->type = Q_SCHEMA; } break; case SQL_DROP_TRIGGER: diff --git a/sql/server/rel_schema.c b/sql/server/rel_schema.c --- a/sql/server/rel_schema.c +++ b/sql/server/rel_schema.c @@ -1807,7 +1807,7 @@ rel_create_table(sql_query *query, int t } static sql_rel * -rel_create_view(sql_query *query, int temp, dlist *qname, dlist *column_spec, symbol *ast, int check, int persistent, int replace) +rel_create_view(sql_query *query, int temp, dlist *qname, dlist *column_spec, symbol *ast, int check, int persistent, int replace, int if_not_exists) { mvc *sql = query->sql; const char *name = qname_schema_object(qname); @@ -1818,11 +1818,16 @@ rel_create_view(sql_query *query, int te int deps = (sql->emode == m_deps); int create = (!instantiate && !deps); sqlid pfoundid = 0, foundid = 0; - const char *base = replace ? "CREATE OR REPLACE VIEW" : "CREATE VIEW"; + const char *base = replace + ? (temp == SQL_LOCAL_TEMP ? "CREATE OR REPLACE TEMPORARY VIEW" : "CREATE OR REPLACE VIEW") + : (temp == SQL_LOCAL_TEMP ? "CREATE TEMPORARY VIEW" : "CREATE VIEW"); const char *action = (temp == SQL_DECLARED_TABLE)?"DECLARE":"CREATE"; (void) check; + if (replace && if_not_exists) + return sql_error(sql, 02, SQLSTATE(42000) "Syntax error: %s in combination with IF NOT EXISTS is not allowed", base); + if (temp == SQL_GLOBAL_TEMP) temp = SQL_PERSIST; /* just normal view */ @@ -1836,12 +1841,14 @@ rel_create_view(sql_query *query, int te if (create && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && persistent == SQL_LOCAL_TEMP))) return sql_error(sql, 02, SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), s->base.name); if (create && (t = mvc_bind_table(sql, s, name))) { - if (!replace) + if (!replace && !if_not_exists) return sql_error(sql, 02, SQLSTATE(42S01) "%s: name '%s' already in use", base, name); if (!isView(t)) return sql_error(sql, 02, SQLSTATE(42000) "%s: '%s' is not a view", base, name); if (t->system) return sql_error(sql, 02, SQLSTATE(42000) "%s: cannot replace system view '%s'", base, name); + if (if_not_exists) + return rel_psm_block(sql->sa, new_exp_list(sql->sa)); foundid = t->base.id; /* when recreating a view, the view itself can't be found */ } @@ -3255,13 +3262,15 @@ rel_schemas(sql_query *query, symbol *s) assert(l->h->type == type_int); assert(l->h->next->next->next->next->type == type_int); assert(l->h->next->next->next->next->next->type == type_int); + assert(l->h->next->next->next->next->next->next->type == type_int); ret = rel_create_view(query, l->h->data.i_val, l->h->next->data.lval, l->h->next->next->data.lval, l->h->next->next->next->data.sym, l->h->next->next->next->next->data.i_val, l->h->next->next->next->next->next->data.i_val, - l->h->next->next->next->next->next->next->data.i_val); /* or replace */ + l->h->next->next->next->next->next->next->data.i_val, /* or replace */ + l->h->next->next->next->next->next->next->next->data.i_val); /* if not exists */ } break; case SQL_DROP_TABLE: { diff --git a/sql/server/sql_parser.y b/sql/server/sql_parser.y --- a/sql/server/sql_parser.y +++ b/sql/server/sql_parser.y @@ -1989,16 +1989,17 @@ table_def: append_symbol(l, NULL); /* only used for merge table */ $$ = _symbol_create_list( SQL_CREATE_TABLE, l ); } - | local_temp VIEW qname opt_column_list AS SelectStmt + | local_temp VIEW if_not_exists qname opt_column_list AS SelectStmt { dlist *l = L(); append_int(l, $1); - append_list(l, $3); append_list(l, $4); - append_symbol(l, $6); + append_list(l, $5); + append_symbol(l, $7); append_int(l, FALSE); append_int(l, TRUE); append_int(l, FALSE); + append_int(l, $3); /* if not exists */ $$ = _symbol_create_list( SQL_CREATE_VIEW, l ); } ; @@ -2511,28 +2512,30 @@ like_table: ; view_def: - create_or_replace VIEW qname opt_column_list AS SelectStmt opt_with_check_option + create_or_replace VIEW if_not_exists qname opt_column_list AS SelectStmt opt_with_check_option { dlist *l = L(); append_int(l, SQL_PERSIST); - append_list(l, $3); append_list(l, $4); - append_symbol(l, $6); - append_int(l, $7); + append_list(l, $5); + append_symbol(l, $7); + append_int(l, $8); append_int(l, TRUE); /* persistent view */ - append_int(l, $1); + append_int(l, $1); /* replace */ + append_int(l, $3); /* if not exists */ $$ = _symbol_create_list( SQL_CREATE_VIEW, l ); } - | CREATE OR REPLACE local_temp VIEW qname opt_column_list AS SelectStmt + | CREATE OR REPLACE local_temp VIEW if_not_exists qname opt_column_list AS SelectStmt { dlist *l = L(); append_int(l, $4); - append_list(l, $6); append_list(l, $7); - append_symbol(l, $9); - append_int(l, FALSE); + append_list(l, $8); + append_symbol(l, $10); + append_int(l, FALSE); /* with check */ append_int(l, TRUE); /* persistent view */ - append_int(l, TRUE); /* replace */ + append_int(l, TRUE); /* replace */ + append_int(l, $6); /* if not exists */ $$ = _symbol_create_list( SQL_CREATE_VIEW, l ); } ; @@ -2605,61 +2608,65 @@ func_def_opt_order_spec: ; func_def: - create_or_replace func_def_type_no_proc qfunc '(' opt_paramlist ')' func_def_opt_returns func_def_opt_order_spec + create_or_replace func_def_type_no_proc if_not_exists qfunc '(' opt_paramlist ')' func_def_opt_returns func_def_opt_order_spec EXTERNAL sqlNAME external_function_name { dlist *f = L(); - append_list(f, $3); - append_list(f, $5); - append_symbol(f, $7); - append_list(f, $11); + append_list(f, $4); + append_list(f, $6); + append_symbol(f, $8); + append_list(f, $12); append_list(f, NULL); append_int(f, $2); append_int(f, FUNC_LANG_MAL); - append_int(f, $1); - append_int(f, $8); + append_int(f, $1); /* replace */ + append_int(f, $9); + append_int(f, $3); /* if not exists */ $$ = _symbol_create_list( SQL_CREATE_FUNC, f ); } - | create_or_replace PROCEDURE qfunc '(' opt_paramlist ')' EXTERNAL sqlNAME external_function_name + | create_or_replace PROCEDURE if_not_exists qfunc '(' opt_paramlist ')' EXTERNAL sqlNAME external_function_name { dlist *f = L(); - append_list(f, $3); _______________________________________________ checkin-list mailing list -- [email protected] To unsubscribe send an email to [email protected]
