aokolnychyi commented on code in PR #50593: URL: https://github.com/apache/spark/pull/50593#discussion_r2047989560
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/ColumnDefinition.scala: ########## @@ -194,17 +194,32 @@ object ColumnDefinition { /** * A fake expression to hold the column/variable default value expression and its original SQL text. */ -case class DefaultValueExpression(child: Expression, originalSQL: String) +case class DefaultValueExpression( + child: Expression, + originalSQL: String, + exposeCurrentDefaultAsExprV2: Boolean) extends UnaryExpression with Unevaluable { override def dataType: DataType = child.dataType + override def stringArgs: Iterator[Any] = Iterator(child, originalSQL) override protected def withNewChildInternal(newChild: Expression): Expression = copy(child = newChild) // Convert the default expression to ColumnDefaultValue, which is required by DS v2 APIs. def toV2(statement: String, colName: String): ColumnDefaultValue = child match { case Literal(value, dataType) => - new ColumnDefaultValue(originalSQL, LiteralValue(value, dataType)) + val literalV2 = LiteralValue(value, dataType) + val exprV2 = if (exposeCurrentDefaultAsExprV2) literalV2 else null + new ColumnDefaultValue(originalSQL, exprV2, literalV2) case _ => throw QueryCompilationErrors.defaultValueNotConstantError(statement, colName, originalSQL) } } + +object DefaultValueExpression { + def apply(expr: Expression, originalSQL: String): DefaultValueExpression = { + // only expose V2 expressions as current defaults if all Catalyst expressions are evaluable + // Catalyst expressions like CURRENT_USER or CURRENT_DATE will require special handling + val exposeCurrentDefaultAsExprV2 = !expr.exists(_.isInstanceOf[FoldableUnevaluable]) Review Comment: I believe `Unevaluable` extends `FoldableUnevaluable`, which is very confusing. The idea is that we can optimize and squash all expressions as long as they are not like `CURRENT_USER()`, `CURRENT_CATALOG()`, `NOW`, etc. This is intentional as we don't want to pass a complicated expression to connectors. Take a look at the example from tests: ``` sql( s""" |CREATE TABLE $tableName ( | id INT, | salary INT DEFAULT (100 + 23), | dep STRING DEFAULT ('h' || 'r'), | active BOOLEAN DEFAULT CAST(1 AS BOOLEAN) |) USING foo |""".stripMargin) ``` It is fine to fold these expressions and pass the result literal to the connector as a write default. If we pass a simplified version of the expression, there is a higher chance the connector will support that expression. The following can't be safely squashed into a literal, however: ``` sql(s"CREATE TABLE $tableName (id INT, cat STRING DEFAULT current_catalog()) USING foo") ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org