You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/07/24 12:52:16 UTC

[GitHub] [spark] gengliangwang commented on a change in pull request #25239: [WIP][SPARK-28495][SQL] Table insertion: follow store assignment rules of ANSI SQL

gengliangwang commented on a change in pull request #25239: [WIP][SPARK-28495][SQL] Table insertion: follow store assignment rules of ANSI SQL
URL: https://github.com/apache/spark/pull/25239#discussion_r306791840
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ##########
 @@ -2665,6 +2666,38 @@ class Analyzer(
       }
     }
   }
+
+  /**
+   * Replace the [[AssignableCast]] expression by [[Cast]], and throw exceptions if the cast is
+   * not assignable.
+   */
+  object ResolveAssignableCast extends Rule[LogicalPlan] {
+    private def fail(from: Expression, to: DataType, walkedTypePath: Seq[String]) = {
+      val fromStr = from match {
+        case l: LambdaVariable => "array element"
+        case e => e.sql
+      }
+      throw new AnalysisException(s"Cannot assign $fromStr from " +
+        s"${from.dataType.catalogString} to ${to.catalogString}.\n" +
+        "The type path of the target object is:\n" + walkedTypePath.mkString("", "\n", "\n") +
+        "You can add an explicit cast to the input data.")
+    }
+
+    def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp {
+      case p if !p.childrenResolved => p
+      case p if p.resolved => p
+
+      case p => p transformExpressions {
+        case u @ AssignableCast(child, _, _) if !child.resolved => u
+
+        case AssignableCast(child, dataType, walkedTypePath)
+          if !Cast.canAssign(child.dataType, dataType) =>
+          fail(child, dataType, walkedTypePath)
+
+        case AssignableCast(child, dataType, _) => Cast(child, dataType.asNullable)
 
 Review comment:
   @maropu nice catch. It seems that it is an existing issue in Cast. We can fix it first.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org