Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor QueryBuilder usage and implement *char join pushdown in PostgreSQL #10059

Merged
merged 13 commits into from
Feb 25, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public abstract class BaseJdbcClient
private static final Logger log = Logger.get(BaseJdbcClient.class);

protected final ConnectionFactory connectionFactory;
protected final QueryBuilder queryBuilder;
protected final String identifierQuote;
protected final Set<String> jdbcTypesMappedToVarchar;
private final IdentifierMapping identifierMapping;
Expand All @@ -97,23 +98,27 @@ public BaseJdbcClient(
BaseJdbcConfig config,
String identifierQuote,
ConnectionFactory connectionFactory,
QueryBuilder queryBuilder,
IdentifierMapping identifierMapping)
{
this(
identifierQuote,
connectionFactory,
queryBuilder,
config.getJdbcTypesMappedToVarchar(),
identifierMapping);
}

public BaseJdbcClient(
String identifierQuote,
ConnectionFactory connectionFactory,
QueryBuilder queryBuilder,
Set<String> jdbcTypesMappedToVarchar,
IdentifierMapping identifierMapping)
{
this.identifierQuote = requireNonNull(identifierQuote, "identifierQuote is null");
this.connectionFactory = requireNonNull(connectionFactory, "connectionFactory is null");
this.queryBuilder = requireNonNull(queryBuilder, "queryBuilder is null");
this.jdbcTypesMappedToVarchar = ImmutableSortedSet.orderedBy(CASE_INSENSITIVE_ORDER)
.addAll(requireNonNull(jdbcTypesMappedToVarchar, "jdbcTypesMappedToVarchar is null"))
.build();
Expand Down Expand Up @@ -377,7 +382,7 @@ public PreparedStatement buildSql(ConnectorSession session, Connection connectio
throws SQLException
{
PreparedQuery preparedQuery = prepareQuery(session, connection, table, Optional.empty(), columns, ImmutableMap.of(), Optional.of(split));
return new QueryBuilder(this).prepareStatement(session, connection, preparedQuery);
return queryBuilder.prepareStatement(this, session, connection, preparedQuery);
}

protected PreparedQuery prepareQuery(
Expand All @@ -389,7 +394,8 @@ protected PreparedQuery prepareQuery(
Map<String, String> columnExpressions,
Optional<JdbcSplit> split)
{
return applyQueryTransformations(table, new QueryBuilder(this).prepareQuery(
return applyQueryTransformations(table, queryBuilder.prepareSelectQuery(
this,
session,
connection,
table.getRelationHandle(),
Expand All @@ -412,23 +418,29 @@ public Optional<PreparedQuery> implementJoin(
JoinStatistics statistics)
{
for (JdbcJoinCondition joinCondition : joinConditions) {
if (!isSupportedJoinCondition(joinCondition)) {
if (!isSupportedJoinCondition(session, joinCondition)) {
return Optional.empty();
}
}

QueryBuilder queryBuilder = new QueryBuilder(this);
return Optional.of(queryBuilder.prepareJoinQuery(
session,
joinType,
leftSource,
rightSource,
joinConditions,
leftAssignments,
rightAssignments));
try (Connection connection = this.connectionFactory.openConnection(session)) {
return Optional.of(queryBuilder.prepareJoinQuery(
this,
session,
connection,
joinType,
leftSource,
rightSource,
joinConditions,
leftAssignments,
rightAssignments));
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

protected boolean isSupportedJoinCondition(JdbcJoinCondition joinCondition)
protected boolean isSupportedJoinCondition(ConnectorSession session, JdbcJoinCondition joinCondition)
{
return false;
}
Expand Down Expand Up @@ -970,9 +982,8 @@ public OptionalLong delete(ConnectorSession session, JdbcTableHandle handle)
checkArgument(handle.getSortOrder().isEmpty(), "Unable to delete when sort order is set: %s", handle);
try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
QueryBuilder queryBuilder = new QueryBuilder(this);
PreparedQuery preparedQuery = queryBuilder.prepareDelete(session, connection, handle.getRequiredNamedRelation(), handle.getConstraint());
try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(session, connection, preparedQuery)) {
PreparedQuery preparedQuery = queryBuilder.prepareDeleteQuery(this, session, connection, handle.getRequiredNamedRelation(), handle.getConstraint());
try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(this, session, connection, preparedQuery)) {
return OptionalLong.of(preparedStatement.executeUpdate());
}
}
Expand Down
Loading