Skip to content

[SPARK-52545][SQL][DOCS] Update string literal docs for quote escaping rules #51379

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/sql-ref-literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ A string literal is used to specify a character string value.

* **char**

One character from the character set. Use `\` to escape special characters (e.g., `'` or `\`).
One character from the character set. Use `\` to escape special characters (e.g., `'` or `\`), additionally, consecutive quotes can be used for escaping (e.g., `'S''park'` equals `'S\'park'`, `"S""park"` equals `"S\"park"`).
To represent unicode characters, use 16-bit or 32-bit unicode escape of the form `\uxxxx` or `\Uxxxxxxxx`,
where xxxx and xxxxxxxx are 16-bit and 32-bit code points in hexadecimal respectively (e.g., `\u3042` for `あ` and `\U0001F44D` for `👍`).
An ASCII character can also be represented as an octal number preceded by `\` like `\101`, which represents `A`.
Expand All @@ -62,9 +62,13 @@ The following escape sequences are recognized in regular string literals (withou
- `\%` -> `\%`;
- `\_` -> `\_`;
- `\<other char>` -> `<other char>`, skip the slash and leave the character as is.
- `""` -> `"`, skip first `"` in double-quoted string.
- `''` -> `'`, skip first `'` in single-quoted string.

The unescaping rules above can be turned off by setting the SQL config `spark.sql.parser.escapedStringLiterals` to `true`.

When consecutive quotes conflict with string concatenation, escaping takes precedence (e.g., `'a''b'` → `a'b` not `a`+`b`). To force string concatenation behavior instead, set `spark.sql.legacy.consecutiveStringLiterals.enabled` to `true`.

#### Examples

```sql
Expand Down Expand Up @@ -95,6 +99,13 @@ SELECT r"'\n' represents newline character." AS col;
+----------------------------------+
|'\n' represents newline character.|
+----------------------------------+

SELECT "S""park" AS f1, 'S''park' AS f2;
+--------+--------+
| f1| f2|
+--------+--------+
| S"park | S'park |
+--------+--------+
```

### Binary Literal
Expand Down