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,44 @@
/*
* 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.execution

import org.apache.paimon.spark.SparkTable
import org.apache.paimon.spark.catalog.SparkBaseCatalog

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.analysis.{ResolvedPartitionSpec, ResolvedTable}
import org.apache.spark.sql.catalyst.plans.logical.{DescribeTablePartition, LogicalPlan}
import org.apache.spark.sql.execution.{PaimonDescribeTableExec, SparkPlan, SparkStrategy}

/**
* Plans Spark 4.2's `DescribeTablePartition` on a Paimon table with [[PaimonDescribeTableExec]],
* which `PaimonStrategy` uses for DESCRIBE ... PARTITION on earlier Spark versions.
*/
case class Spark42DescribePartitionStrategy(spark: SparkSession) extends SparkStrategy {

override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
case DescribeTablePartition(
r @ ResolvedTable(catalog: SparkBaseCatalog, _, table: SparkTable, _),
spec: ResolvedPartitionSpec,
isExtended,
output) =>
PaimonDescribeTableExec(output, catalog, r.identifier, table, Some(spec), isExtended) :: Nil
case _ => Nil
}
}
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 DescribeTableTest extends DescribeTableTestBase {}
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 Spark42UnreportedPartitionStatisticsTest extends UnreportedPartitionStatisticsTest
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,16 @@ trait PaimonPartitionManagement extends SupportsAtomicPartitionManagement with L
override def loadPartitionMetadata(ident: InternalRow): JMap[String, String] = {
table match {
case fileStoreTable: FileStoreTable =>
// The table's own scan, so a read's fallback branch, time travel and query auth apply.
// Reading only the partition columns keeps query auth from asking for every column.
// Level 0 is listed as in CatalogUtils.listPartitionsFromFileSystem, except by the branch
// scans of a fallback table.
val partitionEntry = fileStoreTable
.newSnapshotReader()
.withMode(ScanMode.ALL)
.newScan()
.withReadType(partitionRowType)
.withLevelFilter(_ => true)
.withPartitionFilter(toPartitionPredicate(ident))
.partitionEntries()
.listPartitionEntries()
.asScala
.headOption

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ package org.apache.paimon.spark.catalyst.analysis

import org.apache.paimon.spark.catalyst.Compatibility

import org.apache.spark.sql.PaimonUtils
import org.apache.spark.sql.PaimonUtils.{normalizePartitionSpec, requireExactMatchedPartitionSpec}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{PartitionSpec, ResolvedPartitionSpec, UnresolvedPartitionSpec}
import org.apache.spark.sql.catalyst.analysis.ResolvePartitionSpec.conf
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog}
import org.apache.spark.sql.connector.catalog.{Identifier, SupportsPartitionManagement, TableCatalog}
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._
import org.apache.spark.sql.types.{StringType, StructField, StructType}

Expand All @@ -46,6 +47,21 @@ object PaimonResolvePartitionSpec {
}
}

/**
* Resolves a spec naming every partition column of `table` as Spark resolves the spec of a V2
* partition command, with Spark's conversion of each value to its column type.
*/
def resolveFullSpec(
table: SupportsPartitionManagement,
spec: TablePartitionSpec): ResolvedPartitionSpec = {
val partSchema = table.partitionSchema()
val normalizedSpec = normalizePartitionSpec(spec, partSchema, table.name(), conf.resolver)
requireExactMatchedPartitionSpec(table.name(), normalizedSpec, partSchema.fieldNames)
ResolvedPartitionSpec(
partSchema.fieldNames.toSeq,
PaimonUtils.convertToPartIdent(normalizedSpec, partSchema.fields.toSeq))
}

private def resolvePartitionSpec(
tableName: String,
partSpec: UnresolvedPartitionSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.apache.paimon.partition.PartitionPredicate.splitPartitionPredicatesAn
import org.apache.paimon.predicate.{Predicate, PredicateBuilder}
import org.apache.paimon.spark.{PaimonBaseScan, PaimonRecordReaderIterator, PaimonScan, PostponeMergeInputScan, PostponeMergeOnRead, SparkCatalog, SparkGenericCatalog, SparkTable, SparkUtils}
import org.apache.paimon.spark.catalog.{SparkBaseCatalog, SupportView}
import org.apache.paimon.spark.catalyst.analysis.ResolvedPaimonView
import org.apache.paimon.spark.catalyst.analysis.{PaimonResolvePartitionSpec, ResolvedPaimonView}
import org.apache.paimon.spark.catalyst.optimizer.PushDownMapSelectedKeys
import org.apache.paimon.spark.catalyst.optimizer.RepartitionLateralVectorSearchInput
import org.apache.paimon.spark.catalyst.plans.logical.{CopyIntoLocationCommand, CopyIntoLocationSource, CopyIntoTableCommand, CreateOrReplaceTagCommand, CreatePaimonView, DeleteTagCommand, DropPaimonView, LateralVectorSearch, PaimonCallCommand, PaimonDropPartitions, PaimonTableValuedFunctions, RenameTagCommand, ResolvedIdentifier, ShowPaimonViews, ShowTagsCommand, TruncatePaimonTableWithFilter}
Expand Down Expand Up @@ -206,16 +206,19 @@ case class PaimonStrategy(spark: SparkSession)
case r: ResolvedTable
if r.table.isInstanceOf[SparkTable] &&
r.catalog.isInstanceOf[SparkBaseCatalog] =>
// Spark 4.2 moved partition descriptions to a separate logical command.
val table = r.table.asInstanceOf[SparkTable]
// Before Spark 4.2 the partition spec is a field of DescribeRelation. Spark 4.2 plans
// DESCRIBE ... PARTITION as DescribeTablePartition, see Spark42DescribePartitionStrategy.
val partitionSpec = d.getClass.getMethods
.find(_.getName == "partitionSpec")
.map(_.invoke(d).asInstanceOf[Map[String, String]])
.getOrElse(Map.empty)
.filter(_.nonEmpty)
.map(spec => PaimonResolvePartitionSpec.resolveFullSpec(table, spec))
PaimonDescribeTableExec(
d.output,
r.catalog.asInstanceOf[SparkBaseCatalog],
r.identifier,
r.table.asInstanceOf[SparkTable],
table,
partitionSpec,
d.isExtended) :: Nil
case _ => Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.apache.paimon.spark.execution.adaptive.DisablePostponeCarrierShuffleC
import org.apache.spark.sql.SparkSessionExtensions
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.execution.SparkStrategy
import org.apache.spark.sql.paimon.shims.SparkShimLoader

/** Spark session extension to extends the syntax and adds the rules. */
Expand Down Expand Up @@ -129,6 +130,14 @@ class PaimonSparkSessionExtensions extends (SparkSessionExtensions => Unit) {

// planner extensions
extensions.injectPlannerStrategy(spark => PaimonStrategy(spark))
if (org.apache.spark.SPARK_VERSION >= "4.2") {
// DescribeTablePartition exists only in Spark 4.2 and later.
val strategy = Class
.forName("org.apache.paimon.spark.execution.Spark42DescribePartitionStrategy")
.getConstructor(classOf[org.apache.spark.sql.SparkSession])
extensions.injectPlannerStrategy(
spark => strategy.newInstance(spark).asInstanceOf[SparkStrategy])
}
// old compatible
extensions.injectPlannerStrategy(spark => OldCompatibleStrategy(spark))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ object PartitionStatisticsDisplay {

/** Returns the numeric value, or [[UNKNOWN]] when it is absent, nonnumeric, or negative. */
def render(parameters: collection.Map[String, String], field: String): String =
parameters
.get(field)
.flatMap(asLong)
.filter(count => PartitionStatistics.isKnown(count))
.map(_.toString)
.getOrElse(UNKNOWN)
known(parameters, field).map(_.toString).getOrElse(UNKNOWN)

/** Returns the numeric value, or None when it is absent, nonnumeric, or negative. */
def known(parameters: collection.Map[String, String], field: String): Option[Long] =
parameters.get(field).flatMap(asLong).filter(count => PartitionStatistics.isKnown(count))

private def asLong(value: String): Option[Long] = Try(value.trim.toLong).toOption
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ package org.apache.spark.sql

import org.apache.spark.executor.OutputMetrics
import org.apache.spark.rdd.InputFileBlockHolder
import org.apache.spark.sql.catalyst.analysis.Resolver
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{ResolvePartitionSpec, Resolver}
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, Expression}
import org.apache.spark.sql.catalyst.parser.ParserInterface
Expand All @@ -33,7 +34,7 @@ import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Strategy.transl
import org.apache.spark.sql.internal.connector.PredicateUtils
import org.apache.spark.sql.paimon.shims.SparkShimLoader
import org.apache.spark.sql.sources.Filter
import org.apache.spark.sql.types.{DataType, StructType}
import org.apache.spark.sql.types.{DataType, StructField, StructType}
import org.apache.spark.sql.util.PartitioningUtils
import org.apache.spark.util.{Utils => SparkUtils}

Expand Down Expand Up @@ -159,6 +160,11 @@ object PaimonUtils {
PartitioningUtils.requireExactMatchedPartitionSpec(tableName, spec, partitionColumnNames)
}

/** Spark's conversion of a normalized partition spec to the partition values of `schema`. */
def convertToPartIdent(spec: TablePartitionSpec, schema: Seq[StructField]): InternalRow = {
ResolvePartitionSpec.convertToPartIdent(spec, schema)
}

def sameType(left: DataType, right: DataType): Boolean = {
left.sameType(right)
}
Expand Down
Loading
Loading