[SPARK-27117][SQL] current_date/current_timestamp should not refer to columns with ansi parser mode
## What changes were proposed in this pull request? This PR is a followup of https://github.com/apache/spark/pull/19559 . It revisits https://issues.apache.org/jira/browse/SPARK-27117 , which should be an invalid use case according to the SQL standard. `current_date/current_timestamp` are reserved keywords, if users want to access columns named `current_date/current_timestamp`, they should quote the name like ```select `current_date` from tbl``` If ansi mode is not enabled(which is the default), this PR won't introduce any changes. ## How was this patch tested? a new test case Closes #24039 from cloud-fan/current_datetime. Authored-by: Wenchen Fan <wenchen@databricks.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
This commit is contained in:
parent
1b9fd67904
commit
8114b63d56
|
@ -603,7 +603,8 @@ valueExpression
|
|||
;
|
||||
|
||||
primaryExpression
|
||||
: CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
|
||||
: name=(CURRENT_DATE | CURRENT_TIMESTAMP) #currentDatetime
|
||||
| CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
|
||||
| CASE value=expression whenClause+ (ELSE elseExpression=expression)? END #simpleCase
|
||||
| CAST '(' expression AS dataType ')' #cast
|
||||
| STRUCT '(' (argument+=namedExpression (',' argument+=namedExpression)*)? ')' #struct
|
||||
|
|
|
@ -1212,6 +1212,21 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
|
|||
}
|
||||
}
|
||||
|
||||
override def visitCurrentDatetime(ctx: CurrentDatetimeContext): Expression = withOrigin(ctx) {
|
||||
if (conf.ansiParserEnabled) {
|
||||
ctx.name.getType match {
|
||||
case SqlBaseParser.CURRENT_DATE =>
|
||||
CurrentDate()
|
||||
case SqlBaseParser.CURRENT_TIMESTAMP =>
|
||||
CurrentTimestamp()
|
||||
}
|
||||
} else {
|
||||
// If the parser is not in ansi mode, we should return `UnresolvedAttribute`, in case there
|
||||
// are columns named `CURRENT_DATE` or `CURRENT_TIMESTAMP`.
|
||||
UnresolvedAttribute.quoted(ctx.name.getText)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a [[Cast]] expression.
|
||||
*/
|
||||
|
|
|
@ -133,7 +133,7 @@ class CatalystSqlParser(conf: SQLConf) extends AbstractSqlParser {
|
|||
|
||||
/** For test-only. */
|
||||
object CatalystSqlParser extends AbstractSqlParser {
|
||||
val astBuilder = new AstBuilder(new SQLConf())
|
||||
val astBuilder = new AstBuilder(SQLConf.get)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -722,4 +722,16 @@ class ExpressionParserSuite extends PlanTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
test("current date/timestamp braceless expressions") {
|
||||
withSQLConf(SQLConf.ANSI_SQL_PARSER.key -> "true") {
|
||||
assertEqual("current_date", CurrentDate())
|
||||
assertEqual("current_timestamp", CurrentTimestamp())
|
||||
}
|
||||
|
||||
withSQLConf(SQLConf.ANSI_SQL_PARSER.key -> "false") {
|
||||
assertEqual("current_date", UnresolvedAttribute.quoted("current_date"))
|
||||
assertEqual("current_timestamp", UnresolvedAttribute.quoted("current_timestamp"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue