Derby
  1. Derby
  2. DERBY-6027

ORDER BY a cast expression gives NPE

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 10.2.1.6, 10.2.2.0, 10.3.1.4, 10.3.2.1, 10.3.3.0, 10.4.1.3, 10.4.2.0, 10.5.1.1, 10.5.2.0, 10.5.3.0, 10.6.1.0, 10.6.2.1, 10.7.1.1, 10.8.1.2, 10.8.2.2, 10.9.1.0
    • Fix Version/s: 10.10.1.1
    • Component/s: SQL
    • Bug behavior facts:
      Wrong query result

      Description

      The following gives a NPE:

      ij> values 1,2 order by int(1);

      or

      ij> select * from sysibm.sysdummy1 order by int(1);

      The NPE happens when pulling up the order by expression seemingly due to a compiler phase problem:

      Caused by: java.lang.NullPointerException
      at org.apache.derby.impl.sql.compile.CastNode.getConstantValueAsObject(CastNode.java:851)
      at org.apache.derby.impl.sql.compile.OrderByColumn.isReferedColByNum(OrderByColumn.java:466)
      at org.apache.derby.impl.sql.compile.OrderByColumn.pullUpOrderByColumn(OrderByColumn.java:403)
      at org.apache.derby.impl.sql.compile.OrderByList.pullUpOrderByColumns(OrderByList.java:195)
      at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(CursorNode.java:254)

      The variable sourceCTI is (still) null; being set by CastNode# bindCastNodeOnly, which presumably hasn't yet been run.

      1. derby-6027.diff
        3 kB
        Dag H. Wanvik
      2. derby-6027.stat
        0.2 kB
        Dag H. Wanvik
      3. derby-6027b.diff
        3 kB
        Dag H. Wanvik
      4. derby-6027b.stat
        0.2 kB
        Dag H. Wanvik

        Issue Links

          Activity

          Hide
          Mamta A. Satoor added a comment -

          I will go ahead and mark it as reject for backport to 10.9

          Show
          Mamta A. Satoor added a comment - I will go ahead and mark it as reject for backport to 10.9
          Hide
          Mamta A. Satoor added a comment -

          I tried backporting this jira to 10.9 and when I run subset of the newly added test, I see that it's behavior is different than the 10.10 release. values 1 order by int(1); fails in 10.10 with ERROR 4287B: In this context, the ORDER BY clause may only specify a column number.
          but in 10.9, it passes with following results.
          ij> values 1 order by int(1);
          1 |2
          -----------------------
          1 |1

          1 row selected

          I think the behavior of 10.10 is correct and I think that behavior comes from DERBY-6009 which has been marked with derby_backport_reject_10_9. Based on this, should this jira be marked as backport reject too? The reason DERBY-6009 was marked not to be backported to 10.9 "Candidate for back-porting, but it may be such a corner case it's not worth it."

          Show
          Mamta A. Satoor added a comment - I tried backporting this jira to 10.9 and when I run subset of the newly added test, I see that it's behavior is different than the 10.10 release. values 1 order by int(1); fails in 10.10 with ERROR 4287B: In this context, the ORDER BY clause may only specify a column number. but in 10.9, it passes with following results. ij> values 1 order by int(1); 1 |2 ----------------------- 1 |1 1 row selected I think the behavior of 10.10 is correct and I think that behavior comes from DERBY-6009 which has been marked with derby_backport_reject_10_9. Based on this, should this jira be marked as backport reject too? The reason DERBY-6009 was marked not to be backported to 10.9 "Candidate for back-porting, but it may be such a corner case it's not worth it."
          Hide
          Mamta A. Satoor added a comment -

          Temporarily assinging it to me while doing the backport

          Show
          Mamta A. Satoor added a comment - Temporarily assinging it to me while doing the backport
          Hide
          Dag H. Wanvik added a comment -

          Could be back-ported if deemed valuable enough, but closing for now.

          Show
          Dag H. Wanvik added a comment - Could be back-ported if deemed valuable enough, but closing for now.
          Hide
          Dag H. Wanvik added a comment -

          Attached version "b" of this patch, removing the redundancy. Committed as svn 1430150, resolving.

          Show
          Dag H. Wanvik added a comment - Attached version "b" of this patch, removing the redundancy. Committed as svn 1430150, resolving.
          Hide
          Dag H. Wanvik added a comment -

          Yup, it is indeed redundant now. Thanks.

          Show
          Dag H. Wanvik added a comment - Yup, it is indeed redundant now. Thanks.
          Hide
          Knut Anders Hatlen added a comment -

          The fix looks fine to me.

          Maybe we could also remove

          if(!expression.isConstantExpression())

          { return false; }

          since that's made redundant by the added instanceof check?

          Show
          Knut Anders Hatlen added a comment - The fix looks fine to me. Maybe we could also remove if(!expression.isConstantExpression()) { return false; } since that's made redundant by the added instanceof check?
          Hide
          Dag H. Wanvik added a comment -

          Attaching a patch for this issue. In OderByColumn#isReferedColByNum we add a check that the expression is a numeric constant (we already know it is constant) before we try to check if it is an integer. This makes int(1) return false (correct) instead of causing an NPE in the CastNode.

          Added test cases to lang/orderby.sql. Running regressions.

          Show
          Dag H. Wanvik added a comment - Attaching a patch for this issue. In OderByColumn#isReferedColByNum we add a check that the expression is a numeric constant (we already know it is constant) before we try to check if it is an integer. This makes int(1) return false (correct) instead of causing an NPE in the CastNode. Added test cases to lang/orderby.sql. Running regressions.

            People

            • Assignee:
              Dag H. Wanvik
              Reporter:
              Dag H. Wanvik
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development