ulysses-you commented on a change in pull request #25198: [SPARK-28443][SQL] 
Spark sql add exception when create field type NullType
URL: https://github.com/apache/spark/pull/25198#discussion_r311808608
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
 ##########
 @@ -393,6 +393,43 @@ case class PreprocessTableInsertion(conf: SQLConf) 
extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * SPARK-28443: Spark sql add exception when create field type NullType
+ */
+object DDLCheck extends (LogicalPlan => Unit) {
+
+  def failAnalysis(msg: String): Unit = { throw new AnalysisException(msg) }
+
+  def throwWhenExistsNullType(schema: StructType): Unit = {
+    if (schema.exists(f => DataTypes.NullType.sameType(f.dataType))) {
+      failAnalysis("DataType NullType is not supported for create table ")
+    }
+  }
+
+  override def apply(plan: LogicalPlan): Unit = {
+    plan.foreach {
+      case CreateTable(tableDesc, _, _) =>
+        throwWhenExistsNullType(tableDesc.schema)
+
+      case CreateV2Table(_, _, tableSchema, _, _, _) =>
+        throwWhenExistsNullType(tableSchema)
+
+      // DataSourceAnalysis will convert CreateTable to 
CreateDataSourceTableCommand before check
+      case CreateDataSourceTableCommand(table, _) =>
+        throwWhenExistsNullType(table.schema)
+
+      // HiveAnalysis will convert CreateTable to CreateTableCommand before 
check
+      case CreateTableCommand(table, _) =>
+        throwWhenExistsNullType(table.schema)
+
+      case ReplaceTable(_, _, tableSchema, _, _, _) =>
+        throwWhenExistsNullType(tableSchema)
+
+      case _ => // OK
 
 Review comment:
   Spark allowed `CreateTableAsSelect` and `ReplaceTableAsSelect ` with 
NullType, isn't it ?
   E.g. `create table test as select null as c1`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to