Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
677541b
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 13, 2017
4e70fff
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 14, 2017
3f022f9
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 15, 2017
6d77bf9
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 15, 2017
42aca3d
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 15, 2017
5cbe999
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 16, 2017
732266c
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 16, 2017
c7ff62c
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' configur…
fjh100456 Sep 16, 2017
384ee04
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Sep 20, 2017
8c92074
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Sep 20, 2017
dd5060a
Merge branch 'master' into master
fjh100456 Sep 20, 2017
d427df5
Update InsertSuite.scala
fjh100456 Sep 20, 2017
35cfa01
Update InsertSuite.scala
fjh100456 Sep 20, 2017
5387497
Fix test problems
fjh100456 Sep 20, 2017
676d6a7
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Sep 27, 2017
ae1da8f
Fix scala style issue
fjh100456 Sep 27, 2017
fd73145
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Sep 28, 2017
7615939
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Sep 28, 2017
90cbcb3
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 10, 2017
dd6d635
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 10, 2017
4fe8170
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 12, 2017
aa31261
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 16, 2017
dfb36d9
Merge branch 'master' into master
fjh100456 Oct 16, 2017
c4801f6
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 16, 2017
105e129
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Oct 16, 2017
dc12038
Merge pull request #1 from apache/master
fjh100456 Dec 18, 2017
d779ee6
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Dec 19, 2017
0cb7b7a
[SPARK-21786][SQL] The 'spark.sql.parquet.compression.codec' and 'spa…
fjh100456 Dec 20, 2017
78e0403
Resume the changing, and change it in another pr later.
fjh100456 Dec 23, 2017
7804f60
Change to public
fjh100456 Dec 23, 2017
52cdd75
Fix the code with gatorsmile's suggestion.
fjh100456 Dec 23, 2017
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 @@ -101,6 +101,19 @@ case class InsertIntoHiveTable(
val tmpLocation = getExternalTmpPath(sparkSession, hadoopConf, tableLocation)
val fileSinkConf = new FileSinkDesc(tmpLocation.toString, tableDesc, false)

tableDesc.getOutputFileFormatClassName match {
Copy link
Member

Choose a reason for hiding this comment

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

Move the whole logics into saveAsHiveFile, which is being shared by InsertIntoHiveDirCommand and InsertIntoHiveTable. Both need these logics.

case formatName if formatName.endsWith("ParquetOutputFormat") =>
val parquetCompression = sparkSession.sessionState.conf.parquetCompressionCodec
hadoopConf.set("parquet.compression", parquetCompression)
case formatName if formatName.endsWith("OrcOutputFormat") =>
val orcCompression = sparkSession.sessionState.conf.orcCompressionCodec.toUpperCase match {
case "UNCOMPRESSED" => "NONE"
case _@x => x
}
hadoopConf.set("orc.compress", orcCompression)
case _ =>
}

val numDynamicPartitions = partition.values.count(_.isEmpty)
val numStaticPartitions = partition.values.count(_.nonEmpty)
val partitionSpec = partition.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,95 @@ class InsertSuite extends QueryTest with TestHiveSingleton with BeforeAndAfter
assert(e.contains("mismatched input 'ROW'"))
}
}

test("[SPARK-21786] The 'spark.sql.parquet.compression.codec' " +
"configuration doesn't take effect on tables with partition field(s)") {
val tableWithPartition = "table_with_partition"
val tableNoPartition = "table_no_partition"

def insertOverwriteTable(tableName: String, paramName: String, codec: String,
isPartitioned: Boolean): Unit = {
withSQLConf(paramName -> codec) {
sql(
s"""
|INSERT OVERWRITE TABLE $tableName
|${if (isPartitioned) "partition (p=10000)" else "" }
|SELECT * from table_source
""".stripMargin)
}
}

def getDirFiles(file: File): List[File] = {
if (!file.exists()) Nil
else if (file.isFile) List(file)
else {
file.listFiles().filterNot(_.getName.startsWith(".hive-staging"))
.groupBy(_.isFile).flatMap {
case (isFile, files) if isFile => files.toList
case (_, dirs) => dirs.flatMap(getDirFiles)
}.toList
}
}

def getTableSize(tmpDir: File, tableName: String, paramName: String, codec: String,
isPartitioned: Boolean = false): Long = {
insertOverwriteTable(tableName, paramName, codec, isPartitioned)
val path = s"${tmpDir.getPath.stripSuffix("/")}/$tableName"
val dir = new File(path)
val files = getDirFiles(dir).filter(_.getName.startsWith("part-"))
files.map(_.length()).sum
}

def checkCompressionCodec(format: String)(f: File => Unit): Unit = {
withTempDir { tmpDir =>
withTempView("table_source") {
(0 until 100000).toDF("a").createOrReplaceTempView("table_source")

withTable(tableWithPartition, tableNoPartition) {
sql(
s"""
|CREATE TABLE $tableNoPartition(a int)
|STORED AS $format
|LOCATION '${tmpDir.toURI.toString.stripSuffix("/")}/$tableNoPartition'
""".stripMargin)
sql(
s"""
|CREATE TABLE $tableWithPartition(a int)
|PARTITIONED BY (p int)
|STORED AS $format
|LOCATION '${tmpDir.toURI.toString.stripSuffix("/")}/$tableWithPartition'
""".stripMargin)

f(tmpDir)
}
}
}
}

val parquetCompression = "spark.sql.parquet.compression.codec"
checkCompressionCodec("PARQUET") { tmpDir =>
// In fact, partitioned and unpartitioned table meta information is slightly different,
// and partitioned tables are slightly larger, but the differences are not very large.
// Think less than 1024Byte
val maxDiff = 1024
assert(getTableSize(tmpDir, tableWithPartition, parquetCompression, "uncompressed", true)
- getTableSize(tmpDir, tableNoPartition, parquetCompression, "uncompressed") < maxDiff)
assert(getTableSize(tmpDir, tableWithPartition, parquetCompression, "gzip", true)
- getTableSize(tmpDir, tableNoPartition, parquetCompression, "gzip") < maxDiff)
assert(getTableSize(tmpDir, tableWithPartition, parquetCompression, "uncompressed", true)
- getTableSize(tmpDir, tableWithPartition, parquetCompression, "gzip", true) > maxDiff)
}

val orcCompression = "spark.sql.orc.compression.codec"
checkCompressionCodec("ORC") { tmpDir =>
assert(getTableSize(tmpDir, tableWithPartition, orcCompression, "none", true)
== getTableSize(tmpDir, tableNoPartition, orcCompression, "none"))
assert(getTableSize(tmpDir, tableWithPartition, orcCompression, "uncompressed", true)
== getTableSize(tmpDir, tableNoPartition, orcCompression, "none"))
assert(getTableSize(tmpDir, tableWithPartition, orcCompression, "zlib", true)
== getTableSize(tmpDir, tableNoPartition, orcCompression, "zlib"))
assert(getTableSize(tmpDir, tableWithPartition, orcCompression, "none", true)
> getTableSize(tmpDir, tableWithPartition, orcCompression, "zlib", true))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1438,39 +1438,41 @@ class HiveDDLSuite
}

test("create hive serde table with new syntax") {
withTable("t", "t2", "t3") {
withTempPath { path =>
sql(
s"""
|CREATE TABLE t(id int) USING hive
|OPTIONS(fileFormat 'orc', compression 'Zlib')
|LOCATION '${path.toURI}'
""".stripMargin)
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
assert(DDLUtils.isHiveTable(table))
assert(table.storage.serde == Some("org.apache.hadoop.hive.ql.io.orc.OrcSerde"))
assert(table.storage.properties.get("compression") == Some("Zlib"))
assert(spark.table("t").collect().isEmpty)

sql("INSERT INTO t SELECT 1")
checkAnswer(spark.table("t"), Row(1))
// Check if this is compressed as ZLIB.
val maybeOrcFile = path.listFiles().find(!_.getName.endsWith(".crc"))
assert(maybeOrcFile.isDefined)
val orcFilePath = maybeOrcFile.get.toPath.toString
val expectedCompressionKind =
OrcFileOperator.getFileReader(orcFilePath).get.getCompression
assert("ZLIB" === expectedCompressionKind.name())

sql("CREATE TABLE t2 USING HIVE AS SELECT 1 AS c1, 'a' AS c2")
val table2 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t2"))
assert(DDLUtils.isHiveTable(table2))
assert(table2.storage.serde == Some("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe"))
checkAnswer(spark.table("t2"), Row(1, "a"))

sql("CREATE TABLE t3(a int, p int) USING hive PARTITIONED BY (p)")
sql("INSERT INTO t3 PARTITION(p=1) SELECT 0")
checkAnswer(spark.table("t3"), Row(0, 1))
withSQLConf("spark.sql.orc.compression.codec" -> "zlib") {
withTable("t", "t2", "t3") {
withTempPath { path =>
sql(
s"""
|CREATE TABLE t(id int) USING hive
|OPTIONS(fileFormat 'orc', compression 'Zlib')
|LOCATION '${path.toURI}'
""".stripMargin)
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
assert(DDLUtils.isHiveTable(table))
assert(table.storage.serde == Some("org.apache.hadoop.hive.ql.io.orc.OrcSerde"))
assert(table.storage.properties.get("compression") == Some("Zlib"))
assert(spark.table("t").collect().isEmpty)

sql("INSERT INTO t SELECT 1")
checkAnswer(spark.table("t"), Row(1))
// Check if this is compressed as ZLIB.
val maybeOrcFile = path.listFiles().find(!_.getName.endsWith(".crc"))
assert(maybeOrcFile.isDefined)
val orcFilePath = maybeOrcFile.get.toPath.toString
val expectedCompressionKind =
OrcFileOperator.getFileReader(orcFilePath).get.getCompression
assert("ZLIB" === expectedCompressionKind.name())

sql("CREATE TABLE t2 USING HIVE AS SELECT 1 AS c1, 'a' AS c2")
val table2 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t2"))
assert(DDLUtils.isHiveTable(table2))
assert(table2.storage.serde == Some("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe"))
checkAnswer(spark.table("t2"), Row(1, "a"))

sql("CREATE TABLE t3(a int, p int) USING hive PARTITIONED BY (p)")
sql("INSERT INTO t3 PARTITION(p=1) SELECT 0")
checkAnswer(spark.table("t3"), Row(0, 1))
}
}
}
}
Expand Down