From 3d59f24403b76d80e917ee18eb35eb7fa0deecde Mon Sep 17 00:00:00 2001 From: Mingyu Chen Date: Mon, 1 Jan 2024 17:20:45 +0800 Subject: [PATCH] [fix](compatibility) add some keywords (#29251) 1. Add `BINARY` keywords `BINARY` is used for tell MySQL to treat a String(or String column) as case sensitive. eg: `"abc" = "ABC"` is true, but `BINARY "abc" = "ABC"` is false. But in Doris, `"abc" = "ABC"` is false by default. I add this `BINARY` keyword just for compatibility, it will take no effect. 2. Add `PARTITIONS` and `AUTO_INCREMENT` as reserved words. `PARTITIONS` is the table name in `information_schema` database. `AUTO_INCREMENT` is a column name of a table in `information_schema` --- .../antlr4/org/apache/doris/nereids/DorisLexer.g4 | 1 + .../org/apache/doris/nereids/DorisParser.g4 | 6 ++++-- fe/fe-core/src/main/cup/sql_parser.cup | 4 ++++ .../doris/nereids/parser/NereidsParserTest.java | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 index c7e823ac65..a1eea97161 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 @@ -121,6 +121,7 @@ BEGIN: 'BEGIN'; BETWEEN: 'BETWEEN'; BIGINT: 'BIGINT'; BIN: 'BIN'; +BINARY: 'BINARY'; BINLOG: 'BINLOG'; BITAND: 'BITAND'; BITMAP: 'BITMAP'; diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 880986a6ce..a91f207e47 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -678,7 +678,7 @@ primaryExpression | LEFT_PAREN query RIGHT_PAREN #subqueryExpression | ATSIGN identifierOrText #userVariable | DOUBLEATSIGN (kind=(GLOBAL | SESSION) DOT)? identifier #systemVariable - | identifier #columnReference + | BINARY? identifier #columnReference | base=primaryExpression DOT fieldName=identifier #dereference | LEFT_PAREN expression RIGHT_PAREN #parenthesizedExpression | KEY (dbName=identifier DOT)? keyName=identifier #encryptKey @@ -760,7 +760,7 @@ constant | type=(DATE | DATEV1 | DATEV2 | TIMESTAMP) STRING_LITERAL #typeConstructor | number #numericLiteral | booleanValue #booleanLiteral - | STRING_LITERAL #stringLiteral + | BINARY? STRING_LITERAL #stringLiteral | LEFT_BRACKET (items+=constant)? (COMMA items+=constant)* RIGHT_BRACKET #arrayLiteral | LEFT_BRACE (items+=constant COLON items+=constant)? (COMMA items+=constant COLON items+=constant)* RIGHT_BRACE #mapLiteral @@ -898,6 +898,7 @@ nonReserved | ARRAY | AT | AUTHORS + | AUTO_INCREMENT | BACKENDS | BACKUP | BEGIN @@ -1067,6 +1068,7 @@ nonReserved | PASSWORD_HISTORY | PASSWORD_LOCK_TIME | PASSWORD_REUSE + | PARTITIONS | PATH | PAUSE | PERCENT diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index 26b221c128..976ad030df 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -7557,6 +7557,8 @@ keyword ::= {: RESULT = id; :} | KW_ARRAY:id {: RESULT = id; :} + | KW_AUTO_INCREMENT:id + {: RESULT = id; :} | KW_BACKUP:id {: RESULT = id; :} | KW_BEGIN:id @@ -7783,6 +7785,8 @@ keyword ::= {: RESULT = id; :} | KW_PARAMETER:id {: RESULT = id; :} + | KW_PARTITIONS:id + {: RESULT = id; :} | KW_PASSWORD:id {: RESULT = id; :} | KW_PASSWORD_EXPIRE:id diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java index dc7e336433..6dbcacee40 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java @@ -457,4 +457,19 @@ public class NereidsParserTest extends ParserTestBase { NereidsParser nereidsParser = new NereidsParser(); nereidsParser.parseSingle(sql); } + + @Test + public void testParseBinaryKeyword() { + String sql = "SELECT BINARY 'abc' FROM t"; + NereidsParser nereidsParser = new NereidsParser(); + nereidsParser.parseSingle(sql); + } + + @Test + public void testParseReserveKeyword() { + // partitions and auto_increment are reserve keywords + String sql = "SELECT BINARY 'abc' FROM information_schema.partitions order by AUTO_INCREMENT"; + NereidsParser nereidsParser = new NereidsParser(); + nereidsParser.parseSingle(sql); + } }