prql icon indicating copy to clipboard operation
prql copied to clipboard

Quotes & literals

Open max-sixty opened this issue 4 years ago • 4 comments

Single vs. double quotes for literals (literals like the USA in country = "USA")

  • While it's generally better to only have one way of doing things, being flexible on this dimension is friendlier (like Python is).
  • SQL uses single-quotes for string literals. But some engines use double-quoted strings in some specific circumstances.

Passing through SQL, and f-strings

  • It would be useful to be able to pass through SQL, because there's be lots we haven't implemented in PRQL when we start. for example, UPPER could be defined as:
    func upper col = f"UPPER({col})"
    
  1. We could use double quotes for this
  2. We could also use backticks, but they make pasting code snippets difficult given markdown
  3. We could use f-strings, which would be required even if there were no expression — basically any f-string gets interpolated and passed through as SQL
    • If this is confusing because it's similiar-but-not-the-same as python — because they don't end up as literal strings — we could call them s-strings etc

One potential synthesis of these:

  • Both double quotes and single quotes are allowed for literals
  • We use f-strings for SQL (or s-strings)
  • To represent double quotes, you need to combine these; e.g. f' "column name with spaces" ', would compile to "column name with spaces".

max-sixty avatar Feb 21 '22 02:02 max-sixty

Following the python f-string syntax is very nice.

Will it be possible in PRQL to have string interpolation in a filter? Or is the interpolated string just for SQL snippets?

If it is the former then it might be nice to use a s-string as then it would be possible to have SQL syntax highlighting in an editor and also enable static analysis of the SQL statement.

danjones1618 avatar Feb 21 '22 03:02 danjones1618

I'm for mandatory f-strings for all SQL , whether it needs interpolating or not.

@danjones1618 brings up a good question about inline interpolation for filter strings being supported for v1.

I vote yes , support f-strings in filter and aggregate.

And if calling them s-strings gets us syntax highlighting I'm for that change, conceptually makes sense as @max-sixty pointed out.

qorrect avatar Feb 22 '22 00:02 qorrect

OK great, let's proceed on this, then!

Agree re them working in filters / aggregates. I'm not sure how far we can take this — e.g. we probably can't have a full line in a pipeline as one, but we can explore the margin (can a CTE be one? window func? etc)

On reflection, I think we call it "s-string" (or something like that) given it's different enough from "f-string" — since the result of an f-string is a string literal, but the result of an s-string isn't. Very open to ideas or disagreement. I'm not sure if "s-string" could be confused for "s-expr", my guess is that it's probably OK.

max-sixty avatar Feb 22 '22 00:02 max-sixty

From #120 — this is now implemented in the parser. It's not complete in the compiler.

I've also left adding single-quotes as an easy initial contribution if someone wants to take that

max-sixty avatar Feb 23 '22 04:02 max-sixty