Skip to content

Commit

Permalink
fix(interactive): Fix Bugs of Alias Id in Union (#4404)
Browse files Browse the repository at this point in the history
<!--
Thanks for your contribution! please review
https://github.com/alibaba/GraphScope/blob/main/CONTRIBUTING.md before
opening an issue.
-->

## What do these changes do?
as titled.

<!-- Please give a short brief about these changes. -->

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->

Fixes #4385
  • Loading branch information
shirly121 authored Jan 6, 2025
1 parent f3a1a02 commit 54a22fb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public String getMessage() {
StringBuilder sb = new StringBuilder();
sb.append("ErrorCode: ").append(errorCode.name()).append("\n");
String msg = super.getMessage();
if (!msg.endsWith("\n")) {
if (msg == null || !msg.endsWith("\n")) {
msg += "\n";
}
sb.append("Message: ").append(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import com.google.common.collect.Maps;

import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.*;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.util.Util;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -100,6 +101,46 @@ public RelDataType createArbitraryMapType(
return super.leastRestrictive(types);
}

/**
* reimplement the {@link RelDataTypeFactoryImpl#leastRestrictiveStructuredType(List)} method
* to maintain the original alias id in the input types
* @param types
* @return
*/
@Override
protected @Nullable RelDataType leastRestrictiveStructuredType(final List<RelDataType> types) {
final RelDataType type0 = types.get(0);
if (!type0.isStruct()) {
return null;
}
final int fieldCount = type0.getFieldCount();
boolean isNullable = false;
for (RelDataType type : types) {
if (!type.isStruct()) {
return null;
}
if (type.getFieldList().size() != fieldCount) {
return null;
}
isNullable |= type.isNullable();
}
List<RelDataTypeField> fields = Lists.newArrayList();
for (int j = 0; j < fieldCount; ++j) {
final int k = j;
RelDataType type =
leastRestrictive(Util.transform(types, t -> t.getFieldList().get(k).getType()));
if (type == null) {
return null;
}
fields.add(
new RelDataTypeFieldImpl(
type0.getFieldList().get(j).getName(),
type0.getFieldList().get(j).getIndex(),
type));
}
return new RelRecordType(StructKind.FULLY_QUALIFIED, fields, isNullable);
}

// re-implement lease-restrictive type inference for arbitrary map types
// for each key type and value type, check if they have a least-restrictive type, otherwise
// return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.alibaba.graphscope.common.ir.planner.GraphIOProcessor;
import com.alibaba.graphscope.common.ir.planner.GraphRelOptimizer;
import com.alibaba.graphscope.common.ir.planner.rules.ExpandGetVFusionRule;
import com.alibaba.graphscope.common.ir.rel.GraphLogicalProject;
import com.alibaba.graphscope.common.ir.rex.RexGraphVariable;
import com.alibaba.graphscope.common.ir.runtime.proto.RexToProtoConverter;
import com.alibaba.graphscope.common.ir.tools.GraphBuilder;
import com.alibaba.graphscope.common.ir.tools.GraphStdOperatorTable;
Expand Down Expand Up @@ -1852,4 +1854,36 @@ public void g_V_match_as_a_person_both_as_b_test() {
+ " person]}], alias=[a], opt=[VERTEX])",
after3.explain().trim());
}

@Test
public void union_valueMap_test() {
Configs configs =
new Configs(
ImmutableMap.of(
"graph.planner.is.on",
"true",
"graph.planner.opt",
"CBO",
"graph.planner.rules",
"FilterIntoJoinRule, FilterMatchRule, ExtendIntersectRule,"
+ " ExpandGetVFusionRule"));
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 node1 =
eval(
"g.V().hasLabel(\"PERSON\").has(\"id\","
+ " 1816).union(outE(\"LIKES\").limit(10).as('a').inV().as('b').select('a','b').by(valueMap(\"creationDate\")),"
+ " outE(\"KNOWS\").limit(10).as('c').inV().as('d').select('c','d').by(valueMap(\"creationDate\")))",
builder);
RelNode after1 = optimizer.optimize(node1, new GraphIOProcessor(builder, irMeta));
GraphLogicalProject project = (GraphLogicalProject) after1;
RexNode expr = project.getProjects().get(0);
RexGraphVariable var = (RexGraphVariable) expr;
Assert.assertEquals(2, var.getAliasId());
}
}

0 comments on commit 54a22fb

Please sign in to comment.