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

Vladimir Steshin updated IGNITE-24567:
--------------------------------------
    Description: 
*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}






  was:
*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}
class org.apache.ignite.internal.processors.query.IgniteSQLException: An error 
occurred while query executing - Subquery returned more than 1 value.

        at 
org.apache.ignite.internal.processors.query.calcite.exec.rel.RootNode.checkException(RootNode.java:330)
        at 
org.apache.ignite.internal.processors.query.calcite.exec.rel.RootNode.exchangeBuffers(RootNode.java:317)
        at 
org.apache.ignite.internal.processors.query.calcite.exec.rel.RootNode.hasNext(RootNode.java:216)
        at 
org.apache.ignite.internal.processors.query.calcite.exec.ClosableIteratorsHolder$DelegatingIterator.hasNext(ClosableIteratorsHolder.java:122)
        at 
org.apache.ignite.internal.processors.query.calcite.util.ConvertingClosableIterator.hasNext(ConvertingClosableIterator.java:71)
        at 
org.apache.ignite.internal.processors.query.calcite.util.ListFieldsQueryCursor.getAll(ListFieldsQueryCursor.java:105)
        at 
org.apache.ignite.internal.processors.query.calcite.util.ListFieldsQueryCursor.getAll(ListFieldsQueryCursor.java:87)
        at 
org.apache.ignite.internal.processors.query.calcite.QueryChecker.check(QueryChecker.java:442)
        at 
org.apache.ignite.internal.processors.query.calcite.integration.AggregatesIntegrationTest.test0(AggregatesIntegrationTest.java:52)
        at 
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at 
org.apache.ignite.testframework.junits.GridAbstractTest$6.run(GridAbstractTest.java:2507)
        at java.base/java.lang.Thread.run(Thread.java:829)
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 in agg 
SINGLE_VALUE
        at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
        at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
        at 
org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:164)
        at 
org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:228)
        at 
org.apache.calcite.test.CalciteAssert.assertQuery(CalciteAssert.java:571)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.lambda$returns$1(CalciteAssert.java:1542)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.withConnection(CalciteAssert.java:1481)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returns(CalciteAssert.java:1540)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returns(CalciteAssert.java:1530)
        at 
org.apache.calcite.test.CalciteAssert$AssertQuery.returnsOrdered(CalciteAssert.java:1556)
        at org.apache.calcite.test.JdbcTest.testTest0(JdbcTest.java:5417)
        at 
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at 
org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
        at 
org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
        at 
org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
        at 
org.junit.jupiter.engine.extension.SameThreadTimeoutInvocation.proceed(SameThreadTimeoutInvocation.java:45)
        at 
org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
        at 
org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
        at 
org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
        at 
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
        at 
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
        at 
org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
        at 
org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
        at 
org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
        at 
org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
        at 
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
        at 
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
        at 
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)
        at 
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)
        at 
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
        at 
org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
        at 
org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
        at 
org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
        at 
org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
        at 
org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
        at 
org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
        at 
org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
        at 
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
        at 
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
        at 
org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
        at 
java.base/java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
        at 
java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        at 
java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        at 
java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        at 
java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        at 
java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
        Suppressed: org.apache.calcite.util.TestUtil$ExtraInformation: With 
materializationsEnabled=false, limit=-1
                at 
app//org.apache.calcite.util.TestUtil.rethrow(TestUtil.java:402)
                at 
app//org.apache.calcite.test.CalciteAssert.assertQuery(CalciteAssert.java:610)
                ... 65 more
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}







> Calcite. SINGLE_VALUE function may raise exception with subquery.
> -----------------------------------------------------------------
>
>                 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