From 608f814d35b5d4567a17d310c147179e75e12fb9 Mon Sep 17 00:00:00 2001 From: Xiaoli Zhou Date: Tue, 28 Nov 2023 10:14:59 +0800 Subject: [PATCH] fix(interactive): Support Start Tag for Graph Operators in Compiler (#3376) ## What do these changes do? In the query 'g.V().as('a')...select('a').out()', the 'out()' operator must begin from the 'a' tag. The pull request (PR) modifies the compiler's graph operator structure to fulfill this requirement. ## Related issue number Fixes --- .../common/ir/planner/GraphFieldTrimmer.java | 8 +- .../rel/graph/AbstractBindableTableScan.java | 17 ++++- .../ir/rel/graph/GraphLogicalExpand.java | 14 ++-- .../common/ir/rel/graph/GraphLogicalGetV.java | 14 ++-- .../ir/rel/graph/GraphLogicalPathExpand.java | 32 +++++++- .../common/ir/rel/type/AliasNameWithId.java | 40 ++++++++++ .../ir/runtime/ffi/RelToFfiConverter.java | 15 ++++ .../common/ir/tools/GraphBuilder.java | 39 +++++++++- .../common/ir/tools/config/ExpandConfig.java | 14 ++++ .../common/ir/tools/config/GetVConfig.java | 14 ++++ .../ir/tools/config/PathExpandConfig.java | 34 ++++++++- .../graphscope/common/jna/IrCoreLibrary.java | 6 ++ .../graphscope/common/ir/ExpandTest.java | 75 +++++++++++++++++++ 13 files changed, 298 insertions(+), 24 deletions(-) create mode 100644 interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/type/AliasNameWithId.java diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/planner/GraphFieldTrimmer.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/planner/GraphFieldTrimmer.java index 46ba9855b8b8..f417464a631b 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/planner/GraphFieldTrimmer.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/planner/GraphFieldTrimmer.java @@ -14,7 +14,10 @@ import com.alibaba.graphscope.common.ir.rex.RexPermuteGraphShuttle; import com.alibaba.graphscope.common.ir.rex.RexVariableAliasCollector; import com.alibaba.graphscope.common.ir.tools.GraphBuilder; -import com.alibaba.graphscope.common.ir.tools.config.*; +import com.alibaba.graphscope.common.ir.tools.config.ExpandConfig; +import com.alibaba.graphscope.common.ir.tools.config.GetVConfig; +import com.alibaba.graphscope.common.ir.tools.config.LabelConfig; +import com.alibaba.graphscope.common.ir.tools.config.SourceConfig; import com.alibaba.graphscope.common.ir.type.GraphNameOrId; import com.alibaba.graphscope.common.ir.type.GraphProperty; import com.alibaba.graphscope.common.ir.type.GraphSchemaType; @@ -447,7 +450,8 @@ public TrimResult trimFields(GraphLogicalPathExpand pathExpand, UsedFields field pathExpand.getFetch(), pathExpand.getResultOpt(), pathExpand.getPathOpt(), - pathExpand.getAliasName()); + pathExpand.getAliasName(), + pathExpand.getStartAlias()); return result(newPathExpand, mapping, pathExpand); } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/AbstractBindableTableScan.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/AbstractBindableTableScan.java index fed1448e8e14..19c661e67470 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/AbstractBindableTableScan.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/AbstractBindableTableScan.java @@ -16,6 +16,7 @@ package com.alibaba.graphscope.common.ir.rel.graph; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.rel.type.TableConfig; import com.alibaba.graphscope.common.ir.tools.AliasInference; import com.alibaba.graphscope.common.ir.type.GraphSchemaType; @@ -55,12 +56,15 @@ public abstract class AbstractBindableTableScan extends TableScan { protected final int aliasId; + protected final AliasNameWithId startAlias; + protected AbstractBindableTableScan( GraphOptCluster cluster, List hints, @Nullable RelNode input, TableConfig tableConfig, - @Nullable String aliasName) { + @Nullable String aliasName, + AliasNameWithId startAlias) { super( cluster, RelTraitSet.createEmpty(), @@ -74,6 +78,7 @@ protected AbstractBindableTableScan( AliasInference.inferDefault( aliasName, AliasInference.getUniqueAliasList(input, true)); this.aliasId = cluster.getIdGenerator().generate(this.aliasName); + this.startAlias = Objects.requireNonNull(startAlias); } protected AbstractBindableTableScan( @@ -81,7 +86,7 @@ protected AbstractBindableTableScan( List hints, TableConfig tableConfig, String aliasName) { - this(cluster, hints, null, tableConfig, aliasName); + this(cluster, hints, null, tableConfig, aliasName, AliasNameWithId.DEFAULT); } @Override @@ -132,6 +137,10 @@ public RelWriter explainTerms(RelWriter pw) { return pw.itemIf("input", input, !Objects.isNull(input)) .item("tableConfig", tableConfig) .item("alias", AliasInference.SIMPLE_NAME(getAliasName())) + .itemIf( + "startAlias", + startAlias.getAliasName(), + startAlias.getAliasName() != AliasInference.DEFAULT_NAME) .itemIf("fusedProject", project, !ObjectUtils.isEmpty(project)) .itemIf("fusedFilter", filters, !ObjectUtils.isEmpty(filters)); } @@ -156,4 +165,8 @@ public void setFilters(ImmutableList filters) { public @Nullable ImmutableList getFilters() { return filters; } + + public AliasNameWithId getStartAlias() { + return startAlias; + } } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalExpand.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalExpand.java index bdd0bafe5da4..90612cdc6ffb 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalExpand.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalExpand.java @@ -16,6 +16,7 @@ package com.alibaba.graphscope.common.ir.rel.graph; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.rel.type.TableConfig; import com.alibaba.graphscope.common.ir.tools.config.GraphOpt; @@ -37,8 +38,9 @@ protected GraphLogicalExpand( RelNode input, GraphOpt.Expand opt, TableConfig tableConfig, - @Nullable String alias) { - super(cluster, hints, input, tableConfig, alias); + @Nullable String alias, + AliasNameWithId startAlias) { + super(cluster, hints, input, tableConfig, alias, startAlias); this.opt = opt; } @@ -48,8 +50,9 @@ public static GraphLogicalExpand create( RelNode input, GraphOpt.Expand opt, TableConfig tableConfig, - @Nullable String alias) { - return new GraphLogicalExpand(cluster, hints, input, opt, tableConfig, alias); + @Nullable String alias, + AliasNameWithId startAlias) { + return new GraphLogicalExpand(cluster, hints, input, opt, tableConfig, alias, startAlias); } public GraphOpt.Expand getOpt() { @@ -69,6 +72,7 @@ public GraphLogicalExpand copy(RelTraitSet traitSet, List inputs) { inputs.get(0), this.getOpt(), this.tableConfig, - this.getAliasName()); + this.getAliasName(), + this.getStartAlias()); } } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalGetV.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalGetV.java index f739a78380ee..9a4aa9500653 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalGetV.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalGetV.java @@ -16,6 +16,7 @@ package com.alibaba.graphscope.common.ir.rel.graph; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.rel.type.TableConfig; import com.alibaba.graphscope.common.ir.tools.config.GraphOpt; @@ -37,8 +38,9 @@ protected GraphLogicalGetV( RelNode input, GraphOpt.GetV opt, TableConfig tableConfig, - @Nullable String alias) { - super(cluster, hints, input, tableConfig, alias); + @Nullable String alias, + AliasNameWithId startAlias) { + super(cluster, hints, input, tableConfig, alias, startAlias); this.opt = opt; } @@ -48,8 +50,9 @@ public static GraphLogicalGetV create( RelNode input, GraphOpt.GetV opt, TableConfig tableConfig, - @Nullable String alias) { - return new GraphLogicalGetV(cluster, hints, input, opt, tableConfig, alias); + @Nullable String alias, + AliasNameWithId startAlias) { + return new GraphLogicalGetV(cluster, hints, input, opt, tableConfig, alias, startAlias); } public GraphOpt.GetV getOpt() { @@ -69,6 +72,7 @@ public GraphLogicalGetV copy(RelTraitSet traitSet, List inputs) { inputs.get(0), this.getOpt(), this.tableConfig, - this.getAliasName()); + this.getAliasName(), + this.getStartAlias()); } } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalPathExpand.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalPathExpand.java index b8858e07b00f..e56deff31892 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalPathExpand.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/graph/GraphLogicalPathExpand.java @@ -16,6 +16,7 @@ package com.alibaba.graphscope.common.ir.rel.graph; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.tools.AliasInference; import com.alibaba.graphscope.common.ir.tools.config.GraphOpt; import com.alibaba.graphscope.common.ir.type.GraphPathType; @@ -52,6 +53,8 @@ public class GraphLogicalPathExpand extends SingleRel { private final int aliasId; + private final AliasNameWithId startAlias; + protected GraphLogicalPathExpand( GraphOptCluster cluster, @Nullable List hints, @@ -62,7 +65,8 @@ protected GraphLogicalPathExpand( @Nullable RexNode fetch, GraphOpt.PathExpandResult resultOpt, GraphOpt.PathExpandPath pathOpt, - @Nullable String aliasName) { + @Nullable String aliasName, + AliasNameWithId startAlias) { super(cluster, RelTraitSet.createEmpty(), input); this.expand = Objects.requireNonNull(expand); this.getV = Objects.requireNonNull(getV); @@ -74,6 +78,7 @@ protected GraphLogicalPathExpand( AliasInference.inferDefault( aliasName, AliasInference.getUniqueAliasList(input, true)); this.aliasId = cluster.getIdGenerator().generate(this.aliasName); + this.startAlias = Objects.requireNonNull(startAlias); } public static GraphLogicalPathExpand create( @@ -86,9 +91,20 @@ public static GraphLogicalPathExpand create( @Nullable RexNode fetch, GraphOpt.PathExpandResult resultOpt, GraphOpt.PathExpandPath pathOpt, - String aliasName) { + String aliasName, + AliasNameWithId startAlias) { return new GraphLogicalPathExpand( - cluster, hints, input, expand, getV, offset, fetch, resultOpt, pathOpt, aliasName); + cluster, + hints, + input, + expand, + getV, + offset, + fetch, + resultOpt, + pathOpt, + aliasName, + startAlias); } @Override @@ -100,7 +116,11 @@ public RelWriter explainTerms(RelWriter pw) { .itemIf("fetch", fetch, fetch != null) .item("path_opt", getPathOpt()) .item("result_opt", getResultOpt()) - .item("alias", AliasInference.SIMPLE_NAME(getAliasName())); + .item("alias", AliasInference.SIMPLE_NAME(getAliasName())) + .itemIf( + "start_alias", + startAlias.getAliasName(), + startAlias.getAliasName() != AliasInference.DEFAULT_NAME); } public String getAliasName() { @@ -135,6 +155,10 @@ public RelNode getGetV() { return fetch; } + public AliasNameWithId getStartAlias() { + return startAlias; + } + @Override protected RelDataType deriveRowType() { ObjectUtils.requireNonEmpty( diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/type/AliasNameWithId.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/type/AliasNameWithId.java new file mode 100644 index 000000000000..c45db4a48fe3 --- /dev/null +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/rel/type/AliasNameWithId.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Alibaba Group Holding Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.graphscope.common.ir.rel.type; + +import com.alibaba.graphscope.common.ir.tools.AliasInference; + +public class AliasNameWithId { + public static final AliasNameWithId DEFAULT = + new AliasNameWithId(AliasInference.DEFAULT_NAME, AliasInference.DEFAULT_ID); + + private final String aliasName; + private final int aliasId; + + public AliasNameWithId(String aliasName, int aliasId) { + this.aliasName = aliasName; + this.aliasId = aliasId; + } + + public String getAliasName() { + return aliasName; + } + + public int getAliasId() { + return aliasId; + } +} diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/runtime/ffi/RelToFfiConverter.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/runtime/ffi/RelToFfiConverter.java index 02d0cf48ced7..3f5ed5a9beae 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/runtime/ffi/RelToFfiConverter.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/runtime/ffi/RelToFfiConverter.java @@ -108,6 +108,11 @@ public RelNode visit(GraphLogicalExpand expand) { if (expand.getAliasId() != AliasInference.DEFAULT_ID) { checkFfiResult(LIB.setEdgexpdAlias(ptrExpand, ArgUtils.asAlias(expand.getAliasId()))); } + if (expand.getStartAlias().getAliasId() != AliasInference.DEFAULT_ID) { + checkFfiResult( + LIB.setEdgexpdVtag( + ptrExpand, ArgUtils.asNameOrId(expand.getStartAlias().getAliasId()))); + } checkFfiResult( LIB.setEdgexpdMeta( ptrExpand, @@ -149,6 +154,11 @@ public RelNode visit(GraphLogicalGetV getV) { if (getV.getAliasId() != AliasInference.DEFAULT_ID) { checkFfiResult(LIB.setGetvAlias(ptrGetV, ArgUtils.asAlias(getV.getAliasId()))); } + if (getV.getStartAlias().getAliasId() != AliasInference.DEFAULT_ID) { + checkFfiResult( + LIB.setGetvTag( + ptrGetV, ArgUtils.asNameOrId(getV.getStartAlias().getAliasId()))); + } checkFfiResult( LIB.setGetvMeta( ptrGetV, @@ -175,6 +185,11 @@ public RelNode visit(GraphLogicalPathExpand pxd) { if (pxd.getAliasId() != AliasInference.DEFAULT_ID) { checkFfiResult(LIB.setPathxpdAlias(ptrPxd, ArgUtils.asAlias(pxd.getAliasId()))); } + if (pxd.getStartAlias().getAliasId() != AliasInference.DEFAULT_ID) { + checkFfiResult( + LIB.setPathxpdTag( + ptrPxd, ArgUtils.asNameOrId(pxd.getStartAlias().getAliasId()))); + } return new PhysicalNode(pxd, ptrPxd); } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/GraphBuilder.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/GraphBuilder.java index 523d3fced72a..c851059137a4 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/GraphBuilder.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/GraphBuilder.java @@ -27,6 +27,7 @@ import com.alibaba.graphscope.common.ir.rel.graph.match.AbstractLogicalMatch; import com.alibaba.graphscope.common.ir.rel.graph.match.GraphLogicalMultiMatch; import com.alibaba.graphscope.common.ir.rel.graph.match.GraphLogicalSingleMatch; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.rel.type.TableConfig; import com.alibaba.graphscope.common.ir.rel.type.group.GraphAggCall; import com.alibaba.graphscope.common.ir.rel.type.group.GraphGroupKeys; @@ -66,6 +67,7 @@ import java.math.BigDecimal; import java.util.*; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -143,7 +145,13 @@ public GraphBuilder expand(ExpandConfig config) { input, config.getOpt(), getTableConfig(config.getLabels(), GraphOpt.Source.EDGE), - config.getAlias()); + config.getAlias(), + getAliasNameWithId( + config.getStartAlias(), + (RelDataType type) -> + (type instanceof GraphSchemaType) + && ((GraphSchemaType) type).getScanOpt() + == GraphOpt.Source.VERTEX)); replaceTop(expand); return this; } @@ -163,7 +171,14 @@ public GraphBuilder getV(GetVConfig config) { input, config.getOpt(), getTableConfig(config.getLabels(), GraphOpt.Source.VERTEX), - config.getAlias()); + config.getAlias(), + getAliasNameWithId( + config.getStartAlias(), + (RelDataType type) -> + (type instanceof GraphSchemaType) + && ((GraphSchemaType) type).getScanOpt() + == GraphOpt.Source.EDGE + || type instanceof GraphPathType)); replaceTop(getV); return this; } @@ -199,7 +214,13 @@ public GraphBuilder pathExpand(PathExpandConfig pxdConfig) { fetchNode, pxdConfig.getResultOpt(), pxdConfig.getPathOpt(), - pxdConfig.getAlias()); + pxdConfig.getAlias(), + getAliasNameWithId( + pxdConfig.getStartAlias(), + (RelDataType type) -> + (type instanceof GraphSchemaType) + && ((GraphSchemaType) type).getScanOpt() + == GraphOpt.Source.VERTEX)); replaceTop(pathExpand); return this; } @@ -231,6 +252,18 @@ public TableConfig getTableConfig(LabelConfig labelConfig, GraphOpt.Source opt) return new TableConfig(relOptTables).isAll(labelConfig.isAll()); } + private AliasNameWithId getAliasNameWithId( + @Nullable String aliasName, Predicate checkType) { + aliasName = (aliasName == null) ? AliasInference.DEFAULT_NAME : aliasName; + RexGraphVariable variable = variable(aliasName); + Preconditions.checkArgument( + checkType.test(variable.getType()), + "object with tag=%s mismatch with the expected type, current type is %s", + aliasName, + variable.getType()); + return new AliasNameWithId(aliasName, variable.getAliasId()); + } + /** * get all table names for a specific {@code opt} to handle fuzzy conditions, i.e. g.V() * @param opt diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/ExpandConfig.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/ExpandConfig.java index 7773d5bcbc20..7d54f7bff2e4 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/ExpandConfig.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/ExpandConfig.java @@ -24,6 +24,7 @@ public class ExpandConfig { private final GraphOpt.Expand opt; private final LabelConfig labels; @Nullable private final String alias; + @Nullable private final String startAlias; public ExpandConfig(GraphOpt.Expand opt) { this(opt, LabelConfig.DEFAULT, null); @@ -34,9 +35,18 @@ public ExpandConfig(GraphOpt.Expand opt, LabelConfig labels) { } public ExpandConfig(GraphOpt.Expand opt, LabelConfig labels, @Nullable String alias) { + this(opt, labels, alias, null); + } + + public ExpandConfig( + GraphOpt.Expand opt, + LabelConfig labels, + @Nullable String alias, + @Nullable String startAlias) { this.opt = Objects.requireNonNull(opt); this.labels = Objects.requireNonNull(labels); this.alias = alias; + this.startAlias = startAlias; } public GraphOpt.Expand getOpt() { @@ -50,4 +60,8 @@ public LabelConfig getLabels() { public @Nullable String getAlias() { return alias; } + + public @Nullable String getStartAlias() { + return startAlias; + } } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/GetVConfig.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/GetVConfig.java index 4a887ae40efc..5c1bd377a82e 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/GetVConfig.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/GetVConfig.java @@ -24,6 +24,7 @@ public class GetVConfig { private final GraphOpt.GetV opt; private final LabelConfig labels; @Nullable private final String alias; + @Nullable private final String startAlias; public GetVConfig(GraphOpt.GetV opt) { this(opt, LabelConfig.DEFAULT, null); @@ -34,9 +35,18 @@ public GetVConfig(GraphOpt.GetV opt, LabelConfig labels) { } public GetVConfig(GraphOpt.GetV opt, LabelConfig labels, @Nullable String alias) { + this(opt, labels, alias, null); + } + + public GetVConfig( + GraphOpt.GetV opt, + LabelConfig labels, + @Nullable String alias, + @Nullable String startAlias) { this.opt = Objects.requireNonNull(opt); this.labels = Objects.requireNonNull(labels); this.alias = alias; + this.startAlias = startAlias; } public GraphOpt.GetV getOpt() { @@ -50,4 +60,8 @@ public LabelConfig getLabels() { public @Nullable String getAlias() { return alias; } + + public @Nullable String getStartAlias() { + return startAlias; + } } diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/PathExpandConfig.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/PathExpandConfig.java index 5fcb3d7e8cee..2724739c4c0c 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/PathExpandConfig.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/tools/config/PathExpandConfig.java @@ -18,6 +18,7 @@ import com.alibaba.graphscope.common.ir.rel.graph.GraphLogicalExpand; import com.alibaba.graphscope.common.ir.rel.graph.GraphLogicalGetV; +import com.alibaba.graphscope.common.ir.rel.type.AliasNameWithId; import com.alibaba.graphscope.common.ir.rex.RexGraphVariable; import com.alibaba.graphscope.common.ir.tools.AliasInference; import com.alibaba.graphscope.common.ir.tools.GraphBuilder; @@ -48,6 +49,7 @@ public class PathExpandConfig { private final GraphOpt.PathExpandResult resultOpt; @Nullable private final String alias; + @Nullable private final String startAlias; protected PathExpandConfig( RelNode expand, @@ -57,6 +59,18 @@ protected PathExpandConfig( GraphOpt.PathExpandResult resultOpt, GraphOpt.PathExpandPath pathOpt, @Nullable String alias) { + this(expand, getV, offset, fetch, resultOpt, pathOpt, alias, null); + } + + protected PathExpandConfig( + RelNode expand, + RelNode getV, + int offset, + int fetch, + GraphOpt.PathExpandResult resultOpt, + GraphOpt.PathExpandPath pathOpt, + @Nullable String alias, + @Nullable String startAlias) { this.expand = Objects.requireNonNull(expand); this.getV = Objects.requireNonNull(getV); this.offset = offset; @@ -64,6 +78,7 @@ protected PathExpandConfig( this.resultOpt = resultOpt; this.pathOpt = pathOpt; this.alias = alias; + this.startAlias = startAlias; } public static Builder newBuilder(GraphBuilder innerBuilder) { @@ -74,6 +89,10 @@ public static Builder newBuilder(GraphBuilder innerBuilder) { return alias; } + public @Nullable String getStartAlias() { + return startAlias; + } + public GraphOpt.PathExpandPath getPathOpt() { return pathOpt; } @@ -126,6 +145,7 @@ public static final class Builder { private GraphOpt.PathExpandResult resultOpt; @Nullable private String alias; + @Nullable private String startAlias; protected Builder(GraphBuilder innerBuilder) { this.innerBuilder = @@ -147,7 +167,8 @@ public Builder expand(ExpandConfig config) { config.getOpt(), innerBuilder.getTableConfig( config.getLabels(), GraphOpt.Source.EDGE), - AliasInference.DEFAULT_NAME); + AliasInference.DEFAULT_NAME, + AliasNameWithId.DEFAULT); innerBuilder.push(this.expand); } return this; @@ -163,7 +184,8 @@ public Builder getV(GetVConfig config) { config.getOpt(), innerBuilder.getTableConfig( config.getLabels(), GraphOpt.Source.VERTEX), - AliasInference.DEFAULT_NAME); + AliasInference.DEFAULT_NAME, + AliasNameWithId.DEFAULT); // (the alias of endV is given in the getV // base) innerBuilder.push(this.getV); @@ -228,8 +250,14 @@ public Builder alias(@Nullable String alias) { return this; } + public Builder startAlias(@Nullable String startAlias) { + this.startAlias = startAlias; + return this; + } + public PathExpandConfig build() { - return new PathExpandConfig(expand, getV, offset, fetch, resultOpt, pathOpt, alias); + return new PathExpandConfig( + expand, getV, offset, fetch, resultOpt, pathOpt, alias, startAlias); } public GraphBuilder getInnerBuilder() { diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/jna/IrCoreLibrary.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/jna/IrCoreLibrary.java index 107d4b583611..de5b3a255234 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/jna/IrCoreLibrary.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/jna/IrCoreLibrary.java @@ -75,6 +75,8 @@ FfiResult.ByValue appendEdgexpdOperator( FfiResult.ByValue setEdgexpdAlias(Pointer edgeXpd, FfiAlias.ByValue alias); + FfiResult.ByValue setEdgexpdVtag(Pointer edgeXpd, FfiNameOrId.ByValue vTag); + Pointer initLimitOperator(); FfiResult.ByValue setLimitRange(Pointer limit, int lower, int upper); @@ -163,6 +165,8 @@ FfiResult.ByValue appendSinkOperator( FfiResult.ByValue setGetvAlias(Pointer getV, FfiAlias.ByValue alias); + FfiResult.ByValue setGetvTag(Pointer getV, FfiNameOrId.ByValue vTag); + FfiResult.ByValue appendGetvOperator( Pointer plan, Pointer getV, int parent, IntByReference oprIdx); @@ -182,6 +186,8 @@ Pointer initPathxpdOperatorWithExpandBase( FfiResult.ByValue setPathxpdAlias(Pointer pathXpd, FfiAlias.ByValue alias); + FfiResult.ByValue setPathxpdTag(Pointer pathXpd, FfiNameOrId.ByValue vTag); + FfiResult.ByValue setPathxpdHops(Pointer pathXpd, int lower, int upper); FfiResult.ByValue setPathxpdCondition(Pointer pathXpd, String predicate); diff --git a/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/common/ir/ExpandTest.java b/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/common/ir/ExpandTest.java index fab09a6f9e71..aab3b7ca5a14 100644 --- a/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/common/ir/ExpandTest.java +++ b/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/common/ir/ExpandTest.java @@ -114,4 +114,79 @@ public void expand_3_test() { + " alias=[DEFAULT], opt=[VERTEX])", pathExpand.explain().trim()); } + + // g.V().hasLabel("person").as("a").outE("knows").select("a").outE("knows").as("b") + @Test + public void expand_4_test() { + GraphBuilder builder = Utils.mockGraphBuilder(); + RelNode expand = + builder.source( + new SourceConfig( + GraphOpt.Source.VERTEX, + new LabelConfig(false).addLabel("person"), + "a")) + .expand( + new ExpandConfig( + GraphOpt.Expand.OUT, + new LabelConfig(false).addLabel("knows"))) + .expand( + new ExpandConfig( + GraphOpt.Expand.OUT, + new LabelConfig(false).addLabel("knows"), + "b", + "a")) + .build(); + Assert.assertEquals( + "GraphLogicalExpand(tableConfig=[{isAll=false, tables=[knows]}], alias=[b]," + + " startAlias=[a], opt=[OUT])\n" + + " GraphLogicalExpand(tableConfig=[{isAll=false, tables=[knows]}]," + + " alias=[DEFAULT], opt=[OUT])\n" + + " GraphLogicalSource(tableConfig=[{isAll=false, tables=[person]}]," + + " alias=[a], opt=[VERTEX])", + expand.explain().trim()); + } + + // g.V().hasLabel("person").as("a").outE("knows").select("a").out("1..3", "knows") + @Test + public void expand_5_test() { + GraphBuilder builder = Utils.mockGraphBuilder(); + PathExpandConfig.Builder pxdBuilder = PathExpandConfig.newBuilder(builder); + PathExpandConfig pxdConfig = + pxdBuilder + .expand( + new ExpandConfig( + GraphOpt.Expand.OUT, + new LabelConfig(false).addLabel("knows"))) + .getV( + new GetVConfig( + GraphOpt.GetV.END, + new LabelConfig(false).addLabel("person"))) + .range(1, 3) + .startAlias("a") + .build(); + RelNode expand = + builder.source( + new SourceConfig( + GraphOpt.Source.VERTEX, + new LabelConfig(false).addLabel("person"), + "a")) + .expand( + new ExpandConfig( + GraphOpt.Expand.OUT, + new LabelConfig(false).addLabel("knows"))) + .pathExpand(pxdConfig) + .build(); + Assert.assertEquals( + "GraphLogicalPathExpand(expand=[GraphLogicalExpand(tableConfig=[{isAll=false," + + " tables=[knows]}], alias=[DEFAULT], opt=[OUT])\n" + + "], getV=[GraphLogicalGetV(tableConfig=[{isAll=false, tables=[person]}]," + + " alias=[DEFAULT], opt=[END])\n" + + "], offset=[1], fetch=[3], path_opt=[ARBITRARY], result_opt=[END_V]," + + " alias=[DEFAULT], start_alias=[a])\n" + + " GraphLogicalExpand(tableConfig=[{isAll=false, tables=[knows]}]," + + " alias=[DEFAULT], opt=[OUT])\n" + + " GraphLogicalSource(tableConfig=[{isAll=false, tables=[person]}]," + + " alias=[a], opt=[VERTEX])", + expand.explain().trim()); + } }