use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project drill by apache.
the class JoinPrelRenameVisitor method visitPrel.
@Override
public Prel visitPrel(Prel prel, Void value) throws RuntimeException {
List<RelNode> children = Lists.newArrayList();
for (Prel child : prel) {
child = child.accept(this, null);
children.add(child);
}
return (Prel) prel.copy(prel.getTraitSet(), children);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project drill by apache.
the class SqlConverter method toRel.
public RelNode toRel(final SqlNode validatedNode) {
final RexBuilder rexBuilder = new DrillRexBuilder(typeFactory);
if (planner == null) {
planner = new VolcanoPlanner(costFactory, settings);
planner.setExecutor(new DrillConstExecutor(functions, util, settings));
planner.clearRelTraitDefs();
planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
planner.addRelTraitDef(DrillDistributionTraitDef.INSTANCE);
planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
}
final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(new Expander(), validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
final RelNode rel = sqlToRelConverter.convertQuery(validatedNode, false, !isInnerQuery);
final RelNode rel2 = sqlToRelConverter.flattenTypes(rel, true);
final RelNode rel3 = RelDecorrelator.decorrelateQuery(rel2);
return rel3;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project drill by apache.
the class DefaultSqlHandler method convertToDrel.
/**
* Given a relNode tree for SELECT statement, convert to Drill Logical RelNode tree.
* @param relNode
* @return
* @throws SqlUnsupportedException
* @throws RelConversionException
*/
protected DrillRel convertToDrel(final RelNode relNode) throws SqlUnsupportedException, RelConversionException {
if (context.getOptions().getOption(ExecConstants.EARLY_LIMIT0_OPT) && context.getPlannerSettings().isTypeInferenceEnabled() && FindLimit0Visitor.containsLimit0(relNode)) {
// if the schema is known, return the schema directly
final DrillRel shorterPlan;
if ((shorterPlan = FindLimit0Visitor.getDirectScanRelIfFullySchemaed(relNode)) != null) {
return shorterPlan;
}
if (FindHardDistributionScans.canForceSingleMode(relNode)) {
// disable distributed mode
context.getPlannerSettings().forceSingleMode();
}
}
try {
final RelNode convertedRelNode;
// HEP Directory pruning .
final RelNode pruned = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.DIRECTORY_PRUNING, relNode);
final RelTraitSet logicalTraits = pruned.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
if (!context.getPlannerSettings().isHepOptEnabled()) {
// hep is disabled, use volcano
convertedRelNode = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL_PRUNE_AND_JOIN, pruned, logicalTraits);
} else {
final RelNode intermediateNode2;
if (context.getPlannerSettings().isHepPartitionPruningEnabled()) {
// hep is enabled and hep pruning is enabled.
final RelNode intermediateNode = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL, pruned, logicalTraits);
intermediateNode2 = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.PARTITION_PRUNING, intermediateNode);
} else {
// Only hep is enabled
intermediateNode2 = transform(PlannerType.VOLCANO, PlannerPhase.LOGICAL_PRUNE, pruned, logicalTraits);
}
// Do Join Planning.
convertedRelNode = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.JOIN_PLANNING, intermediateNode2);
}
// Convert SUM to $SUM0
final RelNode convertedRelNodeWithSum0 = transform(PlannerType.HEP_BOTTOM_UP, PlannerPhase.SUM_CONVERSION, convertedRelNode);
final DrillRel drillRel = (DrillRel) convertedRelNodeWithSum0;
if (drillRel instanceof DrillStoreRel) {
throw new UnsupportedOperationException();
} else {
// If the query contains a limit 0 clause, disable distributed mode since it is overkill for determining schema.
if (FindLimit0Visitor.containsLimit0(convertedRelNodeWithSum0) && FindHardDistributionScans.canForceSingleMode(convertedRelNodeWithSum0)) {
context.getPlannerSettings().forceSingleMode();
}
return drillRel;
}
} catch (RelOptPlanner.CannotPlanException ex) {
logger.error(ex.getMessage());
if (JoinUtils.checkCartesianJoin(relNode, new ArrayList<Integer>(), new ArrayList<Integer>(), new ArrayList<Boolean>())) {
throw new UnsupportedRelOperatorException("This query cannot be planned possibly due to either a cartesian join or an inequality join");
} else {
throw ex;
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project hive by apache.
the class HiveRelDecorrelator method decorrelateRel.
/**
* Rewrite LogicalJoin.
*
* @param rel LogicalJoin
*/
public Frame decorrelateRel(LogicalJoin rel) {
//
// Rewrite logic:
//
// 1. rewrite join condition.
// 2. map output positions and produce cor vars if any.
//
final RelNode oldLeft = rel.getInput(0);
final RelNode oldRight = rel.getInput(1);
final Frame leftFrame = getInvoke(oldLeft, rel);
final Frame rightFrame = getInvoke(oldRight, rel);
if (leftFrame == null || rightFrame == null) {
// If any input has not been rewritten, do not rewrite this rel.
return null;
}
final RelNode newJoin = HiveJoin.getJoin(rel.getCluster(), leftFrame.r, rightFrame.r, decorrelateExpr(rel.getCondition()), rel.getJoinType());
// Create the mapping between the output of the old correlation rel
// and the new join rel
Map<Integer, Integer> mapOldToNewOutputs = Maps.newHashMap();
int oldLeftFieldCount = oldLeft.getRowType().getFieldCount();
int newLeftFieldCount = leftFrame.r.getRowType().getFieldCount();
int oldRightFieldCount = oldRight.getRowType().getFieldCount();
assert rel.getRowType().getFieldCount() == oldLeftFieldCount + oldRightFieldCount;
// Left input positions are not changed.
mapOldToNewOutputs.putAll(leftFrame.oldToNewOutputs);
// Right input positions are shifted by newLeftFieldCount.
for (int i = 0; i < oldRightFieldCount; i++) {
mapOldToNewOutputs.put(i + oldLeftFieldCount, rightFrame.oldToNewOutputs.get(i) + newLeftFieldCount);
}
final SortedMap<CorDef, Integer> corDefOutputs = new TreeMap<>(leftFrame.corDefOutputs);
// Right input positions are shifted by newLeftFieldCount.
for (Map.Entry<CorDef, Integer> entry : rightFrame.corDefOutputs.entrySet()) {
corDefOutputs.put(entry.getKey(), entry.getValue() + newLeftFieldCount);
}
return register(rel, newJoin, mapOldToNewOutputs, corDefOutputs);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode in project hive by apache.
the class HiveRelDecorrelator method decorrelateRel.
public Frame decorrelateRel(HiveFilter rel) throws SemanticException {
//
// Rewrite logic:
//
// 1. If a LogicalFilter references a correlated field in its filter
// condition, rewrite the LogicalFilter to be
// LogicalFilter
// LogicalJoin(cross product)
// OriginalFilterInput
// ValueGenerator(produces distinct sets of correlated variables)
// and rewrite the correlated fieldAccess in the filter condition to
// reference the LogicalJoin output.
//
// 2. If LogicalFilter does not reference correlated variables, simply
// rewrite the filter condition using new input.
//
final RelNode oldInput = rel.getInput();
Frame frame = getInvoke(oldInput, rel);
if (frame == null) {
// If input has not been rewritten, do not rewrite this rel.
return null;
}
Frame oldInputFrame = frame;
// and produce the correlated variables in the new output.
if (cm.mapRefRelToCorRef.containsKey(rel)) {
frame = decorrelateInputWithValueGenerator(rel);
}
boolean valueGenerator = true;
if (frame.r == oldInputFrame.r) {
// this means correated value generator wasn't generated
valueGenerator = false;
}
if (oldInput instanceof LogicalCorrelate && ((LogicalCorrelate) oldInput).getJoinType() == SemiJoinType.SEMI && !cm.mapRefRelToCorRef.containsKey(rel)) {
// this conditions need to be pushed into semi-join since this condition
// corresponds to IN
HiveSemiJoin join = ((HiveSemiJoin) frame.r);
final List<RexNode> conditions = new ArrayList<>();
RexNode joinCond = join.getCondition();
conditions.add(joinCond);
conditions.add(decorrelateExpr(rel.getCondition(), valueGenerator));
final RexNode condition = RexUtil.composeConjunction(rexBuilder, conditions, false);
RelNode newRel = HiveSemiJoin.getSemiJoin(frame.r.getCluster(), frame.r.getTraitSet(), join.getLeft(), join.getRight(), condition, join.getLeftKeys(), join.getRightKeys());
return register(rel, newRel, frame.oldToNewOutputs, frame.corDefOutputs);
}
// Replace the filter expression to reference output of the join
// Map filter to the new filter over join
relBuilder.push(frame.r).filter((decorrelateExpr(rel.getCondition(), valueGenerator)));
// input rel.
return register(rel, relBuilder.build(), frame.oldToNewOutputs, frame.corDefOutputs);
}
Aggregations