harelba / q

q - Run SQL directly on delimited files and multi-file sqlite databases

Home Page:http://harelba.github.io/q/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use filename in column disambiguation, must use alias

redsaz opened this issue · comments

Description

When there are two or more tables in a query, and those two tables have an identical column name, the column needs to be disambiguated somehow.

Example 1: In most (all?) SQL DBs, one can use an alias, as shown here:

SELECT low.temperature, high.temperature FROM daily_lows low JOIN daily_highs high ON low.day = high.day;

Example 2: In most SQL DBs (or, at least mysql and postgres), one can use the table name directly:

SELECT daily_lows.temperature, daily_highs.temperature FROM daily_lows JOIN daily_highs ON daily_lows.day = daily_highs.day;

However, in q, only Example 1 works. Being able to refer to the filename to disambiguate columns would help those expecting q to behave similar to other SQL engines.

Steps to reproduce

  1. Create a file, daily_lows:
day,temperature
1,10
2,11
3,12
  1. Create a file, daily_highs:
1,20
2,21
3,22
  1. Query the file with aliases to verify the files work: q-text-as-data -d, -H 'SELECT low.temperature, high.temperature FROM daily_lows low JOIN daily_highs high ON low.day = high.day;'
  2. Query the file without aliases: q-text-as-data -d, -H 'SELECT daily_lows.temperature, daily_highs.temperature FROM daily_lows JOIN daily_highs ON daily_lows.day = daily_highs.day;'

Actual results

The second query fails to find the columns:

query error: no such column: daily_lows.temperature
Warning - There seems to be a "no such column" error, and -H (header line) exists. Please make sure that you are using the column names from the header line and not the default (cXX) column names. Another issue might be that the file contains a BOM. Files that are encoded with UTF8 and contain a BOM can be read by specifying `-e utf-9-sig` in the command line. Support for non-UTF8 encoding will be provided in the future.

Expected results

The second query works just like the first:

10,20
11,21
12,22

Notes

This issue may be a duplicate of #87 "Allow access to the original filename in queries" but I wasn't sure since there was no description.

Hi, sorry for the late response. I'm focused on packaging in order to release a new major version 3.0.0 with lots of big changes.

I'll dive into this right after the release is done.