Repository: alibaba/GraphScope (language: python)

Issue:
[BUG] Bugs in results parsing in GOpt-based GIE
**Describe the bug**

As titled. Run the below query on LDBC dataset, and it outputs errors:
```
gremlin> g.V(72057594037928869,72057594037929065,72061992084440103).as("a").outE("KNOWS").as("b").inV().as("c").select("a","b","c").by(elementMap()).by(elementMap()).by(elementMap())
ErrorCode: GREMLIN_INVALID_RESULT
Message: cannot parse label value=12 from expected type=[VertexLabel(PERSON), VertexLabel(POST), VertexLabel(TAG), VertexLabel(ORGANISATION), VertexLabel(PLACE), VertexLabel(TAGCLASS), VertexLabel(COMMENT), VertexLabel(FORUM)], expected ids are [1, 3, 7, 5, 0, 6, 2, 4]
EC: 03-0108
QueryId: 6708415933427455369
```

This seems related to type inference. In this query, the source vertices are actually of type "PERSON".

In addtion, the below query can output correctly:
```
gremlin> g.V().hasLabel("PERSON").limit(3).as("a").outE("KNOWS").as("b").inV().as("c").select("a","b","c").by(elementMap()).by(elementMap()).by(elementMap()).limit(1)
==>{a={label=PERSON, id=72072987200717017, id=15393162789081, firstName=Luigi, lastName=Donati, gender=male, birthday=19840519000000000, creationDate=20110330090650534, locationIP=46.19.235.19, browserUsed=Internet Explorer}, b={label=KNOWS, id=760157, creationDate=20120410180120834}, c={label=PERSON, id=72086181340250473, id=28587302322537, firstName=Anh, lastName=Nguyen, gender=male, birthday=19801120000000000, creationDate=20120330053305154, locationIP=117.6.98.6, browserUsed=Internet Explorer}}
```


Discussion / hints:
/cc @shirly121 @BingqingLyu, this issus/pr has had no activity for for a long time, could you folks help to review the status ? 
 To suppress further notifications,
 - for issues, 
   - if it is waiting for further response from the reporter/author, please help to add the label `requires-further-info`, 
   - if you have already started working on it, please add the label `work-in-progress` to the issue, 
   - if this issue requires further designing discussion and not in current plan, or won't be fixed, please add the label `requires-further-discussion` or `wontfix` to the issue, 
 - for pull requests, 
   - if you are still working on it and it is not ready for reviewing, please convert this pull request as draft PR, 
   - if you have decided to hold this development on, please add the `requires-further-discussion` label to the pull request. 
 Thanks!

Fix patch:
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<RelDataType> types) {
         if (types.stream().anyMatch(t -> t instanceof GraphLabelType)) {
+            // union all labels
+            List<GraphLabelType.Entry> 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);


Test patch:
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 @@
+<!--
+  ~ /*
+  ~  * 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.
+  ~  */
+  -->
+
+<configuration>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        　　　　　<encoder>
+        　　　　　　　　　<pattern>[%d{ISO8601}][%p][%t][%c:%L] %m%n</pattern>
+        　　　　　</encoder>
+        　　　</appender>
+
+    <root level="WARN">
+        <appender-ref ref="STDOUT" />
+    </root>
+</configuration>