use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class ClickHouseClientTest method testRenameColumn.
/**
* testRenameColumn
*/
@Test
public void testRenameColumn() throws NoSuchFieldException, IllegalAccessException {
String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
// get actual table table_with_float_col_*
String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("table_with_float_col"));
List<String> expectedColumns = Arrays.asList("col1", "col2", "col3", "col4");
List<String> actualColumns = getActualColumns(schemaName, tableName);
assertEquals(actualColumns, expectedColumns);
JdbcIdentity identity = JdbcIdentity.from(SESSION);
SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
JdbcTableHandle tableHandle = clickHouseClient.getTableHandle(identity, schemaTableName).get();
List<JdbcColumnHandle> columns = clickHouseClient.getColumns(SESSION, tableHandle);
JdbcColumnHandle columnHandle = null;
for (JdbcColumnHandle column : columns) {
if ("col4".equalsIgnoreCase(column.getColumnName())) {
columnHandle = column;
break;
}
}
Field field = JdbcTableHandle.class.getDeclaredField("catalogName");
field.setAccessible(true);
field.set(tableHandle, null);
clickHouseClient.renameColumn(identity, tableHandle, columnHandle, "newcol4");
expectedColumns = Arrays.asList("col1", "col2", "col3", "newcol4");
actualColumns = getActualColumns(schemaName, tableName);
assertEquals(actualColumns, expectedColumns);
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class ClickHouseClient method getColumns.
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
PreparedStatement statement = connection.prepareStatement(sql)) {
ResultSetMetaData metadata = statement.getMetaData();
ImmutableMap.Builder<String, ColumnHandle> builder = new ImmutableMap.Builder<>();
for (int i = 1; i <= metadata.getColumnCount(); i++) {
String columnName = metadata.getColumnLabel(i);
String typeName = metadata.getColumnTypeName(i);
int precision = metadata.getPrecision(i);
int dataType = metadata.getColumnType(i);
int scale = metadata.getScale(i);
boolean isNullAble = metadata.isNullable(i) != ResultSetMetaData.columnNoNulls;
if (dataType == Types.DECIMAL) {
String loweredColumnName = columnName.toLowerCase(ENGLISH);
Type type = types.get(loweredColumnName);
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
precision = decimalType.getPrecision();
scale = decimalType.getScale();
}
}
JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
Optional<ColumnMapping> columnMapping;
try {
columnMapping = toPrestoType(session, connection, typeHandle);
} catch (UnsupportedOperationException ex) {
// User configured to fail the query if the data type is not supported
return Collections.emptyMap();
}
// skip unsupported column types
if (columnMapping.isPresent()) {
Type type = columnMapping.get().getType();
JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullAble);
builder.put(columnName.toLowerCase(ENGLISH), handle);
} else {
return Collections.emptyMap();
}
}
return builder.build();
} catch (SQLException | PrestoException e) {
logger.error("in clickhouse push down, clickhouse data source error msg[%s] rewrite sql[%s]", e.getMessage(), sql);
return Collections.emptyMap();
}
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class TestBaseJdbcPushDownBase method tableScan.
protected TableScanNode tableScan(PlanBuilder planBuilder, JdbcTableHandle connectorTableHandle, JdbcColumnHandle... columnHandles) {
List<Symbol> symbols = Arrays.stream(columnHandles).map(column -> new Symbol(column.getColumnName().toLowerCase(Locale.ENGLISH))).collect(toImmutableList());
ImmutableMap.Builder<Symbol, ColumnHandle> assignments = ImmutableMap.builder();
for (int i = 0; i < symbols.size(); i++) {
assignments.put(symbols.get(i), columnHandles[i]);
}
TableHandle tableHandle = new TableHandle(catalogName, connectorTableHandle, TestingTransactionHandle.create(), Optional.empty());
return planBuilder.tableScan(tableHandle, symbols, assignments.build());
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class ClickHouseQueryBuilder method toConjuncts.
private List<String> toConjuncts(JdbcClient client, ConnectorSession session, Connection connection, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain, List<ClickHouseQueryBuilder.TypeAndValue> accumulator) {
if (tupleDomain.isNone()) {
return ImmutableList.of(ALWAYS_FALSE);
}
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (JdbcColumnHandle column : columns) {
Domain domain = tupleDomain.getDomains().get().get(column);
if (domain != null) {
domain = pushDownDomain(client, session, connection, column, domain);
builder.add(toPredicate(column.getColumnName(), domain, column, accumulator));
}
}
return builder.build();
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class ClickHouseClientTest method testDropColumn.
/**
* testDropColumn
*/
@Test
public void testDropColumn() throws NoSuchFieldException, IllegalAccessException {
String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
// get actual table example_*
String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("example"));
List<String> expectedColumns = Arrays.asList("text", "text_short", "value");
List<String> actualColumns = getActualColumns(schemaName, tableName);
assertEquals(actualColumns, expectedColumns);
JdbcIdentity identity = JdbcIdentity.from(SESSION);
SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
JdbcTableHandle tableHandle = clickHouseClient.getTableHandle(identity, schemaTableName).get();
List<JdbcColumnHandle> columns = clickHouseClient.getColumns(SESSION, tableHandle);
JdbcColumnHandle columnHandle = null;
for (JdbcColumnHandle column : columns) {
if ("value".equalsIgnoreCase(column.getColumnName())) {
columnHandle = column;
break;
}
}
Field field = JdbcTableHandle.class.getDeclaredField("catalogName");
field.setAccessible(true);
field.set(tableHandle, null);
clickHouseClient.dropColumn(identity, tableHandle, columnHandle);
expectedColumns = Arrays.asList("text", "text_short");
actualColumns = getActualColumns(schemaName, tableName);
assertEquals(actualColumns, expectedColumns);
}
Aggregations