forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[trinodb#36] Expose columns in unenforced predicate to QueryInfo
- Loading branch information
yangjinde
committed
Apr 24, 2023
1 parent
0ab4ee3
commit 027234a
Showing
14 changed files
with
489 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
core/trino-main/src/main/java/io/trino/execution/ColumnsInPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.execution; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Immutable | ||
public final class ColumnsInPredicate | ||
{ | ||
private final String catalog; | ||
private final String schema; | ||
private final String table; | ||
private final Set<Column> colsInDiscretePredicate; | ||
private final Set<Column> colsInRangePredicate; | ||
|
||
@JsonCreator | ||
public ColumnsInPredicate( | ||
@JsonProperty("catalog") String catalog, | ||
@JsonProperty("schema") String schema, | ||
@JsonProperty("table") String table, | ||
@JsonProperty("colsInDiscretePredicate") Set<Column> colsInDiscretePredicate, | ||
@JsonProperty("colsInRangePredicate") Set<Column> colsInRangePredicate) | ||
{ | ||
this.catalog = requireNonNull(catalog, "catalog is null"); | ||
this.schema = requireNonNull(schema, "schema is null"); | ||
this.table = requireNonNull(table, "table is null"); | ||
requireNonNull(colsInDiscretePredicate, "colsInDiscretePredicate is null"); | ||
this.colsInDiscretePredicate = ImmutableSet.copyOf(colsInDiscretePredicate); | ||
requireNonNull(colsInRangePredicate, "colsInRangePredicate is null"); | ||
this.colsInRangePredicate = ImmutableSet.copyOf(colsInRangePredicate); | ||
} | ||
|
||
@JsonProperty | ||
public String getCatalog() | ||
{ | ||
return catalog; | ||
} | ||
|
||
@JsonProperty | ||
public String getSchema() | ||
{ | ||
return schema; | ||
} | ||
|
||
@JsonProperty | ||
public String getTable() | ||
{ | ||
return table; | ||
} | ||
|
||
@JsonProperty | ||
public Set<Column> getColsInDiscretePredicate() | ||
{ | ||
return colsInDiscretePredicate; | ||
} | ||
|
||
@JsonProperty | ||
public Set<Column> getColsInRangePredicate() | ||
{ | ||
return colsInRangePredicate; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ColumnsInPredicate other = (ColumnsInPredicate) o; | ||
return Objects.equals(catalog, other.catalog) && | ||
Objects.equals(schema, other.schema) && | ||
Objects.equals(table, other.table) && | ||
Objects.equals(colsInDiscretePredicate, other.colsInDiscretePredicate) && | ||
Objects.equals(colsInRangePredicate, other.colsInRangePredicate); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(catalog, schema, table, colsInDiscretePredicate, colsInRangePredicate); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.addValue(catalog) | ||
.addValue(schema) | ||
.addValue(table) | ||
.addValue(colsInDiscretePredicate) | ||
.addValue(colsInRangePredicate) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
.../trino-main/src/main/java/io/trino/sql/planner/ColumnsInUnenforcedPredicateExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.sql.planner; | ||
|
||
import io.trino.Session; | ||
import io.trino.execution.Column; | ||
import io.trino.execution.ColumnsInPredicate; | ||
import io.trino.metadata.Metadata; | ||
import io.trino.metadata.QualifiedObjectName; | ||
import io.trino.metadata.TableHandle; | ||
import io.trino.metadata.TableSchema; | ||
import io.trino.spi.connector.ColumnMetadata; | ||
import io.trino.spi.predicate.TupleDomain; | ||
import io.trino.spi.predicate.ValueSet; | ||
import io.trino.sql.planner.plan.PlanNode; | ||
import io.trino.sql.planner.plan.PlanVisitor; | ||
import io.trino.sql.planner.plan.TableScanNode; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
|
||
public class ColumnsInUnenforcedPredicateExtractor | ||
{ | ||
private final Metadata metadata; | ||
private final Session session; | ||
|
||
public ColumnsInUnenforcedPredicateExtractor(Metadata metadata, Session session) | ||
{ | ||
this.metadata = metadata; | ||
this.session = session; | ||
} | ||
|
||
public Set<ColumnsInPredicate> extract(Plan plan) | ||
{ | ||
Visitor visitor = new Visitor(); | ||
plan.getRoot().accept(visitor, null); | ||
return visitor.getColumnsInPredicateSet(); | ||
} | ||
|
||
private class Visitor | ||
extends PlanVisitor<Void, Void> | ||
{ | ||
private final Map<QualifiedObjectName, ColumnsInPredicate> columnsInPredicateMap = new HashMap<>(); | ||
|
||
public Set<ColumnsInPredicate> getColumnsInPredicateSet() | ||
{ | ||
return columnsInPredicateMap.values().stream().collect(toImmutableSet()); | ||
} | ||
|
||
@Override | ||
public Void visitTableScan(TableScanNode node, Void context) | ||
{ | ||
TableHandle tableHandle = node.getTable(); | ||
|
||
Set<Column> colsInDiscretePredicate = new HashSet<>(); | ||
Set<Column> colsInRangePredicate = new HashSet<>(); | ||
metadata.getTableProperties(session, tableHandle) | ||
.getUnenforcedPredicates() | ||
.flatMap(TupleDomain::getDomains) | ||
.ifPresent(domains -> | ||
domains.forEach(((columnHandle, domain) -> { | ||
ValueSet valueSet = domain.getValues(); | ||
if (!valueSet.isAll() && !valueSet.isNone()) { | ||
ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle, columnHandle); | ||
Column column = new Column(columnMetadata.getName(), columnMetadata.getType().toString()); | ||
if (valueSet.isDiscreteSet()) { | ||
colsInDiscretePredicate.add(column); | ||
} | ||
else { | ||
colsInRangePredicate.add(column); | ||
} | ||
} | ||
}))); | ||
|
||
// merge result from same source table | ||
TableSchema tableSchema = metadata.getTableSchema(session, tableHandle); | ||
QualifiedObjectName qualifiedTableName = tableSchema.getQualifiedName(); | ||
ColumnsInPredicate previous = columnsInPredicateMap.get(qualifiedTableName); | ||
if (previous != null) { | ||
colsInDiscretePredicate.addAll(previous.getColsInDiscretePredicate()); | ||
colsInRangePredicate.addAll(previous.getColsInRangePredicate()); | ||
} | ||
|
||
// put into map even if this node has no unenforced predicates | ||
// so that we can obtain all source tables from the result | ||
columnsInPredicateMap.put( | ||
qualifiedTableName, | ||
new ColumnsInPredicate( | ||
qualifiedTableName.getCatalogName(), | ||
qualifiedTableName.getSchemaName(), | ||
qualifiedTableName.getObjectName(), | ||
colsInDiscretePredicate, | ||
colsInRangePredicate)); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected Void visitPlan(PlanNode node, Void context) | ||
{ | ||
for (PlanNode child : node.getSources()) { | ||
child.accept(this, context); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.