mithrandie / csvq

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

When pasting SQL query containing open parentheses into interactive shell, csvq automatically closes these parentheses, causing syntax errors. #12

Closed derekmahar closed 4 years ago

derekmahar commented 4 years ago

When pasting an SQL query containing open parentheses into the interactive shell, csvq automatically closes these parentheses, causing syntax errors.

For example, when pasting the following query into the interactive shell, csvq appends a trailing closing parenthesis.

WITH RECURSIVE t (n)
  AS (
    SELECT 1
    UNION ALL
    SELECT n + 1
      FROM t
     WHERE n < 5
  )
SELECT n FROM t;

After pasting into interactive shell:

/home/derek > WITH RECURSIVE t (n))
[L:1 C:21] syntax error: unexpected token ")"
/home/derek >   AS ()
[L:1 C:3] syntax error: unexpected token "AS"
/home/derek >     SELECT 1
+---+
| 1 |
+---+
| 1 |
+---+
/home/derek >     UNION ALL
[L:1 C:5] syntax error: unexpected token "UNION"
/home/derek >     SELECT n + 1
[L:1 C:12] field n does not exist
/home/derek >       FROM t
[L:1 C:7] syntax error: unexpected token "FROM"
/home/derek >      WHERE n < 5
[L:1 C:6] syntax error: unexpected token "WHERE"
/home/derek >   )
[L:1 C:3] syntax error: unexpected token ")"
/home/derek > SELECT n FROM t;
[L:1 C:15] file t does not exist

As a minimal example, notice that after pasting the string "()" into the interactive shell, csvq expands this string into "())":

/home/derek > ())
mithrandie commented 4 years ago

Thanks. The behavior of enclosures completion when pasting has been fixed.

However, even though the behavior has been fixed, pasting the code above will not execute correctly. On the interactive shell, you must add a backslash at the end of the line for a line break. In order to paste and execute, the code needs to be as follows.

WITH RECURSIVE t (n) \
  AS ( \
    SELECT 1 \
    UNION ALL \
    SELECT n + 1 \
      FROM t \
     WHERE n < 5 \
  ) \
SELECT n FROM t;

This describing method depends on the external package github.com/chzyer/readline, and many console applications have similar limitations. When executing a query that extends over multiple lines, I recommend to execute by writing the query in a file and loading it.

$ cat query.sql
WITH RECURSIVE t (n)
  AS (
    SELECT 1
    UNION ALL
    SELECT n + 1
      FROM t
     WHERE n < 5
  )
SELECT n FROM t;

$ csvq -s query.sql
+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+
$ csvq
/home/mithrandie > SOURCE `query.sql`;
+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+
/home/mithrandie >
derekmahar commented 4 years ago

Thank you for fixing this issue!