|
| 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 | +package org.apache.iceberg.spark.extensions; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import org.apache.iceberg.ParameterizedTestExtension; |
| 26 | +import org.apache.iceberg.Parameters; |
| 27 | +import org.apache.iceberg.catalog.Catalog; |
| 28 | +import org.apache.iceberg.catalog.Namespace; |
| 29 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 30 | +import org.apache.iceberg.catalog.ViewCatalog; |
| 31 | +import org.apache.iceberg.spark.Spark3Util; |
| 32 | +import org.apache.iceberg.spark.SparkCatalogConfig; |
| 33 | +import org.apache.iceberg.view.ImmutableSQLViewRepresentation; |
| 34 | +import org.apache.iceberg.view.View; |
| 35 | +import org.apache.iceberg.view.ViewRepresentation; |
| 36 | +import org.apache.iceberg.view.ViewVersion; |
| 37 | +import org.junit.jupiter.api.AfterEach; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.TestTemplate; |
| 40 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 41 | + |
| 42 | +@ExtendWith(ParameterizedTestExtension.class) |
| 43 | +public class TestAddViewDialectProcedure extends ExtensionsTestBase { |
| 44 | + private static final Namespace NAMESPACE = Namespace.of("default"); |
| 45 | + private static final String VIEW_NAME = "view_year"; |
| 46 | + private static final TableIdentifier VIEW_IDENTIFIER = TableIdentifier.of(NAMESPACE, VIEW_NAME); |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + public void before() { |
| 50 | + super.before(); |
| 51 | + spark.conf().set("spark.sql.defaultCatalog", catalogName); |
| 52 | + sql("USE %s", catalogName); |
| 53 | + sql("CREATE NAMESPACE IF NOT EXISTS %s", NAMESPACE); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterEach |
| 57 | + public void removeTables() { |
| 58 | + sql("DROP TABLE IF EXISTS %s", tableName); |
| 59 | + sql("DROP VIEW IF EXISTS %s", VIEW_NAME); |
| 60 | + } |
| 61 | + |
| 62 | + @Parameters(name = "catalogName = {0}, implementation = {1}, config = {2}") |
| 63 | + public static Object[][] parameters() { |
| 64 | + return new Object[][] { |
| 65 | + { |
| 66 | + SparkCatalogConfig.SPARK_WITH_VIEWS.catalogName(), |
| 67 | + SparkCatalogConfig.SPARK_WITH_VIEWS.implementation(), |
| 68 | + SparkCatalogConfig.SPARK_WITH_VIEWS.properties() |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + private ViewCatalog viewCatalog() { |
| 74 | + Catalog icebergCatalog = Spark3Util.loadIcebergCatalog(spark, catalogName); |
| 75 | + assertThat(icebergCatalog).isInstanceOf(ViewCatalog.class); |
| 76 | + return (ViewCatalog) icebergCatalog; |
| 77 | + } |
| 78 | + |
| 79 | + @TestTemplate |
| 80 | + public void testAddNewViewDialect() { |
| 81 | + String sparkViewSQL = |
| 82 | + String.format("SELECT * FROM %s WHERE year(order_date) = year(current_date())", tableName); |
| 83 | + ViewRepresentation sparkRep = |
| 84 | + ImmutableSQLViewRepresentation.builder().dialect("spark").sql(sparkViewSQL).build(); |
| 85 | + |
| 86 | + String trinoViewSQL = |
| 87 | + String.format( |
| 88 | + "SELECT * FROM %s WHERE extract(year FROM order_date) = extract(year FROM current_date))", |
| 89 | + tableName); |
| 90 | + ViewRepresentation trinoRep = |
| 91 | + ImmutableSQLViewRepresentation.builder().dialect("trino").sql(trinoViewSQL).build(); |
| 92 | + |
| 93 | + sql("CREATE TABLE %s (order_id int NOT NULL, order_date date) USING iceberg", tableName); |
| 94 | + sql("INSERT INTO TABLE %s VALUES (1, DATE '2024-10-08')", tableName); |
| 95 | + sql("INSERT INTO TABLE %s VALUES (2, DATE '2025-10-08')", tableName); |
| 96 | + |
| 97 | + sql("CREATE VIEW %s AS %s", VIEW_NAME, sparkViewSQL); |
| 98 | + |
| 99 | + ViewCatalog viewCatalog = viewCatalog(); |
| 100 | + View view = viewCatalog.loadView(VIEW_IDENTIFIER); |
| 101 | + int oldVersion = view.currentVersion().versionId(); |
| 102 | + assertThat(view.currentVersion().representations()).containsExactly(sparkRep); |
| 103 | + |
| 104 | + List<Object[]> output = |
| 105 | + sql( |
| 106 | + "CALL %s.system.add_view_dialect('%s', '%s', '%s')", |
| 107 | + catalogName, VIEW_IDENTIFIER, "trino", trinoViewSQL); |
| 108 | + |
| 109 | + ViewVersion newVersion = viewCatalog.loadView(VIEW_IDENTIFIER).currentVersion(); |
| 110 | + assertThat(output).containsExactly(new Object[] {newVersion.versionId()}); |
| 111 | + assertThat(newVersion.versionId()).isNotEqualTo(oldVersion); |
| 112 | + assertThat(newVersion.representations()).containsExactlyInAnyOrder(sparkRep, trinoRep); |
| 113 | + |
| 114 | + // test adding multiple SQL for same dialect |
| 115 | + assertThatThrownBy( |
| 116 | + () -> |
| 117 | + sql( |
| 118 | + "CALL %s.system.add_view_dialect('%s', '%s', '%s')", |
| 119 | + catalogName, VIEW_IDENTIFIER, "spark", "another sql")) |
| 120 | + .hasMessageContaining("Invalid view version: Cannot add multiple queries for dialect spark") |
| 121 | + .isInstanceOf(IllegalArgumentException.class); |
| 122 | + } |
| 123 | + |
| 124 | + @TestTemplate |
| 125 | + public void testInvalidArgs() { |
| 126 | + String sparkViewSQL = |
| 127 | + String.format("SELECT * FROM %s WHERE year(order_date) = year(current_date())", tableName); |
| 128 | + |
| 129 | + sql("CREATE TABLE %s (order_id int NOT NULL, order_date date) USING iceberg", tableName); |
| 130 | + sql("INSERT INTO TABLE %s VALUES (1, DATE '2024-10-08')", tableName); |
| 131 | + |
| 132 | + sql("CREATE VIEW %s AS %s", VIEW_NAME, sparkViewSQL); |
| 133 | + |
| 134 | + assertThatThrownBy( |
| 135 | + () -> |
| 136 | + sql( |
| 137 | + "CALL %s.system.add_view_dialect('%s', '%s', '%s')", |
| 138 | + catalogName, VIEW_IDENTIFIER + "_invalid", "trino", "foo")) |
| 139 | + .hasMessageContaining( |
| 140 | + "Couldn't load view 'default.view_year_invalid' in catalog 'spark_with_views'") |
| 141 | + .isInstanceOf(RuntimeException.class); |
| 142 | + |
| 143 | + assertThatThrownBy( |
| 144 | + () -> |
| 145 | + sql("CALL %s.system.add_view_dialect('', '%s', '%s')", catalogName, "trino", "foo")) |
| 146 | + .hasMessageContaining("Cannot handle an empty identifier for argument view") |
| 147 | + .isInstanceOf(IllegalArgumentException.class); |
| 148 | + |
| 149 | + assertThatThrownBy( |
| 150 | + () -> |
| 151 | + sql( |
| 152 | + "CALL %s.system.add_view_dialect('%s', '', '%s')", |
| 153 | + catalogName, VIEW_IDENTIFIER, "foo")) |
| 154 | + .hasMessageContaining("dialect should not be empty") |
| 155 | + .isInstanceOf(RuntimeException.class); |
| 156 | + |
| 157 | + assertThatThrownBy( |
| 158 | + () -> |
| 159 | + sql( |
| 160 | + "CALL %s.system.add_view_dialect('%s', '%s', '')", |
| 161 | + catalogName, VIEW_IDENTIFIER, "foo")) |
| 162 | + .hasMessageContaining("sql should not be empty") |
| 163 | + .isInstanceOf(RuntimeException.class); |
| 164 | + } |
| 165 | +} |
0 commit comments