vladimirg-db commented on code in PR #49029:
URL: https://github.com/apache/spark/pull/49029#discussion_r1881087183


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/resolver/ExpressionResolutionValidator.scala:
##########
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.analysis.resolver
+
+import org.apache.spark.sql.catalyst.expressions.{
+  Alias,
+  ArrayDistinct,
+  ArrayInsert,
+  ArrayJoin,
+  ArrayMax,
+  ArrayMin,
+  ArraysZip,
+  AttributeReference,
+  BinaryExpression,
+  CreateArray,
+  CreateMap,
+  CreateNamedStruct,
+  Expression,
+  ExtractANSIIntervalDays,
+  GetArrayStructFields,
+  GetMapValue,
+  GetStructField,
+  Literal,
+  MapConcat,
+  MapContainsKey,
+  MapEntries,
+  MapFromEntries,
+  MapKeys,
+  MapValues,
+  NamedExpression,
+  Predicate,
+  RuntimeReplaceable,
+  StringRPad,
+  StringToMap,
+  TimeZoneAwareExpression,
+  UnaryMinus
+}
+import org.apache.spark.sql.types.BooleanType
+
+/**
+ * The [[ExpressionResolutionValidator]] performs the validation work on the 
expression tree for the
+ * [[ResolutionValidator]]. These two components work together recursively 
validating the
+ * logical plan. You can find more info in the [[ResolutionValidator]] 
scaladoc.
+ */
+class ExpressionResolutionValidator(resolutionValidator: ResolutionValidator) {
+
+  /**
+   * Validate resolved expression tree. The principle is the same as
+   * [[ResolutionValidator.validate]].
+   */
+  def validate(expression: Expression): Unit = {
+    expression match {
+      case attributeReference: AttributeReference =>
+        validateAttributeReference(attributeReference)
+      case alias: Alias =>
+        validateAlias(alias)
+      case getMapValue: GetMapValue =>
+        validateGetMapValue(getMapValue)
+      case binaryExpression: BinaryExpression =>
+        validateBinaryExpression(binaryExpression)
+      case extractANSIIntervalDay: ExtractANSIIntervalDays =>
+        validateExtractANSIIntervalDays(extractANSIIntervalDay)
+      case literal: Literal =>
+        validateLiteral(literal)
+      case predicate: Predicate =>
+        validatePredicate(predicate)
+      case stringRPad: StringRPad =>
+        validateStringRPad(stringRPad)
+      case unaryMinus: UnaryMinus =>
+        validateUnaryMinus(unaryMinus)
+      case getStructField: GetStructField =>
+        validateGetStructField(getStructField)
+      case createNamedStruct: CreateNamedStruct =>
+        validateCreateNamedStruct(createNamedStruct)
+      case getArrayStructFields: GetArrayStructFields =>
+        validateGetArrayStructFields(getArrayStructFields)
+      case createMap: CreateMap =>
+        validateCreateMap(createMap)
+      case stringToMap: StringToMap =>
+        validateStringToMap(stringToMap)
+      case mapContainsKey: MapContainsKey =>
+        validateMapContainsKey(mapContainsKey)
+      case mapConcat: MapConcat =>
+        validateMapConcat(mapConcat)
+      case mapKeys: MapKeys =>
+        validateMapKeys(mapKeys)
+      case mapValues: MapValues =>
+        validateMapValues(mapValues)
+      case mapEntries: MapEntries =>
+        validateMapEntries(mapEntries)
+      case mapFromEntries: MapFromEntries =>
+        validateMapFromEntries(mapFromEntries)
+      case createArray: CreateArray =>
+        validateCreateArray(createArray)
+      case arrayDistinct: ArrayDistinct =>
+        validateArrayDistinct(arrayDistinct)
+      case arrayInsert: ArrayInsert =>
+        validateArrayInsert(arrayInsert)
+      case arrayJoin: ArrayJoin =>
+        validateArrayJoin(arrayJoin)
+      case arrayMax: ArrayMax =>
+        validateArrayMax(arrayMax)
+      case arrayMin: ArrayMin =>
+        validateArrayMin(arrayMin)
+      case arraysZip: ArraysZip =>
+        validateArraysZip(arraysZip)
+      case runtimeReplaceable: RuntimeReplaceable =>
+        validateRuntimeReplaceable(runtimeReplaceable)
+      case timezoneExpression: TimeZoneAwareExpression =>
+        validateTimezoneExpression(timezoneExpression)
+    }
+  }
+
+  def validateProjectList(projectList: Seq[NamedExpression]): Unit = {
+    projectList.foreach(expression => {
+      expression match {
+        case attributeReference: AttributeReference =>
+          validateAttributeReference(attributeReference)
+        case alias: Alias =>
+          validateAlias(alias)
+      }
+    })
+  }
+
+  private def validatePredicate(predicate: Predicate) = {
+    predicate.children.foreach(validate)
+    assert(
+      predicate.dataType == BooleanType,
+      s"Output type of a predicate must be a boolean, but got: 
${predicate.dataType.typeName}"
+    )
+    assert(
+      predicate.checkInputDataTypes().isSuccess,
+      s"Input types of a predicate must be valid, but got: " +
+      s"${predicate.children.map(_.dataType.typeName).mkString(", ")}"
+    )
+  }
+
+  private def validateStringRPad(stringRPad: StringRPad) = {
+    validate(stringRPad.first)
+    validate(stringRPad.second)
+    validate(stringRPad.third)
+    assert(
+      stringRPad.checkInputDataTypes().isSuccess,
+      s"Input types of rpad must be valid, but got: " +
+      s"${stringRPad.children.map(_.dataType.typeName).mkString(", ")}"
+    )
+  }
+
+  private def validateAttributeReference(attributeReference: 
AttributeReference): Unit = {
+    assert(
+      resolutionValidator.attributeScopeStack.top.contains(attributeReference),
+      s"Attribute $attributeReference is missing from attribute scope: " +
+      s"${resolutionValidator.attributeScopeStack.top}"
+    )
+  }
+
+  private def validateAlias(alias: Alias): Unit = {
+    validate(alias.child)
+  }
+
+  private def validateBinaryExpression(binaryExpression: BinaryExpression): 
Unit = {
+    validate(binaryExpression.left)
+    validate(binaryExpression.right)
+    assert(
+      binaryExpression.checkInputDataTypes().isSuccess,
+      s"Input types of a binary expression must be valid, but got: " +
+      s"${binaryExpression.children.map(_.dataType.typeName).mkString(", ")}"
+    )
+
+    binaryExpression match {
+      case timezoneExpression: TimeZoneAwareExpression =>
+        assert(timezoneExpression.timeZoneId.nonEmpty, "Timezone expression 
must have a timezone")
+      case _ =>
+    }
+  }
+
+  private def validateExtractANSIIntervalDays(
+      extractANSIIntervalDays: ExtractANSIIntervalDays): Unit = {
+    validate(extractANSIIntervalDays.child)
+  }
+
+  private def validateLiteral(literal: Literal): Unit = {}

Review Comment:
   We have this requirement here: 
https://github.com/apache/spark/pull/49029/files#diff-6043a2614eb32ecd44f6903e28f5fbc786705fe8ba44daebe3ba851df3b9f477R58.
   
   This is a forcing factor for developers to add validation to the 
`ExpressionResolutionValidator`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to