Skip to content

Commit 0c87030

Browse files
authored
Core: Only test if view exists when using SchemaVersion.V1 during table rename (apache#9770)
1 parent 811c920 commit 0c87030

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
import java.util.stream.Stream;
4040
import org.apache.iceberg.CatalogProperties;
4141
import org.apache.iceberg.CatalogUtil;
42+
import org.apache.iceberg.Schema;
4243
import org.apache.iceberg.TableMetadata;
4344
import org.apache.iceberg.TableOperations;
45+
import org.apache.iceberg.Transaction;
4446
import org.apache.iceberg.catalog.Namespace;
4547
import org.apache.iceberg.catalog.SupportsNamespaces;
4648
import org.apache.iceberg.catalog.TableIdentifier;
@@ -313,6 +315,7 @@ public List<TableIdentifier> listTables(Namespace namespace) {
313315
JdbcUtil.namespaceToString(namespace));
314316
}
315317

318+
@SuppressWarnings("checkstyle:CyclomaticComplexity")
316319
@Override
317320
public void renameTable(TableIdentifier from, TableIdentifier to) {
318321
if (from.equals(to)) {
@@ -327,7 +330,7 @@ public void renameTable(TableIdentifier from, TableIdentifier to) {
327330
throw new NoSuchNamespaceException("Namespace does not exist: %s", to.namespace());
328331
}
329332

330-
if (viewExists(to)) {
333+
if (schemaVersion == JdbcUtil.SchemaVersion.V1 && viewExists(to)) {
331334
throw new AlreadyExistsException("Cannot rename %s to %s. View already exists", from, to);
332335
}
333336

@@ -812,4 +815,32 @@ private boolean deleteProperties(Namespace namespace, Set<String> properties) {
812815
protected Map<String, String> properties() {
813816
return catalogProperties == null ? ImmutableMap.of() : catalogProperties;
814817
}
818+
819+
@Override
820+
public TableBuilder buildTable(TableIdentifier identifier, Schema schema) {
821+
return new ViewAwareTableBuilder(identifier, schema);
822+
}
823+
824+
/**
825+
* The purpose of this class is to add view detection only when SchemaVersion.V1 schema is used
826+
* when replacing a table.
827+
*/
828+
protected class ViewAwareTableBuilder extends BaseMetastoreCatalogTableBuilder {
829+
830+
private final TableIdentifier identifier;
831+
832+
public ViewAwareTableBuilder(TableIdentifier identifier, Schema schema) {
833+
super(identifier, schema);
834+
this.identifier = identifier;
835+
}
836+
837+
@Override
838+
public Transaction replaceTransaction() {
839+
if (schemaVersion == JdbcUtil.SchemaVersion.V1 && viewExists(identifier)) {
840+
throw new AlreadyExistsException("View with same name already exists: %s", identifier);
841+
}
842+
843+
return super.replaceTransaction();
844+
}
845+
}
815846
}

core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ private JdbcCatalog initCatalog(String catalogName, Map<String, String> props) {
140140

141141
properties.put(JdbcCatalog.PROPERTY_PREFIX + "username", "user");
142142
properties.put(JdbcCatalog.PROPERTY_PREFIX + "password", "password");
143-
properties.put(JdbcUtil.SCHEMA_VERSION_PROPERTY, JdbcUtil.SchemaVersion.V1.name());
144143
warehouseLocation = this.tableDir.toAbsolutePath().toString();
145144
properties.put(CatalogProperties.WAREHOUSE_LOCATION, warehouseLocation);
146145
properties.put("type", "jdbc");
@@ -154,7 +153,6 @@ public void testInitialize() {
154153
Map<String, String> properties = Maps.newHashMap();
155154
properties.put(CatalogProperties.WAREHOUSE_LOCATION, this.tableDir.toAbsolutePath().toString());
156155
properties.put(CatalogProperties.URI, "jdbc:sqlite:file::memory:?icebergDB");
157-
properties.put(JdbcUtil.SCHEMA_VERSION_PROPERTY, JdbcUtil.SchemaVersion.V1.name());
158156
JdbcCatalog jdbcCatalog = new JdbcCatalog();
159157
jdbcCatalog.setConf(conf);
160158
jdbcCatalog.initialize("test_jdbc_catalog", properties);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.jdbc;
20+
21+
import java.util.Map;
22+
import java.util.UUID;
23+
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.iceberg.CatalogProperties;
25+
import org.apache.iceberg.catalog.CatalogTests;
26+
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.io.TempDir;
29+
30+
public class TestJdbcCatalogWithV1Schema extends CatalogTests<JdbcCatalog> {
31+
32+
private JdbcCatalog catalog;
33+
34+
@TempDir private java.nio.file.Path tableDir;
35+
36+
@Override
37+
protected JdbcCatalog catalog() {
38+
return catalog;
39+
}
40+
41+
@Override
42+
protected boolean supportsNamespaceProperties() {
43+
return true;
44+
}
45+
46+
@Override
47+
protected boolean supportsNestedNamespaces() {
48+
return true;
49+
}
50+
51+
@BeforeEach
52+
public void setupCatalog() {
53+
Map<String, String> properties = Maps.newHashMap();
54+
properties.put(
55+
CatalogProperties.URI,
56+
"jdbc:sqlite:file::memory:?ic" + UUID.randomUUID().toString().replace("-", ""));
57+
properties.put(JdbcCatalog.PROPERTY_PREFIX + "username", "user");
58+
properties.put(JdbcCatalog.PROPERTY_PREFIX + "password", "password");
59+
properties.put(CatalogProperties.WAREHOUSE_LOCATION, tableDir.toAbsolutePath().toString());
60+
properties.put(JdbcUtil.SCHEMA_VERSION_PROPERTY, JdbcUtil.SchemaVersion.V1.name());
61+
62+
catalog = new JdbcCatalog();
63+
catalog.setConf(new Configuration());
64+
catalog.initialize("testCatalog", properties);
65+
}
66+
}

0 commit comments

Comments
 (0)