Changeset: 6106b0b4f72a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6106b0b4f72a
Modified Files:
        sql/backends/monet5/sql_cat.c
        sql/server/rel_schema.c
        sql/server/rel_select.c
        sql/server/rel_updates.c
        sql/server/sql_mvc.c
        sql/test/miscellaneous/Tests/declared_tables.stable.out
        sql/test/miscellaneous/Tests/groupby_error.sql
        sql/test/miscellaneous/Tests/simple_selects.sql
        sql/test/miscellaneous/Tests/simple_selects.stable.err
        sql/test/miscellaneous/Tests/simple_selects.stable.out
Branch: Jun2020
Log Message:

Several declared tables fixes.

- While creating a declare variable on the global scope, the declared propery 
was not taken into account.
- Use 'mvc_bind_table' with NULL schema instead of 'stack_find_table' to find 
declared tables.
- On a SQL query, declared CTEs have inner scope than declared tables.

Also added another query with "inputs size" error.


diffs (199 lines):

diff --git a/sql/backends/monet5/sql_cat.c b/sql/backends/monet5/sql_cat.c
--- a/sql/backends/monet5/sql_cat.c
+++ b/sql/backends/monet5/sql_cat.c
@@ -507,7 +507,9 @@ drop_table(mvc *sql, char *sname, char *
        if (!t && !sname) {
                s = tmp_schema(sql);
                t = mvc_bind_table(sql, s, tname);
-       }
+       } 
+       if (!t) /* Find declared table */
+               t = mvc_bind_table(sql, NULL, tname);
        if (!t) {
                if (if_exists)
                        return MAL_SUCCEED;
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
@@ -2656,7 +2656,7 @@ rel_schemas(sql_query *query, symbol *s)
                dlist *qname = l->h->next->data.lval;
                char *sname = qname_schema(qname);
                char *name = qname_table(qname);
-               int temp = l->h->data.i_val;
+               int temp = (s->token == SQL_DECLARE_TABLE) ? SQL_DECLARED_TABLE 
: l->h->data.i_val;
                dlist *credentials = 
l->h->next->next->next->next->next->data.lval;
                char *username = credentials_username(credentials);
                char *password = credentials_password(credentials);
diff --git a/sql/server/rel_select.c b/sql/server/rel_select.c
--- a/sql/server/rel_select.c
+++ b/sql/server/rel_select.c
@@ -971,10 +971,10 @@ table_ref(sql_query *query, sql_rel *rel
 
                if (sname && !(s = mvc_bind_schema(sql, sname)))
                        return sql_error(sql, 02, SQLSTATE(3F000) "SELECT: no 
such schema '%s'", sname);
-               if (!t && !sname) {
-                       t = stack_find_table(sql, tname);
-                       if (!t) 
-                               temp_table = stack_find_rel_view(sql, tname);
+               if (!sname) {
+                       temp_table = stack_find_rel_view(sql, tname);
+                       if (!temp_table)
+                               t = mvc_bind_table(sql, NULL, tname);
                }
                if (!t && !temp_table) {
                        t = mvc_bind_table(sql, s, tname);
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
@@ -1100,8 +1100,6 @@ update_table(sql_query *query, dlist *qn
                t = mvc_bind_table(sql, s, tname);
                if (!t) 
                        t = mvc_bind_table(sql, NULL, tname);
-               if (!t) 
-                       t = stack_find_table(sql, tname);
        }
        if (update_allowed(sql, t, tname, "UPDATE", "update", 0) != NULL) {
                sql_rel *r = NULL, *bt = rel_basetable(sql, t, t->base.name), 
*res = bt;
@@ -1207,8 +1205,6 @@ delete_table(sql_query *query, dlist *qn
                t = mvc_bind_table(sql, schema, tname);
                if (!t) 
                        t = mvc_bind_table(sql, NULL, tname);
-               if (!t) 
-                       t = stack_find_table(sql, tname);
        }
        if (update_allowed(sql, t, tname, "DELETE FROM", "delete from", 1) != 
NULL) {
                sql_rel *r = NULL;
@@ -1264,8 +1260,6 @@ truncate_table(mvc *sql, dlist *qname, i
                t = mvc_bind_table(sql, schema, tname);
                if (!t)
                        t = mvc_bind_table(sql, NULL, tname);
-               if (!t)
-                       t = stack_find_table(sql, tname);
        }
        if (update_allowed(sql, t, tname, "TRUNCATE", "truncate", 2) != NULL)
                return rel_truncate(sql->sa, rel_basetable(sql, t, tname), 
restart_sequences, drop_action);
@@ -1344,8 +1338,6 @@ merge_into_table(sql_query *query, dlist
                t = mvc_bind_table(sql, s, tname);
                if (!t)
                        t = mvc_bind_table(sql, NULL, tname);
-               if (!t)
-                       t = stack_find_table(sql, tname);
        }
        if (!t)
                return sql_error(sql, 02, SQLSTATE(42S02) "MERGE: no such table 
'%s'", tname);
@@ -1618,8 +1610,8 @@ copyfrom(sql_query *query, dlist *qname,
        if (!t && !sname) {
                s = tmp_schema(sql);
                t = mvc_bind_table(sql, s, tname);
-               if (!t)
-                       t = stack_find_table(sql, tname);
+               if (!t) 
+                       t = mvc_bind_table(sql, NULL, tname);
        }
        if (insert_allowed(sql, t, tname, "COPY INTO", "copy into") == NULL)
                return NULL;
@@ -1831,7 +1823,7 @@ bincopyfrom(sql_query *query, dlist *qna
                s = tmp_schema(sql);
                t = mvc_bind_table(sql, s, tname);
                if (!t) 
-                       t = stack_find_table(sql, tname);
+                       t = mvc_bind_table(sql, NULL, tname);
        }
        if (insert_allowed(sql, t, tname, "COPY INTO", "copy into") == NULL) 
                return NULL;
@@ -1904,8 +1896,8 @@ copyfromloader(sql_query *query, dlist *
        if (!t && !sname) {
                s = tmp_schema(sql);
                t = mvc_bind_table(sql, s, tname);
-               if (!t)
-                       t = stack_find_table(sql, tname);
+               if (!t) 
+                       t = mvc_bind_table(sql, NULL, tname);
        }
        //TODO the COPY LOADER INTO should return an insert relation (instead 
of ddl) to handle partitioned tables properly
        if (insert_allowed(sql, t, tname, "COPY INTO", "copy into") == NULL)
diff --git a/sql/server/sql_mvc.c b/sql/server/sql_mvc.c
--- a/sql/server/sql_mvc.c
+++ b/sql/server/sql_mvc.c
@@ -1440,6 +1440,7 @@ str
 mvc_drop_table(mvc *m, sql_schema *s, sql_table *t, int drop_action)
 {
        TRC_DEBUG(SQL_TRANS, "Drop table: %s %s\n", s->base.name, t->base.name);
+
        if (isRemote(t)) {
                str AUTHres;
                sql_allocator *sa = m->sa;
@@ -1461,8 +1462,9 @@ mvc_drop_table(mvc *m, sql_schema *s, sq
                if(AUTHres != MAL_SUCCEED)
                        return AUTHres;
        }
-
-       if(sql_trans_drop_table(m->session->tr, s, t->base.id, drop_action ? 
DROP_CASCADE_START : DROP_RESTRICT))
+       if (isDeclaredTable(t))
+               return MAL_SUCCEED;
+       if (sql_trans_drop_table(m->session->tr, s, t->base.id, drop_action ? 
DROP_CASCADE_START : DROP_RESTRICT))
                throw(SQL, "sql.mvc_drop_table", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
        return MAL_SUCCEED;
 }
diff --git a/sql/test/miscellaneous/Tests/declared_tables.stable.out 
b/sql/test/miscellaneous/Tests/declared_tables.stable.out
--- a/sql/test/miscellaneous/Tests/declared_tables.stable.out
+++ b/sql/test/miscellaneous/Tests/declared_tables.stable.out
@@ -68,7 +68,7 @@ stdout of test 'declared_tables` in dire
 #insert into iamdeclared values (1, 'one');
 [ 1    ]
 #select a, b from iamdeclared;
-% sys.iamdeclared,     sys.iamdeclared # table_name
+% %dt%.iamdeclared,    %dt%.iamdeclared # table_name
 % a,   b # name
 % int, varchar # type
 % 1,   3 # length
diff --git a/sql/test/miscellaneous/Tests/groupby_error.sql 
b/sql/test/miscellaneous/Tests/groupby_error.sql
--- a/sql/test/miscellaneous/Tests/groupby_error.sql
+++ b/sql/test/miscellaneous/Tests/groupby_error.sql
@@ -62,6 +62,9 @@ SELECT + 2 FROM tab0 AS cor0 WHERE NOT -
 SELECT * FROM tab0 WHERE NOT - col0 - col1 * col2 <= ( + col0 ) AND NOT ( + 
col2 + col1 - col1 ) NOT BETWEEN - col0 AND - col1 + - col2 / col1;
        -- empty
 
+SELECT * FROM tab0 AS cor0 WHERE NOT col1 BETWEEN - col0 AND col0 + col1 * 
col1 AND - col1 BETWEEN col0 AND ( NULL ) OR NOT col0 * col0 + col0 <= NULL;
+       -- empty
+
 SELECT DISTINCT * FROM tab2 WHERE NOT ( - + 50 + ( 70 ) ) = + col2;
        -- 64 77 40
        -- 46 51 23
diff --git a/sql/test/miscellaneous/Tests/simple_selects.sql 
b/sql/test/miscellaneous/Tests/simple_selects.sql
--- a/sql/test/miscellaneous/Tests/simple_selects.sql
+++ b/sql/test/miscellaneous/Tests/simple_selects.sql
@@ -135,3 +135,7 @@ SELECT CAST(COUNT(*) - myvar AS BIGINT) 
 drop table x;
 
 SELECT 1, 2 INTO myvar; --error, number of variables don't match
+
+declare table x (a int);
+declare table x (c int); --error table x already declared
+drop table if exists x;
diff --git a/sql/test/miscellaneous/Tests/simple_selects.stable.err 
b/sql/test/miscellaneous/Tests/simple_selects.stable.err
--- a/sql/test/miscellaneous/Tests/simple_selects.stable.err
+++ b/sql/test/miscellaneous/Tests/simple_selects.stable.err
@@ -165,6 +165,10 @@ MAPI  = (monetdb) /var/tmp/mtest-106925/
 QUERY = SELECT 1, 2 INTO myvar; --error, number of variables don't match
 ERROR = !SELECT INTO: number of values doesn't match number of variables to set
 CODE  = 21S01
+MAPI  = (monetdb) /var/tmp/mtest-881990/.s.monetdb.34703
+QUERY = declare table x (c int); --error table x already declared
+ERROR = !DECLARE TABLE: name 'x' already in use
+CODE  = 42S01
 
 # 17:31:38 >  
 # 17:31:38 >  "Done."
diff --git a/sql/test/miscellaneous/Tests/simple_selects.stable.out 
b/sql/test/miscellaneous/Tests/simple_selects.stable.out
--- a/sql/test/miscellaneous/Tests/simple_selects.stable.out
+++ b/sql/test/miscellaneous/Tests/simple_selects.stable.out
@@ -295,7 +295,7 @@ stdout of test 'simple_selects` in direc
 #select length(myblob), octet_length(myblob), length(mystr), 
octet_length(mystr) 
 #from (values (cast(null as blob), cast(null as char(32)))) as my(myblob, 
mystr);
 % .,   .,      .,      . # table_name
-% %3,  %4,     %5,     %6 # name
+% %5,  %6,     %7,     %10 # name
 % int, int,    int,    int # type
 % 1,   1,      1,      1 # length
 [ NULL,        NULL,   NULL,   NULL    ]
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to