Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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(
target,
source.multipartIdentifier,
provider,
// Spark builds the same storage format for its v1 fallback.
HiveSerDe.buildStorageFormat(
location,
serdeInfo,
QueryCompilationErrors.invalidFileFormatForStoredAsError),
properties,
ifNotExists
)
.getOrElse(c)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.paimon.spark.sql

class DDLTest extends DDLTestBase {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.paimon.spark.sql

class DDLWithHiveCatalogTest extends DDLWithHiveCatalogTestBase {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.paimon.spark.sql

/** Runs the shared suite from `paimon-spark-ut`, whose own suites this module does not run. */
class Spark42CatalogQualifiedCreateTableLikeTest extends CatalogQualifiedCreateTableLikeTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.paimon.spark.catalyst.plans.logical

import org.apache.paimon.spark.leafnode.PaimonLeafParsedStatement

import org.apache.spark.sql.execution.command.{CreateTableLikeCommand => SparkCreateTableLikeCommand}

/**
* A CREATE TABLE LIKE statement parsed by Paimon's grammar, with the target and source names as
* written. `command` carries the other clauses; its table names are placeholders.
*/
case class PaimonCreateTableLikeStatement(
targetParts: Seq[String],
sourceParts: Seq[String],
command: SparkCreateTableLikeCommand)
extends PaimonLeafParsedStatement
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,38 @@ abstract class AbstractPaimonSparkSqlExtensionsParser(val delegate: ParserInterf
MarkHiveDynamicPartitionWrite,
RewritePaimonViewCommands(sparkSession),
RewritePaimonFunctionCommands(sparkSession),
SparkShimLoader.shim.rewritePaimonSQLFunctionCommands(sparkSession),
SparkShimLoader.shim.rewritePaimonSQLFunctionCommands(sparkSession)
) ++ spark42CreateTableLikeRule.map(_.newInstance(sparkSession)) ++ Seq(
RewriteCreateTableLikeCommand(sparkSession),
RewriteSparkDDLCommands(sparkSession)
)
}

/**
* Loaded by name because the class is compiled only for Spark 4.2 and later. Without it on the
* classpath CREATE TABLE LIKE is left to Spark.
*/
private lazy val spark42CreateTableLikeRule
: Option[java.lang.reflect.Constructor[Rule[LogicalPlan]]] = {
if (org.apache.spark.SPARK_VERSION >= "4.2") {
val className =
"org.apache.spark.sql.catalyst.parser.extensions.Spark42RewriteCreateTableLike"
try {
Some(
Class
.forName(className)
.getConstructor(classOf[SparkSession])
.asInstanceOf[java.lang.reflect.Constructor[Rule[LogicalPlan]]])
} catch {
case _: ClassNotFoundException =>
logWarning(s"$className is not on the classpath; CREATE TABLE LIKE is left to Spark.")
None
}
} else {
None
}
}

/** Parses a string to an Expression. */
def parseExpression(sqlText: String): Expression =
delegate.parseExpression(sqlText)
Expand Down Expand Up @@ -184,10 +210,10 @@ abstract class AbstractPaimonSparkSqlExtensionsParser(val delegate: ParserInterf
/**
* Cheap token-level check for `CREATE TABLE [IF NOT EXISTS] x.y[.z] LIKE ...` shape. Used as a
* gate for the Paimon parser fallback when the delegate parser rejects a catalog-qualified CREATE
* TABLE LIKE statement.
* TABLE LIKE statement. Spark 4.2 parses such names itself, into a plan the fallback cannot use.
*/
private def maybeCatalogCreateTableLike(sqlText: String): Boolean = {
if (org.apache.spark.SPARK_VERSION < "3.4") {
if (org.apache.spark.SPARK_VERSION < "3.4" || org.apache.spark.SPARK_VERSION >= "4.2") {
return false
}
if (!startsWithCreateTable(sqlText)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.antlr.v4.runtime._
import org.antlr.v4.runtime.misc.Interval
import org.antlr.v4.runtime.tree.{ParseTree, TerminalNode}
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.parser.ParserInterface
import org.apache.spark.sql.catalyst.parser.extensions.PaimonParserUtils.withOrigin
Expand Down Expand Up @@ -105,9 +104,10 @@ class PaimonSqlExtensionsAstBuilder(delegate: ParserInterface)

/** Create a CREATE TABLE LIKE logical command. */
override def visitCreateTableLike(ctx: CreateTableLikeContext): LogicalPlan = withOrigin(ctx) {
sparkCreateTableLikeCommand(ctx).copy(
targetTable = toTableIdentifier(typedVisit[Seq[String]](ctx.target)),
sourceTable = toTableIdentifier(typedVisit[Seq[String]](ctx.source)))
PaimonCreateTableLikeStatement(
typedVisit[Seq[String]](ctx.target),
typedVisit[Seq[String]](ctx.source),
sparkCreateTableLikeCommand(ctx))
}

/** Create a CREATE OR REPLACE TAG logical command. */
Expand Down Expand Up @@ -311,20 +311,6 @@ class PaimonSqlExtensionsAstBuilder(delegate: ParserInterface)

private def toSeq[T](list: java.util.List[T]) = toBuffer(list)

private def toTableIdentifier(identifier: Seq[String]): TableIdentifier = {
identifier match {
case Seq(table) =>
TableIdentifier(table)
case Seq(database, table) =>
TableIdentifier(table, Some(database))
case parts =>
TableIdentifier(
parts.last,
Some(parts.slice(1, parts.length - 1).mkString(".")),
Some(parts.head))
}
}

private def sparkCreateTableLikeCommand(
ctx: CreateTableLikeContext): SparkCreateTableLikeCommand = {
delegate.parsePlan(createSparkCreateTableLikeSql(ctx)) match {
Expand Down
Loading
Loading