Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDBC: Escape table names when checking the existence #11863

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ private void initializeCatalogTables() {
dbMeta.getTables(
null /* catalog name */,
null /* schemaPattern */,
JdbcUtil.CATALOG_TABLE_VIEW_NAME /* tableNamePattern */,
escape(
JdbcUtil.CATALOG_TABLE_VIEW_NAME,
dbMeta.getSearchStringEscape()) /* tableNamePattern */,
null /* types */);
if (tableExists.next()) {
return true;
Expand All @@ -183,7 +185,9 @@ private void initializeCatalogTables() {
dbMeta.getTables(
null /* catalog name */,
null /* schemaPattern */,
JdbcUtil.NAMESPACE_PROPERTIES_TABLE_NAME /* tableNamePattern */,
escape(
JdbcUtil.NAMESPACE_PROPERTIES_TABLE_NAME,
dbMeta.getSearchStringEscape()) /* tableNamePattern */,
null /* types */);

if (tableExists.next()) {
Expand All @@ -208,6 +212,10 @@ private void initializeCatalogTables() {
}
}

private static String escape(String name, String escape) {
return name.replace("_", escape + "_").replace("%", escape + "%");
}

private void updateSchemaIfRequired() {
try {
connections.run(
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -163,6 +165,32 @@ public void testInitialize() {
jdbcCatalog.initialize("test_jdbc_catalog", properties);
}

@Test
public void testEscape() throws Exception {
java.nio.file.Path dbFile = Files.createTempFile("icebergInitCatalogTables", "db");
String jdbcUrl = "jdbc:sqlite:" + dbFile.toAbsolutePath();

try (Connection connection = DriverManager.getConnection(jdbcUrl);
Statement statement = connection.createStatement()) {
statement.execute(
String.format(
"CREATE TABLE %s(x int)", JdbcUtil.CATALOG_TABLE_VIEW_NAME.replace("_", "x")));
statement.execute(
String.format(
"CREATE TABLE %s(x int)",
JdbcUtil.NAMESPACE_PROPERTIES_TABLE_NAME.replace("_", "x")));
}

Map<String, String> properties = Maps.newHashMap();
properties.put(CatalogProperties.WAREHOUSE_LOCATION, this.tableDir.toAbsolutePath().toString());
properties.put(CatalogProperties.URI, jdbcUrl);
JdbcCatalog jdbcCatalog = new JdbcCatalog();
jdbcCatalog.setConf(conf);
assertThat(catalogTablesExist(jdbcUrl)).isFalse();
jdbcCatalog.initialize("test_jdbc_catalog", properties);
assertThat(catalogTablesExist(jdbcUrl)).isTrue();
}

@Test
public void testDisableInitCatalogTablesOverridesDefault() throws Exception {
// as this test uses different connections, we can't use memory database (as it's per
Expand Down