Skip to content

Commit

Permalink
ignite-sprint-3 - map query sort limit fix (IGNITE-682)
Browse files Browse the repository at this point in the history
  • Loading branch information
S.Vladykin committed Apr 6, 2015
1 parent 88268bf commit 2f34c46
Showing 1 changed file with 18 additions and 4 deletions.
Expand Up @@ -77,8 +77,10 @@ public static GridCacheTwoStepQuery split(JdbcPreparedStatement stmt, Object[] p

Set<String> colNames = new HashSet<>();

boolean aggregateFound = false;

for (int i = 0, len = mapExps.size(); i < len; i++) // Remember len because mapExps list can grow.
splitSelectExpression(mapExps, rdcExps, colNames, i);
aggregateFound |= splitSelectExpression(mapExps, rdcExps, colNames, i);

// Fill select expressions.
mapQry.clearSelect();
Expand Down Expand Up @@ -110,15 +112,20 @@ public static GridCacheTwoStepQuery split(JdbcPreparedStatement stmt, Object[] p

// -- ORDER BY
if (!srcQry.sort().isEmpty()) {
mapQry.clearSort();
if (aggregateFound) // Ordering over aggregates does not make sense.
mapQry.clearSort(); // Otherwise map sort will be used by offset-limit.

for (GridSqlSortColumn sortCol : srcQry.sort().values())
rdcQry.addSort(column(((GridSqlAlias)mapExps.get(sortCol.column())).alias()), sortCol);
}

// -- LIMIT
if (srcQry.limit() != null)
if (srcQry.limit() != null) {
if (aggregateFound)
mapQry.limit(null);

rdcQry.limit(srcQry.limit());
}

// -- OFFSET
if (srcQry.offset() != null) {
Expand Down Expand Up @@ -209,19 +216,24 @@ else if (el instanceof GridSqlSubquery)
* @param rdcSelect Selects for reduce query.
* @param colNames Set of unique top level column names.
* @param idx Index.
* @return {@code true} If aggregate was found.
*/
private static void splitSelectExpression(List<GridSqlElement> mapSelect, GridSqlElement[] rdcSelect,
private static boolean splitSelectExpression(List<GridSqlElement> mapSelect, GridSqlElement[] rdcSelect,
Set<String> colNames, int idx) {
GridSqlElement el = mapSelect.get(idx);

GridSqlAlias alias = null;

boolean aggregateFound = false;

if (el instanceof GridSqlAlias) { // Unwrap from alias.
alias = (GridSqlAlias)el;
el = alias.child();
}

if (el instanceof GridSqlAggregateFunction) {
aggregateFound = true;

GridSqlAggregateFunction agg = (GridSqlAggregateFunction)el;

GridSqlElement mapAgg, rdcAgg;
Expand Down Expand Up @@ -322,6 +334,8 @@ private static void splitSelectExpression(List<GridSqlElement> mapSelect, GridSq
rdcSelect[idx] = rdcEl;
}
}

return aggregateFound;
}

/**
Expand Down

0 comments on commit 2f34c46

Please sign in to comment.