|
| 1 | +/* |
| 2 | + * Copyright (2021) 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 | +package io.delta.kernel.defaults |
| 17 | + |
| 18 | +import io.delta.kernel.Table |
| 19 | +import io.delta.kernel.exceptions.UnknownConfigurationException |
| 20 | +import io.delta.kernel.internal.SnapshotImpl |
| 21 | +import io.delta.kernel.utils.CloseableIterable.emptyIterable |
| 22 | + |
| 23 | +import scala.collection.immutable.Seq |
| 24 | + |
| 25 | +/** |
| 26 | + * Suite to set or get table properties. |
| 27 | + * TODO: for now we just have the support for `set`. API `get` will be added in the next PRs. |
| 28 | + */ |
| 29 | +class TablePropertiesSuite extends DeltaTableWriteSuiteBase { |
| 30 | + test("create/update table - allow arbitrary properties") { |
| 31 | + withTempDir { tempFile => |
| 32 | + val tablePath = tempFile.getAbsolutePath |
| 33 | + |
| 34 | + // create table with arbitrary properties and check if they are set |
| 35 | + createUpdateTableWithProps( |
| 36 | + tablePath, |
| 37 | + createTable = true, |
| 38 | + props = Map("my key" -> "10", "my key2" -> "20") |
| 39 | + ) |
| 40 | + assertHasProp(tablePath, expProps = Map("my key" -> "10", "my key2" -> "20")) |
| 41 | + |
| 42 | + // update table by modifying the arbitrary properties and check if they are updated |
| 43 | + createUpdateTableWithProps(tablePath, props = Map("my key" -> "30")) |
| 44 | + assertHasProp(tablePath, expProps = Map("my key" -> "30", "my key2" -> "20")) |
| 45 | + |
| 46 | + // update table without any new properties and check if the existing properties are retained |
| 47 | + createUpdateTableWithProps(tablePath) |
| 48 | + assertHasProp(tablePath, expProps = Map("my key" -> "30", "my key2" -> "20")) |
| 49 | + |
| 50 | + // update table by adding new arbitrary properties and check if they are set |
| 51 | + createUpdateTableWithProps(tablePath, props = Map("new key3" -> "str")) |
| 52 | + assertHasProp( |
| 53 | + tablePath, |
| 54 | + expProps = Map("my key" -> "30", "my key2" -> "20", "new key3" -> "str")) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + test("create/update table - disallow unknown delta.* properties to Kernel") { |
| 59 | + withTempDir { tempFile => |
| 60 | + val tablePath = tempFile.getAbsolutePath |
| 61 | + val ex1 = intercept[UnknownConfigurationException] { |
| 62 | + createUpdateTableWithProps(tablePath, createTable = true, Map("delta.unknown" -> "str")) |
| 63 | + } |
| 64 | + assert(ex1.getMessage.contains("Unknown configuration was specified: delta.unknown")) |
| 65 | + |
| 66 | + // Try updating in an existing table |
| 67 | + createUpdateTableWithProps(tablePath, createTable = true) |
| 68 | + val ex2 = intercept[UnknownConfigurationException] { |
| 69 | + createUpdateTableWithProps(tablePath, props = Map("Delta.unknown" -> "str")) |
| 70 | + } |
| 71 | + assert(ex2.getMessage.contains("Unknown configuration was specified: Delta.unknown")) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + test("create/update table - delta configs are stored with same case as defined in TableConfig") { |
| 76 | + withTempDir { tempFile => |
| 77 | + val tablePath = tempFile.getAbsolutePath |
| 78 | + createUpdateTableWithProps(tablePath, |
| 79 | + createTable = true, |
| 80 | + Map("delta.CHECKPOINTINTERVAL" -> "20")) |
| 81 | + assertHasProp(tablePath, expProps = Map("delta.checkpointInterval" -> "20")) |
| 82 | + |
| 83 | + // Try updating in an existing table |
| 84 | + createUpdateTableWithProps( |
| 85 | + tablePath, |
| 86 | + props = Map("DELTA.CHECKPOINTINTERVAL" -> "30")) |
| 87 | + assertHasProp(tablePath, expProps = Map("delta.checkpointInterval" -> "30")) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def createUpdateTableWithProps( |
| 92 | + tablePath: String, |
| 93 | + createTable: Boolean = false, |
| 94 | + props: Map[String, String] = null): Unit = { |
| 95 | + createTxn(defaultEngine, tablePath, createTable, testSchema, Seq.empty, props) |
| 96 | + .commit(defaultEngine, emptyIterable()) |
| 97 | + } |
| 98 | + |
| 99 | + // TODO: this will be replaced with get API in the next PRs. |
| 100 | + def assertHasProp(tablePath: String, expProps: Map[String, String]): Unit = { |
| 101 | + val snapshot = Table.forPath(defaultEngine, tablePath) |
| 102 | + .getLatestSnapshot(defaultEngine).asInstanceOf[SnapshotImpl] |
| 103 | + expProps.foreach { case (key, value) => |
| 104 | + assert(snapshot.getMetadata.getConfiguration.get(key) === value, key) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments