Skip to content

Commit

Permalink
adding additional phase for scans and modify
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Oct 29, 2023
1 parent 176bbeb commit 4586802
Show file tree
Hide file tree
Showing 23 changed files with 212 additions and 185 deletions.
7 changes: 2 additions & 5 deletions core/src/main/java/org/polypheny/db/algebra/SingleAlg.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.common.collect.ImmutableList;
import java.util.List;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.algebra.type.AlgDataType;
Expand All @@ -49,6 +50,7 @@
* It is not required that single-input relational expressions use this class as a base class. However, default
* implementations of methods make life easier.
*/
@Getter
@SuperBuilder(toBuilder = true)
public abstract class SingleAlg extends AbstractAlgNode {

Expand All @@ -67,11 +69,6 @@ protected SingleAlg( AlgOptCluster cluster, AlgTraitSet traits, AlgNode input )
}


public AlgNode getInput() {
return input;
}


@Override
public List<AlgNode> getInputs() {
return ImmutableList.of( input );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public AlgOptCost computeSelfCost( AlgOptPlanner planner, AlgMetadataQuery mq )
} else {
rowCount += rightRowCount;
}
return planner.getCostFactory().makeCost( rowCount, 0, 0 );
return planner.getCostFactory().makeCost( rowCount, 0, 0 ).multiplyBy( 100 ); // de-courage usage of enumerable
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ScanRule extends AlgOptRule {
* @param algBuilderFactory Builder for relational expressions
*/
public ScanRule( AlgBuilderFactory algBuilderFactory ) {
super( operand( LogicalRelScan.class, any() ), algBuilderFactory, null );
super( operand( LogicalRelScan.class, any() ), algBuilderFactory, ScanRule.class.getSimpleName() );
}


Expand All @@ -55,4 +55,5 @@ public void onMatch( AlgOptRuleCall call ) {
call.transformTo( newAlg );
}


}
15 changes: 9 additions & 6 deletions core/src/main/java/org/polypheny/db/plan/AlgOptPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.polypheny.db.algebra.metadata.AlgMetadataProvider;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.algebra.metadata.CachingAlgMetadataProvider;
import org.polypheny.db.plan.volcano.VolcanoPlannerPhase;
import org.polypheny.db.rex.RexExecutor;
import org.polypheny.db.util.trace.PolyphenyDbTrace;
import org.slf4j.Logger;
Expand Down Expand Up @@ -106,6 +107,8 @@ public interface AlgOptPlanner {
*/
boolean addRule( AlgOptRule rule );

boolean addRule( AlgOptRule operand, VolcanoPlannerPhase phase );

/**
* Removes a rule.
*
Expand Down Expand Up @@ -171,22 +174,22 @@ public interface AlgOptPlanner {
*
* The expression must not already have been registered. If you are not sure whether it has been registered, call {@link #ensureRegistered(AlgNode, AlgNode)}.
*
* @param alg Relational expression to register (must not already be registered)
* @param equivAlg Relational expression it is equivalent to (may be null)
* @param alg Algebraic expression to register (must not already be registered)
* @param equivAlg Algebraic expression it is equivalent to (may be null)
* @return the same expression, or an equivalent existing expression
*/
AlgNode register( AlgNode alg, AlgNode equivAlg );

/**
* Registers an algebra expression if it is not already registered.
*
* If {@code equivRel} is specified, {@code rel} is placed in the same equivalence set. It is OK if {@code equivRel} has different traits;
* {@code rel} will end up in a different subset of the same set.
* If {@code equivAlg} is specified, {@code alg} is placed in the same equivalence set. It is OK if {@code equivAlg} has different traits;
* {@code alg} will end up in a different subset of the same set.
*
* It is OK if {@code alg} is a subset.
*
* @param alg Relational expression to register
* @param equivAlg Relational expression it is equivalent to (may be null)
* @param alg Algebraic expression to register
* @param equivAlg Algebraic expression it is equivalent to (may be null)
* @return Registered relational expression
*/
AlgNode ensureRegistered( AlgNode alg, AlgNode equivAlg );
Expand Down
17 changes: 7 additions & 10 deletions core/src/main/java/org/polypheny/db/plan/AlgOptRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.Getter;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.convert.Converter;
import org.polypheny.db.algebra.convert.ConverterRule;
Expand All @@ -64,7 +65,13 @@ public abstract class AlgOptRule {

/**
* Root of operand tree.
* -- GETTER --
* Returns the root operand of this rule
*
* @return the root operand of this rule
*/
@Getter
private final AlgOptRuleOperand operand;

/**
Expand Down Expand Up @@ -356,16 +363,6 @@ private void assignSolveOrder() {
}


/**
* Returns the root operand of this rule
*
* @return the root operand of this rule
*/
public AlgOptRuleOperand getOperand() {
return operand;
}


/**
* Returns a flattened list of operands of this rule.
*
Expand Down
47 changes: 9 additions & 38 deletions core/src/main/java/org/polypheny/db/plan/AlgOptRuleCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.core.Filter;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
Expand All @@ -60,11 +61,19 @@ public abstract class AlgOptRuleCall {
private static int nextId = 0;

public final int id;

@Getter
protected final AlgOptRuleOperand operand0;
protected Map<AlgNode, List<AlgNode>> nodeInputs;

@Getter
public final AlgOptRule rule;
public final AlgNode[] algs;

@Getter
private final AlgOptPlanner planner;

@Getter
private final List<AlgNode> parents;


Expand Down Expand Up @@ -94,26 +103,6 @@ protected AlgOptRuleCall( AlgOptPlanner planner, AlgOptRuleOperand operand, AlgN
}


/**
* Returns the root operand matched by this rule.
*
* @return root operand
*/
public AlgOptRuleOperand getOperand0() {
return operand0;
}


/**
* Returns the invoked planner rule.
*
* @return planner rule
*/
public AlgOptRule getRule() {
return rule;
}


/**
* Returns a list of matched relational expressions.
*
Expand Down Expand Up @@ -166,16 +155,6 @@ protected void setChildAlgs( AlgNode alg, List<AlgNode> inputs ) {
}


/**
* Returns the planner.
*
* @return planner
*/
public AlgOptPlanner getPlanner() {
return planner;
}


/**
* Returns the current RelMetadataQuery, to be used for instance by {@link AlgOptRule#onMatch(AlgOptRuleCall)}.
*/
Expand All @@ -184,14 +163,6 @@ public AlgMetadataQuery getMetadataQuery() {
}


/**
* @return list of parents of the first relational expression
*/
public List<AlgNode> getParents() {
return parents;
}


/**
* Registers that a rule has produced an equivalent algebraic expression.
*
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/polypheny/db/plan/hep/HepPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.CommonRelSubExprRule;
import org.polypheny.db.plan.Context;
import org.polypheny.db.plan.volcano.VolcanoPlannerPhase;
import org.polypheny.db.util.Pair;
import org.polypheny.db.util.Util;
import org.polypheny.db.util.graph.BreadthFirstIterator;
Expand Down Expand Up @@ -179,6 +180,12 @@ public boolean addRule( AlgOptRule rule ) {
}


@Override
public boolean addRule( AlgOptRule rule, VolcanoPlannerPhase phase ) {
return addRule( rule );
}


@Override
public void clear() {
super.clear();
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/org/polypheny/db/plan/volcano/RuleQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.polypheny.db.algebra.AlgNodes;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.plan.AlgOptCost;
import org.polypheny.db.plan.AlgOptRule;
import org.polypheny.db.plan.AlgOptRuleOperand;
import org.polypheny.db.util.Util;
import org.polypheny.db.util.trace.PolyphenyDbTrace;
Expand Down Expand Up @@ -139,6 +140,13 @@ class RuleQueue {
}


public boolean addPhaseRuleMapping( VolcanoPlannerPhase phase, AlgOptRule rule ) {
phaseRuleMapping.get( phase ).add( rule.getClass().getSimpleName() );
return true;

}


/**
* Clear internal data structure for this rule queue.
*/
Expand Down Expand Up @@ -616,7 +624,7 @@ public int compare( VolcanoRuleMatch match1, VolcanoRuleMatch match2 ) {
/**
* PhaseMatchList represents a set of {@link VolcanoRuleMatch rule-matches} for a particular {@link VolcanoPlannerPhase phase of the planner's execution}.
*/
private static class PhaseMatchList {
static class PhaseMatchList {

/**
* The VolcanoPlannerPhase that this PhaseMatchList is used in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ public List<AlgOptRule> getRules() {
}


@Override
public boolean addRule( AlgOptRule rule, VolcanoPlannerPhase phase ) {
if ( ruleSet.contains( rule ) ) {
// Rule already exists.
return false;
}
ruleQueue.addPhaseRuleMapping( phase, rule );
return addRule( rule );
}

@Override
public boolean addRule( AlgOptRule rule ) {
if ( locked ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void transformTo( AlgNode alg, Map<AlgNode, AlgNode> equiv ) {
volcanoPlanner.listener.ruleProductionSucceeded( event );
}
} catch ( Exception e ) {
throw new RuntimeException( "Error occurred while applying rule " + getRule(), e );
throw new GenericRuntimeException( "Error occurred while applying rule " + getRule(), e );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
import org.polypheny.db.plan.Convention;
import org.polypheny.db.plan.ConventionTraitDef;
import org.polypheny.db.plan.volcano.VolcanoPlanner;
import org.polypheny.db.plan.volcano.VolcanoPlannerPhase;
import org.polypheny.db.prepare.Prepare.PreparedExplain;
import org.polypheny.db.rex.RexBuilder;
import org.polypheny.db.rex.RexNode;
Expand Down Expand Up @@ -226,15 +227,19 @@ public class PolyphenyDbPrepareImpl implements PolyphenyDbPrepare {
DocumentSortToSortRule.INSTANCE
);

public static final List<AlgOptRule> DEFAULT_RULES =
public static final List<AlgOptRule> PRE_PROCESS_RULES =
ImmutableList.of(
ScanRule.INSTANCE,
AllocationToPhysicalScanRule.REL_INSTANCE,
AllocationToPhysicalScanRule.DOC_INSTANCE,
AllocationToPhysicalScanRule.GRAPH_INSTANCE,
AllocationToPhysicalModifyRule.REL_INSTANCE,
AllocationToPhysicalModifyRule.DOC_INSTANCE,
AllocationToPhysicalModifyRule.GRAPH_INSTANCE,
AllocationToPhysicalModifyRule.GRAPH_INSTANCE
);

public static final List<AlgOptRule> DEFAULT_RULES =
ImmutableList.of(
ScanRule.INSTANCE,
RuntimeConfig.JOIN_COMMUTE.getBoolean()
? JoinAssociateRule.INSTANCE
: ProjectMergeRule.INSTANCE,
Expand Down Expand Up @@ -311,6 +316,12 @@ protected AlgOptPlanner createPlanner( final Context prepareContext, org.polyphe
}

AlgOptUtil.registerAbstractAlgs( planner );

for ( AlgOptRule preProcessRule : PRE_PROCESS_RULES ) {
planner.addRule( preProcessRule, VolcanoPlannerPhase.PRE_PROCESS );
}


for ( AlgOptRule rule : DEFAULT_RULES ) {
planner.addRule( rule );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.polypheny.db.plan.AlgOptRuleOperand;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Context;
import org.polypheny.db.plan.volcano.VolcanoPlannerPhase;
import org.polypheny.db.rex.RexExecutorImpl;
import org.polypheny.db.schema.Schemas;
import org.polypheny.db.util.Pair;
Expand Down Expand Up @@ -96,6 +97,12 @@ public boolean addRule( AlgOptRule rule ) {
}


@Override
public boolean addRule( AlgOptRule operand, VolcanoPlannerPhase phase ) {
return addRule( rule );
}


@Override
public boolean removeRule( AlgOptRule rule ) {
return false;
Expand Down
Loading

0 comments on commit 4586802

Please sign in to comment.