use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class BasePostgreSqlClient method getColumnNameMap.
private Map<String, String> getColumnNameMap(ConnectorSession session, JdbcTableHandle tableHandle) {
// <columnName in lower case, columnName in datasource>
HashMap<String, String> columnNameMap = new HashMap<>();
List<JdbcColumnHandle> columnList = getColumns(session, tableHandle);
for (JdbcColumnHandle columnHandle : columnList) {
String columnName = columnHandle.getColumnName();
columnNameMap.put(columnName.toLowerCase(ENGLISH), columnName);
}
return columnNameMap;
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class MySqlClient method getColumns.
@SuppressWarnings("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> columnBuilder = 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);
// extracted from logical plan during pre-processing
if (dataType == Types.DECIMAL && (precision > MAX_PRECISION || scale < 0)) {
// Covert MySql Decimal type to Presto type
Type type = types.get(columnName.toLowerCase(ENGLISH));
if (type instanceof AbstractType) {
TypeSignature signature = type.getTypeSignature();
typeName = signature.getBase().toUpperCase(ENGLISH);
dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
if (type instanceof DecimalType) {
precision = ((DecimalType) type).getPrecision();
scale = ((DecimalType) type).getScale();
}
}
}
boolean isNullable = metaData.isNullable(i) != ResultSetMetaData.columnNoNulls;
JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
Optional<ColumnMapping> columnMapping;
try {
columnMapping = toPrestoType(session, connection, typeHandle);
} catch (UnsupportedOperationException ex) {
throw new PrestoException(JDBC_UNSUPPORTED_EXPRESSION, format("Data type [%s] is not support", typeHandle.getJdbcTypeName()));
}
// skip unsupported column types
if (columnMapping.isPresent()) {
Type type = columnMapping.get().getType();
JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullable);
columnBuilder.put(columnName.toLowerCase(ENGLISH), handle);
} else {
return Collections.emptyMap();
}
}
return columnBuilder.build();
} catch (SQLException | PrestoException e) {
throw new PrestoException(JDBC_QUERY_GENERATOR_FAILURE, String.format("Query generator failed for [%s]", e.getMessage()));
}
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class GreenPlumSqlClient method getColumns.
@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) {
Map<String, Integer> arrayColumnDimensions = getArrayColumnDimensions(connection, tableHandle);
try (ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) {
List<JdbcColumnHandle> columns = new ArrayList<>();
while (resultSet.next()) {
String columnName = resultSet.getString("COLUMN_NAME");
JdbcTypeHandle typeHandle = new JdbcTypeHandle(resultSet.getInt("DATA_TYPE"), Optional.of(resultSet.getString("TYPE_NAME")), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), Optional.ofNullable(arrayColumnDimensions.get(columnName)));
Optional<ColumnMapping> columnMapping = toPrestoType(session, connection, typeHandle);
// skip unsupported column types
if (columnMapping.isPresent()) {
boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
columns.add(new JdbcColumnHandle(columnName, typeHandle, columnMapping.get().getType(), nullable));
}
}
if (columns.isEmpty()) {
// In rare cases a table might have no columns.
throw new TableNotFoundException(tableHandle.getSchemaTableName());
}
return ImmutableList.copyOf(columns);
}
} catch (SQLException e) {
throw new PrestoException(JDBC_ERROR, e);
}
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class TestHanaClient method testDropColumn.
/**
* testDropColumn
*/
@Test
public void testDropColumn() {
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 = hanaClient.getTableHandle(identity, schemaTableName).get();
List<JdbcColumnHandle> columns = hanaClient.getColumns(SESSION, tableHandle);
JdbcColumnHandle columnHandle = null;
for (JdbcColumnHandle column : columns) {
if ("value".equalsIgnoreCase(column.getColumnName())) {
columnHandle = column;
break;
}
}
hanaClient.dropColumn(identity, tableHandle, columnHandle);
expectedColumns = Arrays.asList("text", "text_short");
actualColumns = getActualColumns(schemaName, tableName);
assertEquals(actualColumns, expectedColumns);
}
use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.
the class ClickHouseQueryBuilder method buildSql.
@Override
public PreparedStatement buildSql(JdbcClient client, ConnectorSession session, Connection connection, String catalog, String schema, String table, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain, Optional<String> additionalPredicate, Function<String, String> sqlFunction) throws SQLException {
StringBuilder sql = new StringBuilder();
String columnNames = columns.stream().map(JdbcColumnHandle::getColumnName).map(this::quote).collect(joining(", "));
sql.append("SELECT ");
sql.append(columnNames);
if (columns.isEmpty()) {
sql.append("null");
}
sql.append(" FROM ");
if (!isNullOrEmpty(schema)) {
sql.append(quote(schema)).append('.');
}
if (isPushSubQueryDown) {
sql.append("(").append(table).append(") pushdown");
} else {
sql.append(quote(table));
}
List<TypeAndValue> accumulator = new ArrayList<>();
List<String> clauses = toConjuncts(client, session, connection, columns, tupleDomain, accumulator);
if (additionalPredicate.isPresent()) {
clauses = ImmutableList.<String>builder().addAll(clauses).add(additionalPredicate.get()).build();
}
if (!clauses.isEmpty()) {
sql.append(" WHERE ").append(Joiner.on(" AND ").join(clauses));
}
String query = sqlFunction.apply(sql.toString());
PreparedStatement statement = client.getPreparedStatement(connection, query);
for (int i = 0; i < accumulator.size(); i++) {
TypeAndValue typeAndValue = accumulator.get(i);
int parameterIndex = i + 1;
Type type = typeAndValue.getType();
WriteFunction writeFunction = client.toPrestoType(session, connection, typeAndValue.getTypeHandle()).orElseThrow(() -> new VerifyException(format("Unsupported type %s with handle %s", type, typeAndValue.getTypeHandle()))).getWriteFunction();
Class<?> javaType = type.getJavaType();
Object value = typeAndValue.getValue();
if (javaType == boolean.class) {
((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, (boolean) value);
} else if (javaType == long.class) {
((LongWriteFunction) writeFunction).set(statement, parameterIndex, (long) value);
} else if (javaType == double.class) {
((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, (double) value);
} else if (javaType == Slice.class) {
((SliceWriteFunction) writeFunction).set(statement, parameterIndex, (Slice) value);
} else if (javaType == Block.class) {
((BlockWriteFunction) writeFunction).set(statement, parameterIndex, (Block) value);
} else {
throw new VerifyException(format("Unexpected type %s with java type %s", type, javaType.getName()));
}
}
return statement;
}
Aggregations