maropu commented on a change in pull request #23880: [SPARK-26976][SQL] Forbid 
reserved keywords as table identifiers when ANSI mode is on
URL: https://github.com/apache/spark/pull/23880#discussion_r259705484
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
 ##########
 @@ -1404,6 +1404,15 @@ class AstBuilder(conf: SQLConf) extends 
SqlBaseBaseVisitor[AnyRef] with Logging
     }
   }
 
+  override def visitIdentifier(ctx: IdentifierContext): String = 
withOrigin(ctx) {
+    val keyword = ctx.getText
+    if (ctx.ansiReserved() != null) {
+      throw new ParseException(
+        s"'$keyword' is reserved and you cannot use this keyword as an 
identifier.", ctx)
 
 Review comment:
   Oh, I see. But, if we just remove the rule, the two cases throw a parse 
error since both colunm name rule and function name rule use `identifier`;
   ```
   scala> sql("SET spark.sql.parser.ansi.enabled=false")
   scala> sql("SELECT CURRENT_TIMESTAMP").show
   +--------------------+                                                       
   
   | current_timestamp()|
   +--------------------+
   |2019-02-25 16:26:...|
   +--------------------+
   
   scala> sql("SELECT CURRENT_TIMESTAMP()").show
   +--------------------+                                                       
   
   | current_timestamp()|
   +--------------------+
   |2019-02-25 16:26:...|
   +--------------------+
   
   scala> sql("SET spark.sql.parser.ansi.enabled=true")
   scala> sql("SELECT CURRENT_TIMESTAMP").show
   org.apache.spark.sql.catalyst.parser.ParseException:
   no viable alternative at input 'CURRENT_TIMESTAMP'(line 1, pos 7)
   
   == SQL ==
   SELECT CURRENT_TIMESTAMP
   -------^^^
   
   scala> sql("SELECT CURRENT_TIMESTAMP()").show
   rg.apache.spark.sql.catalyst.parser.ParseException:
   no viable alternative at input 'CURRENT_TIMESTAMP'(line 1, pos 7)
   
   == SQL ==
   SELECT CURRENT_TIMESTAMP()
   -------^^^
   ```
   To be honest, I'm not 100% sure about what the ANSI reserved means.
   For example, both postgresql and mysql reserve `current_timestamp`, but the 
behviour is different;
   ```
   postgres=# SELECT CURRENT_TIMESTAMP;
                 now              
   -------------------------------
    2019-02-25 16:44:26.320698+09
   (1 row)
   
   postgres=# SELECT CURRENT_TIMESTAMP();
   ERROR:  syntax error at or near ")"
   LINE 1: SELECT CURRENT_TIMESTAMP();
                                    ^
   mysql> SELECT CURRENT_TIMESTAMP;
   +---------------------+
   | CURRENT_TIMESTAMP   |
   +---------------------+
   | 2019-02-25 16:45:02 |
   +---------------------+
   
   mysql> SELECT CURRENT_TIMESTAMP();
   +---------------------+
   | CURRENT_TIMESTAMP() |
   +---------------------+
   | 2019-02-25 16:45:04 |
   +---------------------+
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to