Skip to content

Commit

Permalink
IGNITE-21946: Cover SQL F863(Nested <result offset clause> in <query …
Browse files Browse the repository at this point in the history
…expression>) feature by tests
  • Loading branch information
lowka committed May 10, 2024
1 parent 3dc1fbc commit 3a7e9a6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# name: test/sql/order/test_primary_offset.test
# description: F863: Nested <result offset clause>. Top-level <result offset clause> in <query expression> <query expression>
# group: [order]

statement ok
CREATE TABLE test (a INTEGER);

statement ok
INSERT INTO test VALUES (1), (2), (3), (4)

query I
SELECT a FROM (SELECT a FROM test ORDER BY a OFFSET 2) t(a) UNION ALL SELECT a FROM test ORDER BY a
----
1
2
3
3
4
4


Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class IgniteSqlToRelConvertor extends SqlToRelConverter {
}
}



@Override
protected RexNode convertExtendedExpression(
SqlNode expr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ public final class Commons {
.executor(new RexExecutorImpl(DataContexts.EMPTY))
.sqlToRelConverterConfig(SqlToRelConverter.config()
.withTrimUnusedFields(true)

// Disable buggy `RemoveSortInSubQuery` hint, because a query such as
//
// SELECT a FROM (SELECT a FROM test ORDER BY a OFFSET 2) t(a) UNION ALL SELECT a FROM test ORDER BY a
//
// Is written to:
//
// Sort(sort0=[$0], dir0=[ASC])
// UnionAll
// TableScan
// TableScan
//
// Instead of:
//
// UnionAll
// Limit(offset=[2])
// Sort(sort0=[$0], dir0=[ASC])
// TableScan
// TableScan
.withRemoveSortInSubQuery(false)

// currently SqlToRelConverter creates not optimal plan for both optimization and execution
// so it's better to disable such rewriting right now
// TODO: remove this after IGNITE-14277
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,40 @@ public void testOrderOfRels() throws Exception {
.and(hasChildThat(isInstanceOf(IgniteExchange.class)).negate()));
}

@Test
public void testNestedOffset() throws Exception {
TableBuilder builder = TestBuilders.table()
.name("TEST")
.addColumn("A", NativeTypes.INT32)
.size(ROW_CNT)
.distribution(IgniteDistributions.random());

IgniteSchema schema = createSchema(builder.build());

assertPlan("SELECT a FROM (SELECT a FROM test ORDER BY a OFFSET 2)", schema,
isInstanceOf(IgniteLimit.class)
.and(s -> doubleFromRex(s.offset(), -1) == 2.0)
.and(input(isInstanceOf(IgniteExchange.class))
.and(hasChildThat(isInstanceOf(IgniteSort.class)
))));

assertPlan("SELECT a FROM (SELECT a FROM test ORDER BY a OFFSET 2) t(a) UNION ALL SELECT a FROM test ORDER BY a",
schema, isInstanceOf(IgniteSort.class)
.and(s -> s.offset == null && s.fetch == null)
.and(hasChildThat(isInstanceOf(IgniteUnionAll.class)
.and(hasChildThat(isInstanceOf(IgniteLimit.class)
.and(s -> doubleFromRex(s.offset(), -1) == 2.0)
.and(input(isInstanceOf(IgniteExchange.class))
.and(hasChildThat(isInstanceOf(IgniteSort.class)
.and(s -> s.offset == null)))))
)
.and(hasChildThat(isInstanceOf(IgniteExchange.class)
.and(input(isInstanceOf(IgniteTableScan.class))))
)
))
);
}

/**
* Creates PUBLIC schema with one TEST table.
*/
Expand Down

0 comments on commit 3a7e9a6

Please sign in to comment.