Skip to content
Merged
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
Expand Up @@ -8,7 +8,7 @@ import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.execution.FileSourceScanExec
import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex

case class GCS(project: String, sourceUri: String, fileFormat: String) extends Format {
case class GCS(sourceUri: String, fileFormat: String) extends Format {

override def name: String = fileFormat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.google.cloud.bigquery.connector.common.BigQueryUtil
import com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.TableId
import org.apache.spark.sql.SparkSession

import scala.jdk.CollectionConverters._

case class GcpFormatProvider(sparkSession: SparkSession) extends FormatProvider {

/**
Expand All @@ -30,8 +32,8 @@ case class GcpFormatProvider(sparkSession: SparkSession) extends FormatProvider

override def resolveTableName(tableName: String): String =
format(tableName) match {
case GCS(_, uri, _) => uri
case _ => tableName
case GCS(uri, _) => uri
case _ => tableName
}

override def readFormat(tableName: String): Format = format(tableName)
Expand Down Expand Up @@ -66,11 +68,12 @@ case class GcpFormatProvider(sparkSession: SparkSession) extends FormatProvider
val uri = Option(externalTable.getHivePartitioningOptions)
.map(_.getSourceUriPrefix)
.getOrElse {
val uris = externalTable.getSourceUris
val uris = externalTable.getSourceUris.asScala
require(uris.size == 1, s"External table ${table} can be backed by only one URI.")
uris.get(0).replaceAll("/\\*\\.parquet$", "")
uris.head.replaceAll("/\\*\\.parquet$", "")
}
GCS(table.getTableId.getProject, uri, formatOptions.getType)

GCS(uri, formatOptions.getType)

case _: StandardTableDefinition =>
BigQueryFormat(table.getTableId.getProject, Map.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GCSFormatTest extends AnyFlatSpec {

val df = spark.createDataFrame(testData).toDF("ds", "first", "second")
df.write.partitionBy("ds").format("parquet").mode(SaveMode.Overwrite).save(dir.getAbsolutePath)
val gcsFormat = GCS(project = "test-project", sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val gcsFormat = GCS(sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val partitions = gcsFormat.partitions("unused_table")(spark)

assertEquals(Set(Map("ds" -> "20241223"), Map("ds" -> "20241224"), Map("ds" -> "20241225")), partitions.toSet)
Expand All @@ -53,7 +53,7 @@ class GCSFormatTest extends AnyFlatSpec {

val df = spark.createDataFrame(testData).toDF("ds", "first", "second")
df.write.format("parquet").mode(SaveMode.Overwrite).save(dir.getAbsolutePath)
val gcsFormat = GCS(project = "test-project", sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val gcsFormat = GCS(sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val partitions = gcsFormat.partitions("unused_table")(spark)

assertEquals(Set.empty, partitions.toSet)
Expand Down Expand Up @@ -83,7 +83,7 @@ class GCSFormatTest extends AnyFlatSpec {
.toDF("ds", "first", "second")
.select(to_date(col("ds"), "yyyy-MM-dd").as("ds"), col("first"), col("second"))
df.write.format("parquet").partitionBy("ds").mode(SaveMode.Overwrite).save(dir.getAbsolutePath)
val gcsFormat = GCS(project = "test-project", sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val gcsFormat = GCS(sourceUri = dir.getAbsolutePath, fileFormat = "parquet")
val partitions = gcsFormat.partitions("unused_table")(spark)

assertEquals(Set(Map("ds" -> "2024-12-23"), Map("ds" -> "2024-12-24"), Map("ds" -> "2024-12-25")), partitions.toSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class GcpFormatProviderTest extends AnyFlatSpec with MockitoSugar {

val gcsFormat = gcpFormatProvider.getFormat(mockTable).asInstanceOf[GCS]
assert(gcsFormat.sourceUri == tableName)
assert(gcsFormat.project == "project")
assert(gcsFormat.fileFormat == "PARQUET")
}
}
Loading