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

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

Closed
derekmahar opened this issue Oct 30, 2019 · 2 comments

Comments

@derekmahar
Copy link
Contributor

derekmahar commented Oct 30, 2019

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 > ())
@derekmahar derekmahar changed the title When pasting SQL query containing an open parenthesis into interactive shell, automatic closing of parentheses causes syntax error. When pasting SQL query containing open parentheses into interactive shell, csvq automatically closes these parentheses, causing syntax errors. Oct 30, 2019
mithrandie added a commit that referenced this issue Oct 31, 2019
- Modify the behavior of enclosures completion on the interactive shell. ([Github #12](#12))
@mithrandie
Copy link
Owner

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
Copy link
Contributor Author

Thank you for fixing this issue!

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