|
| 1 | +package ai.chronon.integrations.cloud_gcp |
| 2 | + |
| 3 | +import ai.chronon.spark.Format |
| 4 | +import ai.chronon.spark.FormatProvider |
| 5 | +import ai.chronon.spark.Hive |
| 6 | +import com.google.cloud.bigquery.connector.common.BigQueryUtil |
| 7 | +import org.apache.spark.sql.SparkSession |
| 8 | + |
| 9 | +case class GCPFormatProvider(sparkSession: SparkSession) extends FormatProvider { |
| 10 | + def readFormat(tableName: String): Format = { |
| 11 | + |
| 12 | + val tableIdentifier = sparkSession.sessionState.sqlParser.parseTableIdentifier(tableName) |
| 13 | + val tableMeta = sparkSession.sessionState.catalog.getTableRawMetadata(tableIdentifier) |
| 14 | + |
| 15 | + val storageProvider = tableMeta.provider |
| 16 | + storageProvider match { |
| 17 | + case Some("com.google.cloud.spark.bigquery") => { |
| 18 | + |
| 19 | + val tableProperties = tableMeta.properties |
| 20 | + val project = tableProperties |
| 21 | + .get("FEDERATION_BIGQUERY_TABLE_PROPERTY") |
| 22 | + .map(BigQueryUtil.parseTableId) |
| 23 | + .map(_.getProject) |
| 24 | + .getOrElse(throw new IllegalStateException("bigquery project required!")) |
| 25 | + |
| 26 | + val bigQueryTableType = tableProperties.get("federation.bigquery.table.type") |
| 27 | + bigQueryTableType.map(_.toUpperCase) match { |
| 28 | + case Some("EXTERNAL") => throw new IllegalStateException("External tables not yet supported.") |
| 29 | + case Some("MANAGED") => BQuery(project) |
| 30 | + case None => throw new IllegalStateException("Dataproc federation service must be available.") |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + case Some("hive") | None => Hive |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + // For now, fix to BigQuery. We'll clean this up. |
| 40 | + def writeFormat(tableName: String): Format = ??? |
| 41 | +} |
| 42 | + |
| 43 | +case class BQuery(project: String) extends Format { |
| 44 | + |
| 45 | + override def primaryPartitions(tableName: String, partitionColumn: String, subPartitionsFilter: Map[String, String])( |
| 46 | + implicit sparkSession: SparkSession): Seq[String] = |
| 47 | + super.primaryPartitions(tableName, partitionColumn, subPartitionsFilter) |
| 48 | + |
| 49 | + override def partitions(tableName: String)(implicit sparkSession: SparkSession): Seq[Map[String, String]] = { |
| 50 | + import sparkSession.implicits._ |
| 51 | + val tableIdentifier = BigQueryUtil.parseTableId(tableName) |
| 52 | + val table = tableIdentifier.getTable |
| 53 | + val database = |
| 54 | + Option(tableIdentifier.getDataset).getOrElse(throw new IllegalArgumentException("database required!")) |
| 55 | + |
| 56 | + val originalViewsEnabled = sparkSession.conf.get("viewsEnabled", false.toString) |
| 57 | + val originalMaterializationDataset = sparkSession.conf.get("materializationDataset", "") |
| 58 | + |
| 59 | + // See: https://github.com/GoogleCloudDataproc/spark-bigquery-connector/issues/434#issuecomment-886156191 |
| 60 | + // and: https://cloud.google.com/bigquery/docs/information-schema-intro#limitations |
| 61 | + |
| 62 | + sparkSession.conf.set("viewsEnabled", true) |
| 63 | + sparkSession.conf.set("materializationDataset", database) |
| 64 | + |
| 65 | + try { |
| 66 | + // See: https://cloud.google.com/bigquery/docs/information-schema-columns |
| 67 | + val partColsSql = |
| 68 | + s""" |
| 69 | + |SELECT column_name FROM `${project}.${database}.INFORMATION_SCHEMA.COLUMNS` |
| 70 | + |WHERE table_name = '${table}' AND is_partitioning_column = 'YES' |
| 71 | + | |
| 72 | + |""".stripMargin |
| 73 | + |
| 74 | + val partitionCol = sparkSession.read |
| 75 | + .format("bigquery") |
| 76 | + .option("project", project) |
| 77 | + .option("query", partColsSql) |
| 78 | + .load() |
| 79 | + .as[String] |
| 80 | + .collect |
| 81 | + .headOption |
| 82 | + .getOrElse(throw new UnsupportedOperationException(s"No partition column for table ${tableName} found.")) |
| 83 | + |
| 84 | + // See: https://cloud.google.com/bigquery/docs/information-schema-partitions |
| 85 | + val partValsSql = |
| 86 | + s""" |
| 87 | + |SELECT partition_id FROM `${project}.${database}.INFORMATION_SCHEMA.PARTITIONS` |
| 88 | + |WHERE table_name = '${table}' |
| 89 | + | |
| 90 | + |""".stripMargin |
| 91 | + |
| 92 | + val partitionVals = sparkSession.read |
| 93 | + .format("bigquery") |
| 94 | + .option("project", project) |
| 95 | + .option("query", partValsSql) |
| 96 | + .load() |
| 97 | + .as[String] |
| 98 | + .collect |
| 99 | + .toList |
| 100 | + partitionVals.map((p) => Map(partitionCol -> p)) |
| 101 | + |
| 102 | + } finally { |
| 103 | + sparkSession.conf.set("viewsEnabled", originalViewsEnabled) |
| 104 | + sparkSession.conf.set("materializationDataset", originalMaterializationDataset) |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + def createTableTypeString: String = "BIGQUERY" |
| 110 | + def fileFormatString(format: String): String = "" |
| 111 | + |
| 112 | + override def supportSubPartitionsFilter: Boolean = true |
| 113 | +} |
0 commit comments