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

Adding limited support for case-sensitive table names #8674

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BaseJdbcConfig
private String connectionUrl;
private String connectionUser;
private String connectionPassword;
private boolean preloadSchemaTableMapping = true;

@NotNull
public String getConnectionUrl()
Expand Down Expand Up @@ -61,4 +62,16 @@ public BaseJdbcConfig setConnectionPassword(String connectionPassword)
this.connectionPassword = connectionPassword;
return this;
}

public boolean isPreloadSchemaTableMapping()
{
return preloadSchemaTableMapping;
}

@Config("connection-load-table-mappings")
public BaseJdbcConfig setPreloadSchemaTableMapping(boolean preloadSchemaTableMapping)
{
this.preloadSchemaTableMapping = preloadSchemaTableMapping;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.plugin.jdbc;

import static java.util.Locale.ENGLISH;

public class CaseSensitiveMappedSchemaTableName
{
private final String schemaName;
private final String schemaNameLower;
private final String tableName;
private final String tableNameLower;

public CaseSensitiveMappedSchemaTableName(String schemaName, String tableName)
{
if (schemaName == null) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Presto way to do this appears to be requireNonNull(schemaName, "schemaName is null");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, thank you.

throw new NullPointerException("schemaName is null");
}
if (schemaName.isEmpty()) {
throw new IllegalArgumentException("schemaName is empty");
}
this.schemaName = schemaName;
this.schemaNameLower = schemaName.toLowerCase(ENGLISH);

if (tableName == null) {
throw new NullPointerException("tableName is null");
}
if (tableName.isEmpty()) {
throw new IllegalArgumentException("tableName is empty");
}
this.tableName = tableName;
this.tableNameLower = tableName.toLowerCase(ENGLISH);
}

public String getSchemaName()
{
return schemaName;
}

public String getSchemaNameLower()
{
return schemaNameLower;
}

public String getTableName()
{
return tableName;
}

public String getTableNameLower()
{
return tableNameLower;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void testDefaults()
ConfigAssertions.assertRecordedDefaults(ConfigAssertions.recordDefaults(BaseJdbcConfig.class)
.setConnectionUrl(null)
.setConnectionUser(null)
.setConnectionPassword(null));
.setConnectionPassword(null)
.setPreloadSchemaTableMapping(true));
}

@Test
Expand All @@ -37,12 +38,14 @@ public void testExplicitPropertyMappings()
.put("connection-url", "jdbc:h2:mem:config")
.put("connection-user", "user")
.put("connection-password", "password")
.put("connection-load-table-mappings", "false")
.build();

BaseJdbcConfig expected = new BaseJdbcConfig()
.setConnectionUrl("jdbc:h2:mem:config")
.setConnectionUser("user")
.setConnectionPassword("password");
.setConnectionPassword("password")
.setPreloadSchemaTableMapping(false);

ConfigAssertions.assertFullMapping(properties, expected);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.plugin.jdbc;

import com.facebook.presto.spi.SchemaTableName;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class TestJdbcClientNameMapping
{
private JdbcClient jdbcClient;

@BeforeClass
public void setUp()
throws Exception
{
Map<String, List<String>> schemaTableNames = new TreeMap<>();

schemaTableNames.put("schemaOne", Arrays.asList("MixedCaseTable1", "aTableTwo", "OneMoreTable", "lastone"));
schemaTableNames.put("SchemaTwo", Arrays.asList("MixedCaseTable1", "aTableTwo", "OneMoreTable", "lastone"));
schemaTableNames.put("schema_three", Arrays.asList("table_one", "table_two", "table_three"));

jdbcClient = new BaseJdbcClient(
new JdbcConnectorId("test"),
new BaseJdbcConfig().setConnectionUrl(""),
"\"",
new TestingNameMappingDriver(schemaTableNames));
}

@Test
public void testSchemaAndTableMapping()
{
assertTrue(jdbcClient.getSchemaNames().containsAll(ImmutableSet.of("schemaone", "schematwo", "schema_three")));

assertEquals(jdbcClient.getTableNames("schemaone"), ImmutableList.of(
new SchemaTableName("schemaone", "mixedcasetable1"),
new SchemaTableName("schemaone", "atabletwo"),
new SchemaTableName("schemaone", "onemoretable"),
new SchemaTableName("schemaone", "lastone")));

assertEquals(jdbcClient.getTableNames("schematwo"), ImmutableList.of(
new SchemaTableName("schematwo", "mixedcasetable1"),
new SchemaTableName("schematwo", "atabletwo"),
new SchemaTableName("schematwo", "onemoretable"),
new SchemaTableName("schematwo", "lastone")));

assertEquals(jdbcClient.getTableNames("schema_three"), ImmutableList.of(
new SchemaTableName("schema_three", "table_one"),
new SchemaTableName("schema_three", "table_two"),
new SchemaTableName("schema_three", "table_three")));

SchemaTableName schemaTableName = new SchemaTableName("schemaone", "mixedcasetable1");
JdbcTableHandle table = jdbcClient.getTableHandle(schemaTableName);
assertNotNull(table, "table is null");
assertEquals(table.getSchemaName(), "schemaOne");
assertEquals(table.getTableName(), "MixedCaseTable1");
assertEquals(table.getSchemaTableName(), schemaTableName);
}
}
Loading