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

[BEAM-9661] Fix ORDER BY with LIMIT #11602

Merged
merged 1 commit into from May 5, 2020
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
Expand Up @@ -81,18 +81,19 @@ public RelNode convert(ResolvedLimitOffsetScan zetaNode, List<RelNode> inputs) {

/** Collation is a sort order, as in ORDER BY DESCENDING/ASCENDING. */
private static RelCollation getRelCollation(ResolvedOrderByScan node) {
final long inputOffset = node.getColumnList().get(0).getId();
List<RelFieldCollation> fieldCollations =
node.getOrderByItemList().stream()
.map(LimitOffsetScanToOrderByLimitConverter::orderByItemToFieldCollation)
.map(item -> orderByItemToFieldCollation(item, inputOffset))
.collect(toList());
return RelCollationImpl.of(fieldCollations);
}

private static RelFieldCollation orderByItemToFieldCollation(ResolvedOrderByItem item) {
// TODO: might need a column ref mapping here.
private static RelFieldCollation orderByItemToFieldCollation(
ResolvedOrderByItem item, long inputOffset) {
Direction sortDirection = item.getIsDescending() ? DESCENDING : ASCENDING;
int fieldIndex = (int) item.getColumnRef().getColumn().getId();
return new RelFieldCollation(fieldIndex, sortDirection);
final long fieldIndex = item.getColumnRef().getColumn().getId() - inputOffset;
return new RelFieldCollation((int) fieldIndex, sortDirection);
}

private RelNode convertOrderByScanToLogicalScan(ResolvedOrderByScan node, RelNode input) {
Expand Down
Expand Up @@ -1325,6 +1325,19 @@ public void testZetaSQLSelectNullOffsetParam() {
BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql, params);
}

@Test
public void testZetaSQLSelectFromTableOrderLimit() {
String sql =
"SELECT x, y FROM (SELECT 1 as x, 0 as y UNION ALL SELECT 0, 0 "
+ "UNION ALL SELECT 1, 0 UNION ALL SELECT 1, 1) ORDER BY x LIMIT 1";
ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
final Schema schema = Schema.builder().addInt64Field("field1").addInt64Field("field2").build();
PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(0L, 0L).build());
pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}

@Test
public void testZetaSQLSelectFromTableLimitOffset() {
String sql =
Expand Down