You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "johanl-db (via GitHub)" <gi...@apache.org> on 2023/03/13 15:27:34 UTC

[GitHub] [spark] johanl-db commented on a diff in pull request #40308: [SPARK-42151][SQL] Align UPDATE assignments with table attributes

johanl-db commented on code in PR #40308:
URL: https://github.com/apache/spark/pull/40308#discussion_r1134165086


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/AlignUpdateAssignmentsSuite.scala:
##########
@@ -0,0 +1,786 @@
+/*
+ * 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.execution.command
+
+import java.util.Collections
+
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito.{mock, when}
+import org.mockito.invocation.InvocationOnMock
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, Analyzer, FunctionRegistry, NoSuchTableException, ResolveSessionCatalog}
+import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog}
+import org.apache.spark.sql.catalyst.expressions.{ArrayTransform, AttributeReference, BooleanLiteral, Cast, CheckOverflowInTableInsert, CreateNamedStruct, EvalMode, GetStructField, IntegerLiteral, LambdaFunction, LongLiteral, MapFromArrays, StringLiteral}
+import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan, UpdateTable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogNotFoundException, CatalogV2Util, Column, ColumnDefaultValue, Identifier, Table, TableCapability, TableCatalog}
+import org.apache.spark.sql.connector.expressions.{LiteralValue, Transform}
+import org.apache.spark.sql.execution.datasources.v2.V2SessionCatalog
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.internal.SQLConf.StoreAssignmentPolicy
+import org.apache.spark.sql.types.{BooleanType, IntegerType, StructType}
+
+class AlignUpdateAssignmentsSuite extends AnalysisTest {
+
+  private val primitiveTable: Table = {
+    val t = mock(classOf[Table])
+    val schema = new StructType()
+      .add("i", "INT", nullable = false)
+      .add("l", "LONG")
+      .add("txt", "STRING")
+    when(t.columns()).thenReturn(CatalogV2Util.structTypeToV2Columns(schema))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    t
+  }
+
+  private val nestedStructTable: Table = {
+    val t = mock(classOf[Table])
+    val schema = new StructType()
+      .add("i", "INT")
+      .add(
+        "s",
+        "STRUCT<n_i: INT NOT NULL, n_s: STRUCT<dn_i: INT NOT NULL, dn_l: LONG>>",
+        nullable = false)
+      .add("txt", "STRING")
+    when(t.columns()).thenReturn(CatalogV2Util.structTypeToV2Columns(schema))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    t
+  }
+
+  private val mapArrayTable: Table = {
+    val t = mock(classOf[Table])
+    val schema = new StructType()
+      .add("i", "INT")
+      .add("a", "ARRAY<STRUCT<ac_1: INT, ac_2: INT>>")
+      .add("m", "MAP<STRING, STRING>")
+      .add("txt", "STRING")
+    when(t.columns()).thenReturn(CatalogV2Util.structTypeToV2Columns(schema))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    t
+  }
+
+  private val charVarcharTable: Table = {
+    val t = mock(classOf[Table])
+    val schema = new StructType()
+      .add("c", "CHAR(5)")
+      .add(
+        "s",
+        "STRUCT<n_i: INT, n_vc: VARCHAR(5)>",
+        nullable = false)
+      .add(
+        "a",
+        "ARRAY<STRUCT<n_i: INT, n_vc: VARCHAR(5)>>",
+        nullable = false)
+      .add(
+        "mk",
+        "MAP<STRUCT<n_i: INT, n_vc: VARCHAR(5)>, STRING>",
+        nullable = false)
+      .add(
+        "mv",
+        "MAP<STRING, STRUCT<n_i: INT, n_vc: VARCHAR(5)>>",
+        nullable = false)
+    when(t.columns()).thenReturn(CatalogV2Util.structTypeToV2Columns(schema))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    t
+  }
+
+  private val acceptsAnySchemaTable: Table = {
+    val t = mock(classOf[Table])
+    val schema = new StructType()
+      .add("i", "INT", nullable = false)
+      .add("l", "LONG")
+      .add("txt", "STRING")
+    when(t.columns()).thenReturn(CatalogV2Util.structTypeToV2Columns(schema))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    when(t.capabilities()).thenReturn(Collections.singleton(TableCapability.ACCEPT_ANY_SCHEMA))
+    t
+  }
+
+  private val defaultValuesTable: Table = {
+    val t = mock(classOf[Table])
+    val iDefault = new ColumnDefaultValue("42", LiteralValue(42, IntegerType))
+    when(t.columns()).thenReturn(Array(
+      Column.create("b", BooleanType, true, null, null),
+      Column.create("i", IntegerType, true, null, iDefault, null)))
+    when(t.partitioning()).thenReturn(Array.empty[Transform])
+    t
+  }
+
+  private val v2Catalog: TableCatalog = {
+    val newCatalog = mock(classOf[TableCatalog])
+    when(newCatalog.loadTable(any())).thenAnswer((invocation: InvocationOnMock) => {
+      val ident = invocation.getArgument[Identifier](0)
+      ident.name match {
+        case "primitive_table" => primitiveTable
+        case "nested_struct_table" => nestedStructTable
+        case "map_array_table" => mapArrayTable
+        case "char_varchar_table" => charVarcharTable
+        case "accepts_any_schema_table" => acceptsAnySchemaTable
+        case "default_values_table" => defaultValuesTable
+        case name => throw new NoSuchTableException(Seq(name))
+      }
+    })
+    when(newCatalog.name()).thenReturn("cat")
+    newCatalog
+  }
+
+  private val v1SessionCatalog: SessionCatalog = new SessionCatalog(
+    new InMemoryCatalog(),
+    FunctionRegistry.builtin,
+    new SQLConf())
+
+  private val v2SessionCatalog: TableCatalog = new V2SessionCatalog(v1SessionCatalog)
+
+  private val catalogManager: CatalogManager = {
+    val manager = mock(classOf[CatalogManager])
+    when(manager.catalog(any())).thenAnswer((invocation: InvocationOnMock) => {
+      invocation.getArgument[String](0) match {
+        case "testcat" => v2Catalog
+        case CatalogManager.SESSION_CATALOG_NAME => v2SessionCatalog
+        case name => throw new CatalogNotFoundException(s"No such catalog: $name")
+      }
+    })
+    when(manager.currentCatalog).thenReturn(v2Catalog)
+    when(manager.currentNamespace).thenReturn(Array.empty[String])
+    when(manager.v1SessionCatalog).thenReturn(v1SessionCatalog)
+    when(manager.v2SessionCatalog).thenReturn(v2SessionCatalog)
+    manager
+  }
+
+  test("align assignments (primitive types)") {
+    val sql1 = "UPDATE primitive_table SET txt = 'new', i = 1"
+    parseAndAlignAssignments(sql1) match {
+      case Seq(
+          Assignment(i: AttributeReference, IntegerLiteral(1)),
+          Assignment(l: AttributeReference, lValue: AttributeReference),
+          Assignment(txt: AttributeReference, StringLiteral("new"))) =>
+
+        assert(i.name == "i")
+        assert(l.name == "l" && l == lValue)
+        assert(txt.name == "txt")
+
+      case assignments =>
+        fail(s"Unexpected assignments: $assignments")
+    }
+
+    val sql2 = "UPDATE primitive_table SET l = 10L"
+    parseAndAlignAssignments(sql2) match {
+      case Seq(
+          Assignment(i: AttributeReference, iValue: AttributeReference),
+          Assignment(l: AttributeReference, LongLiteral(10L)),
+          Assignment(txt: AttributeReference, txtValue: AttributeReference)) =>
+
+        assert(i.name == "i" && i == iValue)
+        assert(l.name == "l")
+        assert(txt.name == "txt" && txt == txtValue)
+
+      case assignments =>
+        fail(s"Unexpected assignments: $assignments")
+    }
+
+    val sql3 = "UPDATE primitive_table AS t SET t.txt = 'new', t.l = 10L, t.i = -1"
+    parseAndAlignAssignments(sql3) match {
+      case Seq(
+          Assignment(i: AttributeReference, IntegerLiteral(-1)),
+          Assignment(l: AttributeReference, LongLiteral(10L)),
+          Assignment(txt: AttributeReference, StringLiteral("new"))) =>
+
+        assert(i.name == "i")
+        assert(l.name == "l")
+        assert(txt.name == "txt")
+
+      case assignments =>
+        fail(s"Unexpected assignments: $assignments")
+    }
+  }
+
+  test("align assignments (structs)") {

Review Comment:
   There should be a test case for `UPDATE nested_struct_table SET s.n_i = 1"` that ensures the struct `s.n_s` is preserved as a whole instead of recursing and generating assignments for each of its children.
   
   This is important if `s.n_s` contain null values: the assignments must be (`s.n_i = 1`, `s.n_s = s.n_s`), not (`s.n_i = 1`, `s.n_s.dn_i = s.n_s.dn_i`, `s.n_s.dn_l = s.n_s.dn_l`) so that `s.n_s` is still null after the update.
   



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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