[ 
https://issues.apache.org/jira/browse/IGNITE-26335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18073512#comment-18073512
 ] 

Andrey Mashenkov commented on IGNITE-26335:
-------------------------------------------

The reason is that Calcite simplifies aggregate function call and replace it 
with literal of different type.
RelBuilder calls this method that ignores function return type and creates 
generic literal.
{code:java}
private static @Nullable RexNode constant(RexBuilder rexBuilder,
    ImmutableBitSet groupSet, ImmutableList<ImmutableBitSet> groupSets,
    AggregateCall aggregateCall) {
  // GROUPING(c1, ..., cN) evaluates to zero if every grouping set contains
  // all of c1, ..., cN. For example,
  //
  //   SELECT GROUPING(deptno) AS gd, GROUPING(job) AS gj
  //   FROM Emp
  //   GROUP BY GROUPING SETS (deptno), (deptno, job);
  //
  // "gd" is zero for all rows, because both grouping sets contain "deptno";
  // "gj" is 0 for some rows and 1 for others.
  //
  // Internally we allow GROUPING() with no arguments; it always
  // evaluates to zero.
  final ImmutableBitSet argSet =
      ImmutableBitSet.of(aggregateCall.getArgList());
  if (groupSets.stream().allMatch(set -> set.contains(argSet))) {
    return rexBuilder.makeExactLiteral(BigDecimal.ZERO); <-- here a call return 
type should be passed.
  }
 {code}

> Sql. Failed to parse query with GROUPING aggregate over VALUES.
> ---------------------------------------------------------------
>
>                 Key: IGNITE-26335
>                 URL: https://issues.apache.org/jira/browse/IGNITE-26335
>             Project: Ignite
>          Issue Type: Bug
>          Components: sql ai3
>            Reporter: Andrey Mashenkov
>            Assignee: Andrey Mashenkov
>            Priority: Major
>              Labels: ignite-3
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> Query with GROUPING aggregate over single group and values failed: 
> "Conversion to relational algebra failed to preserve datatypes"
> {noformat}
> SELECT GROUPING(f2), f1
> FROM ( VALUES ... ) as t(f1,f2)
> GROUP BY ALL GROUPING SETS ((f2, f1)){noformat}
> Similar query over 2 values and duplicate groups failed with 
> "java.lang.AssertionError: type mismatch:", when rewriting group sets to 
> UNIONĀ 
> {code:java}
> SELECT GROUPING(f2), f1
> FROM ( VALUES (1,1), (2,2)) as t(f1,f2)
> GROUP BY ALL GROUPING SETS ((f2, f1), (f1, f2)){code}
> The error is similar in both cases, Calcite build projection with incorrect 
> rowType.
> The GROUPING function returns BIGINT, but Calcite sets INTEGER to project 
> without a cast.
> Looks like a bug in Calcite and there is no workaround.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to