Skip to content

Commit

Permalink
[IOTDB-4410] Change antlr grammar for single/double quotation used in…
Browse files Browse the repository at this point in the history
… single/double quotation (apache#7317)
  • Loading branch information
lancelly authored Sep 15, 2022
1 parent 4bebeee commit 349f860
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 27 deletions.
4 changes: 2 additions & 2 deletions antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,11 @@ fragment CN_CHAR
;

fragment DQUOTA_STRING
: '"' ( '\\"' | '""' | ~('"') )* '"'
: '"' ( '""' | ~('"') )* '"'
;

fragment SQUOTA_STRING
: '\'' ( '\\\'' | '\'\'' | ~('\'') )* '\''
: '\'' ( '\'\'' | ~('\'') )* '\''
;

fragment BQUOTA_STRING
Expand Down
5 changes: 0 additions & 5 deletions docs/UserGuide/Reference/Syntax-Conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ Usages of string literals:

There are several ways to include quote characters within a string:

- Precede the quote character by an escape character (\\).
- `'` inside a string quoted with `"` needs no special treatment and need not be doubled or escaped. In the same way, `"` inside a string quoted with `'` needs no special treatment.
- A `'` inside a string quoted with `'` may be written as `''`.
- A `"` inside a string quoted with `"` may be written as `""`.
Expand All @@ -256,15 +255,11 @@ The following examples demonstrate how quoting and escaping work:
'string' // string
'"string"' // "string"
'""string""' // ""string""
'str\'ing' // str'ing
'\'string' // 'string
'''string' // 'string

"string" // string
"'string'" // 'string'
"''string''" // ''string''
"str\"ing" // str"ing
"\"string" // "string
"""string" // "string
```

Expand Down
8 changes: 0 additions & 8 deletions docs/zh/UserGuide/Reference/Syntax-Conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,7 @@ MySQL 对字符串的定义可以参考:[MySQL :: MySQL 8.0 Reference Manual :
#### 如何在字符串内使用引号

- 在单引号引起的字符串内,双引号无需特殊处理。同理,在双引号引起的字符串内,单引号无需特殊处理。

- 在引号前使用转义符 (\\)。

- 在单引号引起的字符串里,可以通过双写单引号来表示一个单引号,即单引号 ' 可以表示为 ''。

- 在双引号引起的字符串里,可以通过双写双引号来表示一个双引号,即双引号 " 可以表示为 ""。

字符串内使用引号的示例如下:
Expand All @@ -256,15 +252,11 @@ MySQL 对字符串的定义可以参考:[MySQL :: MySQL 8.0 Reference Manual :
'string' // string
'"string"' // "string"
'""string""' // ""string""
'str\'ing' // str'ing
'\'string' // 'string
'''string' // 'string
"string" // string
"'string'" // 'string'
"''string''" // ''string''
"str\"ing" // str"ing
"\"string" // "string
"""string" // "string
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ public void testExpression() {
}
Assert.assertEquals(0, cnt);
}

cnt = 0;
try (ResultSet resultSet =
statement.executeQuery(
"SELECT text FROM root.sg1.d1 where text = '\\' and text = 'asdf'")) {
while (resultSet.next()) {
cnt++;
}
Assert.assertEquals(0, cnt);
}
} catch (SQLException e) {
e.printStackTrace();
fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public void testStringLiteralWithSingleQuote() {
"'``string'",
"'\"string\"'",
"'\"\"string'",
"'\\\"string\\\"'",
"'''string'",
"'\\'string'",
"'\\nstring'",
"'\\rstring'",
"'@#$%^&*()string'",
Expand All @@ -78,8 +76,6 @@ public void testStringLiteralWithSingleQuote() {
"``string",
"\"string\"",
"\"\"string",
"\"string\"",
"'string",
"'string",
"\\nstring",
"\\rstring",
Expand Down Expand Up @@ -126,9 +122,7 @@ public void testStringLiteralWithDoubleQuote() {
"\"``string\"",
"\"'string'\"",
"\"''string\"",
"\"\\'string\\'\"",
"\"\"\"string\"",
"\"\\\"string\"",
"\"\\nstring\"",
"\"\\rstring\"",
"\"@#$%^&*()string\"",
Expand All @@ -140,8 +134,6 @@ public void testStringLiteralWithDoubleQuote() {
"``string",
"'string'",
"''string",
"'string'",
"\"string",
"\"string",
"\\nstring",
"\\rstring",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,7 @@ public long parseDateFormat(String timestampStr, long currentTime) throws SQLPar
private String parseStringLiteral(String src) {
if (2 <= src.length()) {
// do not unescape string
String unWrappedString =
src.substring(1, src.length() - 1).replace("\\\"", "\"").replace("\\'", "'");
String unWrappedString = src.substring(1, src.length() - 1);
if (src.charAt(0) == '\"' && src.charAt(src.length() - 1) == '\"') {
// replace "" with "
String replaced = unWrappedString.replace("\"\"", "\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3290,8 +3290,7 @@ private boolean hasDecidedQueryType() {
private String parseStringLiteral(String src) {
if (2 <= src.length()) {
// do not unescape string
String unWrappedString =
src.substring(1, src.length() - 1).replace("\\\"", "\"").replace("\\'", "'");
String unWrappedString = src.substring(1, src.length() - 1);
if (src.charAt(0) == '\"' && src.charAt(src.length() - 1) == '\"') {
// replace "" with "
String replaced = unWrappedString.replace("\"\"", "\"");
Expand Down

0 comments on commit 349f860

Please sign in to comment.