Changeset: 035bc030ee6c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=035bc030ee6c
Modified Files:
        sql/server/rel_rel.c
        sql/server/rel_select.c
        sql/test/miscellaneous/Tests/simple_selects.stable.err
Branch: octbugs
Log Message:

Fixing bugs with aliases. When binding a group by column from an alias, look 
for a possible duplicated one. Also look for ambiguous identifiers when binding 
a column from a projection before returning


diffs (117 lines):

diff --git a/sql/server/rel_rel.c b/sql/server/rel_rel.c
--- a/sql/server/rel_rel.c
+++ b/sql/server/rel_rel.c
@@ -269,18 +269,22 @@ rel_bind_column_(mvc *sql, int *exp_has_
        case op_groupby:
        case op_project:
        case op_table:
-       case op_basetable:
-               if (rel->exps && exps_bind_column(rel->exps, cname, &ambiguous, 
&multi, no_tname))
-                       return rel;
-               if (rel->r && is_groupby(rel->op) && exps_bind_column(rel->r, 
cname, &ambiguous, &multi, no_tname))
-                       return rel;
+       case op_basetable: {
+               sql_exp *found = NULL;
+
+               if (rel->exps)
+                       found = exps_bind_column(rel->exps, cname, &ambiguous, 
&multi, no_tname);
+               if (!found && rel->r && is_groupby(rel->op))
+                       found = exps_bind_column(rel->r, cname, &ambiguous, 
&multi, no_tname);
                if (ambiguous || multi)
                        return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) 
"SELECT: identifier '%s' ambiguous", cname);
+               if (found)
+                       return rel;
                if (is_processed(rel))
                        return NULL;
                if (rel->l && !(is_base(rel->op)))
                        return rel_bind_column_(sql, exp_has_nil, rel->l, 
cname, no_tname);
-               break;
+               } break;
        case op_semi:
        case op_anti:
 
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
@@ -3862,12 +3862,14 @@ rel_next_value_for( mvc *sql, symbol *se
 
 /* some users like to use aliases already in the groupby */
 static sql_exp *
-rel_selection_ref(sql_query *query, sql_rel **rel, symbol *grp, dlist 
*selection )
+rel_selection_ref(sql_query *query, sql_rel **rel, symbol *grp, dlist 
*selection)
 {
        sql_allocator *sa = query->sql->sa;
-       dlist *gl;
+       dlist *nl, *gl;
        char *name = NULL;
        exp_kind ek = {type_value, card_column, FALSE};
+       sql_exp *res = NULL;
+       symbol *nsym;
 
        if (grp->token != SQL_COLUMN && grp->token != SQL_IDENT)
                return NULL;
@@ -3884,32 +3886,28 @@ rel_selection_ref(sql_query *query, sql_
                if (to == SQL_COLUMN || to == SQL_IDENT) {
                        dlist *l = n->data.sym->data.lval;
                        /* AS name */
-                       if (l->h->next->data.sval &&
-                                       strcmp(l->h->next->data.sval, name) == 
0){
+                       if (l->h->next->data.sval && 
strcmp(l->h->next->data.sval, name) == 0) {
                                sql_exp *ve = rel_value_exp(query, rel, 
l->h->data.sym, sql_sel|sql_groupby, ek);
                                if (ve) {
-                                       dlist *l = dlist_create(sa);
-                                       symbol *sym;
+                                       if (res)
+                                               return sql_error(query->sql, 
ERR_AMBIGUOUS, SQLSTATE(42000) "SELECT: identifier '%s' ambiguous", name);
+                                       res = ve;
+
+                                       nl = dlist_create(sa);
                                        exp_setname(sa, ve, NULL, name);
-                                       /* now we should rewrite the selection
-                                          such that it uses the new group
-                                          by column
-                                       */
-                                       dlist_append_string(sa, l,
-                                               sa_strdup(sa, name));
-                                       sym = symbol_create_list(sa, to, l);
-                                       l = dlist_create(sa);
-                                       dlist_append_symbol(sa, l, sym);
+                                       /* now we should rewrite the selection 
such that it uses the new group by column */
+                                       dlist_append_string(sa, nl, 
sa_strdup(sa, name));
+                                       nsym = symbol_create_list(sa, to, nl);
+                                       nl = dlist_create(sa);
+                                       dlist_append_symbol(sa, nl, nsym);
                                        /* no alias */
-                                       dlist_append_symbol(sa, l, NULL);
-                                       n->data.sym = symbol_create_list(sa, 
to, l);
-
+                                       dlist_append_symbol(sa, nl, NULL);
+                                       n->data.sym = symbol_create_list(sa, 
to, nl);
                                }
-                               return ve;
                        }
                }
        }
-       return NULL;
+       return res;
 }
 
 static sql_exp*
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
@@ -266,6 +266,14 @@ MAPI  = (monetdb) /var/tmp/mtest-493793/
 QUERY = select cast(13121 as varchar(2147483647)) || cast(231231 as 
varchar(2147483647)); --error, too big
 ERROR = !SELECT: output number of digits for concat is too large
 CODE  = 42000
+MAPI  = (monetdb) /var/tmp/mtest-921778/.s.monetdb.37437
+QUERY = select x as z, y as z from (select 1, 2) as x(x,y) group by z;
+ERROR = !SELECT: identifier 'z' ambiguous
+CODE  = 42000
+MAPI  = (monetdb) /var/tmp/mtest-921778/.s.monetdb.37437
+QUERY = select x as z, y as z from (select 1, 2) as x(x,y) order by z;
+ERROR = !SELECT: identifier 'z' ambiguous
+CODE  = 42000
 
 # 17:31:38 >  
 # 17:31:38 >  "Done."
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to