Skip to content

Commit

Permalink
fix(interactive): Fix the bug in the RexNode to Proto Conversion (#3171)
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?
For a specific expression, i.e. `a.age-(b.age-1)`, there is an issue
when converting it to a proto structure, resulting in missing
parentheses in the generated proto; it gets converted to
`a.age-b.age-1`. This PR is meant to address the issue.

<!-- 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
  • Loading branch information
shirly121 authored Sep 4, 2023
1 parent b9dcbe6 commit 2d1d139
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private OuterExpression.Expression visitBinaryOperator(RexCall call) {

private boolean needBrace(SqlOperator operator, RexNode operand) {
return operand instanceof RexCall
&& ((RexCall) operand).getOperator().getLeftPrec() < operator.getLeftPrec();
&& ((RexCall) operand).getOperator().getLeftPrec() <= operator.getLeftPrec();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.runtime;

import com.alibaba.graphscope.common.ir.Utils;
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;
import com.alibaba.graphscope.common.ir.tools.config.GraphOpt;
import com.alibaba.graphscope.common.ir.tools.config.LabelConfig;
import com.alibaba.graphscope.common.ir.tools.config.SourceConfig;
import com.alibaba.graphscope.common.utils.FileUtils;
import com.google.protobuf.util.JsonFormat;

import org.apache.calcite.rex.RexNode;
import org.junit.Assert;
import org.junit.Test;

public class RexToProtoTest {
// convert expression 'a.age - (1-a.age)' to proto
@Test
public void test_expression_with_brace() throws Exception {
GraphBuilder builder =
Utils.mockGraphBuilder()
.source(
new SourceConfig(
GraphOpt.Source.VERTEX,
new LabelConfig(false).addLabel("person"),
"a"));

RexNode braceExpr =
builder.call(
GraphStdOperatorTable.MINUS,
builder.variable("a", "age"),
builder.call(
GraphStdOperatorTable.MINUS,
builder.literal(1),
builder.variable("a", "age")));
RexToProtoConverter converter = new RexToProtoConverter(true, false);
Assert.assertEquals(
FileUtils.readJsonFromResource("proto/expression_with_brace.json"),
JsonFormat.printer().print(braceExpr.accept(converter)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"operators": [{
"var": {
"tag": {
"id": 0
},
"property": {
"key": {
"name": "age"
}
},
"nodeType": {
"dataType": "INT32"
}
},
"nodeType": {
"dataType": "INT32"
}
}, {
"arith": "SUB",
"nodeType": {
"dataType": "INT32"
}
}, {
"brace": "LEFT_BRACE"
}, {
"const": {
"i32": 1
},
"nodeType": {
"dataType": "INT32"
}
}, {
"arith": "SUB",
"nodeType": {
"dataType": "INT32"
}
}, {
"var": {
"tag": {
"id": 0
},
"property": {
"key": {
"name": "age"
}
},
"nodeType": {
"dataType": "INT32"
}
},
"nodeType": {
"dataType": "INT32"
}
}, {
"brace": "RIGHT_BRACE"
}]
}

0 comments on commit 2d1d139

Please sign in to comment.