Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 31D4C1841E for ; Thu, 23 Jul 2015 11:49:21 +0000 (UTC) Received: (qmail 91704 invoked by uid 500); 23 Jul 2015 11:49:21 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 91673 invoked by uid 500); 23 Jul 2015 11:49:21 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 91664 invoked by uid 99); 23 Jul 2015 11:49:21 -0000 Received: from Unknown (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jul 2015 11:49:21 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id A5D8DD6FB0 for ; Thu, 23 Jul 2015 11:49:20 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.554 X-Spam-Level: X-Spam-Status: No, score=0.554 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.227, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id uJDel_HcupI7 for ; Thu, 23 Jul 2015 11:49:19 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 1B9F42DC25 for ; Thu, 23 Jul 2015 11:49:13 +0000 (UTC) Received: (qmail 91217 invoked by uid 99); 23 Jul 2015 11:49:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jul 2015 11:49:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D997AE3C3B; Thu, 23 Jul 2015 11:49:12 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: av@apache.org To: commits@ignite.incubator.apache.org Date: Thu, 23 Jul 2015 11:49:37 -0000 Message-Id: <9afee24531204b94b44f4bb7d0b3956e@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [26/38] incubator-ignite git commit: ignite-1015 - refactor ignite-1015 - refactor Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/0871a7a8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/0871a7a8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/0871a7a8 Branch: refs/heads/ignite-630 Commit: 0871a7a8cacdfde1dab817d782dd6de3e7a512b4 Parents: 38d2a93 Author: S.Vladykin Authored: Wed Jul 22 08:23:36 2015 +0300 Committer: S.Vladykin Committed: Wed Jul 22 08:23:36 2015 +0300 ---------------------------------------------------------------------- .../query/h2/sql/GridSqlQuerySplitter.java | 163 +++++++++++-------- 1 file changed, 94 insertions(+), 69 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0871a7a8/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java index 6c1886b..536f72d 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.*; import java.util.*; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunctionType.*; +import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlPlaceholder.*; /** * Splits a single SQL query into two step map-reduce query. @@ -397,77 +398,12 @@ public class GridSqlQuerySplitter { if (!collocated && el instanceof GridSqlAggregateFunction) { aggregateFound = true; - GridSqlAggregateFunction agg = (GridSqlAggregateFunction)el; + if (alias == null) + alias = alias(columnName(idx), el); - GridSqlElement mapAgg, rdcAgg; + splitAggregate(alias, 0, mapSelect, idx, true); - String mapAggAlias = columnName(idx); - - switch (agg.type()) { - case AVG: // SUM( AVG(CAST(x AS DOUBLE))*COUNT(x) )/SUM( COUNT(x) ). - //-- COUNT(x) map - GridSqlElement cntMapAgg = aggregate(agg.distinct(), COUNT).addChild(agg.child()); - - // Add generated alias to COUNT(x). - // Using size as index since COUNT will be added as the last select element to the map query. - String cntMapAggAlias = columnName(mapSelect.size()); - - cntMapAgg = alias(cntMapAggAlias, cntMapAgg); - - mapSelect.add(cntMapAgg); - - //-- AVG(CAST(x AS DOUBLE)) map - mapAgg = aggregate(agg.distinct(), AVG).addChild( // Add function argument. - function(CAST).setCastType("DOUBLE").addChild(agg.child())); - - //-- SUM( AVG(x)*COUNT(x) )/SUM( COUNT(x) ) reduce - GridSqlElement sumUpRdc = aggregate(false, SUM).addChild( - op(GridSqlOperationType.MULTIPLY, - column(mapAggAlias), - column(cntMapAggAlias))); - - GridSqlElement sumDownRdc = aggregate(false, SUM).addChild(column(cntMapAggAlias)); - - rdcAgg = op(GridSqlOperationType.DIVIDE, sumUpRdc, sumDownRdc); - - break; - - case SUM: // SUM( SUM(x) ) - case MAX: // MAX( MAX(x) ) - case MIN: // MIN( MIN(x) ) - mapAgg = aggregate(agg.distinct(), agg.type()).addChild(agg.child()); - rdcAgg = aggregate(agg.distinct(), agg.type()).addChild(column(mapAggAlias)); - - break; - - case COUNT_ALL: // CAST(SUM( COUNT(*) ) AS BIGINT) - case COUNT: // CAST(SUM( COUNT(x) ) AS BIGINT) - mapAgg = aggregate(agg.distinct(), agg.type()); - - if (agg.type() == COUNT) - mapAgg.addChild(agg.child()); - - rdcAgg = aggregate(false, SUM).addChild(column(mapAggAlias)); - rdcAgg = function(CAST).setCastType("BIGINT").addChild(rdcAgg); - - break; - - default: - throw new IgniteException("Unsupported aggregate: " + agg.type()); - } - - assert !(mapAgg instanceof GridSqlAlias); - - // Add generated alias to map aggregate. - mapAgg = alias(mapAggAlias, mapAgg); - - if (alias != null) // Add initial alias if it was set. - rdcAgg = alias(alias.alias(), rdcAgg); - - // Set map and reduce aggregates to their places in selects. - mapSelect.set(idx, mapAgg); - - rdcSelect[idx] = rdcAgg; + rdcSelect[idx] = alias; } else { String mapColAlias = columnName(idx); @@ -500,6 +436,95 @@ public class GridSqlQuerySplitter { } /** + * @param parentExpr Parent expression. + * @param aggIdx Index of the aggregate to split in this expression. + * @param mapSelect List of expressions in map SELECT clause. + * @param exprIdx Index of the original expression in map SELECT clause. + * @param first If this is the first aggregate found in this expression. + */ + private static void splitAggregate( + GridSqlElement parentExpr, + int aggIdx, + List mapSelect, + int exprIdx, + boolean first + ) { + GridSqlAggregateFunction agg = parentExpr.child(aggIdx); + + GridSqlElement mapAgg, rdcAgg; + + // Create stubbed map alias to fill it with correct expression later. + GridSqlAlias mapAggAlias = alias(columnName(first ? exprIdx : mapSelect.size()), EMPTY); + + // Replace original expression if it is the first aggregate in expression or add to the end. + if (first) + mapSelect.set(exprIdx, mapAggAlias); + else + mapSelect.add(mapAggAlias); + + switch (agg.type()) { + case AVG: // SUM( AVG(CAST(x AS DOUBLE))*COUNT(x) )/SUM( COUNT(x) ). + //-- COUNT(x) map + GridSqlElement cntMapAgg = aggregate(agg.distinct(), COUNT).addChild(agg.child()); + + // Add generated alias to COUNT(x). + // Using size as index since COUNT will be added as the last select element to the map query. + String cntMapAggAlias = columnName(mapSelect.size()); + + cntMapAgg = alias(cntMapAggAlias, cntMapAgg); + + mapSelect.add(cntMapAgg); + + //-- AVG(CAST(x AS DOUBLE)) map + mapAgg = aggregate(agg.distinct(), AVG).addChild( // Add function argument. + function(CAST).setCastType("DOUBLE").addChild(agg.child())); + + //-- SUM( AVG(x)*COUNT(x) )/SUM( COUNT(x) ) reduce + GridSqlElement sumUpRdc = aggregate(false, SUM).addChild( + op(GridSqlOperationType.MULTIPLY, + column(mapAggAlias.alias()), + column(cntMapAggAlias))); + + GridSqlElement sumDownRdc = aggregate(false, SUM).addChild(column(cntMapAggAlias)); + + rdcAgg = op(GridSqlOperationType.DIVIDE, sumUpRdc, sumDownRdc); + + break; + + case SUM: // SUM( SUM(x) ) + case MAX: // MAX( MAX(x) ) + case MIN: // MIN( MIN(x) ) + mapAgg = aggregate(agg.distinct(), agg.type()).addChild(agg.child()); + rdcAgg = aggregate(agg.distinct(), agg.type()).addChild(column(mapAggAlias.alias())); + + break; + + case COUNT_ALL: // CAST(SUM( COUNT(*) ) AS BIGINT) + case COUNT: // CAST(SUM( COUNT(x) ) AS BIGINT) + mapAgg = aggregate(agg.distinct(), agg.type()); + + if (agg.type() == COUNT) + mapAgg.addChild(agg.child()); + + rdcAgg = aggregate(false, SUM).addChild(column(mapAggAlias.alias())); + rdcAgg = function(CAST).setCastType("BIGINT").addChild(rdcAgg); + + break; + + default: + throw new IgniteException("Unsupported aggregate: " + agg.type()); + } + + assert !(mapAgg instanceof GridSqlAlias); + + // Fill the map alias with aggregate. + mapAggAlias.child(0, mapAgg); + + // Replace in original expression aggregate with reduce aggregate. + parentExpr.child(aggIdx, rdcAgg); + } + + /** * @param distinct Distinct. * @param type Type. * @return Aggregate function.