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

Parsing fails if -- comment is included inside arguments of function call (but succeeds with /* comment */) #375

Closed
milesrichardson opened this issue Apr 27, 2023 · 1 comment

Comments

@milesrichardson
Copy link

milesrichardson commented Apr 27, 2023

It's possible this is a non-issue. I'm not sure if this (the failing query) is valid SQL or not. Perhaps it's valid SQL when sent in the query, but once it gets normalized (newlines removed) it's not valid SQL.

This query succeeds (no comment):

curl -i \
    -H "Content-Type: application/json" \
https://splitgraph-testing-nomad.seafowl.cloud/q -d@-<<EOF
{"query": "WITH t (
          c_int16_smallint,
          c_int32_int
        ) AS (
          VALUES(
            42::SMALLINT,
            99::INT
          )
        ) SELECT * FROM t;
"}
EOF

# Returns:
HTTP/2 200 
content-type: application/octet-stream
vary: Content-Type, Origin, X-Seafowl-Query
content-length: 41
date: Thu, 27 Apr 2023 00:15:36 GMT
server: Fly/199ec4d3 (2023-04-25)
via: 2 fly.io
fly-request-id: 01GZ011GHJERYT9V5102YEE0XV-lhr

{"c_int16_smallint":42,"c_int32_int":99}

This query fails (note the comment -- Some comment inside the VALUES() function (the result is the same even if the comment starts at the beginning of the line):

curl -i \
    -H "Content-Type: application/json" \
https://splitgraph-testing-nomad.seafowl.cloud/q -d@-<<EOF
{"query": "WITH t (
          c_int16_smallint,
          c_int32_int
        ) AS (
          VALUES(
		  -- Some comment
            42::SMALLINT,
            99::INT
          )
        ) SELECT * FROM t;
"}
EOF

# Returns
HTTP/2 400 
vary: Content-Type, Origin, X-Seafowl-Query
content-length: 61
date: Thu, 27 Apr 2023 00:18:44 GMT
server: Fly/199ec4d3 (2023-04-25)
via: 2 fly.io
fly-request-id: 01GZ0177Q78W55HQ4V9FT7YGR7-lhr

SQL error: ParserError("Expected an expression:, found: EOF")

This query succeeds (note the /* Some comment */:

curl -i \
    -H "Content-Type: application/json" \
https://splitgraph-testing-nomad.seafowl.cloud/q -d@-<<EOF
{"query": "WITH t (
          c_int16_smallint,
          c_int32_int
        ) AS (
          VALUES(
		  /* Some comment */
            42::SMALLINT,
            99::INT
          )
        ) SELECT * FROM t;
"}
EOF


# Returns
HTTP/2 200 
content-type: application/octet-stream
vary: Content-Type, Origin, X-Seafowl-Query
content-length: 41
date: Thu, 27 Apr 2023 00:23:59 GMT
server: Fly/199ec4d3 (2023-04-25)
via: 2 fly.io
fly-request-id: 01GZ01GVS3SGZYZFHMKB2136TC-lhr

{"c_int16_smallint":42,"c_int32_int":99}
@gruuya
Copy link
Contributor

gruuya commented Apr 27, 2023

The reason for this is actually external to Seafowl: the curl's -d option strips the control characters (CR, LF) from the query. Hence, the raw query received by Seafowl in example #2 is

WITH t (          c_int16_smallint,          c_int32_int        ) AS (          VALUES(                  -- Some comment            42::SMALLINT,            99::INT          )        ) SELECT * FROM t;

Note that the part of the query after the comment has turned into a comment, which leads to the parsing error. The /* Some comment */ variant has a closing delimiter, so it doesn't interfere with the parsing.

This shouldn't be an issue with other clients. For instance in Python:

In [99]: query = """WITH t (
    ...:           c_int16_smallint,
    ...:           c_int32_int
    ...:         ) AS (
    ...:           VALUES(
    ...:             -- Some comment
    ...:             42::SMALLINT,
    ...:             99::INT
    ...:           )
    ...:         ) SELECT * FROM t;
    ...: """

In [100]: r = requests.post("http://localhost:8080/q", json={"query": query}, headers={"content-type": "application/json"})

In [101]: print(r.text)
{"c_int16_smallint":42,"c_int32_int":99}

whereby the query received by Seafowl has explicit line breaks:

WITH t (\n          c_int16_smallint,\n          c_int32_int\n        ) AS (\n          VALUES(\n            -- Some comment\n            42::SMALLINT,\n            99::INT\n          )\n        ) SELECT * FROM t;\n

@gruuya gruuya closed this as completed Apr 27, 2023
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