|
| 1 | +package ai.chronon.integrations.cloud_gcp |
| 2 | + |
| 3 | +import com.google.cloud.bigquery.{ |
| 4 | + BigQuery, |
| 5 | + BigQueryOptions, |
| 6 | + ExternalTableDefinition, |
| 7 | + StandardTableDefinition, |
| 8 | + TableDefinition, |
| 9 | + TableId |
| 10 | +} |
| 11 | +import com.google.cloud.spark.bigquery.BigQueryCatalog |
| 12 | +import org.apache.iceberg.spark.SparkCatalog |
| 13 | +import org.apache.spark.sql.SparkSession |
| 14 | +import org.apache.spark.sql.connector.catalog._ |
| 15 | +import org.apache.spark.sql.connector.catalog.functions.UnboundFunction |
| 16 | +import org.apache.spark.sql.connector.expressions.Transform |
| 17 | +import org.apache.spark.sql.connector.read.ScanBuilder |
| 18 | +import org.apache.spark.sql.connector.write.{LogicalWriteInfo, WriteBuilder} |
| 19 | +import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat |
| 20 | +import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetTable |
| 21 | +import org.apache.spark.sql.types.StructType |
| 22 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap |
| 23 | + |
| 24 | +import java.util |
| 25 | +import scala.jdk.CollectionConverters._ |
| 26 | +import scala.util.Try |
| 27 | + |
| 28 | +/** For now, just delegate to the iceberg catalog. |
| 29 | + * todo(tchow): Given a chronon catalog, delegate to the correct catalog. |
| 30 | + * |
| 31 | + * In order of resolution: |
| 32 | + * 1. BigQuery native |
| 33 | + * 2. Iceberg |
| 34 | + * 3. External tables |
| 35 | + * |
| 36 | + * @param chrononCat |
| 37 | + * @param icebergCatalog |
| 38 | + */ |
| 39 | +class DelegatingTable(internalTable: Table, additionalProperties: Map[String, String]) |
| 40 | + extends Table |
| 41 | + with SupportsRead |
| 42 | + with SupportsWrite { |
| 43 | + |
| 44 | + override def name(): String = internalTable.name |
| 45 | + |
| 46 | + override def schema(): StructType = internalTable.schema |
| 47 | + |
| 48 | + override def capabilities(): util.Set[TableCapability] = internalTable.capabilities() |
| 49 | + |
| 50 | + override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder = |
| 51 | + internalTable.asInstanceOf[SupportsRead].newScanBuilder(options) |
| 52 | + |
| 53 | + override def newWriteBuilder(info: LogicalWriteInfo): WriteBuilder = |
| 54 | + internalTable.asInstanceOf[SupportsWrite].newWriteBuilder(info) |
| 55 | + |
| 56 | + override def properties(): util.Map[String, String] = |
| 57 | + (internalTable.properties().asScala ++ additionalProperties).asJava |
| 58 | +} |
| 59 | + |
| 60 | +object DelegatingTable { |
| 61 | + def apply(table: Table, additionalProperties: Map[String, String] = Map.empty): Table = |
| 62 | + new DelegatingTable(table, additionalProperties = additionalProperties) |
| 63 | +} |
| 64 | + |
| 65 | +class DelegatingBigQueryMetastoreCatalog extends CatalogExtension { |
| 66 | + |
| 67 | + @transient private lazy val bqOptions = BigQueryOptions.getDefaultInstance |
| 68 | + @transient private lazy val bigQueryClient: BigQuery = bqOptions.getService |
| 69 | + |
| 70 | + @transient private lazy val icebergCatalog: SparkCatalog = new SparkCatalog() |
| 71 | + @transient private lazy val connectorCatalog: BigQueryCatalog = new BigQueryCatalog() |
| 72 | + private var defaultSessionCatalog: CatalogPlugin = null |
| 73 | + |
| 74 | + override def listNamespaces: Array[Array[String]] = icebergCatalog.listNamespaces() |
| 75 | + |
| 76 | + override def listNamespaces(namespace: Array[String]): Array[Array[String]] = icebergCatalog.listNamespaces(namespace) |
| 77 | + |
| 78 | + override def loadNamespaceMetadata(namespace: Array[String]): util.Map[String, String] = |
| 79 | + icebergCatalog.loadNamespaceMetadata(namespace) |
| 80 | + |
| 81 | + override def createNamespace(namespace: Array[String], metadata: util.Map[String, String]): Unit = { |
| 82 | + icebergCatalog.createNamespace(namespace, metadata) |
| 83 | + } |
| 84 | + |
| 85 | + override def alterNamespace(namespace: Array[String], changes: NamespaceChange*): Unit = { |
| 86 | + icebergCatalog.alterNamespace(namespace, changes: _*) |
| 87 | + } |
| 88 | + |
| 89 | + override def dropNamespace(namespace: Array[String], cascade: Boolean): Boolean = |
| 90 | + icebergCatalog.dropNamespace(namespace, cascade) |
| 91 | + |
| 92 | + override def listTables(namespace: Array[String]): Array[Identifier] = icebergCatalog.listTables(namespace) |
| 93 | + |
| 94 | + override def loadTable(ident: Identifier): Table = { |
| 95 | + Try { icebergCatalog.loadTable(ident) } |
| 96 | + .recover { |
| 97 | + case _ => { |
| 98 | + val connectorTable = connectorCatalog.loadTable(ident) |
| 99 | + val tId = ident.namespace().toList match { |
| 100 | + case database :: Nil => TableId.of(database, ident.name()) |
| 101 | + case project :: database :: Nil => TableId.of(project, database, ident.name()) |
| 102 | + } |
| 103 | + val table = bigQueryClient.getTable(tId) |
| 104 | + table.getDefinition.asInstanceOf[TableDefinition] match { |
| 105 | + case externalTable: ExternalTableDefinition => { |
| 106 | + val uris = externalTable.getSourceUris.asScala |
| 107 | + val uri = scala |
| 108 | + .Option(externalTable.getHivePartitioningOptions) |
| 109 | + .map(_.getSourceUriPrefix) |
| 110 | + .getOrElse { |
| 111 | + require(uris.size == 1, s"External table ${table} can be backed by only one URI.") |
| 112 | + uris.head.replaceAll("/\\*\\.parquet$", "") |
| 113 | + } |
| 114 | + |
| 115 | + val fileBasedTable = ParquetTable(tId.toString, |
| 116 | + SparkSession.active, |
| 117 | + CaseInsensitiveStringMap.empty(), |
| 118 | + List(uri), |
| 119 | + None, |
| 120 | + classOf[ParquetFileFormat]) |
| 121 | + DelegatingTable(fileBasedTable, |
| 122 | + Map(TableCatalog.PROP_EXTERNAL -> "true", TableCatalog.PROP_LOCATION -> uri)) |
| 123 | + } |
| 124 | + case _: StandardTableDefinition => { |
| 125 | + DelegatingTable(connectorTable, Map(TableCatalog.PROP_EXTERNAL -> "false")) |
| 126 | + } |
| 127 | + case _ => throw new IllegalStateException(s"Cannot support table of type: ${table.getFriendlyName}") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + .getOrElse(defaultSessionCatalog.asInstanceOf[TableCatalog].loadTable(ident)) |
| 132 | + } |
| 133 | + |
| 134 | + override def createTable(ident: Identifier, |
| 135 | + schema: StructType, |
| 136 | + partitions: Array[Transform], |
| 137 | + properties: util.Map[String, String]): Table = { |
| 138 | + icebergCatalog.createTable(ident, schema, partitions, properties) |
| 139 | + } |
| 140 | + |
| 141 | + override def alterTable(ident: Identifier, changes: TableChange*): Table = { |
| 142 | + icebergCatalog.alterTable(ident, changes: _*) |
| 143 | + } |
| 144 | + |
| 145 | + override def dropTable(ident: Identifier): Boolean = icebergCatalog.dropTable(ident) |
| 146 | + |
| 147 | + override def renameTable(oldIdent: Identifier, newIdent: Identifier): Unit = { |
| 148 | + icebergCatalog.renameTable(oldIdent, newIdent) |
| 149 | + } |
| 150 | + |
| 151 | + override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = { |
| 152 | + icebergCatalog.initialize(name, options) |
| 153 | + connectorCatalog.initialize(name, options) |
| 154 | + } |
| 155 | + |
| 156 | + override def name() = "bigquery-delegate" |
| 157 | + |
| 158 | + override def setDelegateCatalog(delegate: CatalogPlugin): Unit = { |
| 159 | + defaultSessionCatalog = delegate |
| 160 | + } |
| 161 | + |
| 162 | + override def listFunctions(namespace: Array[String]): Array[Identifier] = icebergCatalog.listFunctions(namespace) |
| 163 | + |
| 164 | + override def loadFunction(ident: Identifier): UnboundFunction = icebergCatalog.loadFunction(ident) |
| 165 | + |
| 166 | +} |
0 commit comments