-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-16948][SQL] Use metastore schema instead of inferring schema for ORC in HiveMetastoreCatalog #14537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-16948][SQL] Use metastore schema instead of inferring schema for ORC in HiveMetastoreCatalog #14537
Changes from all commits
5721b88
4ae92d8
75bca1b
97d21f6
4004c0a
9a8838a
eb8a955
70cf84d
c9d677b
7385a06
1746853
5f7e5ad
e8e2d70
0f901aa
0772a96
6ff7e5d
fc14e2d
9ecb2ed
fa71370
e39715e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ private[hive] class HiveMetastoreCatalog(sparkSession: SparkSession) extends Log | |
defaultSource: FileFormat, | ||
fileFormatClass: Class[_ <: FileFormat], | ||
fileType: String): LogicalRelation = { | ||
val metastoreSchema = StructType.fromAttributes(metastoreRelation.output) | ||
val metastoreSchema = metastoreRelation.schema | ||
val tableIdentifier = | ||
QualifiedTableName(metastoreRelation.databaseName, metastoreRelation.tableName) | ||
val bucketSpec = None // We don't support hive bucketed tables, only ones we write out. | ||
|
@@ -237,21 +237,24 @@ private[hive] class HiveMetastoreCatalog(sparkSession: SparkSession) extends Log | |
new Path(metastoreRelation.catalogTable.storage.locationUri.get), | ||
partitionSpec) | ||
|
||
val inferredSchema = if (fileType.equals("parquet")) { | ||
val inferredSchema = | ||
defaultSource.inferSchema(sparkSession, options, fileCatalog.allFiles()) | ||
inferredSchema.map { inferred => | ||
ParquetFileFormat.mergeMetastoreParquetSchema(metastoreSchema, inferred) | ||
}.getOrElse(metastoreSchema) | ||
} else { | ||
defaultSource.inferSchema(sparkSession, options, fileCatalog.allFiles()).get | ||
val schema = fileType match { | ||
case "parquet" => | ||
val inferredSchema = | ||
defaultSource.inferSchema(sparkSession, options, fileCatalog.allFiles()) | ||
inferredSchema.map { inferred => | ||
ParquetFileFormat.mergeMetastoreParquetSchema(metastoreSchema, inferred) | ||
}.getOrElse(metastoreSchema) | ||
case "orc" => | ||
metastoreSchema | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went through the code path again, seems we must infer the schema here. In metastore, we store the table schema and partition columns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan As we discussed offline yesterday, this is probably fine since ORC supports column pruning. Therefore, when reading an ORC file in a partitioned table, the reader always ignores partition columns stored inside the physical file and uses the value encoded in partition directory path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a test case for this case here. |
||
case _ => | ||
throw new RuntimeException(s"Cannot convert a $fileType to a data source table") | ||
} | ||
|
||
val relation = HadoopFsRelation( | ||
sparkSession = sparkSession, | ||
location = fileCatalog, | ||
partitionSchema = partitionSchema, | ||
dataSchema = inferredSchema, | ||
dataSchema = schema, | ||
bucketSpec = bucketSpec, | ||
fileFormat = defaultSource, | ||
options = options) | ||
|
@@ -281,7 +284,7 @@ private[hive] class HiveMetastoreCatalog(sparkSession: SparkSession) extends Log | |
DataSource( | ||
sparkSession = sparkSession, | ||
paths = paths, | ||
userSpecifiedSchema = Some(metastoreRelation.schema), | ||
userSpecifiedSchema = Some(metastoreSchema), | ||
bucketSpec = bucketSpec, | ||
options = options, | ||
className = fileType).resolveRelation(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
package org.apache.spark.sql.hive.orc | ||
|
||
import java.io.File | ||
import java.nio.charset.StandardCharsets | ||
|
||
import org.scalatest.BeforeAndAfterAll | ||
|
@@ -372,6 +373,40 @@ class OrcQuerySuite extends QueryTest with BeforeAndAfterAll with OrcTest { | |
} | ||
} | ||
|
||
test("support empty orc table when converting hive serde table to data source table") { | ||
withSQLConf((HiveUtils.CONVERT_METASTORE_ORC.key, "true")) { | ||
withTable("empty_orc_partitioned") { | ||
sql( | ||
""" | ||
|CREATE TABLE empty_orc_partitioned(key INT, value STRING) | ||
|PARTITIONED BY (p INT) STORED AS ORC | ||
""".stripMargin) | ||
|
||
val emptyDF = Seq.empty[(Int, String)].toDF("key", "value").coalesce(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't really need |
||
|
||
// Query empty table | ||
checkAnswer( | ||
sql("SELECT key, value FROM empty_orc_partitioned"), | ||
emptyDF) | ||
} | ||
|
||
withTable("empty_orc") { | ||
sql( | ||
""" | ||
|CREATE TABLE empty_orc(key INT, value STRING) | ||
|STORED AS ORC | ||
""".stripMargin) | ||
|
||
val emptyDF = Seq.empty[(Int, String)].toDF("key", "value").coalesce(1) | ||
|
||
// Query empty table | ||
checkAnswer( | ||
sql("SELECT key, value FROM empty_orc"), | ||
emptyDF) | ||
} | ||
} | ||
} | ||
|
||
test("SPARK-10623 Enable ORC PPD") { | ||
withTempPath { dir => | ||
withSQLConf(SQLConf.ORC_FILTER_PUSHDOWN_ENABLED.key -> "true") { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried here. If the table is partitioned,
metastoreSchema
will always contain partition columns, and thus the merged schema will contain partition columns too. This means, we always read parquet files with partition columns, I think we may have a hidden bug.