You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by dl...@apache.org on 2019/04/18 22:38:33 UTC

[asterixdb] branch master updated: [NO ISSUE][COMP] Improve window operator consolidation rule

This is an automated email from the ASF dual-hosted git repository.

dlych pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 20c890a  [NO ISSUE][COMP] Improve window operator consolidation rule
20c890a is described below

commit 20c890acc5949af030c8df45694584595c59d70e
Author: Dmitry Lychagin <dm...@couchbase.com>
AuthorDate: Thu Apr 18 12:08:48 2019 -0700

    [NO ISSUE][COMP] Improve window operator consolidation rule
    
    - user model changes: no
    - storage format changes: no
    - interface changes: no
    
    Details:
    - Consolidate window operators computing FIRST_VALUE()
      and LAST_VALUE() over the same window specification
    
    Change-Id: I4714081d711c98c6e843901aa8a02bc4a0bd214e
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/3347
    Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Reviewed-by: Ali Alsuliman <al...@gmail.com>
---
 .../asterix/optimizer/base/RuleCollections.java    |  4 +-
 .../AsterixConsolidateWindowOperatorsRule.java     | 66 ++++++++++++++++++++++
 .../queries/window/win_opt_01/win_opt_01_10.sqlpp  | 34 +++++++++++
 .../queries/window/win_opt_01/win_opt_01_11.sqlpp  | 34 +++++++++++
 .../results/window/win_opt_01/win_opt_01_10.plan   | 15 +++++
 .../results/window/win_opt_01/win_opt_01_11.plan   | 15 +++++
 .../window/win_opt_01/win_opt_01.10.query.sqlpp    | 34 +++++++++++
 .../window/win_opt_01/win_opt_01.11.query.sqlpp    | 34 +++++++++++
 .../results/window/win_opt_01/win_opt_01.10.adm    | 12 ++++
 .../results/window/win_opt_01/win_opt_01.11.adm    | 12 ++++
 .../algebra/operators/logical/WindowOperator.java  | 13 +++--
 .../visitors/IsomorphismOperatorVisitor.java       |  9 ++-
 .../rules/ConsolidateWindowOperatorsRule.java      | 20 ++++++-
 13 files changed, 293 insertions(+), 9 deletions(-)

diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
index a7d40b4..8677d0f 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
@@ -26,6 +26,7 @@ import java.util.List;
 import org.apache.asterix.common.dataflow.ICcApplicationContext;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.optimizer.rules.AddEquivalenceClassForRecordConstructorRule;
+import org.apache.asterix.optimizer.rules.AsterixConsolidateWindowOperatorsRule;
 import org.apache.asterix.optimizer.rules.AsterixExtractFunctionsFromJoinConditionRule;
 import org.apache.asterix.optimizer.rules.AsterixInlineVariablesRule;
 import org.apache.asterix.optimizer.rules.AsterixIntroduceGroupByCombinerRule;
@@ -95,7 +96,6 @@ import org.apache.hyracks.algebricks.rewriter.rules.BreakSelectIntoConjunctsRule
 import org.apache.hyracks.algebricks.rewriter.rules.ComplexUnnestToProductRule;
 import org.apache.hyracks.algebricks.rewriter.rules.ConsolidateAssignsRule;
 import org.apache.hyracks.algebricks.rewriter.rules.ConsolidateSelectsRule;
-import org.apache.hyracks.algebricks.rewriter.rules.ConsolidateWindowOperatorsRule;
 import org.apache.hyracks.algebricks.rewriter.rules.CopyLimitDownRule;
 import org.apache.hyracks.algebricks.rewriter.rules.EliminateGroupByEmptyKeyRule;
 import org.apache.hyracks.algebricks.rewriter.rules.EnforceOrderByAfterSubplan;
@@ -305,7 +305,7 @@ public final class RuleCollections {
         consolidation.add(new PushUnnestDownThroughUnionRule());
         consolidation.add(new RemoveRedundantListifyRule());
         // Window operator consolidation rules
-        consolidation.add(new ConsolidateWindowOperatorsRule());
+        consolidation.add(new AsterixConsolidateWindowOperatorsRule());
         consolidation.add(new ReuseWindowAggregateRule());
         consolidation.add(new RemoveRedundantWindowOperatorsRule());
         consolidation.add(new RemoveRedundantVariablesRule());
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/AsterixConsolidateWindowOperatorsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/AsterixConsolidateWindowOperatorsRule.java
new file mode 100644
index 0000000..3d1148a
--- /dev/null
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/AsterixConsolidateWindowOperatorsRule.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.asterix.optimizer.rules;
+
+import java.util.List;
+
+import org.apache.asterix.om.functions.BuiltinFunctions;
+import org.apache.commons.lang3.mutable.Mutable;
+import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.WindowOperator;
+import org.apache.hyracks.algebricks.rewriter.rules.ConsolidateWindowOperatorsRule;
+
+/**
+ * Asterix-specific rule that enables consolidation of window operators in the following additional cases
+ * <ul>
+ * <li> Different {@link WindowOperator#getFrameMaxObjects()} values if aggregate function is
+ *      {@link BuiltinFunctions#FIRST_ELEMENT}
+ * </li>
+ * </ul>
+ */
+public final class AsterixConsolidateWindowOperatorsRule extends ConsolidateWindowOperatorsRule {
+
+    @Override
+    protected boolean subsumeFrameMaxObjects(int maxObjects1, int maxObjects2, AggregateOperator aggOp2) {
+        if (allFunctionCalls(aggOp2.getExpressions(), BuiltinFunctions.FIRST_ELEMENT) && maxObjects2 >= 1
+                && (maxObjects1 == WindowOperator.FRAME_MAX_OBJECTS_UNLIMITED || maxObjects1 >= maxObjects2)) {
+            return true;
+        }
+        return super.subsumeFrameMaxObjects(maxObjects1, maxObjects2, aggOp2);
+    }
+
+    private boolean allFunctionCalls(List<Mutable<ILogicalExpression>> exprRefs, FunctionIdentifier fid) {
+        for (Mutable<ILogicalExpression> exprRef : exprRefs) {
+            ILogicalExpression expr = exprRef.getValue();
+            if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
+                return false;
+            }
+            AbstractFunctionCallExpression callExpr = (AbstractFunctionCallExpression) expr;
+            if (!callExpr.getFunctionIdentifier().equals(fid)) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_10.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_10.sqlpp
new file mode 100644
index 0000000..fcb6abd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_10.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Test window operator consolidation rule handling of FIRST_VALUE()
+ *              : followed by LAST_VALUE() over the window specification
+ * Expected Res : SUCCESS (one window operator in the optimized plan)
+ */
+
+SELECT m, t,
+  first_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w1_first,
+  first_value(t + 1) over (partition by m order by t range between unbounded preceding and unbounded following) - 1
+    as w2_first,
+  last_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w3_last
+FROM range(0, 11) t
+LET m = t % 4
+ORDER BY m, t;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_11.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_11.sqlpp
new file mode 100644
index 0000000..3564f4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/window/win_opt_01/win_opt_01_11.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Test window operator consolidation rule handling of LAST_VALUE()
+ *              : followed by FIRST_VALUE() over the same window specification
+ * Expected Res : SUCCESS (one window operator in the optimized plan)
+ */
+
+SELECT m, t,
+  last_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w1_last,
+  first_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w2_first,
+  first_value(t + 1) over (partition by m order by t range between unbounded preceding and unbounded following) - 1
+    as w3_first
+FROM range(0, 11) t
+LET m = t % 4
+ORDER BY m, t;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_10.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_10.plan
new file mode 100644
index 0000000..a131487
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_10.plan
@@ -0,0 +1,15 @@
+-- DISTRIBUTE_RESULT  |LOCAL|
+  -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+    -- STREAM_PROJECT  |LOCAL|
+      -- ASSIGN  |LOCAL|
+        -- WINDOW  |LOCAL|
+                {
+                  -- AGGREGATE  |LOCAL|
+                    -- NESTED_TUPLE_SOURCE  |LOCAL|
+                }
+          -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+            -- STABLE_SORT [$$m(ASC), $$t(ASC)]  |LOCAL|
+              -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                -- ASSIGN  |UNPARTITIONED|
+                  -- UNNEST  |UNPARTITIONED|
+                    -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_11.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_11.plan
new file mode 100644
index 0000000..a131487
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/window/win_opt_01/win_opt_01_11.plan
@@ -0,0 +1,15 @@
+-- DISTRIBUTE_RESULT  |LOCAL|
+  -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+    -- STREAM_PROJECT  |LOCAL|
+      -- ASSIGN  |LOCAL|
+        -- WINDOW  |LOCAL|
+                {
+                  -- AGGREGATE  |LOCAL|
+                    -- NESTED_TUPLE_SOURCE  |LOCAL|
+                }
+          -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+            -- STABLE_SORT [$$m(ASC), $$t(ASC)]  |LOCAL|
+              -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                -- ASSIGN  |UNPARTITIONED|
+                  -- UNNEST  |UNPARTITIONED|
+                    -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.10.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.10.query.sqlpp
new file mode 100644
index 0000000..d75e673
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.10.query.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Test window operator consolidation rule handling of FIRST_VALUE()
+ *              : followed by LAST_VALUE() over the same window specification
+ * Expected Res : SUCCESS (one window operator in the optimized plan)
+ */
+
+SELECT m, t,
+  first_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w1_first,
+  first_value(t + 1) over (partition by m order by t range between unbounded preceding and unbounded following) - 1
+    as w2_first,
+  last_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w3_last
+FROM range(0, 11) t
+LET m = t % 4
+ORDER BY m, t;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.11.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.11.query.sqlpp
new file mode 100644
index 0000000..3564f4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_opt_01/win_opt_01.11.query.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Test window operator consolidation rule handling of LAST_VALUE()
+ *              : followed by FIRST_VALUE() over the same window specification
+ * Expected Res : SUCCESS (one window operator in the optimized plan)
+ */
+
+SELECT m, t,
+  last_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w1_last,
+  first_value(t) over (partition by m order by t range between unbounded preceding and unbounded following)
+    as w2_first,
+  first_value(t + 1) over (partition by m order by t range between unbounded preceding and unbounded following) - 1
+    as w3_first
+FROM range(0, 11) t
+LET m = t % 4
+ORDER BY m, t;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.10.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.10.adm
new file mode 100644
index 0000000..344a66b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.10.adm
@@ -0,0 +1,12 @@
+{ "m": 0, "t": 0, "w1_first": 0, "w2_first": 0, "w3_last": 8 }
+{ "m": 0, "t": 4, "w1_first": 0, "w2_first": 0, "w3_last": 8 }
+{ "m": 0, "t": 8, "w1_first": 0, "w2_first": 0, "w3_last": 8 }
+{ "m": 1, "t": 1, "w1_first": 1, "w2_first": 1, "w3_last": 9 }
+{ "m": 1, "t": 5, "w1_first": 1, "w2_first": 1, "w3_last": 9 }
+{ "m": 1, "t": 9, "w1_first": 1, "w2_first": 1, "w3_last": 9 }
+{ "m": 2, "t": 2, "w1_first": 2, "w2_first": 2, "w3_last": 10 }
+{ "m": 2, "t": 6, "w1_first": 2, "w2_first": 2, "w3_last": 10 }
+{ "m": 2, "t": 10, "w1_first": 2, "w2_first": 2, "w3_last": 10 }
+{ "m": 3, "t": 3, "w1_first": 3, "w2_first": 3, "w3_last": 11 }
+{ "m": 3, "t": 7, "w1_first": 3, "w2_first": 3, "w3_last": 11 }
+{ "m": 3, "t": 11, "w1_first": 3, "w2_first": 3, "w3_last": 11 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.11.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.11.adm
new file mode 100644
index 0000000..1e6c4ff
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_opt_01/win_opt_01.11.adm
@@ -0,0 +1,12 @@
+{ "m": 0, "t": 0, "w1_last": 8, "w2_first": 0, "w3_first": 0 }
+{ "m": 0, "t": 4, "w1_last": 8, "w2_first": 0, "w3_first": 0 }
+{ "m": 0, "t": 8, "w1_last": 8, "w2_first": 0, "w3_first": 0 }
+{ "m": 1, "t": 1, "w1_last": 9, "w2_first": 1, "w3_first": 1 }
+{ "m": 1, "t": 5, "w1_last": 9, "w2_first": 1, "w3_first": 1 }
+{ "m": 1, "t": 9, "w1_last": 9, "w2_first": 1, "w3_first": 1 }
+{ "m": 2, "t": 2, "w1_last": 10, "w2_first": 2, "w3_first": 2 }
+{ "m": 2, "t": 6, "w1_last": 10, "w2_first": 2, "w3_first": 2 }
+{ "m": 2, "t": 10, "w1_last": 10, "w2_first": 2, "w3_first": 2 }
+{ "m": 3, "t": 3, "w1_last": 11, "w2_first": 3, "w3_first": 3 }
+{ "m": 3, "t": 7, "w1_last": 11, "w2_first": 3, "w3_first": 3 }
+{ "m": 3, "t": 11, "w1_last": 11, "w2_first": 3, "w3_first": 3 }
diff --git a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/WindowOperator.java b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/WindowOperator.java
index aa1ef0a..0235dad 100644
--- a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/WindowOperator.java
+++ b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/WindowOperator.java
@@ -52,7 +52,7 @@ import org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalOperatorVisit
  * <li>{@link #frameEndValidationExpressions} - frame end boundary validators</li>
  * <li>{@link #frameExcludeExpressions} - define values to be excluded from the frame</li>
  * <li>{@link #frameOffset} - sets how many tuples to skip inside each frame</li>
- * <li>{@link #frameMaxObjects} - limits number of tuples to be returned for each frame</li>
+ * <li>{@link #frameMaxObjects} - limits number of tuples to be returned for each frame ({@code -1} = unlimited)</li>
  * <li>{@link #variables} - output variables containing return values of these functions</li>
  * <li>{@link #expressions} - window function expressions (running aggregates)</li>
  * </ul>
@@ -61,6 +61,8 @@ import org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalOperatorVisit
  */
 public class WindowOperator extends AbstractOperatorWithNestedPlans {
 
+    public static final int FRAME_MAX_OBJECTS_UNLIMITED = -1;
+
     private final List<Mutable<ILogicalExpression>> partitionExpressions;
 
     private final List<Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>> orderExpressions;
@@ -218,7 +220,7 @@ public class WindowOperator extends AbstractOperatorWithNestedPlans {
     }
 
     public void setFrameMaxObjects(int value) {
-        frameMaxObjects = Math.max(-1, value);
+        frameMaxObjects = value < 0 ? FRAME_MAX_OBJECTS_UNLIMITED : value;
     }
 
     public List<LogicalVariable> getVariables() {
@@ -252,8 +254,11 @@ public class WindowOperator extends AbstractOperatorWithNestedPlans {
 
     /**
      * Allows performing expression transformation only on a subset of this operator's expressions
-     * @param visitor transforming visitor
-     * @param visitVarRefRequiringExprs whether to visit variable reference requiring expressions, or not
+     *
+     * @param visitor
+     *            transforming visitor
+     * @param visitVarRefRequiringExprs
+     *            whether to visit variable reference requiring expressions, or not
      */
     public boolean acceptExpressionTransform(ILogicalExpressionReferenceTransform visitor,
             boolean visitVarRefRequiringExprs) throws AlgebricksException {
diff --git a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/visitors/IsomorphismOperatorVisitor.java b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/visitors/IsomorphismOperatorVisitor.java
index 5e5f18f..dd3053b 100644
--- a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/visitors/IsomorphismOperatorVisitor.java
+++ b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/operators/logical/visitors/IsomorphismOperatorVisitor.java
@@ -649,6 +649,11 @@ public class IsomorphismOperatorVisitor implements ILogicalOperatorVisitor<Boole
     }
 
     public static boolean compareWindowFrameSpec(WindowOperator winOp1, WindowOperator winOp2) {
+        return compareWindowFrameSpecExcludingMaxObjects(winOp1, winOp2)
+                && winOp1.getFrameMaxObjects() == winOp2.getFrameMaxObjects();
+    }
+
+    public static boolean compareWindowFrameSpecExcludingMaxObjects(WindowOperator winOp1, WindowOperator winOp2) {
         return compareIOrderAndExpressions(winOp1.getFrameValueExpressions(), winOp2.getFrameValueExpressions())
                 && compareExpressions(winOp1.getFrameStartExpressions(), winOp2.getFrameStartExpressions())
                 && compareExpressions(winOp1.getFrameStartValidationExpressions(),
@@ -658,8 +663,8 @@ public class IsomorphismOperatorVisitor implements ILogicalOperatorVisitor<Boole
                         winOp2.getFrameEndValidationExpressions())
                 && compareExpressions(winOp1.getFrameExcludeExpressions(), winOp2.getFrameExcludeExpressions())
                 && winOp1.getFrameExcludeNegationStartIdx() == winOp2.getFrameExcludeNegationStartIdx()
-                && Objects.equals(winOp1.getFrameOffset().getValue(), winOp2.getFrameOffset().getValue())
-                && winOp1.getFrameMaxObjects() == winOp2.getFrameMaxObjects();
+                && Objects.equals(winOp1.getFrameOffset().getValue(), winOp2.getFrameOffset().getValue());
+        // do not include WindowOperator.getFrameMaxObjects()
     }
 
     private static boolean compareExpressions(List<Mutable<ILogicalExpression>> opExprs,
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateWindowOperatorsRule.java b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateWindowOperatorsRule.java
index 3aa9152..ee19eaa 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateWindowOperatorsRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateWindowOperatorsRule.java
@@ -76,7 +76,7 @@ public class ConsolidateWindowOperatorsRule implements IAlgebraicRewriteRule {
             return false;
         }
         if (winOp1.hasNestedPlans() && winOp2.hasNestedPlans()
-                && !IsomorphismOperatorVisitor.compareWindowFrameSpec(winOp1, winOp2)) {
+                && !IsomorphismOperatorVisitor.compareWindowFrameSpecExcludingMaxObjects(winOp1, winOp2)) {
             return false;
         }
 
@@ -115,6 +115,17 @@ public class ConsolidateWindowOperatorsRule implements IAlgebraicRewriteRule {
                     aggFrom.getInputs().get(0).getValue())) {
                 return false;
             }
+            int winOpToMaxObjects = winOpTo.getFrameMaxObjects();
+            int winOpFromMaxObjects = winOpFrom.getFrameMaxObjects();
+            if (winOpToMaxObjects != winOpFromMaxObjects) {
+                if (subsumeFrameMaxObjects(winOpFromMaxObjects, winOpToMaxObjects, aggTo)) {
+                    winOpToMaxObjects = winOpFromMaxObjects;
+                } else if (!subsumeFrameMaxObjects(winOpToMaxObjects, winOpFromMaxObjects, aggFrom)) {
+                    return false;
+                }
+            }
+
+            winOpTo.setFrameMaxObjects(winOpToMaxObjects);
             aggTo.getVariables().addAll(aggFrom.getVariables());
             aggTo.getExpressions().addAll(aggFrom.getExpressions());
             context.computeAndSetTypeEnvironmentForOperator(aggTo);
@@ -154,4 +165,11 @@ public class ConsolidateWindowOperatorsRule implements IAlgebraicRewriteRule {
         }
         to.addAll(from);
     }
+
+    /**
+     * Returns {@code true} if {@code maxObjects1} subsumes {@code maxObjects2}
+     */
+    protected boolean subsumeFrameMaxObjects(int maxObjects1, int maxObjects2, AggregateOperator aggOp2) {
+        return false;
+    }
 }