From a8952a4512b675bbd1c7572273d2354b10bff31a Mon Sep 17 00:00:00 2001 From: Xiaoli Zhou Date: Wed, 20 Nov 2024 11:15:22 +0800 Subject: [PATCH] fix(interactive): Fix Bugs of Mismatch Label in `elementMap` (#4326) ## What do these changes do? as titled. ## Related issue number fix #4252 Fixes --- .../common/ir/type/GraphTypeFactoryImpl.java | 5 ++- .../gremlin/antlr4x/GraphBuilderTest.java | 33 +++++++++++++++++++ .../src/test/resources/logback-test.xml | 29 ++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 interactive_engine/compiler/src/test/resources/logback-test.xml diff --git a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/type/GraphTypeFactoryImpl.java b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/type/GraphTypeFactoryImpl.java index 15f13bc03d6e..fdb6aa9e7e55 100644 --- a/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/type/GraphTypeFactoryImpl.java +++ b/interactive_engine/compiler/src/main/java/com/alibaba/graphscope/common/ir/type/GraphTypeFactoryImpl.java @@ -86,10 +86,13 @@ public RelDataType createArbitraryMapType( @Override public @Nullable RelDataType leastRestrictive(List types) { if (types.stream().anyMatch(t -> t instanceof GraphLabelType)) { + // union all labels + List unionLabels = Lists.newArrayList(); for (RelDataType type : types) { if (!(type instanceof GraphLabelType)) return null; + unionLabels.addAll(((GraphLabelType) type).getLabelsEntry()); } - return types.get(0); + return new GraphLabelType(unionLabels.stream().distinct().collect(Collectors.toList())); } if (types.stream().anyMatch(t -> t instanceof ArbitraryMapType)) { return leastRestrictiveForArbitraryMapType(types); diff --git a/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/gremlin/antlr4x/GraphBuilderTest.java b/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/gremlin/antlr4x/GraphBuilderTest.java index 9141dff65570..97279d78a2da 100644 --- a/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/gremlin/antlr4x/GraphBuilderTest.java +++ b/interactive_engine/compiler/src/test/java/com/alibaba/graphscope/gremlin/antlr4x/GraphBuilderTest.java @@ -28,6 +28,8 @@ import com.alibaba.graphscope.common.ir.tools.GraphStdOperatorTable; import com.alibaba.graphscope.common.ir.tools.config.GraphOpt; import com.alibaba.graphscope.common.ir.tools.config.SourceConfig; +import com.alibaba.graphscope.common.ir.type.ArbitraryMapType; +import com.alibaba.graphscope.common.ir.type.GraphLabelType; import com.alibaba.graphscope.common.ir.type.GraphProperty; import com.alibaba.graphscope.common.utils.FileUtils; import com.alibaba.graphscope.gaia.proto.OuterExpression; @@ -40,6 +42,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.rex.RexBuilder; import org.apache.calcite.rex.RexNode; import org.junit.Assert; @@ -86,6 +89,36 @@ public static RelNode eval(String query, GraphBuilder builder) { return visitor.visit(parseTree).build(); } + @Test + public void g_V_elementMap_test() { + GraphRelOptimizer optimizer = new GraphRelOptimizer(configs); + IrMeta irMeta = + Utils.mockIrMeta( + "schema/ldbc.json", + "statistics/ldbc30_statistics.json", + optimizer.getGlogueHolder()); + GraphBuilder builder = Utils.mockGraphBuilder(optimizer, irMeta); + RelNode node = + eval( + "g.V(72057594037928268).as(\"a\").outE(\"KNOWS\").as(\"b\").inV().as(\"c\").select('a'," + + " \"b\").by(elementMap())", + builder); + RelDataType projectType = node.getRowType().getFieldList().get(0).getType(); + RelDataType bValueType = projectType.getValueType(); + Assert.assertTrue(bValueType instanceof ArbitraryMapType); + GraphLabelType labelType = + (GraphLabelType) + ((ArbitraryMapType) bValueType) + .getKeyValueTypeMap().values().stream() + .filter(k -> k.getValue() instanceof GraphLabelType) + .findFirst() + .get() + .getValue(); + // make sure the inferred type contains the label type + Assert.assertTrue( + labelType.getLabelsEntry().stream().anyMatch(k -> k.getLabel().equals("KNOWS"))); + } + @Test public void g_V_test() { RelNode node = eval("g.V()"); diff --git a/interactive_engine/compiler/src/test/resources/logback-test.xml b/interactive_engine/compiler/src/test/resources/logback-test.xml new file mode 100644 index 000000000000..99b422fbc449 --- /dev/null +++ b/interactive_engine/compiler/src/test/resources/logback-test.xml @@ -0,0 +1,29 @@ + + + + +       +          [%d{ISO8601}][%p][%t][%c:%L] %m%n +       +     + + + + +