Skip to content

Commit 5bb76b4

Browse files
committed
Fix another case where special column is in the function
1 parent ad8f55e commit 5bb76b4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,8 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
175175
u.copy(child = newChild)
176176
}
177177

178-
case d @ DefaultValueExpression(u: UnresolvedAttribute, _, _) =>
179-
d.copy(child = LiteralFunctionResolution.resolve(u.nameParts)
180-
.map {
181-
case Alias(child, _) if !isTopLevel => child
182-
case other => other
183-
}
184-
.getOrElse(u))
178+
case d @ DefaultValueExpression(c: Expression, _, _) =>
179+
d.copy(child = resolveLiteralColumns(c))
185180

186181
case _ => e.mapChildren(innerResolve(_, isTopLevel = false))
187182
}
@@ -203,6 +198,13 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
203198
}
204199
}
205200

201+
private def resolveLiteralColumns(e: Expression) = {
202+
e.transformWithPruning(_.containsPattern(UNRESOLVED_ATTRIBUTE)) {
203+
case u @ UnresolvedAttribute(nameParts) =>
204+
LiteralFunctionResolution.resolve(nameParts).getOrElse(u)
205+
}
206+
}
207+
206208
// Resolves `UnresolvedAttribute` to `OuterReference`.
207209
protected def resolveOuterRef(e: Expression): Expression = {
208210
val outerPlan = AnalysisContext.get.outerPlan

sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3898,6 +3898,19 @@ class DataSourceV2SQLSuiteV1Filter
38983898
}
38993899
}
39003900

3901+
test("test default value special column name nested in function") {
3902+
val t = "testcat.ns.t"
3903+
withTable("t") {
3904+
sql(s"""CREATE table $t (
3905+
c1 STRING,
3906+
current_date DATE DEFAULT DATE_ADD(current_date, 7))""")
3907+
sql(s"INSERT INTO $t (c1) VALUES ('a')")
3908+
val result = sql(s"SELECT * FROM $t").collect()
3909+
assert(result.length == 1)
3910+
assert(result(0).getString(0) == "a")
3911+
}
3912+
}
3913+
39013914
test("test default value should not refer to real column") {
39023915
val t = "testcat.ns.t"
39033916
withTable("t") {
@@ -3907,7 +3920,7 @@ class DataSourceV2SQLSuiteV1Filter
39073920
c1 STRING,
39083921
current_timestamp TIMESTAMP DEFAULT c1)""")
39093922
},
3910-
condition = "INVALID_DEFAULT_VALUE.UNRESOLVED_EXPRESSION",
3923+
condition = "INVALID_DEFAULT_VALUE.NOT_CONSTANT",
39113924
parameters = Map(
39123925
"statement" -> "CREATE TABLE",
39133926
"colName" -> "`current_timestamp`",

0 commit comments

Comments
 (0)