|
| 1 | +/* |
| 2 | + * Copyright (2024) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.spark.sql.delta |
| 18 | + |
| 19 | +import org.apache.spark.SparkThrowable |
| 20 | +import org.apache.spark.sql.{AnalysisException, QueryTest, Row} |
| 21 | +import org.apache.spark.sql.catalyst.TableIdentifier |
| 22 | +import org.apache.spark.sql.delta.actions.{Protocol, TableFeatureProtocolUtils} |
| 23 | +import org.apache.spark.sql.delta.test.DeltaSQLCommandTest |
| 24 | +import org.apache.spark.sql.types.StructType |
| 25 | + |
| 26 | +class DeltaVariantSuite |
| 27 | + extends QueryTest |
| 28 | + with DeltaSQLCommandTest { |
| 29 | + |
| 30 | + private def getProtocolForTable(table: String): Protocol = { |
| 31 | + val deltaLog = DeltaLog.forTable(spark, TableIdentifier(table)) |
| 32 | + deltaLog.unsafeVolatileSnapshot.protocol |
| 33 | + } |
| 34 | + |
| 35 | + test("create a new table with Variant, higher protocol and feature should be picked.") { |
| 36 | + withTable("tbl") { |
| 37 | + sql("CREATE TABLE tbl(s STRING, v VARIANT) USING DELTA") |
| 38 | + sql("INSERT INTO tbl (SELECT 'foo', parse_json(cast(id + 99 as string)) FROM range(1))") |
| 39 | + // TODO(r.chen): Enable once `parse_json` is properly implemented in OSS Spark. |
| 40 | + // assert(spark.table("tbl").selectExpr("v::int").head == Row(99)) |
| 41 | + assert( |
| 42 | + getProtocolForTable("tbl") == |
| 43 | + VariantTypeTableFeature.minProtocolVersion.withFeature(VariantTypeTableFeature) |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + test("creating a table without Variant should use the usual minimum protocol") { |
| 49 | + withTable("tbl") { |
| 50 | + sql("CREATE TABLE tbl(s STRING, i INTEGER) USING DELTA") |
| 51 | + assert(getProtocolForTable("tbl") == Protocol(1, 2)) |
| 52 | + |
| 53 | + val deltaLog = DeltaLog.forTable(spark, TableIdentifier("tbl")) |
| 54 | + assert( |
| 55 | + !deltaLog.unsafeVolatileSnapshot.protocol.isFeatureSupported(VariantTypeTableFeature), |
| 56 | + s"Table tbl contains VariantTypeFeature descriptor when its not supposed to" |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + test("add a new Variant column should upgrade to the correct protocol versions") { |
| 62 | + withTable("tbl") { |
| 63 | + sql("CREATE TABLE tbl(s STRING) USING delta") |
| 64 | + assert(getProtocolForTable("tbl") == Protocol(1, 2)) |
| 65 | + |
| 66 | + // Should throw error |
| 67 | + val e = intercept[SparkThrowable] { |
| 68 | + sql("ALTER TABLE tbl ADD COLUMN v VARIANT") |
| 69 | + } |
| 70 | + // capture the existing protocol here. |
| 71 | + // we will check the error message later in this test as we need to compare the |
| 72 | + // expected schema and protocol |
| 73 | + val deltaLog = DeltaLog.forTable(spark, TableIdentifier("tbl")) |
| 74 | + val currentProtocol = deltaLog.unsafeVolatileSnapshot.protocol |
| 75 | + val currentFeatures = currentProtocol.implicitlyAndExplicitlySupportedFeatures |
| 76 | + .map(_.name) |
| 77 | + .toSeq |
| 78 | + .sorted |
| 79 | + .mkString(", ") |
| 80 | + |
| 81 | + // add table feature |
| 82 | + sql( |
| 83 | + s"ALTER TABLE tbl " + |
| 84 | + s"SET TBLPROPERTIES('delta.feature.variantType-dev' = 'supported')" |
| 85 | + ) |
| 86 | + |
| 87 | + sql("ALTER TABLE tbl ADD COLUMN v VARIANT") |
| 88 | + |
| 89 | + // check previously thrown error message |
| 90 | + checkError( |
| 91 | + e, |
| 92 | + errorClass = "DELTA_FEATURES_REQUIRE_MANUAL_ENABLEMENT", |
| 93 | + parameters = Map( |
| 94 | + "unsupportedFeatures" -> VariantTypeTableFeature.name, |
| 95 | + "supportedFeatures" -> currentFeatures |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + sql("INSERT INTO tbl (SELECT 'foo', parse_json(cast(id + 99 as string)) FROM range(1))") |
| 100 | + // TODO(r.chen): Enable once `parse_json` is properly implemented in OSS Spark. |
| 101 | + // assert(spark.table("tbl").selectExpr("v::int").head == Row(99)) |
| 102 | + |
| 103 | + assert( |
| 104 | + getProtocolForTable("tbl") == |
| 105 | + VariantTypeTableFeature.minProtocolVersion |
| 106 | + .withFeature(VariantTypeTableFeature) |
| 107 | + .withFeature(InvariantsTableFeature) |
| 108 | + .withFeature(AppendOnlyTableFeature) |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments