Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public static class JoinHintOptions {
* BREAK: Break right table build process, continue to perform JOIN operation, results might be partial.
*/
public static final String JOIN_OVERFLOW_MODE = "join_overflow_mode";
/**
* Indicat that the join operator(s) within a certain selection scope are colocated
*/
public static final String IS_COLOCATED_BY_JOIN_KEYS = "is_colocated_by_join_keys";
}

public static class TableHintOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,15 @@ public void onMatch(RelOptRuleCall call) {
PinotLogicalExchange right = (PinotLogicalExchange) (join.getRight() instanceof HepRelVertex
? ((HepRelVertex) join.getRight()).getCurrentRel() : join.getRight());

PinotLogicalExchange dynamicBroadcastExchange =
PinotLogicalExchange.create(right.getInput(), RelDistributions.BROADCAST_DISTRIBUTED,
// when colocated join hint is given, dynamic broadcast exchange can be hash-distributed b/c
// 1. currently, dynamic broadcast only works against main table off leaf-stage; (e.g. receive node on leaf)
// 2. when hash key are the same but hash functions are different, it can be done via normal hash shuffle.
boolean isColocatedJoin = PinotHintStrategyTable.isHintOptionTrue(join.getHints(),
PinotHintOptions.JOIN_HINT_OPTIONS, PinotHintOptions.JoinHintOptions.IS_COLOCATED_BY_JOIN_KEYS);
PinotLogicalExchange dynamicBroadcastExchange = isColocatedJoin
? PinotLogicalExchange.create(right.getInput(), RelDistributions.hash(join.analyzeCondition().rightKeys),
PinotRelExchangeType.PIPELINE_BREAKER)
: PinotLogicalExchange.create(right.getInput(), RelDistributions.BROADCAST_DISTRIBUTED,
PinotRelExchangeType.PIPELINE_BREAKER);
Join dynamicFilterJoin =
new LogicalJoin(join.getCluster(), join.getTraitSet(), left.getInput(), dynamicBroadcastExchange,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.calcite.rel.rules;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelTrait;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelDistribution;
import org.apache.calcite.rel.RelDistributions;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Exchange;
import org.apache.calcite.rel.hint.PinotHintOptions;
import org.apache.calcite.rel.hint.PinotHintStrategyTable;
import org.apache.calcite.rel.logical.LogicalAggregate;
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rel.logical.LogicalJoin;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.logical.LogicalTableScan;
import org.apache.calcite.rel.logical.PinotLogicalExchange;
import org.apache.calcite.tools.RelBuilderFactory;
import org.apache.calcite.util.mapping.Mappings;
import org.apache.pinot.query.planner.plannode.AggregateNode;


/**
* Special rule for Pinot, this rule populates {@link RelDistribution} across the entire relational tree.
*
* we implement this rule as a workaround b/c {@link org.apache.calcite.plan.RelTraitPropagationVisitor}, which is
* deprecated. The idea is to associate every node with a RelDistribution derived from {@link RelNode#getInputs()}
* or from the node itself (via hints, or special handling of the type of node in question).
*/
public class PinotRelDistributionTraitRule extends RelOptRule {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had worked on something similar earlier this year, and had come to the conclusion that Calcite's RelDistribution and Exchange are not good enough for some of our use-cases because of the following reasons:

  • A given RelNode can practically have multiple RelDistribution. e.g. in a join node where both inputs are table-scan and partitioned by the join-key, the join node can be said to be distributed on both LeftTable.key and RightTable.key. But given how RelDistribution is, we can only keep information about one of the keys. This is in spite of the fact that RelDistribution is a RelMultipleTrait. For some reason the TraitSet only keeps one RelDistribution (I forgot the reasoning for this)
  • I had to also build my own Exchange nodes, because iirc I wanted to have "Identity" exchange support (i.e. the scenario where shuffle is not needed and partitions in input can be mapped 1:1 to partitions in output).

In the linked PR above you can refer to the JSON Test File changes to see how the plan changes after my changes. I remember that I had gotten all the UTs working. I had abandoned this at the time because some of the other component design was not finalized so it was hard to get consensus on this big a change.

We can discuss this in a call perhaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good point. i think the entire way of how we handle distribution and exchanges needs to be revisited

Status quo

we currently

  1. explicitly insert logical exchanges where we might require data shuffling; and
  2. then determine whether those exchanges are real data shuffle or possible passthrough;
  3. then we determine whether to assign more/less servers to run either side of the exchanges

Current solution

This PR only addresses step-2: to give it a better idea on whether the RelDistribution is the same before & after an exchange. for this purpose, it is OK (at this point) to only keep one of the 2 sides for JOIN Rel.

Needs revisit

there are several problems

  1. should we explicitly insert exchange or should we use other abstractions?
    • there are other ways to add exchange nodes that are more "Calcite-suggested" when managing the Exchange insert instead of applying them during optimization IMO.
  2. should the exchange insert be RelDistribution-based or physical-based?
    • we are mixing the concept of Exchange usage: it can mean (1) altering logical RelDistribution, (2) indicating there potentially could be physical layout differences, (3) whether we can apply leaf-stage optimization
    • although (3) will be addressed by [multistage][feature] leaf planning with multi-semi join support #11937, we should consider whether to still use ExchangeNode as our abstraction or create our own to avoid confusion
  3. should we apply RelDistribution trait before or after Exchange insert
    • currently we have to do this after insert, but technically if we addresses question (2) we can potentially apply that beforehand

ultimately utilizing Exchange was a quick decision during early stage multi-stage engine development and it might not have been the best option. it is worth taking some time to revisit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a newbie here, but IMHO the reason RelDistribution seems to be not enough for our use case is because we are not actually using Calcite as it is designed to be used. Specifically, we are not correctly using conventions to color the AST in order to decide which part is going to be executed on each node. In this talk, @devozerov indicates that Drill or Flink use Calcite in that way.

See
image

What I think we should be doing is to color the nodes we are sure how to color and then use the optimizer to decide what to do in joins. Given we don't inject metrics into Calcite, this is not a shot term solution, but that should be the long term solution.

Copy link
Contributor Author

@walterddr walterddr Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 your analysis is absolutely correct. following up on this we will continue the discussion in

ultimately the way we use trait is kind of a workaround shortcut. should really do this properly

Copy link
Contributor

@ankitsultana ankitsultana Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the paper I thought that the convention is supposed to determine the engine. https://www.osti.gov/servlets/purl/1474637

In addition to these properties, one of the main features of Calcite
is the calling convention trait. Essentially, the trait represents the
data processing system where the expression will be executed.
Including the calling convention as a trait allows Calcite to meet
its goal of optimizing transparently queries whose execution might
span over different engines i.e., the convention will be treated as
any other physical property

public static final PinotRelDistributionTraitRule INSTANCE =
new PinotRelDistributionTraitRule(PinotRuleUtils.PINOT_REL_FACTORY);

public PinotRelDistributionTraitRule(RelBuilderFactory factory) {
super(operand(RelNode.class, any()));
}

@Override
public boolean matches(RelOptRuleCall call) {
return call.rels.length >= 1;
}

@Override
public void onMatch(RelOptRuleCall call) {
RelNode current = call.rel(0);
List<RelNode> inputs = current.getInputs();
RelDistribution relDistribution;

if (inputs == null || inputs.size() == 0) {
relDistribution = computeCurrentDistribution(current);
} else {
// if there's input to the current node, attempt to derive the RelDistribution.
relDistribution = deriveDistribution(current);
}
call.transformTo(attachTrait(current, relDistribution));
}

/**
* currently, Pinot has {@link RelTraitSet} default set to empty and thus we directly pull the cluster trait set,
* then plus the {@link RelDistribution} trait.
*/
private static RelNode attachTrait(RelNode relNode, RelTrait trait) {
RelTraitSet clusterTraitSet = relNode.getCluster().traitSet();
if (relNode instanceof LogicalJoin) {
// work around {@link LogicalJoin#copy(RelTraitSet, RexNode, RelNode, RelNode, JoinRelType, boolean)} not copying
// properly
LogicalJoin join = (LogicalJoin) relNode;
return new LogicalJoin(join.getCluster(), clusterTraitSet.plus(trait), join.getLeft(),
join.getRight(), join.getCondition(), join.getVariablesSet(), join.getJoinType(), join.isSemiJoinDone(),
ImmutableList.copyOf(join.getSystemFieldList()));
} else if (relNode instanceof LogicalTableScan) {
LogicalTableScan tableScan = (LogicalTableScan) relNode;
return new LogicalTableScan(tableScan.getCluster(), clusterTraitSet.plus(trait), tableScan.getTable());
} else {
return relNode.copy(clusterTraitSet.plus(trait), relNode.getInputs());
}
}

private static RelDistribution deriveDistribution(RelNode node) {
List<RelNode> inputs = node.getInputs();
RelNode input = PinotRuleUtils.unboxRel(inputs.get(0));
if (node instanceof PinotLogicalExchange) {
// TODO: derive from input first, only if the result is ANY we change it to current
return computeCurrentDistribution(node);
} else if (node instanceof LogicalProject) {
assert inputs.size() == 1;
RelDistribution inputRelDistribution = input.getTraitSet().getDistribution();
LogicalProject project = (LogicalProject) node;
try {
if (inputRelDistribution != null) {
return inputRelDistribution.apply(project.getMapping());
}
} catch (Exception e) {
// ... skip;
}
} else if (node instanceof LogicalFilter) {
assert inputs.size() == 1;
RelDistribution inputRelDistribution = input.getTraitSet().getDistribution();
if (inputRelDistribution != null) {
return inputRelDistribution;
}
} else if (node instanceof LogicalAggregate) {
assert inputs.size() == 1;
RelDistribution inputRelDistribution = inputs.get(0).getTraitSet().getDistribution();
if (inputRelDistribution != null) {
// create a mapping that only contains the group set
LogicalAggregate agg = (LogicalAggregate) node;
List<Integer> groupSetIndices = new ArrayList<>();
agg.getGroupSet().forEach(groupSetIndices::add);
return inputRelDistribution.apply(Mappings.target(groupSetIndices, input.getRowType().getFieldCount()));
}
} else if (node instanceof LogicalJoin) {
// TODO: we only map a single RelTrait from the LEFT table, later we should support RIGHT table as well
assert inputs.size() == 2;
RelDistribution inputRelDistribution = inputs.get(0).getTraitSet().getDistribution();
if (inputRelDistribution != null) {
// Since we only support LEFT RelTrait propagation, the inputRelDistribution can directly be applied
// b/c the Join node always puts left relation RowTypes then right relation RowTypes sequentially.
return inputRelDistribution;
}
}
// TODO: add the rest of the nodes.
return computeCurrentDistribution(node);
}

private static RelDistribution computeCurrentDistribution(RelNode node) {
if (node instanceof Exchange) {
return ((Exchange) node).getDistribution();
} else if (node instanceof LogicalTableScan) {
LogicalTableScan tableScan = (LogicalTableScan) node;
// convert table scan hints into rel trait
String partitionKey =
PinotHintStrategyTable.getHintOption(tableScan.getHints(), PinotHintOptions.TABLE_HINT_OPTIONS,
PinotHintOptions.TableHintOptions.PARTITION_KEY);
if (partitionKey != null) {
int partitionIndex = tableScan.getRowType().getField(partitionKey, true, true).getIndex();
return RelDistributions.hash(ImmutableList.of(partitionIndex));
} else {
return RelDistributions.of(RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistributions.EMPTY);
}
} else if (node instanceof LogicalAggregate) {
LogicalAggregate agg = (LogicalAggregate) node;
AggregateNode.AggType aggType = AggregateNode.AggType.valueOf(PinotHintStrategyTable.getHintOption(agg.getHints(),
PinotHintOptions.INTERNAL_AGG_OPTIONS, PinotHintOptions.InternalAggregateOptions.AGG_TYPE));
if (aggType == AggregateNode.AggType.FINAL || aggType == AggregateNode.AggType.DIRECT) {
List<Integer> groupSetIndices = new ArrayList<>();
agg.getGroupSet().forEach(groupSetIndices::add);
return RelDistributions.hash(groupSetIndices);
} else {
return RelDistributions.of(RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistributions.EMPTY);
}
}
return RelDistributions.of(RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistributions.EMPTY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.calcite.config.CalciteConnectionProperty;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.hep.HepMatchOrder;
Expand All @@ -42,6 +43,7 @@
import org.apache.calcite.rel.hint.PinotHintStrategyTable;
import org.apache.calcite.rel.logical.LogicalCorrelate;
import org.apache.calcite.rel.rules.PinotQueryRuleSets;
import org.apache.calcite.rel.rules.PinotRelDistributionTraitRule;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.runtime.CalciteContextException;
Expand Down Expand Up @@ -89,7 +91,8 @@ public class QueryEnvironment {
private final Prepare.CatalogReader _catalogReader;
private final RelDataTypeFactory _typeFactory;

private final HepProgram _hepProgram;
private final HepProgram _optProgram;
private final HepProgram _traitProgram;

// Pinot extensions
private final WorkerManager _workerManager;
Expand Down Expand Up @@ -121,38 +124,8 @@ public QueryEnvironment(TypeFactory typeFactory, CalciteSchema rootSchema, Worke
.addRelBuilderConfigTransform(c -> c.withPushJoinCondition(true))
.addRelBuilderConfigTransform(c -> c.withAggregateUnique(true)))
.build();

HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
// Set the match order as DEPTH_FIRST. The default is arbitrary which works the same as DEPTH_FIRST, but it's
// best to be explicit.
hepProgramBuilder.addMatchOrder(HepMatchOrder.DEPTH_FIRST);

// ----
// Run the Calcite CORE rules using 1 HepInstruction per rule. We use 1 HepInstruction per rule for simplicity:
// the rules used here can rest assured that they are the only ones evaluated in a dedicated graph-traversal.
for (RelOptRule relOptRule : PinotQueryRuleSets.BASIC_RULES) {
hepProgramBuilder.addRuleInstance(relOptRule);
}

// ----
// Run Pinot rule to attach aggregation auxiliary info
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.PINOT_AGG_PROCESS_RULES);

// ----
// Pushdown filters using a single HepInstruction.
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.FILTER_PUSHDOWN_RULES);

// ----
// Prune duplicate/unnecessary nodes using a single HepInstruction.
// TODO: We can consider using HepMatchOrder.TOP_DOWN if we find cases where it would help.
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.PRUNE_RULES);

// ----
// Run pinot specific rules that should run after all other rules, using 1 HepInstruction per rule.
for (RelOptRule relOptRule : PinotQueryRuleSets.PINOT_POST_RULES) {
hepProgramBuilder.addRuleInstance(relOptRule);
}
_hepProgram = hepProgramBuilder.build();
_optProgram = getOptProgram();
_traitProgram = getTraitProgram();
}

/**
Expand All @@ -168,7 +141,8 @@ public QueryEnvironment(TypeFactory typeFactory, CalciteSchema rootSchema, Worke
* @return QueryPlannerResult containing the dispatchable query plan and the relRoot.
*/
public QueryPlannerResult planQuery(String sqlQuery, SqlNodeAndOptions sqlNodeAndOptions, long requestId) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _hepProgram)) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _optProgram,
_traitProgram)) {
plannerContext.setOptions(sqlNodeAndOptions.getOptions());
RelRoot relRoot = compileQuery(sqlNodeAndOptions.getSqlNode(), plannerContext);
// TODO: current code only assume one SubPlan per query, but we should support multiple SubPlans per query.
Expand Down Expand Up @@ -196,7 +170,8 @@ public QueryPlannerResult planQuery(String sqlQuery, SqlNodeAndOptions sqlNodeAn
* @return QueryPlannerResult containing the explained query plan and the relRoot.
*/
public QueryPlannerResult explainQuery(String sqlQuery, SqlNodeAndOptions sqlNodeAndOptions, long requestId) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _hepProgram)) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _optProgram,
_traitProgram)) {
SqlExplain explain = (SqlExplain) sqlNodeAndOptions.getSqlNode();
plannerContext.setOptions(sqlNodeAndOptions.getOptions());
RelRoot relRoot = compileQuery(explain.getExplicandum(), plannerContext);
Expand Down Expand Up @@ -229,7 +204,8 @@ public String explainQuery(String sqlQuery, long requestId) {
}

public List<String> getTableNamesForQuery(String sqlQuery) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _hepProgram)) {
try (PlannerContext plannerContext = new PlannerContext(_config, _catalogReader, _typeFactory, _optProgram,
_traitProgram)) {
SqlNode sqlNode = CalciteSqlParser.compileToSqlNodeAndOptions(sqlQuery).getSqlNode();
if (sqlNode.getKind().equals(SqlKind.EXPLAIN)) {
sqlNode = ((SqlExplain) sqlNode).getExplicandum();
Expand Down Expand Up @@ -335,8 +311,12 @@ private RelNode optimize(RelRoot relRoot, PlannerContext plannerContext) {
// 4. optimize relNode
// TODO: add support for traits, cost factory.
try {
plannerContext.getRelOptPlanner().setRoot(relRoot.rel);
return plannerContext.getRelOptPlanner().findBestExp();
RelOptPlanner optPlanner = plannerContext.getRelOptPlanner();
optPlanner.setRoot(relRoot.rel);
RelNode optimized = optPlanner.findBestExp();
RelOptPlanner traitPlanner = plannerContext.getRelTraitPlanner();
traitPlanner.setRoot(optimized);
return traitPlanner.findBestExp();
} catch (Exception e) {
throw new UnsupportedOperationException(
"Cannot generate a valid execution plan for the given query: " + RelOptUtil.toString(relRoot.rel), e);
Expand Down Expand Up @@ -364,4 +344,50 @@ private DispatchableSubPlan toDispatchableSubPlan(RelRoot relRoot, PlannerContex
private HintStrategyTable getHintStrategyTable() {
return PinotHintStrategyTable.PINOT_HINT_STRATEGY_TABLE;
}

private static HepProgram getOptProgram() {
HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
// Set the match order as DEPTH_FIRST. The default is arbitrary which works the same as DEPTH_FIRST, but it's
// best to be explicit.
hepProgramBuilder.addMatchOrder(HepMatchOrder.DEPTH_FIRST);

// ----
// Run the Calcite CORE rules using 1 HepInstruction per rule. We use 1 HepInstruction per rule for simplicity:
// the rules used here can rest assured that they are the only ones evaluated in a dedicated graph-traversal.
for (RelOptRule relOptRule : PinotQueryRuleSets.BASIC_RULES) {
hepProgramBuilder.addRuleInstance(relOptRule);
}

// ----
// Run Pinot rule to attach aggregation auxiliary info
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.PINOT_AGG_PROCESS_RULES);

// ----
// Pushdown filters using a single HepInstruction.
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.FILTER_PUSHDOWN_RULES);

// ----
// Prune duplicate/unnecessary nodes using a single HepInstruction.
// TODO: We can consider using HepMatchOrder.TOP_DOWN if we find cases where it would help.
hepProgramBuilder.addRuleCollection(PinotQueryRuleSets.PRUNE_RULES);
return hepProgramBuilder.build();
}

private static HepProgram getTraitProgram() {
HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();

// Set the match order as BOTTOM_UP.
hepProgramBuilder.addMatchOrder(HepMatchOrder.BOTTOM_UP);

// ----
// Run pinot specific rules that should run after all other rules, using 1 HepInstruction per rule.
for (RelOptRule relOptRule : PinotQueryRuleSets.PINOT_POST_RULES) {
hepProgramBuilder.addRuleInstance(relOptRule);
}

// apply RelDistribution trait to all nodes
hepProgramBuilder.addRuleInstance(PinotRelDistributionTraitRule.INSTANCE);

return hepProgramBuilder.build();
}
}
Loading