You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2017/06/07 17:04:26 UTC

[3/4] phoenix git commit: PHOENIX-3773 Implement FIRST_VALUES aggregate function (Loknath Priyatham Teja Singamsetty)

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3733a4ef/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValuesFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValuesFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValuesFunction.java
new file mode 100644
index 0000000..d4a8061
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValuesFunction.java
@@ -0,0 +1,97 @@
+/*
+ * 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.phoenix.expression.function;
+
+import java.util.List;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.LiteralExpression;
+import org.apache.phoenix.expression.aggregator.Aggregator;
+import org.apache.phoenix.expression.aggregator.FirstLastValueBaseClientAggregator;
+import org.apache.phoenix.expression.aggregator.FirstLastValueServerAggregator;
+import org.apache.phoenix.parse.FunctionParseNode;
+import org.apache.phoenix.parse.LastValuesAggregateParseNode;
+import org.apache.phoenix.schema.types.PArrayDataType;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PInteger;
+
+/**
+ * Built-in function for FIRST_VALUES(<expression>, <expression>) WITHIN GROUP (ORDER BY <expression> ASC/DESC) aggregate
+ * function
+ *
+ */
+@FunctionParseNode.BuiltInFunction(name = LastValuesFunction.NAME, nodeClass = LastValuesAggregateParseNode.class, args = {
+    @FunctionParseNode.Argument(),
+    @FunctionParseNode.Argument(allowedTypes = { PBoolean.class }, isConstant = true),
+    @FunctionParseNode.Argument(),
+    @FunctionParseNode.Argument(allowedTypes = { PInteger.class }, isConstant = true)})
+public class LastValuesFunction extends FirstLastValueBaseFunction {
+    public static final String NAME = "LAST_VALUES";
+    private int offset;
+
+    public LastValuesFunction() {
+    }
+
+    public LastValuesFunction(List<Expression> childExpressions) {
+        this(childExpressions, null);
+    }
+
+    public LastValuesFunction(List<Expression> childExpressions, CountAggregateFunction delegate) {
+        super(childExpressions, delegate);
+    }
+
+    @Override
+    public Aggregator newServerAggregator(Configuration conf) {
+        FirstLastValueServerAggregator aggregator = new FirstLastValueServerAggregator();
+
+        offset = ((Number) ((LiteralExpression) children.get(3)).getValue()).intValue();
+        boolean order = !(Boolean) ((LiteralExpression) children.get(1)).getValue();
+
+        aggregator.init(children, order, offset);
+
+        return aggregator;
+    }
+
+    @Override
+    public Aggregator newClientAggregator() {
+        FirstLastValueBaseClientAggregator aggregator = new FirstLastValueBaseClientAggregator(getDataType());
+
+        if (children.size() < 3) {
+            aggregator.init(offset, true);
+        } else {
+            aggregator.init(((Number) ((LiteralExpression) children.get(3)).getValue()).intValue(), true);
+        }
+
+        return aggregator;
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public PDataType getDataType() {
+        if (children.size() < 3) {
+            return null;
+        }
+        return PDataType.fromTypeId(children.get(2).getDataType().getSqlType() + PArrayDataType.ARRAY_TYPE_BASE);
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3733a4ef/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NthValueFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NthValueFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NthValueFunction.java
index 90c7748..40502e5 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NthValueFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/NthValueFunction.java
@@ -73,9 +73,9 @@ public class NthValueFunction extends FirstLastValueBaseFunction {
         FirstLastValueBaseClientAggregator aggregator = new FirstLastValueBaseClientAggregator();
 
         if (children.size() < 3) {
-            aggregator.init(offset);
+            aggregator.init(offset, false);
         } else {
-            aggregator.init(((Number) ((LiteralExpression) children.get(3)).getValue()).intValue());
+            aggregator.init(((Number) ((LiteralExpression) children.get(3)).getValue()).intValue(), false);
         }
 
         return aggregator;

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3733a4ef/phoenix-core/src/main/java/org/apache/phoenix/parse/FirstValuesAggregateParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/FirstValuesAggregateParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/FirstValuesAggregateParseNode.java
new file mode 100644
index 0000000..50b137a
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/FirstValuesAggregateParseNode.java
@@ -0,0 +1,38 @@
+/*
+ * 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.phoenix.parse;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.phoenix.compile.StatementContext;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.function.FirstValuesFunction;
+import org.apache.phoenix.expression.function.FunctionExpression;
+
+public class FirstValuesAggregateParseNode extends DelegateConstantToCountParseNode {
+
+    public FirstValuesAggregateParseNode(String name, List<ParseNode> children, FunctionParseNode.BuiltInFunctionInfo info) {
+        super(name, children, info);
+    }
+
+    @Override
+    public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
+        return new FirstValuesFunction(children, getDelegateFunction(children, context));
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3733a4ef/phoenix-core/src/main/java/org/apache/phoenix/parse/LastValuesAggregateParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/LastValuesAggregateParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/LastValuesAggregateParseNode.java
new file mode 100644
index 0000000..43fa82d
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/LastValuesAggregateParseNode.java
@@ -0,0 +1,38 @@
+/*
+ * 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.phoenix.parse;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.phoenix.compile.StatementContext;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.function.FunctionExpression;
+import org.apache.phoenix.expression.function.LastValuesFunction;
+
+public class LastValuesAggregateParseNode extends DelegateConstantToCountParseNode {
+
+    public LastValuesAggregateParseNode(String name, List<ParseNode> children, FunctionParseNode.BuiltInFunctionInfo info) {
+        super(name, children, info);
+    }
+
+    @Override
+    public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
+        return new LastValuesFunction(children, getDelegateFunction(children, context));
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3733a4ef/phoenix-core/src/main/java/org/apache/phoenix/util/FirstLastNthValueDataContainer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/FirstLastNthValueDataContainer.java b/phoenix-core/src/main/java/org/apache/phoenix/util/FirstLastNthValueDataContainer.java
index b358dce..9b02103 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/FirstLastNthValueDataContainer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/FirstLastNthValueDataContainer.java
@@ -24,6 +24,7 @@ import java.util.ListIterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.TreeMap;
+
 import org.apache.hadoop.hbase.util.Bytes;
 
 /**
@@ -111,10 +112,10 @@ public class FirstLastNthValueDataContainer {
         is ascending            | 1            | 1 = asc, 0 = desc
         length of order by vals | 4            | 0 if dynamic length, size otherwise
         length of values        | 4            | 0 if dynamic length, size otherwise
-      [ lenght of first order   | 4            | set if order is var length (optional) ]
+      [ length of first order   | 4            | set if order is var length (optional) ]
         first order value       | n            | order by val
-      [ lenght of first value   | 4            | set if value is var length (optional) ]
-        first order value       | n            | data val
+      [ length of first value   | 4            | set if value is var length (optional) ]
+        first value             | n            | data val
         ... and so on, repeat order by values and data values