diff --git a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseClient.java b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseClient.java index ba7b932169f78..1592dff1fde01 100755 --- a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseClient.java +++ b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseClient.java @@ -62,12 +62,7 @@ import static com.facebook.presto.common.type.IntegerType.INTEGER; import static com.facebook.presto.common.type.RealType.REAL; import static com.facebook.presto.common.type.SmallintType.SMALLINT; -import static com.facebook.presto.common.type.TimeType.TIME; -import static com.facebook.presto.common.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE; -import static com.facebook.presto.common.type.TimestampType.TIMESTAMP; -import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE; import static com.facebook.presto.common.type.TinyintType.TINYINT; -import static com.facebook.presto.common.type.VarbinaryType.VARBINARY; import static com.facebook.presto.plugin.clickhouse.ClickHouseEngineType.MERGETREE; import static com.facebook.presto.plugin.clickhouse.ClickHouseErrorCode.JDBC_ERROR; import static com.facebook.presto.plugin.clickhouse.ClickhouseDXLKeyWords.ORDER_BY_PROPERTY; @@ -94,21 +89,6 @@ public class ClickHouseClient { private static final Logger log = Logger.get(ClickHouseClient.class); - private static final Map SQL_TYPES = ImmutableMap.builder() - .put(BOOLEAN, "boolean") - .put(BIGINT, "bigint") - .put(INTEGER, "integer") - .put(SMALLINT, "smallint") - .put(TINYINT, "tinyint") - .put(DOUBLE, "double precision") - .put(REAL, "real") - .put(VARBINARY, "varbinary") - .put(DATE, "Date") - .put(TIME, "time") - .put(TIME_WITH_TIME_ZONE, "time with timezone") - .put(TIMESTAMP, "timestamp") - .put(TIMESTAMP_WITH_TIME_ZONE, "timestamp with timezone") - .build(); private static final String tempTableNamePrefix = "tmp_presto_"; protected static final String identifierQuote = "\""; protected final String connectorId; @@ -185,7 +165,7 @@ public final Set getSchemaNames(ClickHouseIdentity identity) } } - public ConnectorSplitSource getSplits(ClickHouseIdentity identity, ClickHouseTableLayoutHandle layoutHandle) + public ConnectorSplitSource getSplits(ClickHouseTableLayoutHandle layoutHandle) { ClickHouseTableHandle tableHandle = layoutHandle.getTable(); ClickHouseSplit clickHouseSplit = new ClickHouseSplit( @@ -213,7 +193,7 @@ public List getColumns(ConnectorSession session, ClickHo resultSet.getInt("DECIMAL_DIGITS"), Optional.empty(), Optional.empty()); - Optional columnMapping = toPrestoType(session, typeHandle); + Optional columnMapping = toPrestoType(typeHandle); // skip unsupported column types if (columnMapping.isPresent()) { String columnName = resultSet.getString("COLUMN_NAME"); @@ -235,7 +215,7 @@ public List getColumns(ConnectorSession session, ClickHo } } - public Optional toPrestoType(ConnectorSession session, ClickHouseTypeHandle typeHandle) + public Optional toPrestoType(ClickHouseTypeHandle typeHandle) { return jdbcTypeToPrestoType(typeHandle, mapStringAsVarchar); } @@ -293,7 +273,7 @@ public String buildInsertSql(ClickHouseOutputTableHandle handle) String columns = Joiner.on(',').join(nCopies(handle.getColumnNames().size(), "?")); return new StringBuilder() .append("INSERT INTO ") - .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName())) + .append(quoted(handle.getSchemaName(), handle.getTemporaryTableName())) .append(" VALUES (").append(columns).append(")") .toString(); } @@ -389,7 +369,7 @@ private static String escapeNamePattern(String name, String escape) return name; } - protected String quoted(@Nullable String catalog, @Nullable String schema, String table) + protected String quoted(@Nullable String schema, String table) { StringBuilder builder = new StringBuilder(); if (!isNullOrEmpty(schema)) { @@ -406,16 +386,10 @@ public void addColumn(ClickHouseIdentity identity, ClickHouseTableHandle handle, String columnName = column.getName(); String sql = format( "ALTER TABLE %s ADD COLUMN %s", - quoted(handle.getCatalogName(), schema, table), + quoted(schema, table), getColumnDefinitionSql(column, columnName)); try (Connection connection = connectionFactory.openConnection(identity)) { - DatabaseMetaData metadata = connection.getMetaData(); - if (metadata.storesUpperCaseIdentifiers() && !caseSensitiveNameMatchingEnabled) { - schema = schema != null ? schema.toUpperCase(ENGLISH) : null; - table = table.toUpperCase(ENGLISH); - columnName = columnName.toUpperCase(ENGLISH); - } execute(connection, sql); } catch (SQLException e) { @@ -448,14 +422,9 @@ private ClickHouseOutputTableHandle beginWriteTable(ConnectorSession session, Co public void dropColumn(ClickHouseIdentity identity, ClickHouseTableHandle handle, ClickHouseColumnHandle column) { try (Connection connection = connectionFactory.openConnection(identity)) { - DatabaseMetaData metadata = connection.getMetaData(); - String columnName = column.getColumnName(); - if (metadata.storesUpperCaseIdentifiers() && !caseSensitiveNameMatchingEnabled) { - columnName = columnName.toUpperCase(ENGLISH); - } String sql = format( "ALTER TABLE %s DROP COLUMN %s", - quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName()), + quoted(handle.getSchemaName(), handle.getTableName()), quoted(column.getColumnName())); execute(connection, sql); } @@ -466,8 +435,8 @@ public void dropColumn(ClickHouseIdentity identity, ClickHouseTableHandle handle public void finishInsertTable(ClickHouseIdentity identity, ClickHouseOutputTableHandle handle) { - String temporaryTable = quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName()); - String targetTable = quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName()); + String temporaryTable = quoted(handle.getSchemaName(), handle.getTemporaryTableName()); + String targetTable = quoted(handle.getSchemaName(), handle.getTableName()); String insertSql = format("INSERT INTO %s SELECT * FROM %s", targetTable, temporaryTable); String cleanupSql = "DROP TABLE " + temporaryTable; @@ -538,15 +507,11 @@ public void renameColumn(ClickHouseIdentity identity, ClickHouseTableHandle hand { String sql = format( "ALTER TABLE %s RENAME COLUMN %s TO %s", - quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName()), + quoted(handle.getSchemaName(), handle.getTableName()), quoted(clickHouseColumn.getColumnName()), quoted(newColumnName)); try (Connection connection = connectionFactory.openConnection(identity)) { - DatabaseMetaData metadata = connection.getMetaData(); - if (metadata.storesUpperCaseIdentifiers() && !caseSensitiveNameMatchingEnabled) { - newColumnName = newColumnName.toUpperCase(ENGLISH); - } execute(connection, sql); } catch (SQLException e) { @@ -717,7 +682,7 @@ public void dropTable(ClickHouseIdentity identity, ClickHouseTableHandle handle) { StringBuilder sql = new StringBuilder() .append("DROP TABLE ") - .append(quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName())); + .append(quoted(handle.getSchemaName(), handle.getTableName())); try (Connection connection = connectionFactory.openConnection(identity)) { execute(connection, sql.toString()); @@ -744,7 +709,7 @@ public void renameTable(ClickHouseIdentity identity, ClickHouseTableHandle handl renameTable(identity, handle.getCatalogName(), handle.getSchemaTableName(), newTable); } - public void createSchema(ClickHouseIdentity identity, String schemaName, Map properties) + public void createSchema(ClickHouseIdentity identity, String schemaName) { try (Connection connection = connectionFactory.openConnection(identity)) { execute(connection, "CREATE DATABASE " + quoted(schemaName)); @@ -770,20 +735,11 @@ protected void renameTable(ClickHouseIdentity identity, String catalogName, Sche String tableName = oldTable.getTableName(); String newSchemaName = newTable.getSchemaName(); String newTableName = newTable.getTableName(); - String sql = format("RENAME TABLE %s.%s TO %s.%s", - quoted(schemaName), - quoted(tableName), - quoted(newTable.getSchemaName()), - quoted(newTable.getTableName())); + String sql = format("RENAME TABLE %s TO %s", + quoted(schemaName, tableName), + quoted(newSchemaName, newTableName)); try (Connection connection = connectionFactory.openConnection(identity)) { - DatabaseMetaData metadata = connection.getMetaData(); - if (metadata.storesUpperCaseIdentifiers() && !caseSensitiveNameMatchingEnabled) { - schemaName = schemaName.toUpperCase(ENGLISH); - tableName = tableName.toUpperCase(ENGLISH); - newSchemaName = newSchemaName.toUpperCase(ENGLISH); - newTableName = newTableName.toUpperCase(ENGLISH); - } execute(connection, sql); } catch (SQLException e) { @@ -901,8 +857,8 @@ protected void copyTableSchema(ClickHouseIdentity identity, String catalogName, String newCreateTableName = newTableName.getTableName(); String sql = format( "CREATE TABLE %s AS %s ", - quoted(null, schemaName, newCreateTableName), - quoted(null, schemaName, oldCreateTableName)); + quoted(schemaName, newCreateTableName), + quoted(schemaName, oldCreateTableName)); try (Connection connection = connectionFactory.openConnection(identity)) { execute(connection, sql); @@ -917,7 +873,6 @@ protected void copyTableSchema(ClickHouseIdentity identity, String catalogName, private String quoted(RemoteTableName remoteTableName) { return quoted( - remoteTableName.getCatalogName().orElse(null), remoteTableName.getSchemaName().orElse(null), remoteTableName.getTableName()); } diff --git a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseMetadata.java b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseMetadata.java index 8dbd357f08035..0b4649115056d 100755 --- a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseMetadata.java +++ b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseMetadata.java @@ -273,7 +273,7 @@ public TableStatistics getTableStatistics(ConnectorSession session, ConnectorTab @Override public void createSchema(ConnectorSession session, String schemaName, Map properties) { - clickHouseClient.createSchema(ClickHouseIdentity.from(session), schemaName, properties); + clickHouseClient.createSchema(ClickHouseIdentity.from(session), schemaName); } @Override diff --git a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseRecordCursor.java b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseRecordCursor.java index b194e28190253..b2e8d48f700ad 100755 --- a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseRecordCursor.java +++ b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseRecordCursor.java @@ -63,7 +63,7 @@ public ClickHouseRecordCursor(ClickHouseClient clickHouseClient, ConnectorSessio sliceReadFunctions = new SliceReadFunction[columnHandles.size()]; for (int i = 0; i < this.columnHandles.length; i++) { - ReadMapping readMapping = clickHouseClient.toPrestoType(session, columnHandles.get(i).getClickHouseTypeHandle()) + ReadMapping readMapping = clickHouseClient.toPrestoType(columnHandles.get(i).getClickHouseTypeHandle()) .orElseThrow(() -> new VerifyException("Unsupported column type")); Class javaType = readMapping.getType().getJavaType(); ReadFunction readFunction = readMapping.getReadFunction(); diff --git a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseSplitManager.java b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseSplitManager.java index c043a87225ee2..4300e2287d0e9 100755 --- a/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseSplitManager.java +++ b/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/ClickHouseSplitManager.java @@ -41,6 +41,6 @@ public ConnectorSplitSource getSplits( SplitSchedulingContext splitSchedulingContext) { ClickHouseTableLayoutHandle layoutHandle = (ClickHouseTableLayoutHandle) layout; - return clickHouseClient.getSplits(ClickHouseIdentity.from(session), layoutHandle); + return clickHouseClient.getSplits(layoutHandle); } } diff --git a/presto-clickhouse/src/test/java/com/facebook/presto/plugin/clickhouse/TestClickHouseDistributedQueries.java b/presto-clickhouse/src/test/java/com/facebook/presto/plugin/clickhouse/TestClickHouseDistributedQueries.java index aa58f81150100..3a44b568a6a23 100755 --- a/presto-clickhouse/src/test/java/com/facebook/presto/plugin/clickhouse/TestClickHouseDistributedQueries.java +++ b/presto-clickhouse/src/test/java/com/facebook/presto/plugin/clickhouse/TestClickHouseDistributedQueries.java @@ -359,6 +359,22 @@ public void testAddColumn() assertFalse(getQueryRunner().tableExists(getSession(), tableName)); } + @Override + public void testRenameTable() + { + String tableName = "test_rename_table_" + randomTableSuffix(); + String newTableName = "test_rename_table_new_" + randomTableSuffix(); + assertUpdate("CREATE TABLE " + tableName + " (id int NOT NULL, x VARCHAR) WITH (engine = 'MergeTree', order_by = ARRAY['id'])"); + assertUpdate("INSERT INTO " + tableName + " (id, x) VALUES(1, 'first')", 1); + + assertUpdate("ALTER TABLE " + tableName + " RENAME TO " + newTableName); + assertFalse(getQueryRunner().tableExists(getSession(), tableName)); + assertTrue(getQueryRunner().tableExists(getSession(), newTableName)); + assertUpdate("DROP TABLE " + newTableName); + + assertFalse(getQueryRunner().tableExists(getSession(), newTableName)); + } + @Test public void testShowCreateTable() {