Skip to content

Commit

Permalink
fix(interactive): Implement Field Trimmer Rule in Compiler (#3200)
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?

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

## Related issue number
 #3137

<!-- Are there any issues opened that will be resolved by merging this
change? -->
Just a draft pr for pre-review

---------

Co-authored-by: shirly121 <[email protected]>
  • Loading branch information
JialuGong and shirly121 authored Oct 30, 2023
1 parent 648209d commit 9de9055
Show file tree
Hide file tree
Showing 8 changed files with 1,151 additions and 5 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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.
*/

package com.alibaba.graphscope.common.ir.planner.rules;

import com.alibaba.graphscope.common.ir.planner.GraphFieldTrimmer;
import com.alibaba.graphscope.common.ir.tools.GraphBuilder;

import org.apache.calcite.rel.RelNode;

public abstract class FieldTrimRule {
public static RelNode trim(GraphBuilder builder, RelNode rel) {
final GraphFieldTrimmer trimmer = new GraphFieldTrimmer(builder);
return trimmer.trim(rel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import org.apache.calcite.rel.RelWriter;
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.rel.type.*;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.util.ImmutableIntList;
import org.apache.commons.lang3.ObjectUtils;
Expand Down Expand Up @@ -109,6 +106,13 @@ public RelDataType deriveRowType() {
return rowType;
}

public void setRowType(GraphSchemaType graphType) {
rowType =
new RelRecordType(
ImmutableList.of(
new RelDataTypeFieldImpl(getAliasName(), getAliasId(), graphType)));
}

public String getAliasName() {
return this.aliasName;
}
Expand All @@ -117,6 +121,10 @@ public int getAliasId() {
return this.aliasId;
}

public TableConfig getTableConfig() {
return this.tableConfig;
}

// toString

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.graphscope.common.ir.rex;

import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexPermuteInputsShuttle;
import org.apache.calcite.util.mapping.Mappings;

public class RexPermuteGraphShuttle extends RexPermuteInputsShuttle {
private final Mappings.TargetMapping mapping;

/**
* Creates a RexPermuteInputsShuttle.
*
* <p>The mapping provides at most one target for every source. If a source
* has no targets and is referenced in the expression,
* {@link Mappings.TargetMapping#getTarget(int)}
* will give an error. Otherwise the mapping gives a unique target.
*
* @param mapping Mapping
* @param inputs Input relational expressions
*/
public RexPermuteGraphShuttle(Mappings.TargetMapping mapping, RelNode... inputs) {
super(mapping, inputs);
this.mapping = mapping;
}

@Override
public RexNode visitInputRef(RexInputRef local) {
final int index = local.getIndex();
int target = mapping.getTarget(index);
return (local instanceof RexGraphVariable)
? visitGraphVariable((RexGraphVariable) local)
: new RexInputRef(target, local.getType());
}

public RexNode visitGraphVariable(RexGraphVariable variable) {
final int index = variable.getIndex(); // resource column id
int target = mapping.getTarget(index);

return variable.getProperty() == null
? RexGraphVariable.of(
variable.getAliasId(), target, variable.getName(), variable.getType())
: RexGraphVariable.of(
variable.getAliasId(),
variable.getProperty(),
target,
variable.getName(),
variable.getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ public GraphBuilder match(RelNode first, Iterable<? extends RelNode> others) {
return this;
}

@Override
public GraphBuilder push(RelNode node) {
super.push(node);
return this;
}

public RexNode getJoinCondition(RelNode first, RelNode second) {
List<RexNode> conditions = Lists.newArrayList();
List<RelDataTypeField> firstFields =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.graphscope.common.ir.meta.reader.LocalMetaDataReader;
import com.alibaba.graphscope.common.ir.meta.schema.GraphOptSchema;
import com.alibaba.graphscope.common.ir.meta.schema.IrGraphSchema;
import com.alibaba.graphscope.common.ir.planner.rules.FieldTrimRule;
import com.alibaba.graphscope.common.ir.planner.rules.FilterMatchRule;
import com.alibaba.graphscope.common.ir.planner.rules.NotMatchToAntiJoinRule;
import com.alibaba.graphscope.common.ir.runtime.PhysicalBuilder;
Expand Down Expand Up @@ -128,12 +129,17 @@ public LogicalPlan planLogical() {
GraphBuilder graphBuilder =
GraphBuilder.create(
null, this.optCluster, new GraphOptSchema(this.optCluster, schema));

LogicalPlan logicalPlan = logicalPlanFactory.create(graphBuilder, irMeta, query);

// apply optimizations
if (plannerConfig.isOn()
&& logicalPlan.getRegularQuery() != null
&& !logicalPlan.isReturnEmpty()) {
RelNode regularQuery = logicalPlan.getRegularQuery();
if (plannerConfig.getRules().contains(FieldTrimRule.class.getSimpleName())) {
regularQuery = FieldTrimRule.trim(graphBuilder, regularQuery);
}
RelOptPlanner planner = this.optCluster.getPlanner();
planner.setRoot(regularQuery);
logicalPlan =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static RelDataType getOutputType(RelNode topNode) {
List<RelNode> inputs = Lists.newArrayList(topNode);
while (!inputs.isEmpty()) {
RelNode cur = inputs.remove(0);
outputFields.addAll(cur.getRowType().getFieldList());
outputFields.addAll(0, cur.getRowType().getFieldList());
if (AliasInference.removeAlias(cur)) {
break;
}
Expand Down
Loading

0 comments on commit 9de9055

Please sign in to comment.