|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.analysis |
| 19 | + |
| 20 | +import scala.collection.mutable |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.SQLConfHelper |
| 23 | +import org.apache.spark.sql.catalyst.expressions.{Attribute, CreateNamedStruct, Expression, GetStructField, Literal} |
| 24 | +import org.apache.spark.sql.catalyst.plans.logical.Assignment |
| 25 | +import org.apache.spark.sql.catalyst.util.CharVarcharUtils |
| 26 | +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ |
| 27 | +import org.apache.spark.sql.errors.QueryCompilationErrors |
| 28 | +import org.apache.spark.sql.types.{DataType, StructType} |
| 29 | + |
| 30 | +object AssignmentUtils extends SQLConfHelper with CastSupport { |
| 31 | + |
| 32 | + /** |
| 33 | + * Aligns assignments to match table columns. |
| 34 | + * <p> |
| 35 | + * This method processes and reorders given assignments so that each target column gets |
| 36 | + * an expression it should be set to. If a column does not have a matching assignment, |
| 37 | + * it will be set to its current value. For example, if one passes table attributes c1, c2 |
| 38 | + * and an assignment c2 = 1, this method will return c1 = c1, c2 = 1. This allows Spark to |
| 39 | + * construct an updated version of a row. |
| 40 | + * <p> |
| 41 | + * This method also handles updates to nested columns. If there is an assignment to a particular |
| 42 | + * nested field, this method will construct a new struct with one field updated preserving other |
| 43 | + * fields that have not been modified. For example, if one passes table attributes c1, c2 |
| 44 | + * where c2 is a struct with fields n1 and n2 and an assignment c2.n2 = 1, this method will |
| 45 | + * return c1 = c1, c2 = struct(c2.n1, 1). |
| 46 | + * |
| 47 | + * @param attrs table attributes |
| 48 | + * @param assignments assignments to align |
| 49 | + * @return aligned assignments that match table attributes |
| 50 | + */ |
| 51 | + def alignAssignments( |
| 52 | + attrs: Seq[Attribute], |
| 53 | + assignments: Seq[Assignment]): Seq[Assignment] = { |
| 54 | + |
| 55 | + val errors = new mutable.ArrayBuffer[String]() |
| 56 | + |
| 57 | + val output = attrs.map { attr => |
| 58 | + applyAssignments( |
| 59 | + col = restoreActualType(attr), |
| 60 | + colExpr = attr, |
| 61 | + assignments, |
| 62 | + addError = err => errors += err, |
| 63 | + colPath = Seq(attr.name)) |
| 64 | + } |
| 65 | + |
| 66 | + if (errors.nonEmpty) { |
| 67 | + throw QueryCompilationErrors.invalidRowLevelOperationAssignments(assignments, errors.toSeq) |
| 68 | + } |
| 69 | + |
| 70 | + attrs.zip(output).map { case (attr, expr) => Assignment(attr, expr) } |
| 71 | + } |
| 72 | + |
| 73 | + private def restoreActualType(attr: Attribute): Attribute = { |
| 74 | + attr.withDataType(CharVarcharUtils.getRawType(attr.metadata).getOrElse(attr.dataType)) |
| 75 | + } |
| 76 | + |
| 77 | + private def applyAssignments( |
| 78 | + col: Attribute, |
| 79 | + colExpr: Expression, |
| 80 | + assignments: Seq[Assignment], |
| 81 | + addError: String => Unit, |
| 82 | + colPath: Seq[String]): Expression = { |
| 83 | + |
| 84 | + val (exactAssignments, otherAssignments) = assignments.partition { assignment => |
| 85 | + assignment.key.semanticEquals(colExpr) |
| 86 | + } |
| 87 | + |
| 88 | + val fieldAssignments = otherAssignments.filter { assignment => |
| 89 | + assignment.key.exists(_.semanticEquals(colExpr)) |
| 90 | + } |
| 91 | + |
| 92 | + if (exactAssignments.size > 1) { |
| 93 | + val conflictingValuesStr = exactAssignments.map(_.value.sql).mkString(", ") |
| 94 | + addError(s"Multiple assignments for '${colPath.quoted}': $conflictingValuesStr") |
| 95 | + colExpr |
| 96 | + } else if (exactAssignments.nonEmpty && fieldAssignments.nonEmpty) { |
| 97 | + val conflictingAssignments = exactAssignments ++ fieldAssignments |
| 98 | + val conflictingAssignmentsStr = conflictingAssignments.map(_.sql).mkString(", ") |
| 99 | + addError(s"Conflicting assignments for '${colPath.quoted}': $conflictingAssignmentsStr") |
| 100 | + colExpr |
| 101 | + } else if (exactAssignments.isEmpty && fieldAssignments.isEmpty) { |
| 102 | + TableOutputResolver.checkNullability(colExpr, col, conf, colPath) |
| 103 | + } else if (exactAssignments.nonEmpty) { |
| 104 | + val value = exactAssignments.head.value |
| 105 | + TableOutputResolver.resolveUpdate(value, col, conf, addError, colPath) |
| 106 | + } else { |
| 107 | + applyFieldAssignments(col, colExpr, fieldAssignments, addError, colPath) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private def applyFieldAssignments( |
| 112 | + col: Attribute, |
| 113 | + colExpr: Expression, |
| 114 | + assignments: Seq[Assignment], |
| 115 | + addError: String => Unit, |
| 116 | + colPath: Seq[String]): Expression = { |
| 117 | + |
| 118 | + col.dataType match { |
| 119 | + case structType: StructType => |
| 120 | + val fieldAttrs = structType.toAttributes |
| 121 | + val fieldExprs = structType.fields.zipWithIndex.map { case (field, ordinal) => |
| 122 | + GetStructField(colExpr, ordinal, Some(field.name)) |
| 123 | + } |
| 124 | + val updatedFieldExprs = fieldAttrs.zip(fieldExprs).map { case (fieldAttr, fieldExpr) => |
| 125 | + applyAssignments(fieldAttr, fieldExpr, assignments, addError, colPath :+ fieldAttr.name) |
| 126 | + } |
| 127 | + toNamedStruct(structType, updatedFieldExprs) |
| 128 | + |
| 129 | + case otherType => |
| 130 | + addError( |
| 131 | + "Updating nested fields is only supported for StructType but " + |
| 132 | + s"'${colPath.quoted}' is of type $otherType") |
| 133 | + colExpr |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private def toNamedStruct(structType: StructType, fieldExprs: Seq[Expression]): Expression = { |
| 138 | + val namedStructExprs = structType.fields.zip(fieldExprs).flatMap { case (field, expr) => |
| 139 | + Seq(Literal(field.name), expr) |
| 140 | + } |
| 141 | + CreateNamedStruct(namedStructExprs) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Checks whether assignments are aligned and compatible with table columns. |
| 146 | + * |
| 147 | + * @param attrs table attributes |
| 148 | + * @param assignments assignments to check |
| 149 | + * @return true if the assignments are aligned |
| 150 | + */ |
| 151 | + def aligned(attrs: Seq[Attribute], assignments: Seq[Assignment]): Boolean = { |
| 152 | + if (attrs.size != assignments.size) { |
| 153 | + return false |
| 154 | + } |
| 155 | + |
| 156 | + attrs.zip(assignments).forall { case (attr, assignment) => |
| 157 | + val attrType = CharVarcharUtils.getRawType(attr.metadata).getOrElse(attr.dataType) |
| 158 | + val isMatchingAssignment = assignment.key match { |
| 159 | + case key: Attribute if conf.resolver(key.name, attr.name) => true |
| 160 | + case _ => false |
| 161 | + } |
| 162 | + isMatchingAssignment && |
| 163 | + DataType.equalsIgnoreCompatibleNullability(assignment.value.dataType, attrType) && |
| 164 | + (attr.nullable || !assignment.value.nullable) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments