Skip to content

Commit 927496d

Browse files
no23reasongopalldb
andauthored
Fix double backtick wrapping in setCatalog and setSchema (#1454)
## Description When catalog or schema names were passed already wrapped in backticks, setCatalog/setSchema would produce invalid SQL like ``` SET CATALOG ``name`` ``` Strip existing backticks before wrapping and before storing, so getCatalog/ getSchema always return the bare identifier name. ## Testing The changes are covered by unit tests. ## Additional Notes to the Reviewer This fixes #1453 and restores compatibility with v2 of the jdbc driver: it also allowed "pre-quoted" identifiers and handled them correctly. Signed-off-by: Dan Homola <dan.homola@hotmail.cz> Co-authored-by: Gopal Lal <135012033+gopalldb@users.noreply.github.com>
1 parent ec4d74e commit 927496d

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
### Updated
88

99
### Fixed
10+
- Fixed `setCatalog()` and `setSchema()` producing invalid SQL (e.g. `SET CATALOG ``name``) when the catalog or schema name was passed already wrapped in backticks. Backticks are now stripped before wrapping, and `getCatalog()`/`getSchema()` return the bare identifier name.
1011

1112
---
1213
*Note: When making changes, please add your change under the appropriate section
13-
with a brief description.*
14+
with a brief description.*

src/main/java/com/databricks/jdbc/api/impl/DatabricksConnection.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,9 @@ public void setCatalog(String catalog) throws SQLException {
489489
return;
490490
}
491491
Statement statement = this.createStatement();
492-
statement.execute("SET CATALOG `" + catalog + "`");
493-
this.session.setCatalog(catalog);
492+
String cleanCatalog = stripBackticks(catalog);
493+
statement.execute("SET CATALOG `" + cleanCatalog + "`");
494+
this.session.setCatalog(cleanCatalog);
494495
}
495496

496497
@Override
@@ -865,8 +866,9 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
865866
@Override
866867
public void setSchema(String schema) throws SQLException {
867868
Statement statement = this.createStatement();
868-
statement.execute("USE SCHEMA `" + schema + "`");
869-
session.setSchema(schema);
869+
String cleanSchema = stripBackticks(schema);
870+
statement.execute("USE SCHEMA `" + cleanSchema + "`");
871+
session.setSchema(cleanSchema);
870872
}
871873

872874
@Override
@@ -1090,4 +1092,14 @@ private void closeStatementSafely(Statement statement) {
10901092
}
10911093
}
10921094
}
1095+
1096+
private static String stripBackticks(String identifier) {
1097+
if (identifier != null
1098+
&& identifier.startsWith("`")
1099+
&& identifier.endsWith("`")
1100+
&& identifier.length() >= 2) {
1101+
return identifier.substring(1, identifier.length() - 1);
1102+
}
1103+
return identifier;
1104+
}
10931105
}

src/test/java/com/databricks/jdbc/api/impl/DatabricksConnectionTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,60 @@ public void testGetAndSetSchemaAndCatalog() throws SQLException {
134134
assertEquals(connection.getSchema(), DEFAULT_SCHEMA);
135135
}
136136

137+
@Test
138+
public void testSetCatalogAndSchemaWithPreWrappedBackticks() throws SQLException {
139+
when(databricksClient.createSession(
140+
new Warehouse(WAREHOUSE_ID), CATALOG, SCHEMA, new HashMap<>()))
141+
.thenReturn(IMMUTABLE_SESSION_INFO);
142+
connection = new DatabricksConnection(connectionContext, databricksClient);
143+
connection.open();
144+
145+
String backtickWrappedCatalog = "`catalog-with-hyphen`";
146+
when(databricksClient.executeStatement(
147+
eq("SET CATALOG `catalog-with-hyphen`"),
148+
eq(new Warehouse(WAREHOUSE_ID)),
149+
eq(new HashMap<>()),
150+
eq(StatementType.SQL),
151+
any(),
152+
any(),
153+
any()))
154+
.thenReturn(resultSet);
155+
connection.setCatalog(backtickWrappedCatalog);
156+
assertEquals(connection.getCatalog(), "catalog-with-hyphen");
157+
158+
String backtickWrappedSchema = "`schema-with-hyphen`";
159+
when(databricksClient.executeStatement(
160+
eq("USE SCHEMA `schema-with-hyphen`"),
161+
eq(new Warehouse(WAREHOUSE_ID)),
162+
eq(new HashMap<>()),
163+
eq(StatementType.SQL),
164+
any(),
165+
any(),
166+
any()))
167+
.thenReturn(resultSet);
168+
connection.setSchema(backtickWrappedSchema);
169+
assertEquals(connection.getSchema(), "schema-with-hyphen");
170+
171+
verify(databricksClient)
172+
.executeStatement(
173+
eq("SET CATALOG `catalog-with-hyphen`"),
174+
eq(new Warehouse(WAREHOUSE_ID)),
175+
eq(new HashMap<>()),
176+
eq(StatementType.SQL),
177+
any(),
178+
any(),
179+
any());
180+
verify(databricksClient)
181+
.executeStatement(
182+
eq("USE SCHEMA `schema-with-hyphen`"),
183+
eq(new Warehouse(WAREHOUSE_ID)),
184+
eq(new HashMap<>()),
185+
eq(StatementType.SQL),
186+
any(),
187+
any(),
188+
any());
189+
}
190+
137191
@Test
138192
public void testSetCatalogAndSchemaWithHyphenatedIdentifiers() throws SQLException {
139193
when(databricksClient.createSession(

0 commit comments

Comments
 (0)