Skip to content

Commit

Permalink
[GIE Compiler] support labels and type function in cypher queries
Browse files Browse the repository at this point in the history
  • Loading branch information
shirly121 committed Sep 22, 2023
1 parent 6ab796e commit d446d40
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 323 deletions.
2 changes: 2 additions & 0 deletions docs/interactive_engine/neo4j/supported_cypher.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Note that some Aggregator operators, such as `max()`, we listed here are impleme
| Branch | Use with `Project` and `Return` | CASE WHEN | CASE WHEN | <input type="checkbox" disabled />| planned |
| Scalar | Returns the length of a path | length() | length() | <input type="checkbox" disabled checked /> | |
| List | Fold expressions into a single list | [] | [] | <input type="checkbox" disabled checked /> | |
| Labels | Get label name of a vertex type | labels() | labels() | <input type="checkbox" disabled checked /> | |
| Type | Get label name of an edge type | type() | type() | <input type="checkbox" disabled checked /> | |



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.alibaba.graphscope.common.ir.tools.config.GraphOpt;
import com.alibaba.graphscope.common.ir.type.GraphLabelType;
import com.alibaba.graphscope.common.ir.type.GraphSchemaType;
import com.alibaba.graphscope.common.ir.type.GraphSchemaTypeList;
import com.alibaba.graphscope.groot.common.schema.api.*;

import org.apache.calcite.linq4j.tree.Expression;
Expand Down Expand Up @@ -76,27 +75,32 @@ private RelDataType deriveType(GraphElement element) {
}
if (element instanceof GraphVertex) {
GraphLabelType labelType =
(new GraphLabelType()).label(element.getLabel()).labelId(element.getLabelId());
new GraphLabelType(
new GraphLabelType.Entry()
.label(element.getLabel())
.labelId(element.getLabelId()));
return new GraphSchemaType(GraphOpt.Source.VERTEX, labelType, fields);
} else if (element instanceof GraphEdge) {
GraphEdge edge = (GraphEdge) element;
List<EdgeRelation> relations = edge.getRelationList();
List<GraphSchemaType> fuzzyTypes = new ArrayList<>();
for (EdgeRelation relation : relations) {
GraphLabelType labelType =
(new GraphLabelType())
GraphLabelType.Entry labelEntry =
new GraphLabelType.Entry()
.label(element.getLabel())
.labelId(element.getLabelId());
GraphVertex src = relation.getSource();
GraphVertex dst = relation.getTarget();
labelType.srcLabel(src.getLabel()).dstLabel(dst.getLabel());
labelType.srcLabelId(src.getLabelId()).dstLabelId(dst.getLabelId());
fuzzyTypes.add(new GraphSchemaType(GraphOpt.Source.EDGE, labelType, fields));
labelEntry.srcLabel(src.getLabel()).dstLabel(dst.getLabel());
labelEntry.srcLabelId(src.getLabelId()).dstLabelId(dst.getLabelId());
fuzzyTypes.add(
new GraphSchemaType(
GraphOpt.Source.EDGE, new GraphLabelType(labelEntry), fields));
}
ObjectUtils.requireNonEmpty(fuzzyTypes);
return (fuzzyTypes.size() == 1)
? fuzzyTypes.get(0)
: GraphSchemaTypeList.create(fuzzyTypes);
: GraphSchemaType.create(fuzzyTypes, getRelOptSchema().getTypeFactory());
} else {
throw new IllegalArgumentException("element should be vertex or edge");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
import com.alibaba.graphscope.common.ir.type.GraphSchemaTypeList;
import com.google.common.collect.ImmutableList;

import org.apache.calcite.plan.GraphOptCluster;
Expand All @@ -30,6 +29,7 @@
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.hint.RelHint;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
import org.apache.calcite.rel.type.RelRecordType;
import org.apache.calcite.rex.RexNode;
Expand Down Expand Up @@ -91,20 +91,17 @@ protected AbstractBindableTableScan(
public RelDataType deriveRowType() {
List<GraphSchemaType> tableTypes = new ArrayList<>();
List<RelOptTable> tables = ObjectUtils.requireNonEmpty(this.tableConfig.getTables());
RelDataTypeFactory typeFactory = tables.get(0).getRelOptSchema().getTypeFactory();
for (RelOptTable table : tables) {
GraphSchemaType type = (GraphSchemaType) table.getRowType();
// flat fuzzy labels to the list
if (type instanceof GraphSchemaTypeList) {
tableTypes.addAll((GraphSchemaTypeList) type);
} else {
tableTypes.add(type);
}
tableTypes.addAll(type.getSchemaTypeAsList());
}
ObjectUtils.requireNonEmpty(tableTypes);
GraphSchemaType graphType =
(tableTypes.size() == 1)
? tableTypes.get(0)
: GraphSchemaTypeList.create(tableTypes);
: GraphSchemaType.create(tableTypes, typeFactory);
RelRecordType rowType =
new RelRecordType(
ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.alibaba.graphscope.common.ir.type.GraphLabelType;
import com.alibaba.graphscope.common.ir.type.GraphProperty;
import com.alibaba.graphscope.common.ir.type.GraphSchemaType;
import com.alibaba.graphscope.common.ir.type.GraphSchemaTypeList;
import com.alibaba.graphscope.common.jna.IrCoreLibrary;
import com.alibaba.graphscope.common.jna.type.*;
import com.alibaba.graphscope.gaia.proto.OuterExpression;
Expand Down Expand Up @@ -442,7 +441,7 @@ private List<RexGraphVariable> getLeftRightVariables(RexNode condition) {

private Pointer ffiQueryParams(AbstractBindableTableScan tableScan) {
Set<Integer> uniqueLabelIds =
getGraphLabels(tableScan).stream()
getGraphLabels(tableScan).getLabelsEntry().stream()
.map(k -> k.getLabelId())
.collect(Collectors.toSet());
Pointer params = LIB.initQueryParams();
Expand Down Expand Up @@ -570,7 +569,7 @@ private void addFfiBinder(Pointer ptrSentence, RelNode binder, boolean isTail) {

private void addFilterToFfiBinder(Pointer ptrSentence, AbstractBindableTableScan tableScan) {
Set<Integer> uniqueLabelIds =
getGraphLabels(tableScan).stream()
getGraphLabels(tableScan).getLabelsEntry().stream()
.map(k -> k.getLabelId())
.collect(Collectors.toSet());
// add labels as select operator
Expand Down Expand Up @@ -602,20 +601,14 @@ private void addFilterToFfiBinder(Pointer ptrSentence, AbstractBindableTableScan
}
}

private List<GraphLabelType> getGraphLabels(AbstractBindableTableScan tableScan) {
private GraphLabelType getGraphLabels(AbstractBindableTableScan tableScan) {
List<RelDataTypeField> fields = tableScan.getRowType().getFieldList();
Preconditions.checkArgument(
!fields.isEmpty() && fields.get(0).getType() instanceof GraphSchemaType,
"data type of graph operators should be %s ",
GraphSchemaType.class);
GraphSchemaType schemaType = (GraphSchemaType) fields.get(0).getType();
List<GraphLabelType> labelTypes = new ArrayList<>();
if (schemaType instanceof GraphSchemaTypeList) {
((GraphSchemaTypeList) schemaType).forEach(k -> labelTypes.add(k.getLabelType()));
} else {
labelTypes.add(schemaType.getLabelType());
}
return labelTypes;
return schemaType.getLabelType();
}

private void checkFfiResult(FfiResult res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public static final OuterExpression.ExprOpr protoOperator(SqlOperator operator)
}

public static final Common.DataType protoBasicDataType(RelDataType basicType) {
if (basicType instanceof GraphLabelType) return Common.DataType.INT32;
switch (basicType.getSqlTypeName()) {
case NULL:
return Common.DataType.NONE;
Expand Down Expand Up @@ -243,17 +244,10 @@ public static final DataType.IrDataType protoIrDataType(
DataType.GraphDataType.Builder builder = DataType.GraphDataType.newBuilder();
builder.setElementOpt(
protoElementOpt(((GraphSchemaType) dataType).getScanOpt()));
if (dataType instanceof GraphSchemaTypeList) {
((GraphSchemaTypeList) dataType)
.forEach(
k -> {
builder.addGraphDataType(
protoElementType(k, isColumnId));
});
} else {
builder.addGraphDataType(
protoElementType((GraphSchemaType) dataType, isColumnId));
}
((GraphSchemaType) dataType)
.getSchemaTypeAsList()
.forEach(
k -> builder.addGraphDataType(protoElementType(k, isColumnId)));
return DataType.IrDataType.newBuilder().setGraphType(builder.build()).build();
}
throw new UnsupportedOperationException(
Expand Down Expand Up @@ -322,14 +316,17 @@ public static final DataType.GraphDataType.GraphElementType protoElementType(

public static final DataType.GraphDataType.GraphElementLabel protoElementLabel(
GraphLabelType labelType) {
Preconditions.checkArgument(
labelType.getLabelsEntry().size() == 1,
"can not convert label=" + labelType + " to proto 'GraphElementLabel'");
GraphLabelType.Entry entry = labelType.getSingleLabelEntry();
DataType.GraphDataType.GraphElementLabel.Builder builder =
DataType.GraphDataType.GraphElementLabel.newBuilder()
.setLabel(labelType.getLabelId());
if (labelType.getSrcLabelId() != null) {
builder.setSrcLabel(Int32Value.of(labelType.getSrcLabelId()));
DataType.GraphDataType.GraphElementLabel.newBuilder().setLabel(entry.getLabelId());
if (entry.getSrcLabelId() != null) {
builder.setSrcLabel(Int32Value.of(entry.getSrcLabelId()));
}
if (labelType.getDstLabelId() != null) {
builder.setDstLabel(Int32Value.of(labelType.getDstLabelId()));
if (entry.getDstLabelId() != null) {
builder.setDstLabel(Int32Value.of(entry.getDstLabelId()));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.graphscope.common.ir.tools;

import static java.util.Objects.requireNonNull;

import com.alibaba.graphscope.common.ir.meta.schema.GraphOptSchema;
import com.alibaba.graphscope.common.ir.meta.schema.IrGraphSchema;
import com.alibaba.graphscope.common.ir.rel.GraphLogicalAggregate;
Expand All @@ -32,8 +30,8 @@
import com.alibaba.graphscope.common.ir.rel.type.group.GraphGroupKeys;
import com.alibaba.graphscope.common.ir.rel.type.order.GraphFieldCollation;
import com.alibaba.graphscope.common.ir.rel.type.order.GraphRelCollations;
import com.alibaba.graphscope.common.ir.rex.*;
import com.alibaba.graphscope.common.ir.rex.RexCallBinding;
import com.alibaba.graphscope.common.ir.rex.*;
import com.alibaba.graphscope.common.ir.tools.config.*;
import com.alibaba.graphscope.common.ir.type.GraphNameOrId;
import com.alibaba.graphscope.common.ir.type.GraphPathType;
Expand All @@ -44,7 +42,6 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import org.apache.calcite.plan.*;
import org.apache.calcite.rel.AbstractRelNode;
import org.apache.calcite.rel.RelFieldCollation;
Expand All @@ -68,6 +65,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

/**
* Integrate interfaces to build algebra structures,
* including {@link RexNode} for expressions and {@link RelNode} for operators
Expand Down Expand Up @@ -418,12 +417,13 @@ public RexGraphVariable variable(@Nullable String alias, String property) {
+ "]");
}
if (property.equals(GraphProperty.LABEL_KEY)) {
GraphSchemaType schemaType = (GraphSchemaType) aliasField.getType();
return RexGraphVariable.of(
aliasField.getIndex(),
new GraphProperty(GraphProperty.Opt.LABEL),
columnField.left,
varName,
getTypeFactory().createSqlType(SqlTypeName.CHAR));
schemaType.getLabelType());
} else if (property.equals(GraphProperty.ID_KEY)) {
return RexGraphVariable.of(
aliasField.getIndex(),
Expand Down
Loading

0 comments on commit d446d40

Please sign in to comment.