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 @@ -137,6 +137,22 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
}
}

/**
* Checks the validity of column names. Hive metastore disallows the table to use comma in
* data column names. Partition columns do not have such a restriction. Views do not have such
* a restriction.
*/
private def verifyColumnNames(table: CatalogTable): Unit = {
if (table.tableType != VIEW) {
table.dataSchema.map(_.name).foreach { colName =>
if (colName.contains(",")) {
throw new AnalysisException("Cannot create a table having a column whose name contains " +
s"commas in Hive metastore. Table: ${table.identifier}; Column: $colName")
}
}
}
}

// --------------------------------------------------------------------------
// Databases
// --------------------------------------------------------------------------
Expand Down Expand Up @@ -202,6 +218,7 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
val table = tableDefinition.identifier.table
requireDbExists(db)
verifyTableProperties(tableDefinition)
verifyColumnNames(tableDefinition)

if (tableExists(db, table) && !ignoreIfExists) {
throw new TableAlreadyExistsException(db = db, table = table)
Expand Down Expand Up @@ -614,6 +631,7 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
requireTableExists(db, table)
val rawTable = getRawTable(db, table)
val withNewSchema = rawTable.copy(schema = schema)
verifyColumnNames(withNewSchema)
// Add table metadata such as table schema, partition columns, etc. to table properties.
val updatedTable = withNewSchema.copy(
properties = withNewSchema.properties ++ tableMetaToTableProps(withNewSchema))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,30 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
}
}

test("Auto alias construction of get_json_object") {
val df = Seq(("1", """{"f1": "value1", "f5": 5.23}""")).toDF("key", "jstring")
val expectedMsg = "Cannot create a table having a column whose name contains commas " +
"in Hive metastore. Table: `default`.`t`; Column: get_json_object(jstring, $.f1)"

withTable("t") {
val e = intercept[AnalysisException] {
df.select($"key", functions.get_json_object($"jstring", "$.f1"))
.write.format("hive").saveAsTable("t")
}.getMessage
assert(e.contains(expectedMsg))
}

withTempView("tempView") {
withTable("t") {
df.createTempView("tempView")
val e = intercept[AnalysisException] {
sql("CREATE TABLE t AS SELECT key, get_json_object(jstring, '$.f1') FROM tempView")
}.getMessage
assert(e.contains(expectedMsg))
}
}
}

test("SPARK-19912 String literals should be escaped for Hive metastore partition pruning") {
withTable("spark_19912") {
Seq(
Expand Down