gengliangwang commented on a change in pull request #35855: URL: https://github.com/apache/spark/pull/35855#discussion_r827997906
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DefaultColumns.scala ########## @@ -0,0 +1,287 @@ +/* + * 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._ +import org.apache.spark.sql.catalyst.expressions.{Expression, _} +import org.apache.spark.sql.catalyst.optimizer.ConstantFolding +import org.apache.spark.sql.catalyst.parser.{CatalystSqlParser, ParseException} +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.types._ + +/** + * This class contains logic for processing DEFAULT columns in statements such as CREATE TABLE. + */ +object DefaultColumns { + val default = "default" + val analysisPrefix = + " has a DEFAULT value which fails to resolve to a valid constant expression: " + val columnDefaultNotFound = "Column 'default' does not exist" + lazy val parser = new CatalystSqlParser() + lazy val analyzer = + new Analyzer(new SessionCatalog(new InMemoryCatalog, FunctionRegistry.builtin)) + + /** + * Finds DEFAULT expressions in CREATE/REPLACE TABLE commands and constant-folds then. + * + * Example: + * CREATE TABLE T(a INT, b INT DEFAULT 5 + 5) becomes + * CREATE TABLE T(a INT, b INT DEFAULT 10) + * + * @param tableSchema represents the names and types of the columns of the statement to process. + * @param statementType name of the statement being processed, such as INSERT; useful for errors. + * @return a copy of `tableSchema` with field metadata updated with the constant-folded values. + */ + def ConstantFoldDefaultExpressions(tableSchema: StructType, statementType: String): StructType = { Review comment: ~~This method is actually duplicated with the optimizer rule `ConstantFolding`. What if we keep the foldable expression in metadata and leave it to `ConstantFolding`?~~ After second thought, we can have better error message if the default value is not foldable or type compatible. -- 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]
