LuciferYang commented on code in PR #11353: URL: https://github.com/apache/paimon/pull/11353#discussion_r4667546321
########## paimon-spark/paimon-spark-4.2/src/main/scala/org/apache/spark/sql/catalyst/parser/extensions/Spark42RewriteCreateTableLike.scala: ########## @@ -0,0 +1,73 @@ +/* + * 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.parser.extensions + +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.analysis.{UnresolvedIdentifier, UnresolvedTableOrView} +import org.apache.spark.sql.catalyst.plans.logical.{CreateTableLike, LogicalPlan} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.errors.QueryCompilationErrors +import org.apache.spark.sql.internal.HiveSerDe + +/** + * Spark 4.2 parses CREATE TABLE LIKE into the v2 [[CreateTableLike]] plan instead of + * `CreateTableLikeCommand`, so [[RewriteCreateTableLikeCommand]] never sees it. This rule hands the + * same fields to [[RewriteCreateTableLikeCommand.rewrite]]. + */ +case class Spark42RewriteCreateTableLike(spark: SparkSession) extends Rule[LogicalPlan] { + + private lazy val rewriter = RewriteCreateTableLikeCommand(spark) + + override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp { + case c @ CreateTableLike( + UnresolvedIdentifier(target, _), + source: UnresolvedTableOrView, + location, + provider, + serdeInfo, + properties, + ifNotExists) => + rewriter + .rewrite( + toV1Parts(target), + toV1Parts(source.multipartIdentifier), Review Comment: Fixed. `RewriteCreateTableLikeCommand` resolves the catalog with `CatalogAndIdentifier` first. It joins the middle parts into one namespace part only when the name has more than three parts and resolves to `SparkCatalog` or `SparkGenericCatalog`; any other catalog gets the namespace parts as written. `Spark42RewriteCreateTableLike` passes the parsed parts through, and `toV1Parts` is removed. Spark 3.4 to 4.1 had the same problem in Paimon's grammar fallback, which built a v1 `TableIdentifier`. It now passes the parts as written to the same rewrite. Your case is a test in the shared `CatalogQualifiedCreateTableLikeTest`, with Spark's `InMemoryTableCatalog` registered as `external`; without the fix it copies the `wrong` column on 3.5, 4.1 and 4.2. -- 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]
