Skip to content

Commit

Permalink
adjusted toString to allow correct pushdown of operation of jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Oct 31, 2023
1 parent 78254d5 commit b950a71
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 79 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/org/polypheny/db/algebra/BiAlg.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.polypheny.db.plan.AlgOptCluster;
import org.polypheny.db.plan.AlgTraitSet;
Expand All @@ -50,7 +49,6 @@
*/
@EqualsAndHashCode(callSuper = true)
@Getter
@Value
@NonFinal
public abstract class BiAlg extends AbstractAlgNode {

Expand Down
14 changes: 4 additions & 10 deletions core/src/main/java/org/polypheny/db/algebra/core/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgWriter;
import org.polypheny.db.algebra.BiAlg;
Expand Down Expand Up @@ -69,12 +70,14 @@
*/
public abstract class Join extends BiAlg {

@Getter
protected final RexNode condition;
protected final ImmutableSet<CorrelationId> variablesSet;

/**
* Values must be of enumeration {@link JoinAlgType}, except that {@link JoinAlgType#RIGHT} is disallowed.
*/
@Getter
protected final JoinAlgType joinType;

// Next time we need to change the constructor of Join, let's change the "Set<String> variablesStopped" parameter to "Set<CorrelationId> variablesSet".
Expand Down Expand Up @@ -120,16 +123,6 @@ public AlgNode accept( RexShuttle shuttle ) {
}


public RexNode getCondition() {
return condition;
}


public JoinAlgType getJoinType() {
return joinType;
}


@Override
public boolean isValid( Litmus litmus, Context context ) {
if ( !super.isValid( litmus, context ) ) {
Expand Down Expand Up @@ -260,5 +253,6 @@ public String algCompareString() {
(joinType != null ? joinType.name() : "") + "&";
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ public <T extends ConfigObject> T getWithId( Class<T> type, int id ) {
}


@Getter
public enum ConfigType {
BOOLEAN,
DECIMAL,
Expand All @@ -861,7 +862,6 @@ public enum ConfigType {
DOCKER_LIST( ConfigDocker.class ),
PLUGIN_LIST( ConfigPlugin.class );

@Getter
private final Class<? extends ConfigObject> clazz;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ void updateImportance( AlgSubset subset, Double importance ) {
void addMatch( VolcanoRuleMatch match ) {
final String matchName = match.toString();
for ( PhaseMatchList matchList : matchListMap.values() ) {
if ( matchList.names.contains( matchName ) ) {
if ( !matchList.names.add( matchName ) ) {
// Identical match has already been added.
continue;
}
matchList.names.add( matchName );
//matchList.names.add( matchName );

String ruleClassName = match.getRule().getClass().getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public class VolcanoPlanner extends AbstractRelOptPlanner {
final RuleQueue ruleQueue = new RuleQueue( this );

/**
* Holds the currently registered RelTraitDefs.
* Holds the currently registered AlgTraitDefs.
*/
private final List<AlgTraitDef<?>> traitDefs = new ArrayList<>();

Expand Down Expand Up @@ -388,6 +388,7 @@ public boolean addRule( AlgOptRule rule, VolcanoPlannerPhase phase ) {
return addRule( rule );
}


@Override
public boolean addRule( AlgOptRule rule ) {
if ( locked ) {
Expand Down Expand Up @@ -602,7 +603,7 @@ private void registerMetadataAlgs() {
* Ensures that the subset that is the root algebra expression contains converters to all other subsets
* in its equivalence set.
*
* Thus the planner tries to find cheap implementations of those other subsets, which can then be converted to the root.
* Thus, the planner tries to find cheap implementations of those other subsets, which can then be converted to the root.
* This is the only place in the plan where explicit converters are required; elsewhere, a consumer will be asking for
* the result in a particular convention, but the root has no consumers.
*/
Expand Down Expand Up @@ -1228,7 +1229,7 @@ private AlgSubset canonize( final AlgSubset subset ) {
* @param alg Algebra expression which has just been created (or maybe from the queue)
* @param deferred If true, each time a rule matches, just add an entry to the queue.
*/
void fireRules( AlgNode alg, boolean deferred ) {
public void fireRules( AlgNode alg, boolean deferred ) {
for ( AlgOptRuleOperand operand : classOperands.get( alg.getClass() ) ) {
if ( operand.matches( alg ) ) {
final VolcanoRuleCall ruleCall;
Expand All @@ -1249,10 +1250,19 @@ public void addRuleDuringRuntime( AlgOptRule rule ) {
if ( getRuleByDescription( rule.toString() ) != null || !addRule( rule ) ) {
return;
}

List<Pair<AlgNode, AlgSubset>> matches = new ArrayList<>();
AlgOptRuleOperand operand = rule.getOperand();
AlgVisitor visitor = new AlgVisitor() {

List<Pair<AlgNode, AlgSet>> matches = new ArrayList<>();
for ( AlgSet set : allSets ) {
for ( AlgNode node : set.getAlgsFromAllSubsets() ) {
if ( operand.matches( node ) ) {
matches.add( Pair.of( node, set ) );
}
}

}

/*AlgVisitor visitor = new AlgVisitor() {
final Set<AlgSubset> visitedSubsets = new HashSet<>();
Expand All @@ -1279,9 +1289,8 @@ public void visit( AlgNode node, int ordinal, AlgNode parent ) {
}
}
};
visitor.go( root );
for ( Pair<AlgNode, AlgSubset> pair : matches ) {
ruleQueue.recompute( pair.right );
visitor.go( root );*/
for ( Pair<AlgNode, AlgSet> pair : matches ) {
fireRules( pair.left, true );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void transformTo( AlgNode alg, Map<AlgNode, AlgNode> equiv ) {
volcanoPlanner.listener.ruleProductionSucceeded( event );
}

// Registering the root relational expression implicitly registers its descendants. Register any explicit equivalences first, so we don't register twice and cause churn.
// Registering the root algebraic expression implicitly registers its descendants. Register any explicit equivalences first, so we don't register twice and cause churn.
for ( Map.Entry<AlgNode, AlgNode> entry : equiv.entrySet() ) {
volcanoPlanner.ensureRegistered( entry.getKey(), entry.getValue(), this );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Model trait models the different possible "schema", also namespace models,
* this is on purpose different to the {@link Convention} trait to preserve the model information on push-down of each {@link AlgNode}
*/
@Getter
public class ModelTrait implements AlgTrait<ModelTraitDef> {

public static final ModelTrait RELATIONAL = new ModelTrait( NamespaceType.RELATIONAL );
Expand All @@ -36,7 +37,6 @@ public class ModelTrait implements AlgTrait<ModelTraitDef> {

public static final ModelTrait GRAPH = new ModelTrait( NamespaceType.GRAPH );

@Getter
private final NamespaceType dataModel;


Expand Down
83 changes: 65 additions & 18 deletions dbms/src/test/java/org/polypheny/db/planner/PlannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.polypheny.db.plan.AlgOptRuleCall;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Convention;
import org.polypheny.db.plan.ConventionTraitDef;
import org.polypheny.db.plan.volcano.VolcanoPlanner;
import org.polypheny.db.rex.RexBuilder;
import org.polypheny.db.type.PolyTypeFactoryImpl;
Expand All @@ -62,64 +61,112 @@ public static void init() {
public void basicTest() {
VolcanoPlanner planner = new VolcanoPlanner();

// add rule to transform to Physical
planner.addRule( new PhysRule() );
planner.addAlgTraitDef( ConventionTraitDef.INSTANCE );

// add trait so it is known to the optimizer
planner.addAlgTraitDef( PHYS_CALLING_CONVENTION.getTraitDef() );

// build the nodes
AlgOptCluster cluster = DummyCluster.newCluster( planner );
AlgNode node = new NoneDummyNode( cluster, cluster.traitSetOf( Convention.NONE ) );
AlgNode node = new LeafDummyNode( cluster, cluster.traitSetOf( Convention.NONE ) );
AlgNode root = new SingleDummyNode( cluster, cluster.traitSetOf( Convention.NONE ), node );

planner.setRoot( node );
AlgNode newRoot = planner.changeTraits( root, root.getTraitSet().replace( PHYS_CALLING_CONVENTION ) );
planner.setRoot( newRoot );

AlgNode newAlg = planner.changeTraits( node, cluster.traitSetOf( PHYS_CALLING_CONVENTION ) );
AlgNode newAlg = planner.findBestExp();
assert newAlg.getTraitSet().contains( PHYS_CALLING_CONVENTION );
}


public void dynamicRegister() {
@Test
public void dynamicAddRuleTest() {
VolcanoPlanner planner = new VolcanoPlanner();

planner.addRule( new PhysRule() );
planner.addAlgTraitDef( ConventionTraitDef.INSTANCE );
// add rule to transform to Physical
planner.addRule( new DynamicRule() );

// add trait so it is known to the optimizer
planner.addAlgTraitDef( PHYS_CALLING_CONVENTION.getTraitDef() );

// build the nodes
AlgOptCluster cluster = DummyCluster.newCluster( planner );
AlgNode node = new NoneDummyNode( cluster, cluster.traitSetOf( Convention.NONE ) );
AlgNode node = new LeafDummyNode( cluster, cluster.traitSetOf( Convention.NONE ) );
AlgNode root = new SingleDummyNode( cluster, cluster.traitSetOf( Convention.NONE ), node );

AlgNode newRoot = planner.changeTraits( root, root.getTraitSet().replace( PHYS_CALLING_CONVENTION ) );
planner.setRoot( newRoot );

AlgNode newAlg = planner.findBestExp();
assert newAlg.getTraitSet().contains( PHYS_CALLING_CONVENTION );
}


public static class DynamicRule extends AlgOptRule {

public DynamicRule() {
super( operandJ( NoneDummyNode.class, ), description );
super( operandJ( LeafDummyNode.class, Convention.NONE, r -> true, any() ), DynamicRule.class.getSimpleName() );
}


@Override
public void onMatch( AlgOptRuleCall call ) {
AlgNode alg = call.alg( 0 );

//call.getPlanner().addRule( PhysRule.INSTANCE );
call.getPlanner().addRule( PhysRule.INSTANCE );
}

}


public static class PhysRule extends AlgOptRule {

public final static PhysRule INSTANCE = new PhysRule();


public PhysRule() {
super( operandJ( AbstractAlgNode.class, Convention.NONE, r -> true, any() ), AddRule.class.getSimpleName() );
super( operandJ( AlgNode.class, Convention.NONE, r -> r instanceof LeafDummyNode || r instanceof SingleDummyNode, any() ), PhysRule.class.getSimpleName() );
}


@Override
public void onMatch( AlgOptRuleCall call ) {
AlgNode alg = call.alg( 0 );

AlgNode dummy = alg.copy( alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ), List.of() );
AlgNode dummy;
if ( alg.getInputs().isEmpty() ) {
dummy = alg.copy( alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ), alg.getInputs() );
} else {
AlgNode converted = convert( alg.getInputs().get( 0 ), alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ) );
dummy = alg.copy( alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ), List.of( converted ) );
}

call.transformTo( dummy );
}

}


public static class RootConverterRule extends AlgOptRule {

public RootConverterRule() {
super( operandJ( LeafDummyNode.class, Convention.NONE, r -> true, any() ), RootConverterRule.class.getSimpleName() );
}


@Override
public void onMatch( AlgOptRuleCall call ) {
AlgNode alg = call.alg( 0 );
//AlgNode input = convert( alg.getInput( 0 ), alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ) );

call.transformTo( new SingleDummyNode( alg.getCluster(), alg.getTraitSet().replace( PHYS_CALLING_CONVENTION ), alg ) );
}

}


public static class AddRule extends AlgOptRule {

public AddRule() {
Expand Down Expand Up @@ -161,7 +208,7 @@ public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
@Override
public AlgOptCost computeSelfCost( AlgOptPlanner planner, AlgMetadataQuery mq ) {
if ( traitSet.contains( PHYS_CALLING_CONVENTION ) ) {
return planner.getCostFactory().makeCost( 1d, 1d, 1d );
return planner.getCostFactory().makeTinyCost();
}
return planner.getCostFactory().makeInfiniteCost();
}
Expand All @@ -178,17 +225,17 @@ protected AlgDataType deriveRowType() {
}


public static class NoneDummyNode extends AbstractAlgNode {
public static class LeafDummyNode extends AbstractAlgNode {


public NoneDummyNode( AlgOptCluster cluster, AlgTraitSet traitSet ) {
public LeafDummyNode( AlgOptCluster cluster, AlgTraitSet traitSet ) {
super( cluster, traitSet );
}


@Override
public String algCompareString() {
return "$" + NoneDummyNode.class;
return "$" + LeafDummyNode.class;
}


Expand All @@ -204,14 +251,14 @@ protected AlgDataType deriveRowType() {
@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
assert inputs.isEmpty();
return new NoneDummyNode( getCluster(), traitSet );
return new LeafDummyNode( getCluster(), traitSet );
}


@Override
public AlgOptCost computeSelfCost( AlgOptPlanner planner, AlgMetadataQuery mq ) {
if ( traitSet.contains( PHYS_CALLING_CONVENTION ) ) {
return planner.getCostFactory().makeCost( 1d, 1d, 1d );
return planner.getCostFactory().makeTinyCost();
}
return planner.getCostFactory().makeInfiniteCost();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public static JdbcConvention of( SqlDialect dialect, Expression expression, Stri
@Override
public void register( AlgOptPlanner planner ) {
for ( AlgOptRule rule : JdbcRules.rules( this ) ) {
planner.addRule( rule );
planner.addRuleDuringRuntime( rule );
}
planner.addRule( FilterSetOpTransposeRule.INSTANCE );
planner.addRuleDuringRuntime( FilterSetOpTransposeRule.INSTANCE );
//planner.addRule( ProjectRemoveRule.INSTANCE );
}

Expand Down
Loading

0 comments on commit b950a71

Please sign in to comment.