Skip to content

Commit 8af5b2a

Browse files
committed
Update conf key and unit test.
1 parent 5a67903 commit 8af5b2a

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveContext.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,20 @@ class HiveContext(sc: SparkContext) extends SQLContext(sc) {
6161
protected[sql] def convertMetastoreParquet: Boolean =
6262
getConf("spark.sql.hive.convertMetastoreParquet", "true") == "true"
6363

64-
protected[sql] def convertHiveCTASWithoutStorageSpec: Boolean =
65-
getConf("spark.sql.sources.convertHiveCTASWithoutStorageSpec", "false") == "true"
64+
/**
65+
* When true, a table created by a Hive CTAS statement (no USING clause) will be
66+
* converted to a data source table, using the data source set by spark.sql.sources.default.
67+
* The table in CTAS statement will be converted when it meets any of the following conditions:
68+
* - The CTAS does not specify any of a SerDe (ROW FORMAT SERDE), a File Format (STORED AS), or
69+
* a Storage Hanlder (STORED BY), and the value of hive.default.fileformat in hive-site.xml
70+
* is either TextFile or SequenceFile.
71+
* - The CTAS statement specifies TextFile (STORED AS TEXTFILE) as the file format and no SerDe
72+
* is specified (no ROW FORMAT SERDE clause).
73+
* - The CTAS statement specifies SequenceFile (STORED AS SEQUENCEFILE) as the file format
74+
* and no SerDe is specified (no ROW FORMAT SERDE clause).
75+
*/
76+
protected[sql] def convertCTAS: Boolean =
77+
getConf("spark.sql.hive.convertCTAS", "false") == "true"
6678

6779
override protected[sql] def executePlan(plan: LogicalPlan): this.QueryExecution =
6880
new this.QueryExecution(plan)

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveMetastoreCatalog.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,13 @@ private[hive] class HiveMetastoreCatalog(hive: HiveContext) extends Catalog with
506506
case None => false
507507
}
508508

509-
if (hive.convertHiveCTASWithoutStorageSpec && !hasStorageSpec) {
509+
if (hive.convertCTAS && !hasStorageSpec) {
510510
// Do the conversion when convertHiveCTASWithoutStorageSpec is true and the query
511511
// does not specify any storage format (file format and storage handler).
512512
if (dbName.isDefined) {
513513
throw new AnalysisException(
514514
"Cannot specify database name in a CTAS statement " +
515-
"when spark.sql.sources.convertHiveCTASWithoutStorageSpec is set to true.")
515+
"when spark.sql.hive.convertCTAS is set to true.")
516516
}
517517

518518
val mode = if (allowExisting) SaveMode.Ignore else SaveMode.ErrorIfExists
@@ -537,11 +537,11 @@ private[hive] class HiveMetastoreCatalog(hive: HiveContext) extends Catalog with
537537

538538
case p @ CreateTableAsSelect(db, tableName, child, allowExisting, None) =>
539539
val (dbName, tblName) = processDatabaseAndTableName(db, tableName)
540-
if (hive.convertHiveCTASWithoutStorageSpec) {
540+
if (hive.convertCTAS) {
541541
if (dbName.isDefined) {
542542
throw new AnalysisException(
543543
"Cannot specify database name in a CTAS statement " +
544-
"when spark.sql.sources.convertHiveCTASWithoutStorageSpec is set to true.")
544+
"when spark.sql.hive.convertCTAS is set to true.")
545545
}
546546

547547
val mode = if (allowExisting) SaveMode.Ignore else SaveMode.ErrorIfExists

sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/commands.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ case class CreateMetastoreDataSourceAsSelect(
165165
mode match {
166166
case SaveMode.ErrorIfExists =>
167167
sys.error(s"Table $tableName already exists. " +
168-
s"If you want to append into it, please set mode to SaveMode.Append. " +
169-
s"Or, if you want to overwrite it, please set mode to SaveMode.Overwrite.")
168+
s"If you are using saveAsTable, you can set SaveMode to SaveMode.Append to" +
169+
s"insert data into the table or set SaveMode to SaveMode.Overwrite to overwrite" +
170+
s"the existing data. " +
171+
s"Or, if you are using SQL CREATE TABLE, you need to drop $tableName first.")
170172
case SaveMode.Ignore =>
171173
// Since the table already exists and the save mode is Ignore, we will just return.
172174
return Seq.empty[Row]

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,27 @@ class SQLQuerySuite extends QueryTest {
6565
}
6666
}
6767

68-
val originalConf = getConf("spark.sql.sources.convertHiveCTASWithoutStorageSpec", "false")
68+
val originalConf = getConf("spark.sql.hive.convertCTAS", "false")
6969

70-
setConf("spark.sql.sources.convertHiveCTASWithoutStorageSpec", "true")
70+
setConf("spark.sql.hive.convertCTAS", "true")
7171

7272
sql("CREATE TABLE ctas1 AS SELECT key k, value FROM src ORDER BY k, value")
73+
sql("CREATE TABLE IF NOT EXISTS ctas1 AS SELECT key k, value FROM src ORDER BY k, value")
74+
var message = intercept[RuntimeException] {
75+
sql("CREATE TABLE ctas1 AS SELECT key k, value FROM src ORDER BY k, value")
76+
}.getMessage
77+
assert(message.contains("Table ctas1 already exists"))
7378
checkRelation("ctas1", true)
7479
sql("DROP TABLE ctas1")
7580

7681
// Specifying database name for query can be converted to data source write path
7782
// is not allowed right now.
78-
val message = intercept[AnalysisException] {
83+
message = intercept[AnalysisException] {
7984
sql("CREATE TABLE default.ctas1 AS SELECT key k, value FROM src ORDER BY k, value")
8085
}.getMessage
8186
assert(
8287
message.contains("Cannot specify database name in a CTAS statement"),
83-
"When spark.sql.sources.convertHiveCTASWithoutStorageSpec is true, we should not allow " +
88+
"When spark.sql.hive.convertCTAS is true, we should not allow " +
8489
"database name specified.")
8590

8691
sql("CREATE TABLE ctas1 stored as textfile AS SELECT key k, value FROM src ORDER BY k, value")
@@ -105,7 +110,7 @@ class SQLQuerySuite extends QueryTest {
105110
// checkRelation("ctas1", false)
106111
// sql("DROP TABLE ctas1")
107112

108-
setConf("spark.sql.sources.convertHiveCTASWithoutStorageSpec", originalConf)
113+
setConf("spark.sql.hive.convertCTAS", originalConf)
109114
}
110115

111116
test("CTAS with serde") {

0 commit comments

Comments
 (0)