|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.spark.catalyst.analysis |
| 20 | + |
| 21 | +import org.apache.paimon.spark.SparkTypeUtils.CURRENT_DEFAULT_COLUMN_METADATA_KEY |
| 22 | +import org.apache.paimon.spark.catalyst.analysis.expressions.ExpressionHelper |
| 23 | + |
| 24 | +import org.apache.spark.sql.SparkSession |
| 25 | +import org.apache.spark.sql.catalyst.SQLConfHelper |
| 26 | +import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, CreateNamedStruct, Expression, GetStructField, Literal, NamedExpression} |
| 27 | +import org.apache.spark.sql.catalyst.plans.logical.{Assignment, DeleteAction, InsertAction, InsertStarAction, MergeAction, MergeIntoTable, UpdateAction, UpdateStarAction} |
| 28 | +import org.apache.spark.sql.types.StructType |
| 29 | + |
| 30 | +trait AssignmentAlignmentHelper extends SQLConfHelper with ExpressionHelper { |
| 31 | + |
| 32 | + private lazy val resolver = conf.resolver |
| 33 | + |
| 34 | + /** |
| 35 | + * @param ref |
| 36 | + * attribute reference seq, e.g. a => Seq["a"], s.c1 => Seq["s", "c1"] |
| 37 | + * @param expr |
| 38 | + * update expression |
| 39 | + */ |
| 40 | + private case class AttrUpdate(ref: Seq[String], expr: Expression) |
| 41 | + |
| 42 | + /** |
| 43 | + * Generate aligned expressions, only supports PrimitiveType and StructType. For example, if attrs |
| 44 | + * are [a int, b int, s struct(c1 int, c2 int)] and update assignments are [a = 1, s.c1 = 2], will |
| 45 | + * return [1, b, struct(2, c2)]. |
| 46 | + * @param attrs |
| 47 | + * target attrs |
| 48 | + * @param assignments |
| 49 | + * update assignments |
| 50 | + * @return |
| 51 | + * aligned expressions |
| 52 | + */ |
| 53 | + protected def generateAlignedExpressions( |
| 54 | + attrs: Seq[Attribute], |
| 55 | + assignments: Seq[Assignment], |
| 56 | + isInsert: Boolean = false): Seq[Expression] = { |
| 57 | + val attrUpdates = assignments.map(a => AttrUpdate(toRefSeq(a.key), a.value)) |
| 58 | + recursiveAlignUpdates(attrs, attrUpdates, Nil, isInsert) |
| 59 | + } |
| 60 | + |
| 61 | + protected def alignAssignments( |
| 62 | + attrs: Seq[Attribute], |
| 63 | + assignments: Seq[Assignment], |
| 64 | + isInsert: Boolean = false): Seq[Assignment] = { |
| 65 | + generateAlignedExpressions(attrs, assignments, isInsert).zip(attrs).map { |
| 66 | + case (expression, field) => Assignment(field, expression) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Align assignments in a MergeAction based on the target table's output attributes. |
| 72 | + * - DeleteAction: returns as-is |
| 73 | + * - UpdateAction: aligns assignments for update |
| 74 | + * - InsertAction: aligns assignments for insert |
| 75 | + */ |
| 76 | + protected def alignMergeAction(action: MergeAction, targetOutput: Seq[Attribute]): MergeAction = { |
| 77 | + action match { |
| 78 | + case d @ DeleteAction(_) => d |
| 79 | + case u @ PaimonUpdateAction(_, assignments) => |
| 80 | + u.copy(assignments = alignAssignments(targetOutput, assignments)) |
| 81 | + case i @ InsertAction(_, assignments) => |
| 82 | + i.copy(assignments = alignAssignments(targetOutput, assignments, isInsert = true)) |
| 83 | + case _: UpdateStarAction => |
| 84 | + throw new RuntimeException("UpdateStarAction should not be here.") |
| 85 | + case _: InsertStarAction => |
| 86 | + throw new RuntimeException("InsertStarAction should not be here.") |
| 87 | + case _ => |
| 88 | + throw new RuntimeException(s"Can't recognize this action: $action") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private def recursiveAlignUpdates( |
| 93 | + targetAttrs: Seq[NamedExpression], |
| 94 | + updates: Seq[AttrUpdate], |
| 95 | + namePrefix: Seq[String] = Nil, |
| 96 | + isInsert: Boolean = false): Seq[Expression] = { |
| 97 | + |
| 98 | + // build aligned updated expression for each target attr |
| 99 | + targetAttrs.map { |
| 100 | + targetAttr => |
| 101 | + val headMatchedUpdates = updates.filter(u => resolver(u.ref.head, targetAttr.name)) |
| 102 | + if (headMatchedUpdates.isEmpty) { |
| 103 | + if (isInsert) { |
| 104 | + // For Insert, use default value or NULL for missing columns |
| 105 | + getDefaultValueOrNull(targetAttr) |
| 106 | + } else { |
| 107 | + // For Update, return the attr as is |
| 108 | + targetAttr |
| 109 | + } |
| 110 | + } else { |
| 111 | + val exactMatchedUpdate = headMatchedUpdates.find(_.ref.size == 1) |
| 112 | + if (exactMatchedUpdate.isDefined) { |
| 113 | + if (headMatchedUpdates.size == 1) { |
| 114 | + // when an exact match (no nested fields) occurs, it must be the only match, then return it's expr |
| 115 | + castIfNeeded(exactMatchedUpdate.get.expr, targetAttr.dataType) |
| 116 | + } else { |
| 117 | + // otherwise, there must be conflicting updates, for example: |
| 118 | + // - update the same attr multiple times |
| 119 | + // - update a struct attr and its fields at the same time (e.g. s and s.c1) |
| 120 | + val conflictingAttrNames = |
| 121 | + headMatchedUpdates.map(u => (namePrefix ++ u.ref).mkString(".")).distinct |
| 122 | + throw new UnsupportedOperationException( |
| 123 | + s"Conflicting update/insert on attrs: ${conflictingAttrNames.mkString(", ")}" |
| 124 | + ) |
| 125 | + } |
| 126 | + } else { |
| 127 | + targetAttr.dataType match { |
| 128 | + case StructType(fields) => |
| 129 | + val fieldExprs = fields.zipWithIndex.map { |
| 130 | + case (field, ordinal) => |
| 131 | + Alias(GetStructField(targetAttr, ordinal, Some(field.name)), field.name)() |
| 132 | + } |
| 133 | + val newUpdates = updates.map(u => u.copy(ref = u.ref.tail)) |
| 134 | + // process StructType's nested fields recursively |
| 135 | + val updatedFieldExprs = |
| 136 | + recursiveAlignUpdates( |
| 137 | + fieldExprs, |
| 138 | + newUpdates, |
| 139 | + namePrefix :+ targetAttr.name, |
| 140 | + isInsert) |
| 141 | + |
| 142 | + // build updated struct expression |
| 143 | + CreateNamedStruct(fields.zip(updatedFieldExprs).flatMap { |
| 144 | + case (field, expr) => |
| 145 | + Seq(Literal(field.name), expr) |
| 146 | + }) |
| 147 | + case _ => |
| 148 | + // can't reach here |
| 149 | + throw new UnsupportedOperationException("") |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** Get the default value expression for an attribute, or NULL if no default value is defined. */ |
| 157 | + private def getDefaultValueOrNull(attr: NamedExpression): Expression = { |
| 158 | + attr match { |
| 159 | + case a: Attribute if a.metadata.contains(CURRENT_DEFAULT_COLUMN_METADATA_KEY) => |
| 160 | + val defaultValueStr = a.metadata.getString(CURRENT_DEFAULT_COLUMN_METADATA_KEY) |
| 161 | + parseAndResolveDefaultValue(defaultValueStr, a) |
| 162 | + case _ => |
| 163 | + Literal(null, attr.dataType) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** Parse the default value string and resolve it to an expression. */ |
| 168 | + private def parseAndResolveDefaultValue(defaultValueStr: String, attr: Attribute): Expression = { |
| 169 | + try { |
| 170 | + val spark = SparkSession.active |
| 171 | + val parsed = spark.sessionState.sqlParser.parseExpression(defaultValueStr) |
| 172 | + castIfNeeded(parsed, attr.dataType) |
| 173 | + } catch { |
| 174 | + case _: Exception => |
| 175 | + // If parsing fails, fall back to NULL |
| 176 | + Literal(null, attr.dataType) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments