Skip to content

Commit 477aeee

Browse files
committed
Restore prettier exception for non-deterministic case
1 parent 141285b commit 477aeee

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,13 +1022,25 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString
10221022
}
10231023
}
10241024

1025+
def checkExistsDefaultDeterministic(colsToAdd: Seq[QualifiedColType]): Unit = {
1026+
colsToAdd.foreach { col =>
1027+
col.default.foreach { d =>
1028+
if (!d.deterministic) {
1029+
throw QueryCompilationErrors.defaultValueNotConstantError(
1030+
"ALTER TABLE", col.colName, d.originalSQL)
1031+
}
1032+
}
1033+
}
1034+
}
1035+
10251036
alter match {
10261037
case AddColumns(table: ResolvedTable, colsToAdd) =>
10271038
colsToAdd.foreach { colToAdd =>
10281039
checkColumnNotExists("add", colToAdd.name, table.schema)
10291040
}
10301041
checkColumnNameDuplication(colsToAdd)
10311042
checkNoCollationsInMapKeys(colsToAdd)
1043+
checkExistsDefaultDeterministic(colsToAdd)
10321044

10331045
case ReplaceColumns(_: ResolvedTable, colsToAdd) =>
10341046
checkColumnNameDuplication(colsToAdd)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,10 +3560,11 @@ class DataSourceV2SQLSuiteV1Filter
35603560
val exception = analysisException(
35613561
// Rand function is not foldable
35623562
s"ALTER TABLE tab ADD COLUMN col2 DOUBLE DEFAULT rand()")
3563-
assert(exception.getSqlState == "42K0E")
3564-
assert(exception.errorClass.get == "INVALID_NON_DETERMINISTIC_EXPRESSIONS")
3565-
assert(exception.messageParameters("sqlExprs") ==
3566-
"\"qualifiedcoltype(defaultvalueexpression(rand()))\"")
3563+
assert(exception.getSqlState == "42623")
3564+
assert(exception.errorClass.get == "INVALID_DEFAULT_VALUE.NOT_CONSTANT")
3565+
assert(exception.messageParameters("colName") == "`col2`")
3566+
assert(exception.messageParameters("defaultValue") == "rand()")
3567+
assert(exception.messageParameters("statement") == "ALTER TABLE")
35673568
}
35683569
foldableExpressions.foreach(expr => {
35693570
withTable("tab") {

0 commit comments

Comments
 (0)