[
https://issues.apache.org/jira/browse/CALCITE-4848?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17427770#comment-17427770
]
Thomas Rebele commented on CALCITE-4848:
----------------------------------------
I've debugged this a bit: In the HEP phase, ReduceExpressionsRule tries to
reduce <($0, 1000). The reducer infers that $0 is the same as ?0. The HEP
planner has a DataContexts.EmptyDataContext, where every param is null. So
<($0, 1000) evaluates <(?0, 1000). The expression is then evaluated with the
following generated code:
{code:java}
public Object[] apply(Object root0) {
final Integer value_dynamic_param = (Integer)
((org.apache.calcite.DataContext) root0).get("?0");
return new Object[] {
value_dynamic_param != null && value_dynamic_param.intValue() < 1000};
} {code}
EmptyDataContext.get(String) returns null, so the generated code returns false.
The LogicalFilter(condition=false) gets simplified to
EnumerableValues(tuples=[[]]).
How can we solve this? Should the constancy deduction logic in
ReduceExpressionsRule be changed so that dynamic parameters are considered as
UNKNOWN?
> Adding a HAVING condition to a query with a dynamic parameter makes the
> result empty
> ------------------------------------------------------------------------------------
>
> Key: CALCITE-4848
> URL: https://issues.apache.org/jira/browse/CALCITE-4848
> Project: Calcite
> Issue Type: Bug
> Affects Versions: 1.27.0
> Reporter: Thomas Rebele
> Priority: Major
> Attachments: CALCITE-4848.patch
>
>
> The query
> {code:java}
> SELECT sal, COUNT(1) AS count_val
> FROM emp t WHERE sal = ?
> GROUP BY sal HAVING sal < 1000 {code}
> gets translated to the following logical plan:
> {code:java}
> LogicalFilter(condition=[<($0, 1000)])
> LogicalAggregate(group=[{0}], COUNT_VAL=[COUNT()])
> LogicalProject(SAL=[$5], $f1=[1])
> LogicalFilter(condition=[=($5, ?0)])
> LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
> However, applying CoreRules.FILTER_REDUCE_EXPRESSIONS to the plan it just
> becomes
> {code:java}
> LogicalValues(tuples=[[]]) {code}
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)