use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.
the class VolcanoPlanner method changeTraitsUsingConverters.
private RelNode changeTraitsUsingConverters(RelNode rel, RelTraitSet toTraits, boolean allowAbstractConverters) {
final RelTraitSet fromTraits = rel.getTraitSet();
assert fromTraits.size() >= toTraits.size();
final boolean allowInfiniteCostConverters = SaffronProperties.INSTANCE.allowInfiniteCostConverters().get();
// Traits may build on top of another...for example a collation trait
// would typically come after a distribution trait since distribution
// destroys collation; so when doing the conversion below we use
// fromTraits as the trait of the just previously converted RelNode.
// Also, toTraits may have fewer traits than fromTraits, excess traits
// will be left as is. Finally, any null entries in toTraits are
// ignored.
RelNode converted = rel;
for (int i = 0; (converted != null) && (i < toTraits.size()); i++) {
RelTrait fromTrait = converted.getTraitSet().getTrait(i);
final RelTraitDef traitDef = fromTrait.getTraitDef();
RelTrait toTrait = toTraits.getTrait(i);
if (toTrait == null) {
continue;
}
assert traitDef == toTrait.getTraitDef();
// if (fromTrait.subsumes(toTrait)) {
if (fromTrait.equals(toTrait)) {
// No need to convert; it's already correct.
continue;
}
rel = traitDef.convert(this, converted, toTrait, allowInfiniteCostConverters);
if (rel != null) {
assert rel.getTraitSet().getTrait(traitDef).satisfies(toTrait);
rel = completeConversion(rel, allowInfiniteCostConverters, toTraits, Expressions.list(traitDef));
if (rel != null) {
register(rel, converted);
}
}
if ((rel == null) && allowAbstractConverters) {
RelTraitSet stepTraits = converted.getTraitSet().replace(toTrait);
rel = getSubset(converted, stepTraits);
}
converted = rel;
}
// make sure final converted traitset subsumes what was required
if (converted != null) {
assert converted.getTraitSet().satisfies(toTraits);
}
return converted;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.
the class VolcanoRuleCall method transformTo.
// ~ Methods ----------------------------------------------------------------
// implement RelOptRuleCall
public void transformTo(RelNode rel, Map<RelNode, RelNode> equiv) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Transform to: rel#{} via {}{}", rel.getId(), getRule(), equiv.isEmpty() ? "" : " with equivalences " + equiv);
if (generatedRelList != null) {
generatedRelList.add(rel);
}
}
try {
// It's possible that rel is a subset or is already registered.
// Is there still a point in continuing? Yes, because we might
// discover that two sets of expressions are actually equivalent.
// Make sure traits that the new rel doesn't know about are
// propagated.
RelTraitSet rels0Traits = rels[0].getTraitSet();
new RelTraitPropagationVisitor(getPlanner(), rels0Traits).go(rel);
if (LOGGER.isTraceEnabled()) {
// Cannot call RelNode.toString() yet, because rel has not
// been registered. For now, let's make up something similar.
String relDesc = "rel#" + rel.getId() + ":" + rel.getRelTypeName();
LOGGER.trace("call#{}: Rule {} arguments {} created {}", id, getRule(), Arrays.toString(rels), relDesc);
}
if (volcanoPlanner.listener != null) {
RelOptListener.RuleProductionEvent event = new RelOptListener.RuleProductionEvent(volcanoPlanner, rel, this, true);
volcanoPlanner.listener.ruleProductionSucceeded(event);
}
// don't register twice and cause churn.
for (Map.Entry<RelNode, RelNode> entry : equiv.entrySet()) {
volcanoPlanner.ensureRegistered(entry.getKey(), entry.getValue(), this);
}
volcanoPlanner.ensureRegistered(rel, rels[0], this);
rels[0].getCluster().invalidateMetadataQuery();
if (volcanoPlanner.listener != null) {
RelOptListener.RuleProductionEvent event = new RelOptListener.RuleProductionEvent(volcanoPlanner, rel, this, false);
volcanoPlanner.listener.ruleProductionSucceeded(event);
}
} catch (Exception e) {
throw new RuntimeException("Error occurred while applying rule " + getRule(), e);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.
the class RelSet method add.
/**
* Adds a relational expression to a set, with its results available under a
* particular calling convention. An expression may be in the set several
* times with different calling conventions (and hence different costs).
*/
public RelSubset add(RelNode rel) {
assert equivalentSet == null : "adding to a dead set";
final RelTraitSet traitSet = rel.getTraitSet().simplify();
final RelSubset subset = getOrCreateSubset(rel.getCluster(), traitSet);
subset.add(rel);
return subset;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.
the class RelCollationTraitDef method convert.
public RelNode convert(RelOptPlanner planner, RelNode rel, RelCollation toCollation, boolean allowInfiniteCostConverters) {
if (toCollation.getFieldCollations().isEmpty()) {
// An empty sort doesn't make sense.
return null;
}
// Create a logical sort, then ask the planner to convert its remaining
// traits (e.g. convert it to an EnumerableSortRel if rel is enumerable
// convention)
final Sort sort = LogicalSort.create(rel, toCollation, null, null);
RelNode newRel = planner.register(sort, rel);
final RelTraitSet newTraitSet = rel.getTraitSet().replace(toCollation);
if (!newRel.getTraitSet().equals(newTraitSet)) {
newRel = planner.changeTraits(newRel, newTraitSet);
}
return newRel;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.
the class FrameworksTest method testOptimize.
@Test
public void testOptimize() {
RelNode x = Frameworks.withPlanner(new Frameworks.PlannerAction<RelNode>() {
public RelNode apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
final Table table = new AbstractTable() {
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
final RelDataType stringType = typeFactory.createJavaType(String.class);
final RelDataType integerType = typeFactory.createJavaType(Integer.class);
return typeFactory.builder().add("s", stringType).add("i", integerType).build();
}
};
// "SELECT * FROM myTable"
final RelOptAbstractTable relOptTable = new RelOptAbstractTable(relOptSchema, "myTable", table.getRowType(typeFactory)) {
};
final EnumerableTableScan tableRel = EnumerableTableScan.create(cluster, relOptTable);
// "WHERE i > 1"
final RexBuilder rexBuilder = cluster.getRexBuilder();
final RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(tableRel), "i", true), rexBuilder.makeExactLiteral(BigDecimal.ONE));
final LogicalFilter filter = LogicalFilter.create(tableRel, condition);
// Specify that the result should be in Enumerable convention.
final RelNode rootRel = filter;
final RelOptPlanner planner = cluster.getPlanner();
RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
final RelNode rootRel2 = planner.changeTraits(rootRel, desiredTraits);
planner.setRoot(rootRel2);
// Now, plan.
return planner.findBestExp();
}
});
String s = RelOptUtil.dumpPlan("", x, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES);
assertThat(Util.toLinux(s), equalTo("EnumerableFilter(condition=[>($1, 1)])\n" + " EnumerableTableScan(table=[[myTable]])\n"));
}
Aggregations