Skip to content

Bug: ROWNUM in SELECT list of subquery returns 0 instead of actual values #13

@rophy

Description

@rophy

Bug Description

When ROWNUM is used in the SELECT list of a subquery, it incorrectly returns 0 instead of preserving its actual values.

Environment

  • IvorySQL Version: 5beta1 (PostgreSQL 18beta1)
  • Oracle Version (reference): 23.26.0.0 Free
  • Branch: test/rownum
  • Commit: 1fdd54b

Reproduction Steps

CREATE TABLE test_sub (
    id INT,
    name TEXT,
    value NUMERIC
);

INSERT INTO test_sub VALUES 
    (1, 'Alice', 100),
    (2, 'Bob', 200),
    (3, 'Charlie', 150),
    (4, 'David', 300),
    (5, 'Eve', 250);

-- Test: ROWNUM in SELECT list of subquery
SELECT * FROM (
    SELECT ROWNUM as rn, id, name, value
    FROM test_sub
    ORDER BY value DESC
) WHERE ROWNUM <= 3;

Expected Behavior (Oracle 23.26)

 RN | ID | NAME  | VALUE
----|----+-------+-------
  4 |  4 | David |   300
  5 |  5 | Eve   |   250
  2 |  2 | Bob   |   200

Explanation: ROWNUM is assigned to each row BEFORE the ORDER BY is applied. So David (id=4) gets ROWNUM=4, Eve (id=5) gets ROWNUM=5, Bob (id=2) gets ROWNUM=2, etc. After sorting by value DESC, these original ROWNUM values are preserved in the output.

Actual Behavior (IvorySQL)

 rn | id | name  | value 
----+----+-------+-------
  0 |  4 | David |   300
  0 |  5 | Eve   |   250
  0 |  2 | Bob   |   200

Bug: All rn values are 0 instead of the actual ROWNUM values (4, 5, 2).

Impact

This bug affects queries that need to capture and return the original ROWNUM value from before a sort operation. Common use cases include:

  1. Debugging queries - capturing original row order before sorting
  2. Pagination with sorting - preserving original row numbers
  3. Data provenance - tracking which rows were selected from the original dataset

Root Cause Analysis

The ROWNUM pseudocolumn appears to not preserve its value when used as a column in the SELECT list of a subquery. When the subquery result is consumed by the outer query, ROWNUM has lost its original value and defaults to 0.

Test Case Location

This bug was discovered in:

  • Test: src/pl/plisql/src/sql/plisql_rownum.sql - Test 11
  • Test Report: test_report_rownum.md - Section 10, Test 11

Related Work

This issue was found during comprehensive ROWNUM testing comparing IvorySQL against Oracle Database 23.26 Free. Out of 89 total tests (76 SQL + 13 PL/iSQL), this is the only known incompatibility.

Workaround

None currently available. Users needing to capture ROWNUM values before sorting cannot use this pattern and would need to restructure their queries.

Additional Context

  • All other ROWNUM functionality works correctly (98.9% compatibility)
  • ROWNUM in WHERE clauses works correctly
  • ROWNUM with ORDER BY in outer query works correctly
  • The issue only appears when ROWNUM is in the SELECT list of a subquery

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions