use of io.prestosql.plugin.jdbc.JdbcTypeHandle 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.JdbcTypeHandle in project hetu-core by openlookeng.
the class OracleClient 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;
// the type of that column as integer.
if ((dataType == Types.DECIMAL || NUMBER_DATA_TYPE_NAME.equalsIgnoreCase(typeName)) && (precision == 0 || scale < 0)) {
// Covert Oracle NUMBER(scale) type to Presto types
String loweredColumnName = columnName.toLowerCase(ENGLISH);
Type hetuType = types.get(loweredColumnName);
if (hetuType instanceof AbstractType) {
TypeSignature signature = hetuType.getTypeSignature();
typeName = signature.getBase().toUpperCase(ENGLISH);
dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
if (hetuType instanceof DecimalType && this.roundingMode != RoundingMode.UNNECESSARY) {
precision = ((DecimalType) hetuType).getPrecision();
scale = ((DecimalType) hetuType).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);
}
}
return builder.build();
} catch (SQLException | PrestoException e) {
// Returning empty map will indicate that something wrong and let the Presto to execute the query as usual.
return Collections.emptyMap();
}
}
use of io.prestosql.plugin.jdbc.JdbcTypeHandle in project hetu-core by openlookeng.
the class KylinUtil method getJdbcTypeHandle.
public static JdbcTypeHandle getJdbcTypeHandle(String outColumn, Type prestoType, Optional<Integer> optType) {
int precision = 0;
int scale = 0;
if (prestoType instanceof DecimalType) {
DecimalType originalType = (DecimalType) prestoType;
precision = originalType.getPrecision();
scale = originalType.getScale();
// TODO we need to find solution from the explain plan and handle precision and scale
if (isReservedName(outColumn)) {
precision = MAX_PRECISION - MAX_SCALE;
scale = MAX_SCALE;
}
} else if (prestoType instanceof VarcharType) {
VarcharType originalType = (VarcharType) prestoType;
precision = originalType.getLength().orElse(VarcharType.MAX_LENGTH);
}
JdbcTypeHandle typeHandle = new JdbcTypeHandle(optType.get(), Optional.ofNullable(prestoType.getDisplayName()), precision, scale, Optional.empty());
return typeHandle;
}
use of io.prestosql.plugin.jdbc.JdbcTypeHandle in project hetu-core by openlookeng.
the class HanaClient 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<>();
Map<String, Type> nodeMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
nodeMap.putAll(types);
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);
// use hetu's precision and scale define from hetu plan tree node's output column type.
if (dataType == Types.DECIMAL) {
Type type = nodeMap.get(columnName);
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
precision = decimalType.getPrecision();
scale = decimalType.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) {
// 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 e1) {
// No need to raise an error.
// This method is used inside applySubQuery method to extract the column types from a sub-query.
// Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
logger.warn("in hana push down, hana data source error msg[%s] rewrite sql[%s]", e1.getMessage(), sql);
return Collections.emptyMap();
} catch (PrestoException e2) {
// No need to raise an error.
// This method is used inside applySubQuery method to extract the column types from a sub-query.
// Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
logger.warn("in hana push down, hana connector error msg[%s] rewrite sql[%s]", e2.getMessage(), sql);
return Collections.emptyMap();
}
}
use of io.prestosql.plugin.jdbc.JdbcTypeHandle in project hetu-core by openlookeng.
the class GreenPlumSqlClient method getColumns.
@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);
// need to deal with different data type here
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) {
// 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) {
// No need to raise an error.
// This method is used inside applySubQuery method to extract the column types from a sub-query.
// Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
logger.warn("in greenplum push down, greenplum data source error msg[%s] rewrite sql[%s]", e.getMessage(), sql);
return Collections.emptyMap();
}
}
Aggregations