Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate table name error in SELECT UNION query. #53

Closed
derekmahar opened this issue Apr 14, 2021 · 2 comments
Closed

Duplicate table name error in SELECT UNION query. #53

derekmahar opened this issue Apr 14, 2021 · 2 comments

Comments

@derekmahar
Copy link
Contributor

derekmahar commented Apr 14, 2021

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.

@derekmahar derekmahar changed the title Duplicate table name error in SELECT UNION operation. Duplicate table name error in SELECT UNION query. Apr 14, 2021
@mithrandie
Copy link
Owner

mithrandie commented Apr 14, 2021

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 added a commit that referenced this issue May 5, 2021
- Allow same table names on both sides of set operators. ([GitHub #53](#53))
- Add the command option "--allow-uneven-fields". ([GitHub #54](#54))
- Fix a bug in parsing of set operations.
@mithrandie
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants