use of io.prestosql.plugin.jdbc.BlockWriteFunction 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;
}
use of io.prestosql.plugin.jdbc.BlockWriteFunction in project hetu-core by openlookeng.
the class OracleClient method setStatement.
private void setStatement(ConnectorSession session, ConnectorTableHandle tableHandle, PreparedStatement statement, Block block, int position, int channel) throws SQLException {
JdbcTableHandle jdbcTableHandle = (JdbcTableHandle) tableHandle;
List<Type> updatedColumnTypes = jdbcTableHandle.getUpdatedColumnTypes();
List<WriteMapping> writeMappings = updatedColumnTypes.stream().map(type -> {
WriteMapping writeMapping = toWriteMapping(session, type);
WriteFunction writeFunction = writeMapping.getWriteFunction();
verify(type.getJavaType() == writeFunction.getJavaType(), "openLooKeng type %s is not compatible with write function %s accepting %s", type, writeFunction, writeFunction.getJavaType());
return writeMapping;
}).collect(toImmutableList());
List<WriteFunction> columnWriters = writeMappings.stream().map(WriteMapping::getWriteFunction).collect(toImmutableList());
List<WriteNullFunction> nullWriters = writeMappings.stream().map(WriteMapping::getWriteNullFunction).collect(toImmutableList());
int parameterIndex = channel + 1;
if (block.isNull(position)) {
nullWriters.get(channel).setNull(statement, parameterIndex);
return;
}
Type type = jdbcTableHandle.getUpdatedColumnTypes().get(channel);
Class<?> javaType = type.getJavaType();
WriteFunction writeFunction = columnWriters.get(channel);
if (javaType == boolean.class) {
((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, type.getBoolean(block, position));
} else if (javaType == long.class) {
((LongWriteFunction) writeFunction).set(statement, parameterIndex, type.getLong(block, position));
} else if (javaType == double.class) {
((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, type.getDouble(block, position));
} else if (javaType == Slice.class) {
((SliceWriteFunction) writeFunction).set(statement, parameterIndex, type.getSlice(block, position));
} else if (javaType == Block.class) {
((BlockWriteFunction) writeFunction).set(statement, parameterIndex, (Block) type.getObject(block, position));
} else {
throw new VerifyException(format("Unexpected type %s with java type %s", type, javaType.getName()));
}
}
use of io.prestosql.plugin.jdbc.BlockWriteFunction in project hetu-core by openlookeng.
the class BasePostgreSqlClient method setStatement.
private void setStatement(ConnectorSession session, ConnectorTableHandle tableHandle, PreparedStatement statement, Block block, int position, int channel) throws SQLException {
JdbcTableHandle jdbcTableHandle = (JdbcTableHandle) tableHandle;
List<Type> updatedColumnTypes = jdbcTableHandle.getUpdatedColumnTypes();
List<WriteMapping> writeMappings = updatedColumnTypes.stream().map(type -> {
WriteMapping writeMapping = toWriteMapping(session, type);
WriteFunction writeFunction = writeMapping.getWriteFunction();
verify(type.getJavaType() == writeFunction.getJavaType(), "openLooKeng type %s is not compatible with write function %s accepting %s", type, writeFunction, writeFunction.getJavaType());
return writeMapping;
}).collect(toImmutableList());
List<WriteFunction> columnWriters = writeMappings.stream().map(WriteMapping::getWriteFunction).collect(toImmutableList());
List<WriteNullFunction> nullWriters = writeMappings.stream().map(WriteMapping::getWriteNullFunction).collect(toImmutableList());
int parameterIndex = channel + 1;
if (block.isNull(position)) {
nullWriters.get(channel).setNull(statement, parameterIndex);
return;
}
Type type = jdbcTableHandle.getUpdatedColumnTypes().get(channel);
Class<?> javaType = type.getJavaType();
WriteFunction writeFunction = columnWriters.get(channel);
if (javaType == boolean.class) {
((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, type.getBoolean(block, position));
} else if (javaType == long.class) {
((LongWriteFunction) writeFunction).set(statement, parameterIndex, type.getLong(block, position));
} else if (javaType == double.class) {
((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, type.getDouble(block, position));
} else if (javaType == Slice.class) {
((SliceWriteFunction) writeFunction).set(statement, parameterIndex, type.getSlice(block, position));
} else if (javaType == Block.class) {
((BlockWriteFunction) writeFunction).set(statement, parameterIndex, (Block) type.getObject(block, position));
} else {
throw new VerifyException(format("Unexpected type %s with java type %s", type, javaType.getName()));
}
}
Aggregations