[ 
https://issues.apache.org/jira/browse/IGNITE-24567?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Vladimir Steshin updated IGNITE-24567:
--------------------------------------
    Summary: Calcite. SubQueryRemoveRule creates SINGLE_VALUE which may raise 
exception.  (was: Calcite. SINGLE_VALUE function may raise exception.)

> Calcite. SubQueryRemoveRule creates SINGLE_VALUE which may raise exception.
> ---------------------------------------------------------------------------
>
>                 Key: IGNITE-24567
>                 URL: https://issues.apache.org/jira/browse/IGNITE-24567
>             Project: Ignite
>          Issue Type: Bug
>            Reporter: Vladimir Steshin
>            Priority: Minor
>              Labels: calcite, ignite-2, ise
>
> *Reproducer:*
> {code:java}
>          sql("CREATE TABLE TEST(ID INTEGER PRIMARY KEY, VAL0 INTEGER, VAL1 
> INTEGER, GRP0 INTEGER, GRP1 INTEGER)");
>         int sz = 5000;
>         for (int i = 0; i < sz; ++i)
>             sql("INSERT INTO TEST VALUES(?,?,?,?,?)", i, i % 2, i % 3, i % 4, 
> i % 5);
>         assertQuery("SELECT (SELECT val0 FROM TEST t ORDER BY 1 LIMIT 2) FROM 
> TEST").returns(0).returns(1).check();
> {code}
> *Plan:*
> {code:java}
> IgniteProject(EXPR$0=[$1])
>   IgniteNestedLoopJoin(condition=[true], joinType=[left])
>     IgniteExchange(distribution=[single])
>       IgniteIndexScan(table=[[PUBLIC, TEST]], index=[_key_PK], 
> projects=[[0]], requiredColumns=[{}], collation=[[0 ASC-nulls-first]])
>     IgniteColocatedHashAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
>       IgniteLimit(fetch=[2])
>         IgniteExchange(distribution=[single])
>           IgniteSort(sort0=[$0], dir0=[ASC-nulls-first], fetch=[2])
>             IgniteTableScan(table=[[PUBLIC, TEST]], requiredColumns=[{3}])
> {code}
> *Result:*
> {code:java}
> Caused by: java.lang.IllegalArgumentException: Subquery returned more than 1 
> value.
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.Accumulators$SingleVal.add(Accumulators.java:322)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorsFactory$AccumulatorWrapperImpl.add(AccumulatorsFactory.java:331)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.HashAggregateNode$Grouping.addOnMapper(HashAggregateNode.java:329)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.HashAggregateNode$Grouping.add(HashAggregateNode.java:304)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.HashAggregateNode.push(HashAggregateNode.java:132)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.LimitNode.push(LimitNode.java:99)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.Inbox.pushOrdered(Inbox.java:252)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.Inbox.push(Inbox.java:201)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.rel.Inbox.onBatchReceived(Inbox.java:177)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.ExchangeServiceImpl.onMessage(ExchangeServiceImpl.java:324)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.ExchangeServiceImpl.lambda$init$2(ExchangeServiceImpl.java:195)
>       at 
> org.apache.ignite.internal.processors.query.calcite.message.MessageServiceImpl.onMessageInternal(MessageServiceImpl.java:233)
>       at 
> org.apache.ignite.internal.processors.query.calcite.message.MessageServiceImpl.lambda$onMessage$0(MessageServiceImpl.java:211)
>       at 
> org.apache.ignite.internal.processors.query.calcite.exec.task.AbstractQueryTaskExecutor$SecurityAwareTask.run(AbstractQueryTaskExecutor.java:75)
>       at 
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
>       at 
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> {code}
> _SubQueryRemoveRule #rewriteScalarQuery()_ rule adds aggregate 
> _SqlStdOperatorTable.SINGLE_VALUE_ which forbids several values at input. But 
> the input is a table scan in this case.
> As far as I can see, same test in Calcite raises the same error:
> {code:java}
> // Some comments here
>   @Test void testTest0() {
>     String sql = "select (SELECT \"salary\" FROM \"hr\".\"emps\" e order by 1 
> limit 2) from \"hr\".\"emps\"";
>     CalciteAssert.hr().query(sql).returnsOrdered("salary=7000.0", 
> "salary=8000.0");
>   }
> {code}
> {code:java}
> Error while executing SQL "select (SELECT "salary" FROM "hr"."emps" e order 
> by 1 limit 2) from "hr"."emps"": more than one value in agg SINGLE_VALUE
> java.sql.SQLException: Error while executing SQL "select (SELECT "salary" 
> FROM "hr"."emps" e order by 1 limit 2) from "hr"."emps"": more than one value
> ...
> Caused by: java.lang.IllegalStateException: more than one value in agg 
> SINGLE_VALUE
>       at Baz$2.apply(Unknown Source)
>       at Baz$2.apply(Unknown Source)
>       at Baz$2.apply(Unknown Source)
>       at 
> org.apache.calcite.adapter.enumerable.BasicAggregateLambdaFactory$AccumulatorAdderSeq.apply(BasicAggregateLambdaFactory.java:81)
>       at 
> org.apache.calcite.linq4j.EnumerableDefaults.aggregate(EnumerableDefaults.java:133)
>       at 
> org.apache.calcite.linq4j.DefaultEnumerable.aggregate(DefaultEnumerable.java:107)
>       at Baz.bind(Unknown Source)
>       at 
> org.apache.calcite.jdbc.CalcitePrepare$CalciteSignature.enumerable(CalcitePrepare.java:367)
>       at 
> org.apache.calcite.jdbc.CalciteConnectionImpl.enumerable(CalciteConnectionImpl.java:335)
>       at 
> org.apache.calcite.jdbc.CalciteMetaImpl._createIterable(CalciteMetaImpl.java:657)
> {code}



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

Reply via email to