Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -358,12 +358,17 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
"Spark SQL specific format, which is NOT compatible with Hive."
(None, message)

// our bucketing is un-compatible with hive(different hash function)
case _ if table.bucketSpec.nonEmpty =>
// our bucketing is un-compatible with hive(different hash function).
// but downstream(Hive/Presto) still can read it as not bucketed table.
// We set the SerDe correctly and bucketing_version to spark.
// The downstream decides how to read it by themselves, a similar implementation:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the impact if the downstream makes a wrong decision?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. It's not bucketed table at Hive side. Related code:

if (bucketSpec.isDefined) {
val BucketSpec(numBuckets, bucketColumnNames, sortColumnNames) = bucketSpec.get
properties.put(DATASOURCE_SCHEMA_NUMBUCKETS, numBuckets.toString)
properties.put(DATASOURCE_SCHEMA_NUMBUCKETCOLS, bucketColumnNames.length.toString)
bucketColumnNames.zipWithIndex.foreach { case (bucketCol, index) =>
properties.put(s"$DATASOURCE_SCHEMA_BUCKETCOL_PREFIX$index", bucketCol)
}
if (sortColumnNames.nonEmpty) {
properties.put(DATASOURCE_SCHEMA_NUMSORTCOLS, sortColumnNames.length.toString)
sortColumnNames.zipWithIndex.foreach { case (sortCol, index) =>
properties.put(s"$DATASOURCE_SCHEMA_SORTCOL_PREFIX$index", sortCol)
}
}
}

table.bucketSpec match {
case Some(bucketSpec) if DDLUtils.isHiveTable(table) =>
hiveTable.setNumBuckets(bucketSpec.numBuckets)
hiveTable.setBucketCols(bucketSpec.bucketColumnNames.toList.asJava)
if (bucketSpec.sortColumnNames.nonEmpty) {
hiveTable.setSortCols(
bucketSpec.sortColumnNames
.map(col => new Order(col, HIVE_COLUMN_ORDER_ASC))
.toList
.asJava
)
}
case _ =>
}

// https://github.com/prestosql/presto/pull/512
case Some(serde) if table.bucketSpec.nonEmpty =>
val message =
s"Persisting bucketed data source table $qualifiedTableName into " +
"Hive metastore in Spark SQL specific format, which is NOT compatible with Hive. "
(None, message)
tableProperties.put("bucketing_version", "spark")
(Some(newHiveCompatibleMetastoreTable(serde)), message)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set bucketSpec = None?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary:

table.bucketSpec match {
case Some(bucketSpec) if DDLUtils.isHiveTable(table) =>
hiveTable.setNumBuckets(bucketSpec.numBuckets)
hiveTable.setBucketCols(bucketSpec.bucketColumnNames.toList.asJava)
if (bucketSpec.sortColumnNames.nonEmpty) {
hiveTable.setSortCols(
bucketSpec.sortColumnNames
.map(col => new Order(col, HIVE_COLUMN_ORDER_ASC))
.toList
.asJava
)
}
case _ =>
}


case Some(serde) =>
val message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.apache.spark.sql.catalyst.catalog.CatalogTableType
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.plans.logical.SubqueryAlias
import org.apache.spark.sql.hive.test.TestHiveSingleton
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.{HiveSerDe, SQLConf}
import org.apache.spark.sql.test.{ExamplePointUDT, SQLTestUtils}
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -273,4 +273,26 @@ class DataSourceWithHiveMetastoreCatalogSuite
}

}

test("Set the bucketed data source table SerDe correctly") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's include the jira id in test name.

val provider = "parquet"
withTable("t") {
spark.sql(
s"""
|CREATE TABLE t (c1 INT, c2 INT)
|USING $provider
|CLUSTERED BY (c1)
|SORTED BY (c1)
|INTO 2 BUCKETS
""".stripMargin)

val metadata = sessionState.catalog.getTableMetadata(TableIdentifier("t", Some("default")))
assert(metadata.properties.get("bucketing_version") === Some("spark"))

val hiveSerDe = HiveSerDe.sourceToSerDe(provider).get
assert(metadata.storage.serde === hiveSerDe.serde)
assert(metadata.storage.inputFormat === hiveSerDe.inputFormat)
assert(metadata.storage.outputFormat === hiveSerDe.outputFormat)
}
}
}