Skip to content

cannot order by columns with numeric names #5405

@nwoolmer

Description

@nwoolmer

To reproduce

SELECT * FROM (
  SELECT 456 AS "5"
  UNION ALL 
  SELECT 789 AS "5"
  UNION ALL 
  SELECT 123 AS "5"
)
ORDER BY "5"

order column position is out of range [max=1]

Works fine on Duck: https://sql.info/h/fiddle/duckdb.html?query=U0VMRUNUICogRlJPTSAoCiAgU0VMRUNUIDQ1NiBBUyAiNSIKICBVTklPTiBBTEwgCiAgU0VMRUNUIDc4OSBBUyAiNSIKICBVTklPTiBBTEwgCiAgU0VMRUNUIDEyMyBBUyAiNSIKKQpPUkRFUiBCWSAiNSI=&result=W3siNSI6MTIzfSx7IjUiOjQ1Nn0seyI1Ijo3ODl9XQ==

There is ambiguity in two cases:

  • When the column name is a number and outside of the range i.e. 2000 and there are only 3 columns
  • When the column name is a number, and within the range, but the name doesn't match the position, like this:
3, 1, 2
ORDER BY 2 -- do we sort by column "1" or by column "2"?
    @Test
    public void testOrderByWithNumericNamedColumnInsideOfRange() throws Exception {
        assertException("select * from (" +
                "select 200 as \"3\", 201 as \"1\", 202 as \"2\" " +
                "union all " +
                "select 500 as \"3\", 501 as \"1\", 502 as \"2\" " +
                "union all " +
                "select 50 as \"3\", 51 as \"1\", 52 as \"2\") " +
                "order by 2", 123, "ambiguous order by column [name=2]");
    }

    @Test
    public void testOrderByWithNumericNamedColumnOutsideOfRange() throws Exception {
        assertQuery("999\n50\n200\n500", "select * from (" +
                        "select 200 as \"999\" " +
                        "union all " +
                        "select 500 as \"999\" " +
                        "union all " +
                        "select 50 as \"999\") " +
                        "order by \"999\"",
                null, null,
                false,
                true);
    }

QuestDB version:

Master (8.2.2+)

OS, in case of Docker specify Docker and the Host OS:

N/A

File System, in case of Docker specify Host File System:

N/A

Full Name:

Nick Woolmer

Affiliation:

QuestDB

Have you followed Linux, MacOs kernel configuration steps to increase Maximum open files and Maximum virtual memory areas limit?

  • Yes, I have

Additional context

No response

Metadata

Metadata

Assignees

Labels

BugIncorrect or unexpected behaviorSQLIssues or changes relating to SQL execution

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions