Skip to content

Commit

Permalink
Accept PreparedQuery in QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jan 25, 2021
1 parent 75baaae commit 6442c96
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Expand Up @@ -428,7 +428,7 @@ public PreparedStatement buildSql(ConnectorSession session, Connection connectio
PreparedQuery preparedQuery = queryBuilder.prepareQuery(
session,
connection,
table.getRemoteTableName(),
table.getRelationHandle(),
table.getGroupingSets(),
columns,
table.getConstraint(),
Expand Down
Expand Up @@ -20,6 +20,7 @@
import io.airlift.slice.Slice;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.predicate.Domain;
import io.trino.spi.predicate.Range;
import io.trino.spi.predicate.TupleDomain;
Expand Down Expand Up @@ -78,7 +79,10 @@ public PreparedStatement buildSql(
PreparedQuery preparedQuery = prepareQuery(
session,
connection,
remoteTableName,
new JdbcNamedRelationHandle(
// This dummy SchemaTableName is not used for anything here. It's provided only to implement the deprecated buildSql() method
new SchemaTableName(remoteTableName.getSchemaName().orElse(""), remoteTableName.getTableName()),
remoteTableName),
groupingSets,
columns,
tupleDomain,
Expand All @@ -90,17 +94,27 @@ public PreparedStatement buildSql(
public PreparedQuery prepareQuery(
ConnectorSession session,
Connection connection,
RemoteTableName remoteTableName,
JdbcRelationHandle baseRelation,
Optional<List<List<JdbcColumnHandle>>> groupingSets,
List<JdbcColumnHandle> columns,
TupleDomain<ColumnHandle> tupleDomain,
Optional<String> additionalPredicate)
{
String sql = "SELECT " + getProjection(columns);
sql += " FROM " + getRelation(remoteTableName);

ImmutableList.Builder<QueryParameter> accumulator = ImmutableList.builder();

String sql = "SELECT " + getProjection(columns);
if (baseRelation instanceof JdbcNamedRelationHandle) {
sql += " FROM " + getRelation(((JdbcNamedRelationHandle) baseRelation).getRemoteTableName());
}
else if (baseRelation instanceof JdbcQueryRelationHandle) {
PreparedQuery preparedQuery = ((JdbcQueryRelationHandle) baseRelation).getPreparedQuery();
sql += " FROM (" + preparedQuery.getQuery() + ") o";
accumulator.addAll(preparedQuery.getParameters());
}
else {
throw new IllegalArgumentException("Unsupported relation: " + baseRelation);
}

List<String> clauses = toConjuncts(client, session, connection, tupleDomain, accumulator::add);
if (additionalPredicate.isPresent()) {
clauses = ImmutableList.<String>builder()
Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Multiset;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.predicate.Domain;
import io.trino.spi.predicate.Range;
import io.trino.spi.predicate.SortedRangeSet;
Expand Down Expand Up @@ -87,7 +88,9 @@
@Test(singleThreaded = true)
public class TestJdbcQueryBuilder
{
private static final RemoteTableName TEST_TABLE = new RemoteTableName(Optional.empty(), Optional.empty(), "test_table");
private static final JdbcNamedRelationHandle TEST_TABLE = new JdbcNamedRelationHandle(new SchemaTableName(
"some_test_schema", "test_table"),
new RemoteTableName(Optional.empty(), Optional.empty(), "test_table"));
private static final ConnectorSession SESSION = TestingConnectorSession.builder()
.setPropertyMetadata(new JdbcMetadataSessionProperties(new JdbcMetadataConfig(), Optional.empty()).getSessionProperties())
.build();
Expand Down

0 comments on commit 6442c96

Please sign in to comment.