Skip to content
Closed
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 @@ -998,12 +998,6 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {

selectQuery match {
case Some(q) =>
// Just use whatever is projected in the select statement as our schema
if (schema.nonEmpty) {
operationNotAllowed(
"Schema may not be specified in a Create Table As Select (CTAS) statement",
ctx)
}
// Hive does not allow to use a CTAS statement to create a partitioned table.
if (tableDesc.partitionColumnNames.nonEmpty) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a dead code if we do not change the order.

val errorMessage = "A Create Table As Select (CTAS) statement is not allowed to " +
Expand All @@ -1013,6 +1007,12 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {
"CTAS statement."
operationNotAllowed(errorMessage, ctx)
}
// Just use whatever is projected in the select statement as our schema
if (schema.nonEmpty) {
operationNotAllowed(
"Schema may not be specified in a Create Table As Select (CTAS) statement",
ctx)
}

val hasStorageProperties = (ctx.createFileFormat != null) || (ctx.rowFormat != null)
if (conf.convertCTAS && !hasStorageProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,19 +642,35 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
}

test("specifying the column list for CTAS") {
Seq((1, "111111"), (2, "222222")).toDF("key", "value").createOrReplaceTempView("mytable1")
withTempView("mytable1") {
Seq((1, "111111"), (2, "222222")).toDF("key", "value").createOrReplaceTempView("mytable1")
withTable("gen__tmp") {
sql("create table gen__tmp as select key as a, value as b from mytable1")
checkAnswer(
sql("SELECT a, b from gen__tmp"),
sql("select key, value from mytable1").collect())
}

sql("create table gen__tmp as select key as a, value as b from mytable1")
checkAnswer(
sql("SELECT a, b from gen__tmp"),
sql("select key, value from mytable1").collect())
sql("DROP TABLE gen__tmp")
withTable("gen__tmp") {
val e = intercept[AnalysisException] {
sql("create table gen__tmp(a int, b string) as select key, value from mytable1")
}.getMessage
assert(e.contains("Schema may not be specified in a Create Table As Select (CTAS)"))
}

intercept[AnalysisException] {
sql("create table gen__tmp(a int, b string) as select key, value from mytable1")
withTable("gen__tmp") {
val e = intercept[AnalysisException] {
sql(
"""
|CREATE TABLE gen__tmp
|PARTITIONED BY (key string)
|AS SELECT key, value FROM mytable1
""".stripMargin)
}.getMessage
assert(e.contains("A Create Table As Select (CTAS) statement is not allowed to " +
"create a partitioned table using Hive's file formats"))
}
}

sql("drop table mytable1")
}

test("command substitution") {
Expand Down