szehon-ho commented on code in PR #60368:
URL: https://github.com/apache/spark/pull/60368#discussion_r4568832923


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/GeneratedColumn.scala:
##########
@@ -132,29 +144,47 @@ object GeneratedColumn {
   }
 
   /**
-   * Returns `relation`'s output with every generated column's generation 
expression recorded in
-   * the attribute's metadata, which is where [[TableOutputResolver]] looks 
when it auto-fills the
-   * generated columns a write does not provide a value for.
-   *
-   * Generation expressions are internal metadata: they should neither surface 
in a DataFrame's
-   * schema nor propagate into tables created from it. A table's V2 columns 
are the persisted form
-   * and the source of truth, so the write path copies the expression into 
plan attribute metadata
-   * only for as long as resolving the write takes.
-   *
-   * The output is returned unchanged if the table does not ask Spark to 
handle generated columns.
+   * Reads a generation expression for both auto-fill and validation. Convert 
the modeled expression
+   * once, falling back to SQL text when it is absent or cannot be converted, 
as defaults and CHECK
+   * constraints do. Conversion is deferred so presence checks do not read 
either form.
    */
-  def attachGenerationExpressions(relation: DataSourceV2Relation): 
Seq[AttributeReference] = {
-    if (!supportsGeneratedColumnsOnWrite(relation.table)) {
-      return relation.output
+  private[sql] class ExpressionReader(
+      relation: DataSourceV2Relation,
+      colName: String,
+      genExpr: GenerationExpression) {
+    private lazy val modeledExpression = Option(genExpr.getExpression).flatMap 
{ expression =>
+      SQLConf.withExistingConf(StoredExpressionText.fixedConf(SQLConf.get)) {
+        V2ExpressionUtils.toCatalyst(expression)
+      }
     }
-    val genExprs = relation.table.columns()
-      .flatMap(col => Option(col.generationExpression()).map(col.name -> _))
-      .toMap
-    relation.output.map { attr =>
-      genExprs.get(attr.name) match {
-        case Some(genExpr) =>
-          withMetadataEntry(attr, GENERATION_EXPRESSION_METADATA_KEY, genExpr)
-        case None => attr
+
+    def toCatalyst(readSql: String => Expression = 
StoredExpressionText.parse): Expression = {
+      modeledExpression.orElse(Option(genExpr.getSql).map(readSql))

Review Comment:
   Thanks, I reproduced both examples. I'd like to retain the modeled 
expression's folded constants in this PR.
   
   Spark's [generated-column 
validation](https://github.com/apache/spark/blob/743d05e10835e632a0dca43bb557da67fae40832/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/GeneratedColumnExpression.scala#L57)
 requires:
   
   > The expression must be deterministic
   
   For this case, `CREATE TABLE` with ANSI off has already captured 
`COALESCE(a, NULL)`, or `COALESCE(a, -2147483648)`, in the modeled definition. 
Using that definition preserves the creation-time result when a later writer 
changes its ANSI setting, and gives auto-fill and explicit-value validation the 
same behavior. I think these cases should continue to work.
   
   Determinism alone does not capture the SQL configuration. We need a stronger 
way to capture creation-time settings comprehensively, and I'd defer that 
design to a separate discussion. Here I'd keep the semantics already encoded in 
the model. I've added local documentation and regression coverage for the 
folded values under both settings of the legacy flag; the docs also clarify 
that this does not capture every creation-time setting.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to