Changeset: dcf96232d80f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=dcf96232d80f
Modified Files:
        sql/backends/monet5/rel_bin.c
        sql/backends/monet5/sql.c
        sql/common/sql_types.c
        sql/common/sql_types.h
        sql/server/rel_schema.c
        sql/server/rel_updates.c
        sql/test/pg_regress/Tests/All
        sql/test/pg_regress/Tests/update.stable.err
        sql/test/pg_regress/Tests/update.stable.out
Branch: Mar2018
Log Message:

Use proper casting for DEFAULT values.


diffs (219 lines):

diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -3896,7 +3896,11 @@ sql_delete_set_Fkeys(backend *be, sql_ke
                if (action == ACT_SET_DEFAULT) {
                        if (fc->c->def) {
                                stmt *sq;
-                               char *msg = sa_message(sql->sa, "select %s;", 
fc->c->def);
+                               char *msg, *typestr = 
subtype2string2(&fc->c->type);
+                               if(!typestr)
+                                       return sql_error(sql, 02, 
SQLSTATE(HY001) MAL_MALLOC_FAIL);
+                               msg = sa_message(sql->sa, "select cast(%s as 
%s);", fc->c->def, typestr);
+                               _DELETE(typestr);
                                sq = rel_parse_value(be, msg, sql->emode);
                                if (!sq) 
                                        return NULL;
@@ -3955,7 +3959,11 @@ sql_update_cascade_Fkeys(backend *be, sq
                } else if (action == ACT_SET_DEFAULT) {
                        if (fc->c->def) {
                                stmt *sq;
-                               char *msg = sa_message(sql->sa, "select %s;", 
fc->c->def);
+                               char *msg, *typestr = 
subtype2string2(&fc->c->type);
+                               if(!typestr)
+                                       return sql_error(sql, 02, 
SQLSTATE(HY001) MAL_MALLOC_FAIL);
+                               msg = sa_message(sql->sa, "select cast(%s as 
%s);", fc->c->def, typestr);
+                               _DELETE(typestr);
                                sq = rel_parse_value(be, msg, sql->emode);
                                if (!sq) 
                                        return NULL;
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -311,7 +311,7 @@ create_table_or_view(mvc *sql, char *sna
                sql_column *c = n->data;
 
                if (c->def) {
-                       char *buf;
+                       char *buf, *typestr;
                        sql_rel *r = NULL;
 
                        sql->sa = sa_create();
@@ -320,7 +320,11 @@ create_table_or_view(mvc *sql, char *sna
                        buf = sa_alloc(sql->sa, strlen(c->def) + 8);
                        if(!buf)
                                throw(SQL, "sql.catalog",SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
-                       snprintf(buf, BUFSIZ, "select %s;", c->def);
+                       typestr = subtype2string2(&c->type);
+                       if(!typestr)
+                               throw(SQL, "sql.catalog",SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
+                       snprintf(buf, BUFSIZ, "select cast(%s as %s);", c->def, 
typestr);
+                       _DELETE(typestr);
                        r = rel_parse(sql, s, buf, m_deps);
                        if (!r || !is_project(r->op) || !r->exps || 
list_length(r->exps) != 1 || rel_check_type(sql, &c->type, r->exps->h->data, 
type_equal) == NULL)
                                throw(SQL, "sql.catalog", SQLSTATE(42000) "%s", 
sql->errstr);
diff --git a/sql/common/sql_types.c b/sql/common/sql_types.c
--- a/sql/common/sql_types.c
+++ b/sql/common/sql_types.c
@@ -418,6 +418,28 @@ subtype2string(sql_subtype *t)
        return _STRDUP(buf);
 }
 
+char *
+subtype2string2(sql_subtype *tpe) //distinguish char(n), decimal(n,m) from 
other SQL types
+{
+       char buf[BUFSIZ];
+
+       switch (tpe->type->eclass) {
+               case EC_SEC:
+                       snprintf(buf, BUFSIZ, "BIGINT");
+                       break;
+               case EC_MONTH:
+                       snprintf(buf, BUFSIZ, "INT");
+                       break;
+               case EC_CHAR:
+               case EC_STRING:
+               case EC_DEC:
+                       return subtype2string(tpe);
+               default:
+                       snprintf(buf, BUFSIZ, "%s", tpe->type->sqlname);
+       }
+       return _STRDUP(buf);
+}
+
 int 
 subaggr_cmp( sql_subaggr *a1, sql_subaggr *a2)
 {
diff --git a/sql/common/sql_types.h b/sql/common/sql_types.h
--- a/sql/common/sql_types.h
+++ b/sql/common/sql_types.h
@@ -82,6 +82,7 @@ extern int subtype_cmp(sql_subtype *t1, 
 extern int arg_subtype_cmp(sql_arg *a, sql_subtype *t);
 extern int is_subtype(sql_subtype *t1, sql_subtype *t2);
 extern char *subtype2string(sql_subtype *t);
+extern char *subtype2string2(sql_subtype *tpe);
 
 extern sql_type *sql_create_type(sql_allocator *sa, const char *sqlname, 
unsigned int digits, unsigned int scale, unsigned char radix, unsigned char 
eclass, const char *name);
 extern void type_destroy(sql_type *t);
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
@@ -1402,7 +1402,11 @@ sql_alter_table(mvc *sql, dlist *qname, 
                        for (n = nt->columns.nelm; n; n = n->next) {
                                sql_column *c = n->data;
                                if (c->def) {
-                                       char *d = sql_message("select %s;", 
c->def);
+                                       char *d, *typestr = 
subtype2string2(&c->type);
+                                       if(!typestr)
+                                               return sql_error(sql, 02, 
SQLSTATE(HY001) MAL_MALLOC_FAIL);
+                                       d = sql_message("select cast(%s as 
%s);", c->def, typestr);
+                                       _DELETE(typestr);
                                        e = rel_parse_val(sql, d, sql->emode);
                                        _DELETE(d);
                                } else {
diff --git a/sql/server/rel_updates.c b/sql/server/rel_updates.c
--- a/sql/server/rel_updates.c
+++ b/sql/server/rel_updates.c
@@ -25,7 +25,12 @@ insert_value(mvc *sql, sql_column *c, sq
                return exp_atom(sql->sa, atom_general(sql->sa, &c->type, NULL));
        } else if (s->token == SQL_DEFAULT) {
                if (c->def) {
-                       sql_exp *e = rel_parse_val(sql, sa_message(sql->sa, 
"select CAST(%s AS %s);", c->def, c->type.type->sqlname), sql->emode);
+                       sql_exp *e;
+                       char *typestr = subtype2string2(&c->type);
+                       if(!typestr)
+                               return sql_error(sql, 02, SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
+                       e = rel_parse_val(sql, sa_message(sql->sa, "select 
cast(%s as %s);", c->def, typestr), sql->emode);
+                       _DELETE(typestr);
                        if (!e || (e = rel_check_type(sql, &c->type, e, 
type_equal)) == NULL)
                                return NULL;
                        return e;
@@ -351,7 +356,11 @@ rel_inserts(mvc *sql, sql_table *t, sql_
                                                sql_exp *e = NULL;
 
                                                if (c->def) {
-                                                       char *q = 
sa_message(sql->sa, "select %s;", c->def);
+                                                       char *q, *typestr = 
subtype2string2(&c->type);
+                                                       if(!typestr)
+                                                               return 
sql_error(sql, 02, SQLSTATE(HY001) MAL_MALLOC_FAIL);
+                                                       q = sa_message(sql->sa, 
"select cast(%s as %s);", c->def, typestr);
+                                                       _DELETE(typestr);
                                                        e = rel_parse_val(sql, 
q, sql->emode);
                                                        if (!e || (e = 
rel_check_type(sql, &c->type, e, type_equal)) == NULL)
                                                                return NULL;
@@ -1010,7 +1019,11 @@ update_table(mvc *sql, dlist *qname, dli
                                        char *colname = 
assignment->h->next->data.sval;
                                        sql_column *col = mvc_bind_column(sql, 
t, colname);
                                        if (col->def) {
-                                               v = rel_parse_val(sql, 
sa_message(sql->sa, "select CAST(%s AS %s);", col->def, 
col->type.type->sqlname), sql->emode);
+                                               char *typestr = 
subtype2string2(&col->type);
+                                               if(!typestr)
+                                                       return sql_error(sql, 
02, SQLSTATE(HY001) MAL_MALLOC_FAIL);
+                                               v = rel_parse_val(sql, 
sa_message(sql->sa, "select cast(%s as %s);", col->def, typestr), sql->emode);
+                                               _DELETE(typestr);
                                        } else {
                                                return sql_error(sql, 02, 
SQLSTATE(42000) "UPDATE: column '%s' has no valid default value", 
col->base.name);
                                        }
diff --git a/sql/test/pg_regress/Tests/All b/sql/test/pg_regress/Tests/All
--- a/sql/test/pg_regress/Tests/All
+++ b/sql/test/pg_regress/Tests/All
@@ -79,7 +79,7 @@ random        #[MK] causes an non-displayed err
 arrays
 #btree_index   [MK]MonetDB does not support construction of btrees
 #hash_index            [MK]MonetDB does not support hash index management by 
the user
-#ToDo update
+update
 #ToDo namespace
 #ToDo privileges
 #ToDo rules
diff --git a/sql/test/pg_regress/Tests/update.stable.err 
b/sql/test/pg_regress/Tests/update.stable.err
--- a/sql/test/pg_regress/Tests/update.stable.err
+++ b/sql/test/pg_regress/Tests/update.stable.err
@@ -29,9 +29,10 @@ stderr of test 'update` in directory 'sq
 # 17:11:19 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-1142" "--port=38959"
 # 17:11:19 >  
 
-#--
-#-- UPDATE ... SET <col> = DEFAULT;
-#--
+MAPI  = (monetdb) /var/tmp/mtest-21763/.s.monetdb.39738
+QUERY = UPDATE update_test SET a = DEFAULT, b = DEFAULT;
+ERROR = !UPDATE: column 'b' has no valid default value
+CODE  = 42000
 
 # 17:11:19 >  
 # 17:11:19 >  "Done."
diff --git a/sql/test/pg_regress/Tests/update.stable.out 
b/sql/test/pg_regress/Tests/update.stable.out
--- a/sql/test/pg_regress/Tests/update.stable.out
+++ b/sql/test/pg_regress/Tests/update.stable.out
@@ -38,22 +38,21 @@ Ready.
 #INSERT INTO update_test VALUES (10, 15);
 [ 1 ]
 #SELECT * FROM update_test;
-% sys.L0,      sys.L0 # table_name
+% sys.update_test,     sys.update_test # table_name
 % a,   b # name
-% ?,   ? # type
-% ?,   ? # length
+% int, int # type
+% 2,   2 # length
 [ 5,   10 ]
 [ 10,  15 ]
 
 #UPDATE update_test SET a = DEFAULT, b = DEFAULT;
 #SELECT * FROM update_test;
-% sys.L0,      sys.L0 # table_name
+% sys.update_test,     sys.update_test # table_name
 % a,   b # name
-% ?,   ? # type
-% ?,   ? # length
-[ 10,  "" ]
-[ 10,  "" ]
-
+% int, int # type
+% 2,   2 # length
+[ 5,   10      ]
+[ 10,  15      ]
 #DROP TABLE update_test;
 
 = ! Correct / expected output still needs to be provided / verified / approved 
! =
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to