Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.5.0
    • Fix Version/s: 1.6.0
    • Component/s: core
    • Labels:
      None

      Description

      select a."value" a, b."value" b from "bools" a, "bools" b offset 0
      Error while executing command OkCommand [sql: select a."value" a, b."value" b from "bools" a, "bools" b offset 0
      ]
      java.lang.AssertionError: Internal error: While invoking method 'public org.apache.calcite.sql2rel.RelFieldTrimmer$TrimResult org.apache.calcite.sql2rel.RelFieldTrimmer.trimFields(org.apache.calcite.rel.core.Sort,org.apache.calcite.util.ImmutableBitSet,java.util.Set)'
              at org.apache.calcite.util.Util.newInternal(Util.java:792)
              at org.apache.calcite.util.ReflectUtil$2.invoke(ReflectUtil.java:534)
              at org.apache.calcite.sql2rel.RelFieldTrimmer.dispatchTrimFields(RelFieldTrimmer.java:248)
              at org.apache.calcite.sql2rel.RelFieldTrimmer.trim(RelFieldTrimmer.java:155)
              at org.apache.calcite.sql2rel.SqlToRelConverter.trimUnusedFields(SqlToRelConverter.java:520)
              at org.apache.calcite.prepare.Prepare.trimUnusedFields(Prepare.java:347)
              at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:264)
              at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:190)
              at org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:727)
              at org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:586)
              at org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:556)
              at org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:214)
              at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:573)
              at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:571)
              at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:136)
              at org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:186)
              at net.hydromatic.quidem.Quidem$CheckResultCommand.execute(Quidem.java:778)
              at net.hydromatic.quidem.Quidem$CompositeCommand.execute(Quidem.java:1312)
              at net.hydromatic.quidem.Quidem.execute(Quidem.java:171)
              at org.apache.calcite.test.JdbcTest.checkRun(JdbcTest.java:4801)
              at org.apache.calcite.test.JdbcTest.testBooleansInWhere(JdbcTest.java:4317)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:497)
              at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
              at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
              at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
              at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
              at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
              at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
              at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
              at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
              at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
              at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:497)
              at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
      Caused by: java.lang.reflect.InvocationTargetException
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:497)
              at org.apache.calcite.util.ReflectUtil$2.invoke(ReflectUtil.java:531)
              ... 45 more
      Caused by: java.lang.AssertionError: trivial sort
              at org.apache.calcite.rel.core.Sort.<init>(Sort.java:96)
              at org.apache.calcite.rel.logical.LogicalSort.<init>(LogicalSort.java:37)
              at org.apache.calcite.rel.logical.LogicalSort.create(LogicalSort.java:63)
              at org.apache.calcite.rel.core.RelFactories$SortFactoryImpl.createSort(RelFactories.java:141)
              at org.apache.calcite.tools.RelBuilder.sortLimit(RelBuilder.java:1263)
              at org.apache.calcite.sql2rel.RelFieldTrimmer.trimFields(RelFieldTrimmer.java:491)
              ... 50 more
      

        Activity

        Hide
        zhenw zhen wang added a comment - - edited

        workspace/apache/calcite/core/src/main/java/org/apache/calcite/rel/core/Sort.java

            assert !(fetch == null
                && offset == null
                && collation.getFieldCollations().isEmpty())
                : "trivial sort";
        

        removing the assertion yield correct result.
        sort node occurs simply because of the `offset` in query. what is a trivial sort anyways? meaning no sort need to be performed?

        it feels to me this is not that important, will someone specify `offset 0` ?

        Show
        zhenw zhen wang added a comment - - edited workspace/apache/calcite/core/src/main/java/org/apache/calcite/rel/core/Sort.java assert !(fetch == null && offset == null && collation.getFieldCollations().isEmpty()) : "trivial sort" ; removing the assertion yield correct result. sort node occurs simply because of the `offset` in query. what is a trivial sort anyways? meaning no sort need to be performed? it feels to me this is not that important, will someone specify `offset 0` ?
        Hide
        vladimirsitnikov Vladimir Sitnikov added a comment -

        offset 0 is wildly used for PostgreSQL as an optimization fence.
        I was trying to use offset 0 here to workaround "wrong results from cross join" bug.

        Show
        vladimirsitnikov Vladimir Sitnikov added a comment - offset 0 is wildly used for PostgreSQL as an optimization fence. I was trying to use offset 0 here to workaround "wrong results from cross join" bug.
        Hide
        zhenw zhen wang added a comment -

        so the correct result should be : no sort node (as it is trivial) should be created in the first place ?

        Show
        zhenw zhen wang added a comment - so the correct result should be : no sort node (as it is trivial) should be created in the first place ?
        Hide
        julianhyde Julian Hyde added a comment -

        zhen wang, Correct. No Sort should be created. I think the bug is in RelBuilder.sortLimit. RelBuilder methods are supposed to just "do the right thing", which in this case is to omit the Sort.

        Show
        julianhyde Julian Hyde added a comment - zhen wang , Correct. No Sort should be created. I think the bug is in RelBuilder.sortLimit. RelBuilder methods are supposed to just "do the right thing", which in this case is to omit the Sort.
        Hide
        zhenw zhen wang added a comment -

        https://github.com/apache/calcite/pull/181
        still some questions
        1. if the trivial sort is removed at first, how would the `optimisation fence` Vladimir Sitnikov mentioned apply in later stages
        2. the outer join result not correct, what's the JIRA? Vladimir Sitnikov

        Show
        zhenw zhen wang added a comment - https://github.com/apache/calcite/pull/181 still some questions 1. if the trivial sort is removed at first, how would the `optimisation fence` Vladimir Sitnikov mentioned apply in later stages 2. the outer join result not correct, what's the JIRA? Vladimir Sitnikov
        Hide
        julianhyde Julian Hyde added a comment -

        It may work as an "optimization fence" on Postgres but it is not a fence on Calcite nor do we intend it to be. (We don't have any optimization fences.) Vladimir Sitnikov was just trying something, and as it is a valid SQL query, it should have worked and not thrown an assertion error.

        Your patch is a good start, but you also need to fix RelBuilder.sortLimit, and add a test to RelBuilderTest. That bug is present, and will be hit by people using the RelBuilder API if you don't fix it.

        In SqlToRelConverter.convertOrder, convert an offset of 0 to null then I think the trivial sort will get skipped.

        Show
        julianhyde Julian Hyde added a comment - It may work as an "optimization fence" on Postgres but it is not a fence on Calcite nor do we intend it to be. (We don't have any optimization fences.) Vladimir Sitnikov was just trying something, and as it is a valid SQL query, it should have worked and not thrown an assertion error. Your patch is a good start, but you also need to fix RelBuilder.sortLimit, and add a test to RelBuilderTest. That bug is present, and will be hit by people using the RelBuilder API if you don't fix it. In SqlToRelConverter.convertOrder, convert an offset of 0 to null then I think the trivial sort will get skipped.
        Hide
        zhenw zhen wang added a comment -

        updated to fix RelBuilder. removed fix in sqlToRelConverter

        in SqlToRelConverter.convertOrder, firstly in the error case, the `getOrderList` is empty instead of null, this case need to be guarded. secondly, as everything is in node need to do the cast to convert to `null`

        and that case will get correctly fixed by RelBuilder later, so do we still want the fix in it ?

        https://github.com/apache/calcite/pull/181/files

        Show
        zhenw zhen wang added a comment - updated to fix RelBuilder. removed fix in sqlToRelConverter in SqlToRelConverter.convertOrder, firstly in the error case, the `getOrderList` is empty instead of null, this case need to be guarded. secondly, as everything is in node need to do the cast to convert to `null` and that case will get correctly fixed by RelBuilder later, so do we still want the fix in it ? https://github.com/apache/calcite/pull/181/files
        Hide
        julianhyde Julian Hyde added a comment -
        Show
        julianhyde Julian Hyde added a comment - Fixed in http://git-wip-us.apache.org/repos/asf/calcite/commit/f12aa72e . zhen wang , thanks for the PR!
        Hide
        julianhyde Julian Hyde added a comment -

        Resolved in release 1.6.0 (2016-01-22).

        Show
        julianhyde Julian Hyde added a comment - Resolved in release 1.6.0 (2016-01-22).

          People

          • Assignee:
            julianhyde Julian Hyde
            Reporter:
            vladimirsitnikov Vladimir Sitnikov
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development