Skip to content
Closed
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Copy link
Member

@maropu maropu Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change the current behaviour of select current_date/select current_timestamp in the non-ansi mode? If so, we need to update the migration guide:

// the current behaivour in the non-ansi mode
scala> sql("SET spark.sql.parser.ansi.enabled=false")
scala> sql("SELECT CURRENT_TIMESTAMP").show
+--------------------+                                                          
| current_timestamp()|
+--------------------+
|2019-02-25 16:26:...|
+--------------------+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this behavior change only makes sense in ansi mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I missed. You're right.

/**
* Create a [[Cast]] expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
}