srielau commented on code in PR #35982: URL: https://github.com/apache/spark/pull/35982#discussion_r1067327791
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveDefaultColumns.scala: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.analysis + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.TableIdentifier +import org.apache.spark.sql.catalyst.catalog.{SessionCatalog, UnresolvedCatalogRelation} +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns._ +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types._ + +/** + * This is a rule to process DEFAULT columns in statements such as CREATE/REPLACE TABLE. + * + * Background: CREATE TABLE and ALTER TABLE invocations support setting column default values for + * later operations. Following INSERT, and INSERT MERGE commands may then reference the value + * using the DEFAULT keyword as needed. + * + * Example: + * CREATE TABLE T(a INT DEFAULT 4, b INT NOT NULL DEFAULT 5); + * INSERT INTO T VALUES (1, 2); + * INSERT INTO T VALUES (1, DEFAULT); + * INSERT INTO T VALUES (DEFAULT, 6); + * SELECT * FROM T; + * (1, 2) + * (1, 5) + * (4, 6) + * + * @param analyzer analyzer to use for processing DEFAULT values stored as text. + * @param catalog the catalog to use for looking up the schema of INSERT INTO table objects. + */ +case class ResolveDefaultColumns( + analyzer: Analyzer, + catalog: SessionCatalog) extends Rule[LogicalPlan] { + + // This field stores the enclosing INSERT INTO command, once we find one. + var enclosingInsert: Option[InsertIntoStatement] = None + + override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning( + (_ => SQLConf.get.enableDefaultColumns), ruleId) { + case i@InsertIntoStatement(_, _, _, _, _, _) + if i.query.collectFirst { case u: UnresolvedInlineTable => u }.isDefined => + enclosingInsert = Some(i) + i + + case table: UnresolvedInlineTable + if enclosingInsert.isDefined && + table.rows.nonEmpty && table.rows.forall(_.size == table.rows(0).size) => + val expanded: UnresolvedInlineTable = addMissingDefaultColumnValues(table).getOrElse(table) + replaceExplicitDefaultColumnValues(analyzer, expanded).getOrElse(table) + + case i@InsertIntoStatement(_, _, _, project: Project, _, _) => + enclosingInsert = Some(i) + val expanded: Project = addMissingDefaultColumnValues(project).getOrElse(project) + val replaced: Option[LogicalPlan] = replaceExplicitDefaultColumnValues(analyzer, expanded) + if (replaced.isDefined) i.copy(query = replaced.get) else i + } + + // Helper method to check if an expression is an explicit DEFAULT column reference. + private def isExplicitDefaultColumn(expr: Expression): Boolean = expr match { + case u: UnresolvedAttribute if u.name.equalsIgnoreCase(CURRENT_DEFAULT_COLUMN_NAME) => true Review Comment: 1. column in FROM 2. DEFAULT keyword 3. LCA If nothing else fear of regression dictates that order -- 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]
