Skip to content

Commit

Permalink
Add support for converting from PolyAlg to various ParamTypes and Alg…
Browse files Browse the repository at this point in the history
…Nodes
  • Loading branch information
tobias-weber committed Apr 10, 2024
1 parent e8123e6 commit 5a2cd02
Show file tree
Hide file tree
Showing 27 changed files with 550 additions and 178 deletions.
14 changes: 10 additions & 4 deletions core/src/main/codegen/PolyAlgParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ TOKEN :
| <EQUALS: "=">
| <COLON: ":">
| <AS: "AS">
| <BOOL: (<TRUE> | <FALSE>)>
| <#TRUE: "TRUE">
| <#FALSE: "FALSE">
| <IDENTIFIER: (<LETTER> | <IDENTIFIER_SYMBOL>) (<LETTER> | <DIGIT> | <IDENTIFIER_SYMBOL>)*>
| <NUMBER: (<DIGIT>)+ (".")? (<DIGIT>)*>
| <#LETTER: ["_","a"-"z","A"-"Z","ö", "Ö", "ä", "Ä", "ü", "Ü", "à", "À", "ç","Ç", "á", "Á", "è", "È","í","Í", "î", "Î","ó","Ó","ò", "ô", "Ô", "Ò" , "í", "Í", "ë", "Ë", "â", "Â", "ï", "Ï", "é", "É", "ñ", "Ñ", "ß"] >
Expand Down Expand Up @@ -312,6 +315,8 @@ PolyAlgLiteral Literal() :
{
Token t;
boolean isNumber = false;
boolean isBoolean = false;
boolean isQuoted = false;

}
{
Expand All @@ -322,11 +327,13 @@ PolyAlgLiteral Literal() :
|
t = <SYMBOL>
|
t = <QUOTED>
t = <QUOTED> {isQuoted = true;}
|
t = <EQUALS>
|
t = <BOOL> {isBoolean = true;}
)
{return new PolyAlgLiteral(t.image, isNumber, getPos());}
{return new PolyAlgLiteral(t.image, isNumber, isQuoted, isBoolean, getPos());}

}

Expand All @@ -350,8 +357,7 @@ String AliasName() :
(
name = OpName()
|
t = <QUOTED>
{name = t.image;}
t = <QUOTED> {name = t.image;}
)
{return name;}
}
Expand Down
21 changes: 11 additions & 10 deletions core/src/main/java/org/polypheny/db/algebra/AbstractAlgNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,15 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.checkerframework.checker.units.qual.A;
import org.polypheny.db.algebra.constant.ExplainLevel;
import org.polypheny.db.algebra.core.CorrelationId;
import org.polypheny.db.algebra.core.SetOp;
import org.polypheny.db.algebra.externalize.AlgWriterImpl;
import org.polypheny.db.algebra.logical.relational.LogicalRelProject;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
Expand All @@ -62,7 +59,6 @@
import org.polypheny.db.algebra.polyalg.PolyAlgUtils;
import org.polypheny.db.algebra.polyalg.arguments.PolyAlgArgs;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgOptCost;
Expand Down Expand Up @@ -360,7 +356,10 @@ public AlgWriter explainTerms( AlgWriter pw ) {
public void buildPolyAlgebra( StringBuilder sb, String prefix ) {
final String INDENT = " ";
String nextPrefix = prefix == null ? null : prefix + INDENT;
List<String> inputFieldNames = PolyAlgUtils.uniquifiedInputFieldNames( this );
boolean makeFieldsUnique = !(this instanceof SetOp); // set operations like UNION require duplicate field names
List<String> inputFieldNames = makeFieldsUnique ?
PolyAlgUtils.uniquifiedInputFieldNames( this ) :
PolyAlgUtils.getInputFieldNamesList( this );

PolyAlgDeclaration decl = getPolyAlgDeclaration();
sb.append( prefix == null ? "" : prefix ).append( decl.opName );
Expand All @@ -374,16 +373,18 @@ public void buildPolyAlgebra( StringBuilder sb, String prefix ) {
sb.append( "(\n" );
int inputIdx = 0;
for ( AlgNode child : getInputs() ) {
List<String> projections = PolyAlgUtils.getAuxProjections( child, inputFieldNames, inputIdx );
List<String> projections = makeFieldsUnique ?
PolyAlgUtils.getAuxProjections( child, inputFieldNames, inputIdx ) :
List.of();
inputIdx += child.getTupleType().getFieldCount();

if ( projections.isEmpty() ) {
child.buildPolyAlgebra( sb, nextPrefix );
} else {
sb.append( nextPrefix )
.append( PolyAlgRegistry.getDeclaration( LogicalRelProject.class ).opName ).append( "#" ) // TODO: select Project depending on data model, logical / physical
.append( "[" ).append( PolyAlgUtils.joinMultiValued( projections, true ) ).append( "]")
.append("(\n" );
.append( "[" ).append( PolyAlgUtils.joinMultiValued( projections, true ) ).append( "]" )
.append( "(\n" );
child.buildPolyAlgebra( sb, nextPrefix == null ? null : nextPrefix + INDENT );
sb.append( ")" );
}
Expand All @@ -406,7 +407,7 @@ public void buildPolyAlgebra( StringBuilder sb, String prefix ) {
*/
@Override
public PolyAlgDeclaration getPolyAlgDeclaration() {
return PolyAlgRegistry.getDeclaration( getClass(), getInputs().size() );
return PolyAlgRegistry.getDeclaration( getClass(), getModel(), getInputs().size() );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import org.polypheny.db.algebra.AlgCollationTraitDef;
Expand All @@ -48,6 +49,8 @@
import org.polypheny.db.algebra.metadata.AlgMdCollation;
import org.polypheny.db.algebra.metadata.AlgMdDistribution;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.algebra.polyalg.arguments.CorrelationArg;
import org.polypheny.db.algebra.polyalg.arguments.ListArg;
import org.polypheny.db.algebra.polyalg.arguments.PolyAlgArgs;
import org.polypheny.db.algebra.polyalg.arguments.RexArg;
import org.polypheny.db.plan.AlgCluster;
Expand Down Expand Up @@ -104,6 +107,13 @@ public static LogicalRelFilter create( final AlgNode input, RexNode condition, I
}


public static LogicalRelFilter create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
RexArg condition = args.getArg( "condition", RexArg.class );
List<CorrelationId> variables = args.getListArg( "variables", CorrelationArg.class ).map( CorrelationArg::getCorrId );
return create( children.get( 0 ), condition.getNode(), ImmutableSet.copyOf( variables ) );
}


@Override
public ImmutableSet<CorrelationId> getVariablesSet() {
return variablesSet;
Expand Down Expand Up @@ -132,8 +142,8 @@ public AlgWriter explainTerms( AlgWriter pw ) {
@Override
public PolyAlgArgs collectAttributes() {
PolyAlgArgs args = new PolyAlgArgs( getPolyAlgDeclaration() );

args.put( 0, new RexArg( condition ) );
args.put( 0, new RexArg( condition ) )
.put( "variables", new ListArg<>( variablesSet.asList(), CorrelationArg::new ) );
return args;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public static LogicalRelIntersect create( List<AlgNode> inputs, boolean all ) {
}


public static LogicalRelIntersect create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
return create( children, args.getArg( "all", BooleanArg.class ).toBool() );
}


@Override
public LogicalRelIntersect copy( AlgTraitSet traitSet, List<AlgNode> inputs, boolean all ) {
return new LogicalRelIntersect( getCluster(), traitSet, inputs, all );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
package org.polypheny.db.algebra.logical.relational;


import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgShuttle;
Expand All @@ -44,7 +46,9 @@
import org.polypheny.db.algebra.core.relational.RelAlg;
import org.polypheny.db.algebra.polyalg.PolyAlgDeclaration.ParamType;
import org.polypheny.db.algebra.polyalg.arguments.BooleanArg;
import org.polypheny.db.algebra.polyalg.arguments.CorrelationArg;
import org.polypheny.db.algebra.polyalg.arguments.EnumArg;
import org.polypheny.db.algebra.polyalg.arguments.ListArg;
import org.polypheny.db.algebra.polyalg.arguments.PolyAlgArgs;
import org.polypheny.db.algebra.polyalg.arguments.RexArg;
import org.polypheny.db.plan.AlgCluster;
Expand All @@ -70,7 +74,6 @@ public final class LogicalRelJoin extends Join implements RelAlg {
private final boolean semiJoinDone;



/**
* Creates a LogicalJoin.
*
Expand Down Expand Up @@ -122,6 +125,15 @@ public static LogicalRelJoin create( AlgNode left, AlgNode right, RexNode condit
}


public static LogicalRelJoin create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
RexArg condition = args.getArg( "condition", RexArg.class );
EnumArg<JoinAlgType> type = args.getEnumArg( "type", JoinAlgType.class );
List<CorrelationId> variables = args.getListArg( "variables", CorrelationArg.class ).map( CorrelationArg::getCorrId );
BooleanArg semiJoinDone = args.getArg( "semiJoinDone", BooleanArg.class );
return create( children.get( 0 ), children.get( 1 ), condition.getNode(), new HashSet<>( variables ), type.getArg(), semiJoinDone.toBool() );
}


@Override
public LogicalRelJoin copy( AlgTraitSet traitSet, RexNode conditionExpr, AlgNode left, AlgNode right, JoinAlgType joinType, boolean semiJoinDone ) {
assert traitSet.containsIfApplicable( Convention.NONE );
Expand All @@ -147,14 +159,17 @@ public boolean isSemiJoinDone() {
return semiJoinDone;
}


@Override
public PolyAlgArgs collectAttributes() {
PolyAlgArgs args = new PolyAlgArgs( getPolyAlgDeclaration() );

args.put( 0, new RexArg( condition, true ) )
args.put( 0, new RexArg( condition ) )
.put( "type", new EnumArg<>( joinType, ParamType.JOIN_TYPE_ENUM ) )
.put( "variables", new ListArg<>( variablesSet.asList(), CorrelationArg::new ) )
.put( "semiJoinDone", new BooleanArg( semiJoinDone ) );
return args;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public static LogicalRelMinus create( List<AlgNode> inputs, boolean all ) {
}


public static LogicalRelMinus create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
return create( children, args.getArg( "all", BooleanArg.class ).toBool() );
}


@Override
public LogicalRelMinus copy( AlgTraitSet traitSet, List<AlgNode> inputs, boolean all ) {
assert traitSet.containsIfApplicable( Convention.NONE );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,9 @@ public static LogicalRelProject create( final AlgNode input, final List<? extend
}


public static AlgNode create(PolyAlgArgs args, List<AlgNode> children) {
System.out.println("Creating AlgNode from LogicalRelProject");
ListArg<RexArg> projects= args.getArg( 0, ListArg.class );


return create( children.get( 0 ), projects.getArgs().stream().map( RexArg::getNode ).toList(), projects.getAliases() );
public static LogicalRelProject create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
ListArg<RexArg> projects = args.getListArg( 0, RexArg.class );
return create( children.get( 0 ), projects.map( RexArg::getNode ), projects.map( RexArg::getAlias ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgShuttle;
import org.polypheny.db.algebra.core.relational.RelScan;
import org.polypheny.db.algebra.polyalg.arguments.EntityArg;
import org.polypheny.db.algebra.polyalg.arguments.PolyAlgArgs;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgTraitSet;
Expand Down Expand Up @@ -126,6 +128,10 @@ public static LogicalRelScan create( AlgCluster cluster, final Entity entity ) {
return new LogicalRelScan( cluster, traitSet, entity );
}

public static LogicalRelScan create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster) {
return create( cluster, args.getArg( 0, EntityArg.class ).getEntity());
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.algebra.AlgCollation;
import org.polypheny.db.algebra.AlgCollationTraitDef;
import org.polypheny.db.algebra.AlgCollations;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgShuttle;
import org.polypheny.db.algebra.core.Sort;
Expand Down Expand Up @@ -87,6 +88,14 @@ public static AlgNode create( AlgNode input, List<RexNode> fieldExps, AlgCollati
}


public static LogicalRelSort create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
ListArg<CollationArg> collations = args.getListArg( "sort", CollationArg.class );
RexArg limit = args.getArg( "limit", RexArg.class );
RexArg offset = args.getArg( "offset", RexArg.class );
return create( children.get( 0 ), AlgCollations.of( collations.map( CollationArg::getColl ) ), offset.getNode(), limit.getNode() );
}


@Override
public Sort copy( AlgTraitSet traitSet, AlgNode newInput, AlgCollation newCollation, ImmutableList<RexNode> fieldExps, RexNode offset, RexNode fetch ) {
return new LogicalRelSort( getCluster(), traitSet, newInput, newCollation, fieldExps, offset, fetch );
Expand All @@ -105,7 +114,7 @@ public PolyAlgArgs collectAttributes() {

PolyAlgArg collArg = new ListArg<>(
collation.getFieldCollations(),
c -> new CollationArg( c ),
CollationArg::new,
args.getDecl().canUnpackValues() );

args.put( "sort", collArg )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public static LogicalRelUnion create( List<AlgNode> inputs, boolean all ) {
}


public static LogicalRelUnion create( PolyAlgArgs args, List<AlgNode> children, AlgCluster cluster ) {
return create( children, args.getArg( "all", BooleanArg.class ).toBool() );
}


@Override
public LogicalRelUnion copy( AlgTraitSet traitSet, List<AlgNode> inputs, boolean all ) {
assert traitSet.containsIfApplicable( Convention.NONE );
Expand Down
Loading

0 comments on commit 5a2cd02

Please sign in to comment.