mtxr / vscode-sqltools

Database management for VSCode
https://vscode-sqltools.mteixeira.dev?utm_source=github&utm_medium=homepage-link
MIT License
1.43k stars 290 forks source link

Results table names all additional unnamed columns "?column? (1)" and conflates their output #1276

Open kit-transue opened 5 months ago

kit-transue commented 5 months ago

Describe the bug

If a query returns more than two unnamed columns, the results table mistakenly uses the values from only the last column for all the unnamed columns after the first.

To Reproduce Steps to reproduce the behavior:

  1. In a .sql file (the default connection log is excellent)
  2. Add the SQL: "SELECT 1, 2, 3;"
  3. Run on a connection
  4. Observe: results table names the column headers: "?column?", "?column? (1)", "?column? (1)"
  5. Observe: results row shows values: 1, 3, 3. (The first 3--in the second column--is unexpected)

Expected behavior

Screenshots Attached showing "3" as wrong value in middle column of results table; column header wrong for last column in results table.

Desktop (please complete the following information):

SQLTools-3unnamed-columns
kit-transue commented 5 months ago

This is a result of PostgreSQL's naming of anonymous columns or aggregate functions:

postgres=# select sum(a), sum(b), sum(c) FROM (select 1 a, 2 b, 3 c);
 sum | sum | sum 
-----+-----+-----
   1 |   2 |   3
(1 row)

Other engines may generate distinct column names with aggregate functions:

sqlite> select sum(a), sum(b), sum(c) FROM (select 1 a, 2 b, 3 c);
sum(a)|sum(b)|sum(c)
1|2|3

I don't know if the columns names will always be distinct, and the error can be induced by forcing the columns to collide:

SELECT 1 AS count, 2 AS count, 3 AS count;

SQLTools-SQLite-forced-column-collision