Skip to content

Commit 3de55d6

Browse files
committed
Another approach
1 parent 5bb76b4 commit 3de55d6

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
140140
}
141141
matched(ordinal)
142142

143+
// Try to resolve literal functions first (for DefaultValueExpression)
144+
case u @ UnresolvedLiteralFunction(nameParts) => withPosition(u) {
145+
LiteralFunctionResolution.resolve(nameParts).getOrElse(u)
146+
}
147+
143148
case u @ UnresolvedAttribute(nameParts) =>
144149
val result = withPosition(u) {
145150
resolveColumnByName(nameParts)
146-
.orElse(LiteralFunctionResolution.resolve(nameParts))
147151
.map {
148152
// We trim unnecessary alias here. Note that, we cannot trim the alias at top-level,
149153
// as we should resolve `UnresolvedAttribute` to a named expression. The caller side
@@ -175,9 +179,6 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
175179
u.copy(child = newChild)
176180
}
177181

178-
case d @ DefaultValueExpression(c: Expression, _, _) =>
179-
d.copy(child = resolveLiteralColumns(c))
180-
181182
case _ => e.mapChildren(innerResolve(_, isTopLevel = false))
182183
}
183184
resolved.copyTagsFrom(e)
@@ -198,13 +199,6 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
198199
}
199200
}
200201

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-
208202
// Resolves `UnresolvedAttribute` to `OuterReference`.
209203
protected def resolveOuterRef(e: Expression): Expression = {
210204
val outerPlan = AnalysisContext.get.outerPlan

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ object UnresolvedTVFAliases {
270270
}
271271
}
272272

273+
/**
274+
* Holds the name of a literal function that has yet to be resolved.
275+
* Similar to UnresolvedAttribute but specifically for literal functions.
276+
*/
277+
case class UnresolvedLiteralFunction(nameParts: Seq[String]) extends Expression with Unevaluable {
278+
279+
def name: String =
280+
nameParts.map(n => if (n.contains(".")) s"`$n`" else n).mkString(".")
281+
282+
override def dataType: DataType = throw new UnresolvedException("dataType")
283+
override def nullable: Boolean = throw new UnresolvedException("nullable")
284+
override lazy val resolved = false
285+
override def children: Seq[Expression] = Nil
286+
287+
final override val nodePatterns: Seq[TreePattern] = Seq(UNRESOLVED_LITERAL_FUNCTION)
288+
289+
override def toString: String = s"'$name"
290+
291+
override def sql: String = nameParts.map(quoteIfNeeded).mkString(".")
292+
293+
override protected def withNewChildrenInternal(
294+
newChildren: IndexedSeq[Expression]): Expression = this
295+
}
296+
273297
/**
274298
* Holds the name of an attribute that has yet to be resolved.
275299
*/

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,9 +2932,9 @@ class AstBuilder extends DataTypeAstBuilder
29322932
CurrentUser()
29332933
}
29342934
} else {
2935-
// If the parser is not in ansi mode, we should return `UnresolvedAttribute`, in case there
2936-
// are columns named `CURRENT_DATE` or `CURRENT_TIMESTAMP` or `CURRENT_TIME`
2937-
UnresolvedAttribute.quoted(ctx.name.getText)
2935+
// If the parser is not in ansi mode, we should return `UnresolvedLiteralFunction`,
2936+
// in case there are columns named `CURRENT_DATE` or `CURRENT_TIMESTAMP` or `CURRENT_TIME`
2937+
UnresolvedLiteralFunction(Seq(ctx.name.getText))
29382938
}
29392939
}
29402940

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreePatterns.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ object TreePattern extends Enumeration {
109109
val UNRESOLVED_DF_STAR: Value = Value
110110
val UNRESOLVED_FUNCTION: Value = Value
111111
val UNRESOLVED_IDENTIFIER: Value = Value
112+
val UNRESOLVED_LITERAL_FUNCTION: Value = Value
112113
val UNRESOLVED_ORDINAL: Value = Value
113114
val UNRESOLVED_PLAN_ID: Value = Value
114115
val UNRESOLVED_WINDOW_EXPRESSION: Value = Value

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3917,7 +3917,7 @@ class DataSourceV2SQLSuiteV1Filter
39173917
checkError(
39183918
exception = intercept[AnalysisException] {
39193919
sql(s"""CREATE table $t (
3920-
c1 STRING,
3920+
c1 timestamp,
39213921
current_timestamp TIMESTAMP DEFAULT c1)""")
39223922
},
39233923
condition = "INVALID_DEFAULT_VALUE.NOT_CONSTANT",

0 commit comments

Comments
 (0)