Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sql): fix order-by priority in case of duplicate column names #2339

Merged
merged 8 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/src/main/java/io/questdb/griffin/SqlOptimiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,12 @@ private void createOrderHash(QueryModel model) {
if (n > 0) {
final IntList orderByDirection = model.getOrderByDirection();
for (int i = 0; i < n; i++) {
bziobrowski marked this conversation as resolved.
Show resolved Hide resolved
hash.put(orderBy.getQuick(i).token, orderByDirection.getQuick(i));
// if a column appears multiple times in the ORDER BY clause then we use only the first occurrence.
// why? consider this clause: ORDER BY A DESC, A ASC. In this case we want to order to be "A DESC" only.
// why? "A ASC" is a lower in priority, and it's applicable if and only if the items within first clause
// are equal. but if the items are already equal then there is no point in ordering by the same column again.
// unconditional put() would be a bug as a lower priority ordering would replace ordering with a higher priority.
hash.putIfAbsent(orderBy.getQuick(i).token, orderByDirection.getQuick(i));
}
}

Expand Down
130 changes: 130 additions & 0 deletions core/src/test/java/io/questdb/griffin/OrderByAdviceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,4 +1186,134 @@ public void testDistinctWithOrderByAnotherColumn() throws Exception {
TestUtils.assertContains(e.getFlyweightMessage(), "ORDER BY expressions must appear in select list.");
}
}

jerrinot marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testOrderByPriority_columnNameLiteral() throws Exception {
assertQuery("a\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
"select a from x order by a asc, a desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnNameLiteral_differentCases() throws Exception {
assertQuery("a\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
"select a from x order by a asc, A desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnPositions() throws Exception {
assertQuery("a\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
"select a from x order by 1 asc, 1 desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnAliases() throws Exception {
assertQuery("aliased\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
jerrinot marked this conversation as resolved.
Show resolved Hide resolved
"select a as aliased from x order by aliased asc, aliased desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnsInterleaved() throws Exception {
assertQuery("a\tb\n" +
"9\t0\n" +
"6\t0\n" +
"3\t0\n" +
"7\t1\n" +
"4\t1\n" +
"1\t1\n" +
"8\t2\n" +
"5\t2\n" +
"2\t2\n",
"select * from x order by b asc, a desc, b desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnQuoted() throws Exception {
assertQuery("a\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
"select a from x order by \"a\" asc, \"a\" desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnLiteralsMixedWithPositions() throws Exception {
assertQuery("a\n1\n2\n3\n4\n5\n6\n7\n8\n9\n",
"select a from x order by a asc, 1 desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}

@Test
public void testOrderByPriority_columnDoubleAliased() throws Exception {
assertQuery("a1\ta2\n" +
"1\t1\n" +
"2\t2\n" +
"3\t3\n" +
"4\t4\n" +
"5\t5\n" +
"6\t6\n" +
"7\t7\n" +
"8\t8\n" +
"9\t9\n",
"select a as a1, a as a2 from x order by a1 asc, a2 desc;",
"create table x as (" +
"select" +
" x a," +
" x % 3 b" +
" from long_sequence(9)" +
")",
null,
true, true, true);
}
}