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 @@ -167,12 +167,17 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
}
}

case u @ UnresolvedExtractValue(child, fieldName) =>
case u @ UnresolvedExtractValue(child, field) =>
val newChild = innerResolve(child, isTopLevel = false)
val resolvedField = if (conf.getConf(SQLConf.PREFER_COLUMN_OVER_LCA_IN_ARRAY_INDEX)) {
innerResolve(field, isTopLevel = false)
} else {
field
}
if (newChild.resolved) {
ExtractValue(newChild, fieldName, resolver)
ExtractValue(child = newChild, extraction = resolvedField, resolver = resolver)
} else {
u.copy(child = newChild)
u.copy(child = newChild, extraction = resolvedField)
}

case _ => e.mapChildren(innerResolve(_, isTopLevel = false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ object SQLConf {
}
}

val PREFER_COLUMN_OVER_LCA_IN_ARRAY_INDEX =
buildConf("spark.sql.analyzer.preferColumnOverLcaInArrayIndex")
.internal()
.doc(
"When true, prefer the column from the underlying relation over the lateral column alias " +
"reference with the same name (see SPARK-53734)."
)
.booleanConf
.createWithDefault(true)

val DONT_DEDUPLICATE_EXPRESSION_IF_EXPR_ID_IN_OUTPUT =
buildConf("spark.sql.analyzer.dontDeduplicateExpressionIfExprIdInOutput")
.internal()
Expand Down
11 changes: 11 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 @@ -5079,6 +5079,17 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark

checkAnswer(df, Row(1))
}

test("SPARK-53734: Prefer table column over LCA when resolving array index") {
val query = "SELECT 1 AS col1, col2[col1] FROM VALUES(0, ARRAY(1, 2));"
withSQLConf(SQLConf.PREFER_COLUMN_OVER_LCA_IN_ARRAY_INDEX.key -> "true") {
checkAnswer(sql(query), Row(1, 1))
}

withSQLConf(SQLConf.PREFER_COLUMN_OVER_LCA_IN_ARRAY_INDEX.key -> "false") {
checkAnswer(sql(query), Row(1, 2))
}
}
}

case class Foo(bar: Option[String])