naveenp2708 commented on code in PR #57644: URL: https://github.com/apache/spark/pull/57644#discussion_r3799147033
########## sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/AutoCdcReservedColumnMaterializationSuite.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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.pipelines.graph + +import java.util.Locale + +import org.apache.spark.sql.Row +import org.apache.spark.sql.execution.streaming.runtime.MemoryStream +import org.apache.spark.sql.functions +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.pipelines.autocdc.{AutoCdcReservedNames, Scd1BatchProcessor} +import org.apache.spark.sql.pipelines.utils.{ExecutionTest, TestGraphRegistrationContext} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType} + +/** + * Materialization-level tests for AUTO CDC's engine-owned reserved metadata column (SPARK-58118). + * + * The reserved `__spark_autocdc_metadata` column is engine-owned: a user-declared schema may omit + * it, and materialization appends the engine-owned shape so the created table matches what the + * AUTO CDC MERGE writes at runtime. Reserved-column matching goes through the flow's effective + * case sensitivity -- a pipeline-level `SET spark.sql.caseSensitive` can differ from the session -- + * so these tests inspect the created table's schema rather than only validation. + */ +class AutoCdcReservedColumnMaterializationSuite + extends ExecutionTest + with SharedSparkSession + with AutoCdcGraphExecutionTestMixin { + + test("materialization appends the engine-owned reserved metadata column when the user " + + "schema omits it") { + val session = spark + import session.implicits._ + + // The user declares only the logical data columns and omits the engine-owned reserved + // metadata column. Materialization must append it so the created target has exactly what the + // AUTO CDC MERGE writes; otherwise the MERGE fails with an unresolved metadata column. + val declaredSchema = new StructType() + .add("id", IntegerType, nullable = false) + .add("name", StringType) + .add("version", LongType, nullable = false) + + val stream = MemoryStream[(Int, String, Long)] + stream.addData((1, "alice", 5L)) + val ctx = new TestGraphRegistrationContext(spark) { + registerTable( + "target", + catalog = Some(catalog), + database = Some(namespace), + specifiedSchema = Some(declaredSchema)) + registerFlow(autoCdcFlow( + name = "auto_cdc_flow", + target = "target", + query = dfFlowFunc(stream.toDF().toDF("id", "name", "version")), + keys = Seq("id"), + sequencing = functions.col("version"))) + } + runPipeline(ctx) + + val targetSchema = spark.table(s"$catalog.$namespace.target").schema + assert( + targetSchema.fieldNames.contains(AutoCdcReservedNames.cdcMetadataColName), + "target should carry the engine-owned reserved metadata column, got " + + targetSchema.fieldNames.mkString(", ")) + checkAnswer( + spark.table(s"$catalog.$namespace.target"), + Seq(Row(1, "alice", 5L, cdcMeta(None, Some(5L)))) + ) + } + + test("materialization matches the reserved metadata column through the flow's effective case " + + "sensitivity, not the session's") { + // The session is case-sensitive, but the flow sets spark.sql.caseSensitive=false, so the Review Comment: Added it. Session case-insensitive, flow case-sensitive. The user's __SPARK_AUTOCDC_METADATA stays distinct from the engine's __spark_autocdc_metadata, so both survive and the engine only writes the lower-case one. The user's column just stays null. Also checked it fails without the fix. One thing worth flagging: the table ends up with two columns that differ only in case, so you have to read it back under case-sensitive analysis. Reading it case-insensitively throws AMBIGUOUS_REFERENCE, which is expected, so the test does the read that way. -- 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]
