|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.spark.sql; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import org.apache.iceberg.AssertHelpers; |
| 24 | +import org.apache.iceberg.catalog.Namespace; |
| 25 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 26 | +import org.apache.iceberg.hadoop.HadoopCatalog; |
| 27 | +import org.apache.iceberg.spark.SparkCatalogTestBase; |
| 28 | +import org.apache.iceberg.types.Types; |
| 29 | +import org.apache.iceberg.types.Types.NestedField; |
| 30 | +import org.apache.spark.sql.AnalysisException; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Assume; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class TestAlterTable extends SparkCatalogTestBase { |
| 38 | + private final TableIdentifier renamedIdent = TableIdentifier.of(Namespace.of("default"), "table2"); |
| 39 | + |
| 40 | + public TestAlterTable(String catalogName, String implementation, Map<String, String> config) { |
| 41 | + super(catalogName, implementation, config); |
| 42 | + } |
| 43 | + |
| 44 | + @Before |
| 45 | + public void createTable() { |
| 46 | + sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", tableName); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void removeTable() { |
| 51 | + sql("DROP TABLE IF EXISTS %s", tableName); |
| 52 | + sql("DROP TABLE IF EXISTS %s2", tableName); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testAddColumn() { |
| 57 | + sql("ALTER TABLE %s ADD COLUMN point struct<x: double NOT NULL, y: double NOT NULL> AFTER id", tableName); |
| 58 | + |
| 59 | + Types.StructType expectedSchema = Types.StructType.of( |
| 60 | + NestedField.required(1, "id", Types.LongType.get()), |
| 61 | + NestedField.optional(3, "point", Types.StructType.of( |
| 62 | + NestedField.required(4, "x", Types.DoubleType.get()), |
| 63 | + NestedField.required(5, "y", Types.DoubleType.get()) |
| 64 | + )), |
| 65 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 66 | + |
| 67 | + Assert.assertEquals("Schema should match expected", |
| 68 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 69 | + |
| 70 | + sql("ALTER TABLE %s ADD COLUMN point.z double COMMENT 'May be null' FIRST", tableName); |
| 71 | + |
| 72 | + Types.StructType expectedSchema2 = Types.StructType.of( |
| 73 | + NestedField.required(1, "id", Types.LongType.get()), |
| 74 | + NestedField.optional(3, "point", Types.StructType.of( |
| 75 | + NestedField.optional(6, "z", Types.DoubleType.get(), "May be null"), |
| 76 | + NestedField.required(4, "x", Types.DoubleType.get()), |
| 77 | + NestedField.required(5, "y", Types.DoubleType.get()) |
| 78 | + )), |
| 79 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 80 | + |
| 81 | + Assert.assertEquals("Schema should match expected", |
| 82 | + expectedSchema2, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testDropColumn() { |
| 87 | + sql("ALTER TABLE %s DROP COLUMN data", tableName); |
| 88 | + |
| 89 | + Types.StructType expectedSchema = Types.StructType.of( |
| 90 | + NestedField.required(1, "id", Types.LongType.get())); |
| 91 | + |
| 92 | + Assert.assertEquals("Schema should match expected", |
| 93 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testRenameColumn() { |
| 98 | + sql("ALTER TABLE %s RENAME COLUMN id TO row_id", tableName); |
| 99 | + |
| 100 | + Types.StructType expectedSchema = Types.StructType.of( |
| 101 | + NestedField.required(1, "row_id", Types.LongType.get()), |
| 102 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 103 | + |
| 104 | + Assert.assertEquals("Schema should match expected", |
| 105 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testAlterColumnComment() { |
| 110 | + sql("ALTER TABLE %s ALTER COLUMN id COMMENT 'Record id'", tableName); |
| 111 | + |
| 112 | + Types.StructType expectedSchema = Types.StructType.of( |
| 113 | + NestedField.required(1, "id", Types.LongType.get(), "Record id"), |
| 114 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 115 | + |
| 116 | + Assert.assertEquals("Schema should match expected", |
| 117 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testAlterColumnType() { |
| 122 | + sql("ALTER TABLE %s ADD COLUMN count int", tableName); |
| 123 | + sql("ALTER TABLE %s ALTER COLUMN count TYPE bigint", tableName); |
| 124 | + |
| 125 | + Types.StructType expectedSchema = Types.StructType.of( |
| 126 | + NestedField.required(1, "id", Types.LongType.get()), |
| 127 | + NestedField.optional(2, "data", Types.StringType.get()), |
| 128 | + NestedField.optional(3, "count", Types.LongType.get())); |
| 129 | + |
| 130 | + Assert.assertEquals("Schema should match expected", |
| 131 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testAlterColumnDropNotNull() { |
| 136 | + sql("ALTER TABLE %s ALTER COLUMN id DROP NOT NULL", tableName); |
| 137 | + |
| 138 | + Types.StructType expectedSchema = Types.StructType.of( |
| 139 | + NestedField.optional(1, "id", Types.LongType.get()), |
| 140 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 141 | + |
| 142 | + Assert.assertEquals("Schema should match expected", |
| 143 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testAlterColumnSetNotNull() { |
| 148 | + // no-op changes are allowed |
| 149 | + sql("ALTER TABLE %s ALTER COLUMN id SET NOT NULL", tableName); |
| 150 | + |
| 151 | + Types.StructType expectedSchema = Types.StructType.of( |
| 152 | + NestedField.required(1, "id", Types.LongType.get()), |
| 153 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 154 | + |
| 155 | + Assert.assertEquals("Schema should match expected", |
| 156 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 157 | + |
| 158 | + AssertHelpers.assertThrows("Should reject adding NOT NULL constraint to an optional column", |
| 159 | + AnalysisException.class, "Cannot change nullable column to non-nullable: data", |
| 160 | + () -> sql("ALTER TABLE %s ALTER COLUMN data SET NOT NULL", tableName)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testAlterColumnPositionAfter() { |
| 165 | + sql("ALTER TABLE %s ADD COLUMN count int", tableName); |
| 166 | + sql("ALTER TABLE %s ALTER COLUMN count AFTER id", tableName); |
| 167 | + |
| 168 | + Types.StructType expectedSchema = Types.StructType.of( |
| 169 | + NestedField.required(1, "id", Types.LongType.get()), |
| 170 | + NestedField.optional(3, "count", Types.IntegerType.get()), |
| 171 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 172 | + |
| 173 | + Assert.assertEquals("Schema should match expected", |
| 174 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testAlterColumnPositionFirst() { |
| 179 | + sql("ALTER TABLE %s ADD COLUMN count int", tableName); |
| 180 | + sql("ALTER TABLE %s ALTER COLUMN count FIRST", tableName); |
| 181 | + |
| 182 | + Types.StructType expectedSchema = Types.StructType.of( |
| 183 | + NestedField.optional(3, "count", Types.IntegerType.get()), |
| 184 | + NestedField.required(1, "id", Types.LongType.get()), |
| 185 | + NestedField.optional(2, "data", Types.StringType.get())); |
| 186 | + |
| 187 | + Assert.assertEquals("Schema should match expected", |
| 188 | + expectedSchema, validationCatalog.loadTable(tableIdent).schema().asStruct()); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testTableRename() { |
| 193 | + Assume.assumeFalse("Hadoop catalog does not support rename", validationCatalog instanceof HadoopCatalog); |
| 194 | + |
| 195 | + Assert.assertTrue("Initial name should exist", validationCatalog.tableExists(tableIdent)); |
| 196 | + Assert.assertFalse("New name should not exist", validationCatalog.tableExists(renamedIdent)); |
| 197 | + |
| 198 | + sql("ALTER TABLE %s RENAME TO %s2", tableName, tableName); |
| 199 | + |
| 200 | + Assert.assertFalse("Initial name should not exist", validationCatalog.tableExists(tableIdent)); |
| 201 | + Assert.assertTrue("New name should exist", validationCatalog.tableExists(renamedIdent)); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testSetTableProperties() { |
| 206 | + sql("ALTER TABLE %s SET TBLPROPERTIES ('prop'='value')", tableName); |
| 207 | + |
| 208 | + Assert.assertEquals("Should have the new table property", |
| 209 | + "value", validationCatalog.loadTable(tableIdent).properties().get("prop")); |
| 210 | + |
| 211 | + sql("ALTER TABLE %s UNSET TBLPROPERTIES ('prop')", tableName); |
| 212 | + |
| 213 | + Assert.assertNull("Should not have the removed table property", |
| 214 | + validationCatalog.loadTable(tableIdent).properties().get("prop")); |
| 215 | + } |
| 216 | +} |
0 commit comments