Skip to content

Commit

Permalink
Make Constraint use generic type instead of ColumnHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
homar committed Aug 5, 2023
1 parent 5ded09c commit 4eba5ba
Show file tree
Hide file tree
Showing 41 changed files with 214 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
}

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint constraint)
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint<ColumnHandle> constraint)
{
InformationSchemaTableHandle table = (InformationSchemaTableHandle) handle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>>
return Optional.of(new ConstraintApplicationResult<>(table, constraint.getSummary(), false));
}

private Constraint effectiveConstraint(TupleDomain<ColumnHandle> oldDomain, Constraint newConstraint, TupleDomain<ColumnHandle> effectiveDomain)
private Constraint<ColumnHandle> effectiveConstraint(TupleDomain<ColumnHandle> oldDomain, Constraint<ColumnHandle> newConstraint, TupleDomain<ColumnHandle> effectiveDomain)
{
if (effectiveDomain.isNone() || newConstraint.predicate().isEmpty()) {
return new Constraint(effectiveDomain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public ConnectorTableMetadata getTableMetadata()
}

@Override
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession connectorSession, Constraint constraint)
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession connectorSession, Constraint<ColumnHandle> constraint)
{
TupleDomain<ColumnHandle> tupleDomain = constraint.getSummary();
if (tupleDomain.isNone() || constraint.predicate().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final Distribution getDistribution()
* and without column handles it's currently not possible to express Constraint or ConstraintApplicationResult.
* TODO provide equivalent API in the SystemTable interface
*/
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession session, Constraint constraint)
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession session, Constraint<ColumnHandle> constraint)
{
return constraint.getSummary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.trino.metadata.TableFunctionHandle;
import io.trino.metadata.TableHandle;
import io.trino.spi.connector.CatalogHandle;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorSplitManager;
import io.trino.spi.connector.ConnectorSplitSource;
Expand Down Expand Up @@ -54,7 +55,7 @@ public SplitSource getSplits(
Span parentSpan,
TableHandle table,
DynamicFilter dynamicFilter,
Constraint constraint)
Constraint<ColumnHandle> constraint)
{
CatalogHandle catalogHandle = table.getCatalogHandle();
ConnectorSplitManager splitManager = splitManagerProvider.getService(catalogHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ private SplitSource createSplitSource(TableHandle table, Map<Symbol, ColumnHandl
dynamicFilter = dynamicFilterService.createDynamicFilter(session.getQueryId(), dynamicFilters, assignments, typeProvider);
}

Constraint constraint = filterPredicate
Constraint<ColumnHandle> constraint = filterPredicate
.map(predicate -> filterConjuncts(plannerContext.getMetadata(), predicate, expression -> !DynamicFilters.isDynamicFilter(expression)))
.map(predicate -> new LayoutConstraintEvaluator(plannerContext, typeAnalyzer, session, typeProvider, assignments, predicate))
.map(evaluator -> new Constraint(TupleDomain.all(), evaluator::isCandidate, evaluator.getArguments())) // we are interested only in functional predicate here, so we set the summary to ALL.
.map(evaluator -> new Constraint<>(TupleDomain.all(), evaluator::isCandidate, evaluator.getArguments())) // we are interested only in functional predicate here, so we set the summary to ALL.
.orElse(alwaysTrue());

// get dataSource for table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static Optional<PlanNode> pushFilterIntoTableScan(

Map<ColumnHandle, Symbol> assignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();

Constraint constraint;
Constraint<ColumnHandle> constraint;
// use evaluator only when there is some predicate which could not be translated into tuple domain
if (pruneWithPredicateExpression && !TRUE_LITERAL.equals(decomposedPredicate.getRemainingExpression())) {
LayoutConstraintEvaluator evaluator = new LayoutConstraintEvaluator(
Expand All @@ -207,12 +207,12 @@ public static Optional<PlanNode> pushFilterIntoTableScan(
// Simplify the tuple domain to avoid creating an expression with too many nodes,
// which would be expensive to evaluate in the call to isCandidate below.
domainTranslator.toPredicate(session, newDomain.simplify().transformKeys(assignments::get))));
constraint = new Constraint(newDomain, expressionTranslation.connectorExpression(), connectorExpressionAssignments, evaluator::isCandidate, evaluator.getArguments());
constraint = new Constraint<>(newDomain, expressionTranslation.connectorExpression(), connectorExpressionAssignments, evaluator::isCandidate, evaluator.getArguments());
}
else {
// Currently, invoking the expression interpreter is very expensive.
// TODO invoke the interpreter unconditionally when the interpreter becomes cheap enough.
constraint = new Constraint(newDomain, expressionTranslation.connectorExpression(), connectorExpressionAssignments);
constraint = new Constraint<>(newDomain, expressionTranslation.connectorExpression(), connectorExpressionAssignments);
}

// check if new domain is wider than domain already provided by table scan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
}

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint constraint)
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint<ColumnHandle> constraint)
{
Span span = startSpan("applyFilter", handle);
try (var ignored = scopedSpan(span)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public Optional<TopNApplicationResult<ConnectorTableHandle>> applyTopN(
}

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint constraint)
public Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle handle, Constraint<ColumnHandle> constraint)
{
return applyFilter.apply(session, handle, constraint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Optional<TopNApplicationResult<ConnectorTableHandle>> apply(
@FunctionalInterface
public interface ApplyFilter
{
Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> apply(ConnectorSession session, ConnectorTableHandle handle, Constraint constraint);
Optional<ConstraintApplicationResult<ConnectorTableHandle, ColumnHandle>> apply(ConnectorSession session, ConnectorTableHandle handle, Constraint<ColumnHandle> constraint);
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testInformationSchemaPredicatePushdown()
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_schema"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_schema")));
domains.put(new InformationSchemaColumnHandle("table_name"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_view")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.withColumnDomains(domains.buildOrThrow()));

ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
Expand All @@ -132,7 +132,7 @@ public void testInformationSchemaPredicatePushdown()
public void testInformationSchemaPredicatePushdownWithConstraintPredicate()
{
TransactionId transactionId = transactionManager.beginTransaction(false);
Constraint constraint = new Constraint(TupleDomain.all(), TestInformationSchemaMetadata::testConstraint, testConstraintColumns());
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.all(), TestInformationSchemaMetadata::testConstraint, testConstraintColumns());

ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
Expand All @@ -154,7 +154,7 @@ public void testInformationSchemaPredicatePushdownWithoutSchemaPredicate()
// predicate without schema predicates should cause schemas to be enumerated when table predicates are present
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_name"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_view")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.withColumnDomains(domains.buildOrThrow()));

ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
Expand All @@ -178,7 +178,7 @@ public void testInformationSchemaPredicatePushdownWithoutTablePredicate()
// predicate without table name predicates should not cause table level prefixes to be evaluated
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_schema"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_schema")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.withColumnDomains(domains.buildOrThrow()));

ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
Expand All @@ -197,7 +197,7 @@ public void testInformationSchemaPredicatePushdownWithConstraintPredicateOnViews
TransactionId transactionId = transactionManager.beginTransaction(false);

// predicate on non columns enumerating table should not cause tables to be enumerated
Constraint constraint = new Constraint(TupleDomain.all(), TestInformationSchemaMetadata::testConstraint, testConstraintColumns());
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.all(), TestInformationSchemaMetadata::testConstraint, testConstraintColumns());
ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
InformationSchemaTableHandle tableHandle = (InformationSchemaTableHandle)
Expand All @@ -217,7 +217,7 @@ public void testInformationSchemaPredicatePushdownOnCatalogWiseTables()

// Predicate pushdown shouldn't work for catalog-wise tables because the table prefixes for them are always
// ImmutableSet.of(new QualifiedTablePrefix(catalogName));
Constraint constraint = new Constraint(TupleDomain.all());
Constraint<ColumnHandle> constraint = new Constraint<>(TupleDomain.all());
ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata, MAX_PREFIXES_COUNT);
InformationSchemaTableHandle tableHandle = (InformationSchemaTableHandle)
Expand All @@ -237,7 +237,7 @@ public void testInformationSchemaPredicatePushdownForEmptyNames()
ConnectorTableHandle tableHandle = metadata.getTableHandle(session, new SchemaTableName("information_schema", "tables"));

// Empty schema name
InformationSchemaTableHandle filtered = metadata.applyFilter(session, tableHandle, new Constraint(TupleDomain.withColumnDomains(
InformationSchemaTableHandle filtered = metadata.applyFilter(session, tableHandle, new Constraint<>(TupleDomain.withColumnDomains(
ImmutableMap.of(tableSchemaColumn, Domain.singleValue(VARCHAR, Slices.utf8Slice(""))))))
.map(ConstraintApplicationResult::getHandle)
.map(InformationSchemaTableHandle.class::cast)
Expand All @@ -247,7 +247,7 @@ public void testInformationSchemaPredicatePushdownForEmptyNames()
assertEquals(filtered.getPrefixes(), ImmutableSet.of(new QualifiedTablePrefix("test_catalog", "")));

// Empty table name
filtered = metadata.applyFilter(session, tableHandle, new Constraint(TupleDomain.withColumnDomains(
filtered = metadata.applyFilter(session, tableHandle, new Constraint<>(TupleDomain.withColumnDomains(
ImmutableMap.of(tableNameColumn, Domain.singleValue(VARCHAR, Slices.utf8Slice(""))))))
.map(ConstraintApplicationResult::getHandle)
.map(InformationSchemaTableHandle.class::cast)
Expand Down
Loading

0 comments on commit 4eba5ba

Please sign in to comment.