mithrandie / csvq

SQL-like query language for csv
https://mithrandie.github.io/csvq
MIT License
1.5k stars 65 forks source link

Duplicate table name error in SELECT UNION query. #53

Closed derekmahar closed 3 years ago

derekmahar commented 3 years ago

In a SELECT UNION query, shouldn't csvq allow the use of the same table name in any or all of its component queries? For example, instead of reporting a duplicate table name error, shouldn't the following query calculate the union of the input rows?

Actual result:

$ printf "a,b\n1,2\n3,4\n" | csvq "SELECT * FROM STDIN UNION ALL SELECT * FROM STDIN"
[L:1 C:45] table name STDIN is a duplicate

Expected result:

$ printf "a,b\n1,2\n3,4\n" | csvq "SELECT * FROM STDIN UNION ALL SELECT * FROM STDIN"
+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 3 | 4 |
| 1 | 2 |
| 3 | 4 |
+---+---+
$ csvq --version
csvq version 1.15.0

Note that this query worked in an earlier version of csvq, but I don't recall exactly which version.

mithrandie commented 3 years ago

You cannot use the same table name in the same scope, so that is the expected behavior. However, it may not be a problem to allow the same table name on both sides of the set operator.

The same table can be merged by adding aliases to the table name.

$ printf "a,b\n1,2\n3,4\n" | csvq "SELECT * FROM STDIN t1 UNION ALL SELECT * FROM STDIN t2"

You can also enclose the queries on both sides of the set operator in parentheses.

$ printf "a,b\n1,2\n3,4\n" | csvq "(SELECT * FROM STDIN) UNION ALL (SELECT * FROM STDIN)"

...Oh? I'm getting a syntax error with this query. Maybe this is a bug.

mithrandie commented 3 years ago

The version 1.15.1 allows same table names on both sides of set operators.