use of io.prestosql.plugin.jdbc.ColumnMapping 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.ColumnMapping 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.ColumnMapping 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.ColumnMapping 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.ColumnMapping in project hetu-core by openlookeng.
the class HanaClient method toPrestoType.
@Override
public Optional<ColumnMapping> toPrestoType(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
// web address, can not change it
// CHECKSTYLE:OFF:LineLength
// https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.03/en-US/20a1569875191014b507cf392724b7eb.html
// CHECKSTYLE:ON:LineLength
Optional<ColumnMapping> columnMapping;
int columnSize = typeHandle.getColumnSize();
switch(typeHandle.getJdbcType()) {
case Types.DECIMAL:
int decimalDigits = typeHandle.getDecimalDigits();
// Map decimal(p, -s) (negative scale) to
// decimal(p+s, 0).
int precision = columnSize + max(-decimalDigits, 0);
if (precision > Decimals.MAX_PRECISION) {
columnMapping = Optional.empty();
break;
}
DecimalType decimalType = createDecimalType(precision, max(decimalDigits, 0));
if (precision == HANA_SMALLDECIMAL_DEFAULT_SCALE && decimalType.getScale() == 0) {
// hana smalldecimal type, or decimal p==HANA_SMALLDECIMAL_DEFAULT_SCALE, s==0
columnMapping = Optional.of(hanaSmallDecimalColumnMapping());
break;
} else {
columnMapping = super.toPrestoType(session, connection, typeHandle);
break;
}
default:
columnMapping = super.toPrestoType(session, connection, typeHandle);
}
return columnMapping;
}
Aggregations