|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.spark |
| 20 | + |
| 21 | +import org.apache.paimon.CoreOptions |
| 22 | +import org.apache.paimon.CoreOptions.BucketFunctionType |
| 23 | +import org.apache.paimon.options.Options |
| 24 | +import org.apache.paimon.spark.catalog.functions.BucketFunction |
| 25 | +import org.apache.paimon.spark.schema.PaimonMetadataColumn |
| 26 | +import org.apache.paimon.spark.util.OptionUtils |
| 27 | +import org.apache.paimon.spark.write.{PaimonV2WriteBuilder, PaimonWriteBuilder} |
| 28 | +import org.apache.paimon.table.{Table, _} |
| 29 | +import org.apache.paimon.table.BucketMode.{BUCKET_UNAWARE, HASH_FIXED, POSTPONE_MODE} |
| 30 | +import org.apache.paimon.utils.StringUtils |
| 31 | + |
| 32 | +import org.apache.spark.sql.connector.catalog._ |
| 33 | +import org.apache.spark.sql.connector.expressions.{Expressions, Transform} |
| 34 | +import org.apache.spark.sql.connector.read.ScanBuilder |
| 35 | +import org.apache.spark.sql.connector.write.{LogicalWriteInfo, WriteBuilder} |
| 36 | +import org.apache.spark.sql.types.StructType |
| 37 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap |
| 38 | + |
| 39 | +import java.util.{Collections, EnumSet => JEnumSet, HashMap => JHashMap, Map => JMap, Set => JSet} |
| 40 | + |
| 41 | +import scala.collection.JavaConverters._ |
| 42 | +import scala.collection.mutable.ArrayBuffer |
| 43 | + |
| 44 | +abstract class PaimonSparkTableBase(val table: Table) |
| 45 | + extends org.apache.spark.sql.connector.catalog.Table |
| 46 | + with SupportsRead |
| 47 | + with SupportsWrite |
| 48 | + with SupportsMetadataColumns |
| 49 | + with PaimonPartitionManagement { |
| 50 | + |
| 51 | + lazy val coreOptions = new CoreOptions(table.options()) |
| 52 | + |
| 53 | + private lazy val useV2Write: Boolean = { |
| 54 | + val v2WriteConfigured = OptionUtils.useV2Write() |
| 55 | + v2WriteConfigured && supportsV2Write |
| 56 | + } |
| 57 | + |
| 58 | + private def supportsV2Write: Boolean = { |
| 59 | + coreOptions.bucketFunctionType() == BucketFunctionType.DEFAULT && { |
| 60 | + table match { |
| 61 | + case storeTable: FileStoreTable => |
| 62 | + storeTable.bucketMode() match { |
| 63 | + case HASH_FIXED => BucketFunction.supportsTable(storeTable) |
| 64 | + case BUCKET_UNAWARE | POSTPONE_MODE => true |
| 65 | + case _ => false |
| 66 | + } |
| 67 | + |
| 68 | + case _ => false |
| 69 | + } |
| 70 | + } && coreOptions.clusteringColumns().isEmpty |
| 71 | + } |
| 72 | + |
| 73 | + def getTable: Table = table |
| 74 | + |
| 75 | + override def name: String = table.fullName |
| 76 | + |
| 77 | + override lazy val schema: StructType = SparkTypeUtils.fromPaimonRowType(table.rowType) |
| 78 | + |
| 79 | + override def partitioning: Array[Transform] = { |
| 80 | + table.partitionKeys().asScala.map(p => Expressions.identity(StringUtils.quote(p))).toArray |
| 81 | + } |
| 82 | + |
| 83 | + override def properties: JMap[String, String] = { |
| 84 | + table match { |
| 85 | + case dataTable: DataTable => |
| 86 | + val properties = new JHashMap[String, String](dataTable.coreOptions.toMap) |
| 87 | + if (!table.primaryKeys.isEmpty) { |
| 88 | + properties.put(CoreOptions.PRIMARY_KEY.key, String.join(",", table.primaryKeys)) |
| 89 | + } |
| 90 | + properties.put(TableCatalog.PROP_PROVIDER, SparkSource.NAME) |
| 91 | + if (table.comment.isPresent) { |
| 92 | + properties.put(TableCatalog.PROP_COMMENT, table.comment.get) |
| 93 | + } |
| 94 | + if (properties.containsKey(CoreOptions.PATH.key())) { |
| 95 | + properties.put(TableCatalog.PROP_LOCATION, properties.get(CoreOptions.PATH.key())) |
| 96 | + } |
| 97 | + properties |
| 98 | + case _ => Collections.emptyMap() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + override def capabilities: JSet[TableCapability] = { |
| 103 | + val capabilities = JEnumSet.of( |
| 104 | + TableCapability.BATCH_READ, |
| 105 | + TableCapability.OVERWRITE_BY_FILTER, |
| 106 | + TableCapability.MICRO_BATCH_READ |
| 107 | + ) |
| 108 | + |
| 109 | + if (useV2Write) { |
| 110 | + capabilities.add(TableCapability.ACCEPT_ANY_SCHEMA) |
| 111 | + capabilities.add(TableCapability.BATCH_WRITE) |
| 112 | + capabilities.add(TableCapability.OVERWRITE_DYNAMIC) |
| 113 | + } else { |
| 114 | + capabilities.add(TableCapability.ACCEPT_ANY_SCHEMA) |
| 115 | + capabilities.add(TableCapability.V1_BATCH_WRITE) |
| 116 | + } |
| 117 | + |
| 118 | + capabilities |
| 119 | + } |
| 120 | + |
| 121 | + override def metadataColumns: Array[MetadataColumn] = { |
| 122 | + val partitionType = SparkTypeUtils.toSparkPartitionType(table) |
| 123 | + |
| 124 | + val _metadataColumns = ArrayBuffer[MetadataColumn]() |
| 125 | + |
| 126 | + if (coreOptions.rowTrackingEnabled()) { |
| 127 | + _metadataColumns.append(PaimonMetadataColumn.ROW_ID) |
| 128 | + _metadataColumns.append(PaimonMetadataColumn.SEQUENCE_NUMBER) |
| 129 | + } |
| 130 | + |
| 131 | + _metadataColumns.appendAll( |
| 132 | + Seq( |
| 133 | + PaimonMetadataColumn.FILE_PATH, |
| 134 | + PaimonMetadataColumn.ROW_INDEX, |
| 135 | + PaimonMetadataColumn.PARTITION(partitionType), |
| 136 | + PaimonMetadataColumn.BUCKET |
| 137 | + )) |
| 138 | + |
| 139 | + _metadataColumns.toArray |
| 140 | + } |
| 141 | + |
| 142 | + override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder = { |
| 143 | + table match { |
| 144 | + case t: KnownSplitsTable => |
| 145 | + new PaimonSplitScanBuilder(t) |
| 146 | + case _: InnerTable => |
| 147 | + new PaimonScanBuilder(table.copy(options.asCaseSensitiveMap).asInstanceOf[InnerTable]) |
| 148 | + case _ => |
| 149 | + throw new RuntimeException("Only InnerTable can be scanned.") |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + override def newWriteBuilder(info: LogicalWriteInfo): WriteBuilder = { |
| 154 | + table match { |
| 155 | + case fileStoreTable: FileStoreTable => |
| 156 | + val options = Options.fromMap(info.options) |
| 157 | + if (useV2Write) { |
| 158 | + new PaimonV2WriteBuilder(fileStoreTable, info.schema(), options) |
| 159 | + } else { |
| 160 | + new PaimonWriteBuilder(fileStoreTable, options) |
| 161 | + } |
| 162 | + case _ => |
| 163 | + throw new RuntimeException("Only FileStoreTable can be written.") |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + override def toString: String = { |
| 168 | + s"${table.getClass.getSimpleName}[${table.fullName()}]" |
| 169 | + } |
| 170 | +} |
0 commit comments