Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Double quoted as string literal instead of identifier #974

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public static String unquoteText(String text) {
}

/**
* Unquote Identifier which has " or ` as mark.
* Unquote Identifier which has ` as mark.
* @param identifier identifier that possibly enclosed by double quotes or back ticks
* @return An unquoted string whose outer pair of (double/back-tick) quotes have been
* removed
*/
public static String unquoteIdentifier(String identifier) {
if (isQuoted(identifier, "\"") || isQuoted(identifier, "`")) {
if (isQuoted(identifier, "`")) {
return identifier.substring(1, identifier.length() - 1);
} else {
return identifier;
Expand Down
1 change: 1 addition & 0 deletions docs/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"sql_cli": [
"user/dql/expressions.rst",
"user/general/datatypes.rst",
"user/general/identifiers.rst",
"user/general/values.rst",
"user/dql/basics.rst",
Expand Down
2 changes: 1 addition & 1 deletion docs/experiment/ppl/general/datatypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,5 @@ Conversion from TIMESTAMP
String Data Types
=================

TODO
A string is a sequence of characters enclosed in either single or double quotes. For example, both 'text' and "text" will be treated as string literal.

11 changes: 10 additions & 1 deletion docs/user/general/datatypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,16 @@ Conversion from TIMESTAMP
String Data Types
=================

TODO
A string is a sequence of characters enclosed in either single or double quotes. For example, both 'text' and "text" will be treated as string literal. To use quote characters in a string literal, you can include double quotes within single quoted string or single quotes within double quoted string::

od> SELECT 'hello', "world", '"hello"', "'world'"
fetched rows / total rows = 1/1
+-----------+-----------+-------------+-------------+
| 'hello' | "world" | '"hello"' | "'world'" |
|-----------+-----------+-------------+-------------|
| hello | world | "hello" | 'world' |
+-----------+-----------+-------------+-------------+




Expand Down
2 changes: 1 addition & 1 deletion docs/user/general/identifiers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Delimited Identifiers
Description
-----------

A delimited identifier is an identifier enclosed in back ticks ````` or double quotation marks ``"``. In this case, the identifier enclosed is not necessarily a regular identifier. In other words, it can contain any special character not allowed by regular identifier.
A delimited identifier is an identifier enclosed in back ticks `````. In this case, the identifier enclosed is not necessarily a regular identifier. In other words, it can contain any special character not allowed by regular identifier.

Please note the difference between single quote and double quotes in SQL syntax. Single quote is used to enclose a string literal while double quotes have same purpose as back ticks to escape special characters in an identifier.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void testSpecialIndexNames() throws IOException {
public void testQuotedIndexNames() throws IOException {
createIndexWithOneDoc("logs+2020+01", "logs.2020.01");
queryAndAssertTheDoc("SELECT * FROM `logs+2020+01`");
queryAndAssertTheDoc("SELECT * FROM \"logs.2020.01\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT `Cancelled`, `AvgTicketPrice` FROM kibana_sample_data_flights
SELECT ABS(DistanceMiles), (FlightDelayMin * 2) - 3 FROM kibana_sample_data_flights
SELECT abs(DistanceMiles), Abs(FlightDelayMin) FROM kibana_sample_data_flights
SELECT Cancelled AS Cancel, AvgTicketPrice AS ATP FROM kibana_sample_data_flights
SELECT Cancelled AS `Cancel`, AvgTicketPrice AS "ATP" FROM kibana_sample_data_flights
SELECT Cancelled AS `Cancel` FROM kibana_sample_data_flights
SELECT kibana_sample_data_flights.AvgTicketPrice FROM kibana_sample_data_flights
SELECT flights.AvgTicketPrice, Carrier FROM kibana_sample_data_flights flights
SELECT flights.AvgTicketPrice, Carrier FROM kibana_sample_data_flights AS flights
Expand Down
1 change: 0 additions & 1 deletion sql/src/main/antlr/OpenDistroSQLIdentifierParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ qualifiedName

ident
: DOT? ID
| DOUBLE_QUOTE_ID
| BACKTICK_QUOTE_ID
| keywordsCanBeId
;
Expand Down
1 change: 1 addition & 0 deletions sql/src/main/antlr/OpenDistroSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ decimalLiteral

stringLiteral
: STRING_LITERAL
| DOUBLE_QUOTE_ID
;

booleanLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void canParseSelectFieldWithAlias() {

@Test
public void canParseSelectFieldWithQuotedAlias() {
assertNotNull(parser.parse("SELECT name AS \"n\", age AS `a` FROM accounts"));
assertNotNull(parser.parse("SELECT name AS `n` FROM accounts"));
}

@Test
Expand All @@ -76,7 +76,6 @@ public void canNotParseIndexNameWithSpecialChar() {
@Test
public void canParseIndexNameWithSpecialCharQuoted() {
assertNotNull(parser.parse("SELECT * FROM `hello+world`"));
assertNotNull(parser.parse("SELECT * FROM \"hello$world\""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public void can_build_select_literals() {
values(emptyList()),
alias("123", intLiteral(123)),
alias("'hello'", stringLiteral("hello")),
alias("\"world\"", stringLiteral("world")),
alias("false", booleanLiteral(false)),
alias("-4.567", doubleLiteral(-4.567))
),
buildAST("SELECT 123, 'hello', false, -4.567")
buildAST("SELECT 123, 'hello', \"world\", false, -4.567")
);
}

Expand Down Expand Up @@ -137,19 +138,13 @@ public void can_build_select_fields_with_alias_quoted() {
assertEquals(
project(
relation("test"),
alias(
"name",
qualifiedName("name"),
"first name"
),
alias(
"(age + 10)",
function("+", qualifiedName("age"), intLiteral(10)),
"Age_Expr"
)
),
buildAST("SELECT"
+ " name AS \"first name\", "
+ " (age + 10) AS `Age_Expr` "
+ "FROM test"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public void canBuildStringLiteral() {
stringLiteral("hello"),
buildExprAst("'hello'")
);
assertEquals(
stringLiteral("hello"),
buildExprAst("\"hello\"")
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void canBuildRegularIdentifierForElasticsearch() {

@Test
public void canBuildDelimitedIdentifier() {
buildFromIdentifier("\"hello$world\"").expectQualifiedName("hello$world");
buildFromIdentifier("`logs.2020.01`").expectQualifiedName("logs.2020.01");
}

Expand Down