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 @@ -2030,6 +2030,14 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
return false
}

/**
* Returns whether the pattern is a regex expression (instead of a normal
* string). Normal string is a string with all alphabets/digits and "_".
*/
private def isRegex(pattern: String): Boolean = {
pattern.exists(p => !Character.isLetterOrDigit(p) && p != '_')
}

/**
* Create a dereference expression. The return type depends on the type of the parent.
* If the parent is an [[UnresolvedAttribute]], it can be a [[UnresolvedAttribute]] or
Expand All @@ -2042,7 +2050,8 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
case unresolved_attr @ UnresolvedAttribute(nameParts) =>
ctx.fieldName.getStart.getText match {
case escapedIdentifier(columnNameRegex)
if conf.supportQuotedRegexColumnName && canApplyRegex(ctx) =>
if conf.supportQuotedRegexColumnName &&
isRegex(columnNameRegex) && canApplyRegex(ctx) =>
UnresolvedRegex(columnNameRegex, Some(unresolved_attr.name),
conf.caseSensitiveAnalysis)
case _ =>
Expand All @@ -2060,7 +2069,8 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
override def visitColumnReference(ctx: ColumnReferenceContext): Expression = withOrigin(ctx) {
ctx.getStart.getText match {
case escapedIdentifier(columnNameRegex)
if conf.supportQuotedRegexColumnName && canApplyRegex(ctx) =>
if conf.supportQuotedRegexColumnName &&
isRegex(columnNameRegex) && canApplyRegex(ctx) =>
UnresolvedRegex(columnNameRegex, None, conf.caseSensitiveAnalysis)
case _ =>
UnresolvedAttribute.quoted(ctx.getText)
Expand Down
13 changes: 13 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4259,6 +4259,19 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
sql("select * from test_temp_view"),
Row(1, 2, 3, 1, 2, 3, 1, 1))
}

test("SPARK-38173: Quoted column cannot be recognized correctly " +
"when quotedRegexColumnNames is true") {
withSQLConf(SQLConf.SUPPORT_QUOTED_REGEX_COLUMN_NAME.key -> "true") {
checkAnswer(
sql(
"""
|SELECT `(C3)?+.+`,T.`C1` * `C2` AS CC
|FROM (SELECT 3 AS C1,2 AS C2,1 AS C3) T
|""".stripMargin),
Row(3, 2, 6) :: Nil)
}
}
}

case class Foo(bar: Option[String])