[
https://issues.apache.org/jira/browse/CALCITE-2966?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16817510#comment-16817510
]
Danny Chan commented on CALCITE-2966:
-------------------------------------
[~julian.feinauer]
I have got the reason, for PREV($UP.x, $t5), we first got a RexCall (<($t6,
$t8), which comes from the RexProgram, which $t6 and $t8 are all LocalRefs in
RexProgram), then we invoke[1], the local refs are replaced by real exprs and
we got the code you write:
{code:java}
map.put(PREV, new CallImplementor() {
@Override public Expression implement(RexToLixTranslator translator,
RexCall call, NullAs nullAs) {
final RexNode node = call.getOperands().get(0);
final RexNode offset = call.getOperands().get(1);
final Expression offs = Expressions.multiply(translator.translate(offset),
Expressions.constant(-1));
((EnumerableMatch.PrevInputGetter) translator.inputGetter).setOffset(offs);
return translator.translate(node); // where cause the bug.
}
});
{code}
The nullAs we passed in is NullAs.IS_NOT_NUlL, but when we do not take it when
return, and finally we got an inferred NullAs.NULL from[2], and finally we
translate the expr as[3], which cause this problem.
Well, here is where causes this bug.
[1]
https://github.com/apache/calcite/blob/38e3c3015b661f8f9dc61d8d0ef4be6186a4bc7a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java#L693
[2]
https://github.com/apache/calcite/blob/38e3c3015b661f8f9dc61d8d0ef4be6186a4bc7a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java#L180
[3]
https://github.com/apache/calcite/blob/38e3c3015b661f8f9dc61d8d0ef4be6186a4bc7a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java#L1128
> Problem with Code Generation
> ----------------------------
>
> Key: CALCITE-2966
> URL: https://issues.apache.org/jira/browse/CALCITE-2966
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.20.0
> Reporter: Danny Chan
> Assignee: Julian Feinauer
> Priority: Major
> Fix For: 1.20.0
>
>
> From the mailing list:
> Hi all,
> I have some problems with the code generation from Linq4j which I'm unable to
> resolve myself.
> Basically, I want to translate a condition from Rex to a Linq4j expression to
> use it in generated code.
> In my example the Condition is from Match Recognize and in SQL is:
> `up."commission" > prev(up."commission")`.
>
> ```
> RexBuilder rexBuilder = new RexBuilder(implementor.getTypeFactory());
> RexProgramBuilder rexProgramBuilder = new
> RexProgramBuilder(physType.getRowType(), rexBuilder);
>
> rexProgramBuilder.addCondition(entry.getValue());
>
> final Expression condition =
> RexToLixTranslator.translateCondition(rexProgramBuilder.getProgram(),
> (JavaTypeFactory) getCluster().getTypeFactory(),
> builder2,
> inputGetter1,
> implementor.allCorrelateVariables,
> implementor.getConformance());
>
> builder2.add(Expressions.return_(null, condition));
> ```
>
> Here, the condition seems okay, it is: ">(PREV(UP.$4, 0), PREV(UP.$4, 1))",
> so it should be a comparison of two variables (I rewrite the PREV with a
> custom Input Getter".
> But, the generated code (for Janino) is:
>
> ```
> Object p1 = row_.get($L4J$C$0_1);
> org.apache.calcite.test.JdbcTest.Employee p0 =
> (org.apache.calcite.test.JdbcTest.Employee) p1;
> Object p3 = row_.get($L4J$C$1_1);
> org.apache.calcite.test.JdbcTest.Employee p2 =
> (org.apache.calcite.test.JdbcTest.Employee) p3;
> Object p5 = row_.get($L4J$C$0_1);
> org.apache.calcite.test.JdbcTest.Employee p4 =
> (org.apache.calcite.test.JdbcTest.Employee) p5;
> Object p7 = row_.get($L4J$C$1_1);
> org.apache.calcite.test.JdbcTest.Employee p6 =
> (org.apache.calcite.test.JdbcTest.Employee) p7;
> return p0.commission && p2.commission && p4.commission > p6.commission;
> ```
>
> This confuses me a lot as I do not know where the check for p0.commission and
> p2.commission comes from.
> It seems that Linq4j adds them as it expects these variables to be nullable,
> but I have no idea on how to avoid this.
> These fields are Numeric so I always get a compilation exception.
>
> Can someone help me with this issue?
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)