Bug: CREATE TABLE AS with GROUP BY preserves backticks in generated column name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug: CREATE TABLE AS with GROUP BY preserves backticks in generated column name

Alek Storm
> CREATE TABLE t1 (col1 INT);
> CREATE TABLE t2 AS SELECT `col1` FROM t1 GROUP BY col1;
> .schema t2
CREATE TABLE t2("`col1`" INT);

I expected: CREATE TABLE t2(col1 INT);

Note the following generate the schema I expect:
# With backticks, without GROUP BY:
> CREATE TABLE t2 AS SELECT `col1` FROM t1;
> .schema t2
CREATE TABLE t2(col1 INT);

# Without backticks, with GROUP BY:
> CREATE TABLE t2 AS SELECT col1 FROM t1 GROUP BY col1;
> .schema t2
CREATE TABLE t2(col1 INT);

Alek
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Bug: CREATE TABLE AS with GROUP BY preserves backticks in generated column name

Cezary H. Noweta
Hello,

On 2017-02-10 20:34, Alek Storm wrote:
>> CREATE TABLE t1 (col1 INT);
>> CREATE TABLE t2 AS SELECT `col1` FROM t1 GROUP BY col1;
>> .schema t2
> CREATE TABLE t2("`col1`" INT);
>
> I expected: CREATE TABLE t2(col1 INT);

This comes from the fact that all TK_COLUMN expressions in SELECT ...
GROUP BY command are transformed into TK_AGG_COLUMN ones and as such
their names are taken from ``zSpan'' (i.e. original text of an
expression). Because the original text contains back quotes, it is
further enclosed in double quotes. The following patch would help,
however I'm not sure if an additional checking/validation is not required:

File ``select.c'', foo ``sqlite3ColumnsFromExprList(...)'', change the
following line:

       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){

into

       if( (pColExpr->op==TK_COLUMN || pColExpr->op==TK_AGG_COLUMN) &&
ALWAYS(pColExpr->pTab!=0) ){


-- best regards

Cezary H. Noweta
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users