szehon-ho commented on code in PR #58632: URL: https://github.com/apache/spark/pull/58632#discussion_r4190433218
########## sql/core/src/test/scala/org/apache/spark/sql/connector/catalog/SchemaAlignmentConfigSuite.scala: ########## @@ -0,0 +1,311 @@ +/* + * 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.connector.catalog + +import scala.util.{Failure, Try} + +import org.apache.spark.SparkConf +import org.apache.spark.SparkThrowable +import org.apache.spark.sql.{AnalysisException, DataFrame, QueryTest, Row} +import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.internal.SQLConf.StoreAssignmentPolicy +import org.apache.spark.sql.internal.connector.SchemaAlignmentConfig +import org.apache.spark.sql.internal.connector.SchemaAlignmentConfig.AnsiStoreAssignmentCastCheck +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StructType} + +/** + * A catalog that creates [[InMemoryRowLevelOperationTable]]s carrying the fixed + * [[SchemaAlignmentConfig]] supplied by the concrete subclass. + */ +abstract class SchemaAlignmentTestCatalog extends InMemoryRowLevelOperationTableCatalog { + + protected def tableConfig: SchemaAlignmentConfig + + override def createTable(ident: Identifier, tableInfo: TableInfo): Table = { + if (tables.containsKey(ident)) { + throw new TableAlreadyExistsException(ident.asMultipartIdentifier) + } + val name = s"${this.name}.${ident.quoted}" + val schema = CatalogV2Util.v2ColumnsToStructType(tableInfo.columns) + val table = new InMemoryRowLevelOperationTable( + name, schema, tableInfo.partitions, tableInfo.properties, tableInfo.constraints(), + schemaAlignmentConfig = tableConfig) + tables.put(ident, table) + namespaces.putIfAbsent(ident.namespace.toList, Map()) + table + } +} + +/** A catalog whose tables defer the ANSI store-assignment cast check to runtime. */ +class RelaxedSchemaAlignmentCatalog extends SchemaAlignmentTestCatalog { + override protected def tableConfig: SchemaAlignmentConfig = new SchemaAlignmentConfig { + override def ansiStoreAssignmentCastCheck(): AnsiStoreAssignmentCastCheck = + AnsiStoreAssignmentCastCheck.RUNTIME + } +} + +/** A catalog whose tables keep the strict data source v2 defaults. */ +class StrictSchemaAlignmentCatalog extends SchemaAlignmentTestCatalog { + override protected def tableConfig: SchemaAlignmentConfig = SchemaAlignmentConfig.DEFAULT +} + +/** + * End-to-end coverage for [[SchemaAlignmentConfig]]: a table that opts into a relaxation gets the + * more permissive analyzer behavior, while an otherwise identical table using the default (strict) + * config keeps the data source v2 behavior. Exercised on both the INSERT path + * ([[org.apache.spark.sql.catalyst.analysis.Analyzer.ResolveOutputRelation]]) and the row-level + * path ([[org.apache.spark.sql.catalyst.analysis.ResolveRowLevelCommandAssignments]]). + */ +class SchemaAlignmentConfigSuite extends QueryTest with SharedSparkSession { + + private val relaxed = "relaxed" + private val strict = "strict" + + override def sparkConf: SparkConf = + super.sparkConf + .set(s"spark.sql.catalog.$relaxed", classOf[RelaxedSchemaAlignmentCatalog].getName) + .set(s"spark.sql.catalog.$strict", classOf[StrictSchemaAlignmentCatalog].getName) + + private def withAnsiPolicy(f: => Unit): Unit = + withSQLConf( + SQLConf.STORE_ASSIGNMENT_POLICY.key -> StoreAssignmentPolicy.ANSI.toString)(f) + + test("RUNTIME: INSERT of an ANSI-incompatible cast") { + withTable(s"$relaxed.t", s"$strict.t") { + sql(s"CREATE TABLE $relaxed.t (id INT) USING foo") + sql(s"CREATE TABLE $strict.t (id INT) USING foo") + withAnsiPolicy { + sql(s"INSERT INTO $relaxed.t VALUES ('1')") Review Comment: Non-blocking: please add tests with non-foldable malformed strings and out-of-range numeric strings assigned to INT, asserting the specific Spark errors during execution. Cover INSERT and UPDATE/MERGE, including `spark.sql.ansi.enabled=false`, to verify that the inserted casts still enforce ANSI behavior. Null-bearing inputs assigned to NOT NULL columns would also cover the null checks. The current tests cover valid literals and analysis-time rejection. Repeating the [earlier suggestion](https://github.com/apache/spark/pull/58632#discussion_r4169924741) here because that discussion is outdated. -- 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]
