Skip to content

Commit

Permalink
Correctly serialize SubstitutionGraph in EntityArg
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-weber committed May 24, 2024
1 parent 780692f commit 47a0866
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
9 changes: 2 additions & 7 deletions core/src/main/codegen/PolyAlgParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,9 @@ PolyValue PolyNode() :
<POLY_NODE> <LPAR>
[t = <IDENTIFIER> {name = new PolyString( t.image );}]

[<COLON>
(<COLON>
t = <IDENTIFIER> {labels.add(new PolyString( t.image ));}
|
(<LBRACK>
t = <IDENTIFIER> {labels.add(new PolyString( t.image ));}
(<SEP> t = <IDENTIFIER> {labels.add(new PolyString( t.image ));})*
<RBRACK>)
]
)*

[<LCURL>[
key = <IDENTIFIER> <EQUALS> (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,7 @@ private String visitPolyEdge( PolyEdge edge, boolean withPrefix ) {

private String visitGraphLabelProps( PolyList<PolyString> lbls, PolyDictionary props, PolyString varName ) {
String name = varName.isNull() ? "" : varName.toString();
String labels = lbls.toString();
if ( lbls.size() < 2 ) {
labels = labels.substring( 1, labels.length() - 1 );
}
String labels = String.join(":", lbls.stream().map( PolyString::toString ).toList());
String properties = props.map.toString();
if ( properties.equals( "{}" ) ) {
properties = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.polyalg.PolyAlgDeclaration.ParamType;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.catalog.entity.logical.LogicalGraph.SubstitutionGraph;
import org.polypheny.db.catalog.entity.logical.LogicalNamespace;
import org.polypheny.db.catalog.logistic.DataModel;
import org.polypheny.db.catalog.snapshot.Snapshot;
import org.polypheny.db.type.entity.PolyString;

public class EntityArg implements PolyAlgArg {

Expand All @@ -46,8 +48,12 @@ public EntityArg( Entity entity, Snapshot snapshot, DataModel model ) {
this.entity = entity;

if ( model == DataModel.GRAPH || entity.dataModel == DataModel.GRAPH ) {
// origin or target data model is graph -> only namespaceName is relevant
this.entityName = null;
// origin or target data model is graph
if ( entity instanceof SubstitutionGraph sub && !sub.names.isEmpty() ) {
this.entityName = String.join( ".", sub.names.stream().map( PolyString::toString ).toList() );
} else {
this.entityName = null;
}
} else {
this.entityName = entity.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -161,15 +162,15 @@ private PolyAlgArgs buildArgs( PolyAlgDeclaration decl, List<PolyAlgNamedArgumen
continue;
} else {
p = decl.getPos( i ); // TODO: handle invalid arguments
if (p == null) {
if ( p == null ) {
throw new GenericRuntimeException( "Too many positional arguments were given for " + decl.opName );
}
}
} else {
noMorePosArgs = true;

p = decl.getParam( name );
if (p == null) {
if ( p == null ) {
throw new GenericRuntimeException( "Unexpected keyword argument '" + name + "' for " + decl.opName );
}
}
Expand Down Expand Up @@ -257,7 +258,7 @@ private PolyAlgArg convertExpression( Parameter p, PolyAlgExpression exp, String
}
case AGGREGATE -> new AggArg( convertAggCall( exp, alias, ctx ) );
case LAX_AGGREGATE -> new LaxAggArg( convertLaxAggCall( exp, alias, ctx ) );
case ENTITY -> new EntityArg( convertEntity( exp ), snapshot, ctx.dataModel );
case ENTITY -> new EntityArg( convertEntity( exp, ctx ), snapshot, ctx.dataModel );
case JOIN_TYPE_ENUM -> new EnumArg<>( exp.toEnum( JoinAlgType.class ), pType );
case SEMI_JOIN_TYPE_ENUM -> new EnumArg<>( exp.toEnum( SemiJoinType.class ), pType );
case MODIFY_OP_ENUM -> new EnumArg<>( exp.toEnum( Modify.Operation.class ), pType );
Expand Down Expand Up @@ -361,8 +362,8 @@ private RexNode convertRexLiteral( PolyAlgLiteral literal, AlgDataType type, Con
}


private Entity convertEntity( PolyAlgExpression exp ) {
String[] names = exp.toIdentifier().split( "\\.", 3 );
private Entity convertEntity( PolyAlgExpression exp, Context ctx ) {
String[] names = exp.toIdentifier().split( "\\.", 2 );
GenericRuntimeException exception = new GenericRuntimeException( "Invalid entity name: " + String.join( ".", names ) );
String namespaceName;
String entityName = null;
Expand All @@ -374,12 +375,14 @@ private Entity convertEntity( PolyAlgExpression exp ) {
} else {
throw exception;
}

LogicalNamespace ns = snapshot.getNamespace( namespaceName ).orElseThrow( () -> new GenericRuntimeException( "no namespace named " + namespaceName ) );
return switch ( ns.dataModel ) {
case RELATIONAL -> {
if ( entityName == null ) {
yield new SubstitutionGraph( ns.id, "sub", false, ns.caseSensitive, List.of() );
} else if ( ctx.dataModel == DataModel.GRAPH ) {
List<String> subNames = Arrays.asList( entityName.split( "\\." ) );
yield new SubstitutionGraph( ns.id, "sub", false, ns.caseSensitive, subNames.stream().map( PolyString::of ).toList() );
}
yield snapshot.rel().getTable( ns.id, entityName ).orElseThrow( () -> exception );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ private void attachQueryPlans( AlgRoot logicalRoot ) {
AlgOptUtil.dumpPlan( "Logical Query Plan", logicalRoot.alg, ExplainFormat.JSON, ExplainLevel.ALL_ATTRIBUTES ) );
queryAnalyzer.registerInformation( informationQueryPlan );

testPolyAlgParserDuringDevelopment(logicalRoot.alg); // TODO: Delete as soon as PolyAlgParser is working

attachPolyAlgPlan(logicalRoot.alg);
//testPolyAlgParserDuringDevelopment(logicalRoot.alg); // TODO: Delete as soon as PolyAlgParser is working
}

private void testPolyAlgParserDuringDevelopment(AlgNode alg) {
AlgDataTypeFactory factory = AlgDataTypeFactory.DEFAULT;
AlgCluster cluster = AlgCluster.create( new VolcanoPlanner(), new RexBuilder( factory ), null, null );
Snapshot snapshot = Catalog.snapshot();

attachPolyAlgPlan(alg);

StringBuilder sb = new StringBuilder();
alg.buildPolyAlgebra( sb, "" );

Expand Down Expand Up @@ -304,12 +304,10 @@ private void testPolyAlgParserDuringDevelopment(AlgNode alg) {
}

private void attachPolyAlgPlan(AlgNode alg) {
System.out.println( "===== Logical Query Plan as JSON =====" );
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = alg.serializePolyAlgebra(objectMapper);
try {
String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode);
System.out.println(jsonString);

InformationManager queryAnalyzer = statement.getTransaction().getQueryAnalyzer();
InformationPage page = new InformationPage( "PolyAlg Query Plan" ).setLabel( "plans" );
Expand Down
1 change: 1 addition & 0 deletions webui/src/main/java/org/polypheny/db/webui/Crud.java
Original file line number Diff line number Diff line change
Expand Up @@ -2955,6 +2955,7 @@ public void buildPlanFromPolyAlg( final Context ctx ) {
AlgNode node = PolyPlanBuilder.buildFromPolyAlg( request.polyAlg ).alg;
ctx.json( node.serializePolyAlgebra( new ObjectMapper() ) );
} catch ( Exception e ) {
e.printStackTrace(); // TODO: remove after when PolyAlg is finished
ctx.json( Map.of( "errorMsg", e.getMessage() ) );
ctx.status( 400 );
}
Expand Down

0 comments on commit 47a0866

Please sign in to comment.