Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-37133][SQL] Add a config to optionally enforce ANSI reserved keywords #34403

Closed
wants to merge 4 commits into from
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
4 changes: 3 additions & 1 deletion docs/sql-ref-ansi-compliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ When the ANSI mode is disabled, Spark SQL has two kinds of keywords:
* Non-reserved keywords: Same definition as the one when the ANSI mode enabled.
* Strict-non-reserved keywords: A strict version of non-reserved keywords, which can not be used as table alias.

By default `spark.sql.ansi.enabled` is false.
If you want to still use reserved keywords as identifiers with ANSI mode, you can set `spark.sql.ansi.enforceReservedKeywords` to false.

By default `spark.sql.ansi.enabled` is false and `spark.sql.ansi.enforceReservedKeywords` is true.

Below is a list of all the keywords in Spark SQL.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
}

override def visitCurrentLike(ctx: CurrentLikeContext): Expression = withOrigin(ctx) {
if (conf.ansiEnabled) {
if (conf.enforceReservedKeywords) {
ctx.name.getType match {
case SqlBaseParser.CURRENT_DATE =>
CurrentDate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ abstract class AbstractSqlParser extends ParserInterface with SQLConfHelper with
parser.addErrorListener(ParseErrorListener)
parser.legacy_setops_precedence_enabled = conf.setOpsPrecedenceEnforced
parser.legacy_exponent_literal_as_decimal_enabled = conf.exponentLiteralAsDecimalEnabled
parser.SQL_standard_keyword_behavior = conf.ansiEnabled
parser.SQL_standard_keyword_behavior = conf.enforceReservedKeywords

try {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,14 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val ENFORCE_RESERVED_KEYWORDS = buildConf("spark.sql.ansi.enforceReservedKeywords")
.doc(s"When true and '${ANSI_ENABLED.key}' is true, the Spark SQL parser enforces the ANSI " +
"reserved keywords and forbids SQL queries that use reserved keywords as alias names " +
"and/or identifiers for table, view, function, etc.")
.version("3.3.0")
.booleanConf
.createWithDefault(true)

val SORT_BEFORE_REPARTITION =
buildConf("spark.sql.execution.sortBeforeRepartition")
.internal()
Expand Down Expand Up @@ -4041,6 +4049,8 @@ class SQLConf extends Serializable with Logging {

def ansiEnabled: Boolean = getConf(ANSI_ENABLED)

def enforceReservedKeywords: Boolean = ansiEnabled && getConf(ENFORCE_RESERVED_KEYWORDS)

def timestampType: AtomicType = getConf(TIMESTAMP_TYPE) match {
case "TIMESTAMP_LTZ" =>
// For historical reason, the TimestampType maps to TIMESTAMP WITH LOCAL TIME ZONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,10 +933,20 @@ class ExpressionParserSuite extends AnalysisTest {
assertEqual("current_timestamp", CurrentTimestamp())
}

withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
def testNonAnsiBehavior(): Unit = {
assertEqual("current_date", UnresolvedAttribute.quoted("current_date"))
assertEqual("current_timestamp", UnresolvedAttribute.quoted("current_timestamp"))
}
withSQLConf(
SQLConf.ANSI_ENABLED.key -> "false",
SQLConf.ENFORCE_RESERVED_KEYWORDS.key -> "true") {
testNonAnsiBehavior()
}
withSQLConf(
SQLConf.ANSI_ENABLED.key -> "true",
SQLConf.ENFORCE_RESERVED_KEYWORDS.key -> "false") {
testNonAnsiBehavior()
}
}

test("SPARK-36736: (NOT) ILIKE (ANY | SOME | ALL) expressions") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@ class TableIdentifierParserSuite extends SQLKeywordUtils {
assert(TableIdentifier(keyword, Option("db")) === parseTableIdentifier(s"db.$keyword"))
}
}

withSQLConf(
SQLConf.ANSI_ENABLED.key -> "true",
SQLConf.ENFORCE_RESERVED_KEYWORDS.key -> "false") {
reservedKeywordsInAnsiMode.foreach { keyword =>
assert(TableIdentifier(keyword) === parseTableIdentifier(s"$keyword"))
assert(TableIdentifier(keyword, Option("db")) === parseTableIdentifier(s"db.$keyword"))
}
}
}

test("table identifier - strict keywords") {
Expand Down