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:16:08 UTC
[1/3] phoenix git commit: PHOENIX-3913 Support
PArrayDataType.appendItemToArray to append item to array when null or empty
Repository: phoenix
Updated Branches:
refs/heads/master dbd70f56d -> 9095b3ec8
PHOENIX-3913 Support PArrayDataType.appendItemToArray to append item to array when null or empty
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/c93de246
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/c93de246
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/c93de246
Branch: refs/heads/master
Commit: c93de246638b778260484c6ba32ab1cf9f5a253b
Parents: dbd70f5
Author: James Taylor <ja...@apache.org>
Authored: Tue Jun 6 18:38:49 2017 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Wed Jun 7 10:13:38 2017 -0700
----------------------------------------------------------------------
.../phoenix/end2end/ArrayAppendFunctionIT.java | 42 +++++++++++++++--
.../phoenix/end2end/ArrayConcatFunctionIT.java | 39 +++++++++++++---
.../coprocessor/HashJoinRegionScanner.java | 4 +-
.../expression/ProjectedColumnExpression.java | 7 ++-
.../expression/StringConcatExpression.java | 3 ++
.../function/ArrayConcatFunction.java | 12 +++--
.../function/ArrayModifierFunction.java | 2 -
.../function/ConvertTimezoneFunction.java | 10 ++++
.../expression/function/EncodeFunction.java | 5 +-
.../phoenix/iterate/FilterResultIterator.java | 5 +-
.../phoenix/schema/types/PArrayDataType.java | 48 ++++++++++++++++++--
.../schema/types/PArrayDataTypeEncoder.java | 6 +--
.../expression/ArrayConcatFunctionTest.java | 27 ++++++++++-
13 files changed, 180 insertions(+), 30 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayAppendFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayAppendFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayAppendFunctionIT.java
index e83b395..089cd41 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayAppendFunctionIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayAppendFunctionIT.java
@@ -17,11 +17,16 @@
*/
package org.apache.phoenix.end2end;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import java.sql.*;
+import java.sql.Array;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
import org.apache.phoenix.schema.TypeMismatchException;
import org.junit.Test;
@@ -29,7 +34,9 @@ import org.junit.Test;
public class ArrayAppendFunctionIT extends ParallelStatsDisabledIT {
private String initTables(Connection conn) throws Exception {
String tableName = generateUniqueName();
- String ddl = "CREATE TABLE " + tableName + " (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[],integers INTEGER[],doubles DOUBLE[],bigints BIGINT[],chars CHAR(15)[],double1 DOUBLE,char1 CHAR(17),nullcheck INTEGER,chars2 CHAR(15)[])";
+ String ddl = "CREATE TABLE " + tableName
+ + " (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[],integers INTEGER[],doubles DOUBLE[],bigints BIGINT[],"
+ + "chars CHAR(15)[],double1 DOUBLE,char1 CHAR(17),nullcheck INTEGER,chars2 CHAR(15)[], nullVarchar VARCHAR[], nullBigInt BIGINT[])";
conn.createStatement().execute(ddl);
String dml = "UPSERT INTO " + tableName + "(region_name,varchars,integers,doubles,bigints,chars,double1,char1,nullcheck,chars2) VALUES('SF Bay Area'," +
"ARRAY['2345','46345','23234']," +
@@ -69,6 +76,35 @@ public class ArrayAppendFunctionIT extends ParallelStatsDisabledIT {
}
@Test
+ public void testEmptyArrayModification() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ String tableName = initTables(conn);
+
+ ResultSet rs;
+ rs = conn.createStatement().executeQuery("SELECT ARRAY_APPEND(nullVarChar,'34567'),ARRAY_PREPEND('34567',nullVarChar) FROM " + tableName + " LIMIT 1");
+ assertTrue(rs.next());
+
+ String[] strings = new String[]{"34567"};
+
+ Array array = conn.createArrayOf("VARCHAR", strings);
+
+ assertEquals(array, rs.getArray(1));
+ assertEquals(array, rs.getArray(2));
+ assertFalse(rs.next());
+
+ rs = conn.createStatement().executeQuery("SELECT ARRAY_APPEND(nullBigint,123),ARRAY_PREPEND(123,nullBigint) FROM " + tableName + " LIMIT 1");
+ assertTrue(rs.next());
+
+ Long[] longs = new Long[]{123L};
+
+ array = conn.createArrayOf("BIGINT", longs);
+
+ assertEquals(array, rs.getArray(1));
+ assertEquals(array, rs.getArray(2));
+ assertFalse(rs.next());
+ }
+
+ @Test
public void testArrayAppendFunctionVarchar() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
String tableName = initTables(conn);
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayConcatFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayConcatFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayConcatFunctionIT.java
index e8ca9dc..c4307af 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayConcatFunctionIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayConcatFunctionIT.java
@@ -17,20 +17,25 @@
*/
package org.apache.phoenix.end2end;
-import org.apache.phoenix.schema.TypeMismatchException;
-import org.junit.Test;
-
-import java.sql.*;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.sql.Array;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.phoenix.schema.TypeMismatchException;
+import org.junit.Test;
+
public class ArrayConcatFunctionIT extends ParallelStatsDisabledIT {
private String initTables(Connection conn) throws Exception {
String tableName = generateUniqueName();
- String ddl = "CREATE TABLE " + tableName + " (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[],integers INTEGER[],doubles DOUBLE[],bigints BIGINT[],chars CHAR(15)[],double1 DOUBLE,char1 CHAR(17),nullcheck INTEGER,chars2 CHAR(15)[])";
+ String ddl = "CREATE TABLE " + tableName + " (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[],integers INTEGER[],doubles DOUBLE[],bigints BIGINT[],chars CHAR(15)[],double1 DOUBLE,char1 CHAR(17),nullcheck INTEGER,chars2 CHAR(15)[], nullvarchar VARCHAR[], nullbigint BIGINT[])";
conn.createStatement().execute(ddl);
String dml = "UPSERT INTO " + tableName + "(region_name,varchars,integers,doubles,bigints,chars,double1,char1,nullcheck,chars2) VALUES('SF Bay Area'," +
"ARRAY['2345','46345','23234']," +
@@ -67,6 +72,28 @@ public class ArrayConcatFunctionIT extends ParallelStatsDisabledIT {
}
@Test
+ public void testNullArrayConcat() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ String tableName = initTables(conn);
+
+ ResultSet rs;
+ rs = conn.createStatement().executeQuery("SELECT ARRAY_CAT(nullvarchar,varchars) FROM " + tableName + " WHERE region_name = 'SF Bay Area'");
+ assertTrue(rs.next());
+
+ String[] strings = new String[]{"2345", "46345", "23234"};
+
+ Array array = conn.createArrayOf("VARCHAR", strings);
+
+ assertEquals(array, rs.getArray(1));
+ assertFalse(rs.next());
+
+ rs = conn.createStatement().executeQuery("SELECT ARRAY_CAT(varchars,nullvarchar) FROM " + tableName + " WHERE region_name = 'SF Bay Area'");
+ assertTrue(rs.next());
+ assertEquals(array, rs.getArray(1));
+ assertFalse(rs.next());
+ }
+
+ @Test
public void testArrayConcatFunctionInteger() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
String tableName = initTables(conn);
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/HashJoinRegionScanner.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/HashJoinRegionScanner.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/HashJoinRegionScanner.java
index 79ff4b0..ced29bd 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/HashJoinRegionScanner.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/HashJoinRegionScanner.java
@@ -203,7 +203,7 @@ public class HashJoinRegionScanner implements RegionScanner {
postFilter.reset();
ImmutableBytesPtr tempPtr = new ImmutableBytesPtr();
try {
- if (!postFilter.evaluate(t, tempPtr)) {
+ if (!postFilter.evaluate(t, tempPtr) || tempPtr.getLength() == 0) {
iter.remove();
continue;
}
@@ -212,7 +212,7 @@ public class HashJoinRegionScanner implements RegionScanner {
continue;
}
Boolean b = (Boolean)postFilter.getDataType().toObject(tempPtr);
- if (!b.booleanValue()) {
+ if (!Boolean.TRUE.equals(b)) {
iter.remove();
}
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/ProjectedColumnExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ProjectedColumnExpression.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/ProjectedColumnExpression.java
index 2744f35..f851efa 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ProjectedColumnExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ProjectedColumnExpression.java
@@ -32,6 +32,7 @@ import org.apache.phoenix.schema.PColumn;
import org.apache.phoenix.schema.PTable;
import org.apache.phoenix.schema.ValueBitSet;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.util.ByteUtil;
import org.apache.phoenix.util.SchemaUtil;
public class ProjectedColumnExpression extends ColumnExpression {
@@ -113,8 +114,10 @@ public class ProjectedColumnExpression extends ColumnExpression {
int maxOffset = ptr.getOffset() + ptr.getLength() - bitSet.getEstimatedLength();
schema.iterator(ptr, position, bitSet);
Boolean hasValue = schema.next(ptr, position, maxOffset, bitSet);
- if (hasValue == null || !hasValue.booleanValue())
- return false;
+ if (hasValue == null || !hasValue.booleanValue()) {
+ ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
+ return true;
+ }
} catch (IOException e) {
return false;
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/StringConcatExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/StringConcatExpression.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/StringConcatExpression.java
index 85eb0f3..dc9868d 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/StringConcatExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/StringConcatExpression.java
@@ -86,6 +86,9 @@ public class StringConcatExpression extends BaseCompoundExpression {
// have enough information yet.
return false;
}
+ if (ptr.getLength() == 0) {
+ continue;
+ }
PDataType childType = children.get(i).getDataType();
SortOrder sortOrder = children.get(i).getSortOrder();
// We could potentially not invert the bytes, but we might as well since we're allocating
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayConcatFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayConcatFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayConcatFunction.java
index 85655c6..28774cb 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayConcatFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayConcatFunction.java
@@ -48,7 +48,7 @@ public class ArrayConcatFunction extends ArrayModifierFunction {
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
- if (!getLHSExpr().evaluate(tuple, ptr)|| ptr.getLength() == 0){
+ if (!getLHSExpr().evaluate(tuple, ptr)){
return false;
}
boolean isLHSRowKeyOrderOptimized = PArrayDataType.isRowKeyOrderOptimized(getLHSExpr().getDataType(), getLHSExpr().getSortOrder(), ptr);
@@ -58,8 +58,11 @@ public class ArrayConcatFunction extends ArrayModifierFunction {
int lengthArray1 = ptr.getLength();
int offsetArray1 = ptr.getOffset();
byte[] array1Bytes = ptr.get();
- if (!getRHSExpr().evaluate(tuple, ptr)|| ptr.getLength() == 0){
- sortOrder = getLHSExpr().getSortOrder();
+ if (!getRHSExpr().evaluate(tuple, ptr)) {
+ return false;
+ }
+ // If second array is null, return first array
+ if (ptr.getLength() == 0){
ptr.set(array1Bytes, offsetArray1, lengthArray1);
return true;
}
@@ -73,6 +76,9 @@ public class ArrayConcatFunction extends ArrayModifierFunction {
getLHSExpr().getDataType().coerceBytes(ptr, null, getRHSExpr().getDataType(), getRHSExpr().getMaxLength(),
getRHSExpr().getScale(), getRHSExpr().getSortOrder(), getLHSExpr().getMaxLength(),
getLHSExpr().getScale(), getLHSExpr().getSortOrder(), isLHSRowKeyOrderOptimized);
+ if (lengthArray1 == 0) {
+ return true;
+ }
return modifierFunction(ptr, lengthArray1, offsetArray1, array1Bytes, getLHSBaseType(), actualLengthOfArray1, getMaxLength(), getLHSExpr());
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java
index bcf2a5a..2039690 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java
@@ -93,8 +93,6 @@ public abstract class ArrayModifierFunction extends ScalarFunction {
}
if (!arrayExpr.evaluate(tuple, ptr)) {
return false;
- } else if (ptr.getLength() == 0) {
- return true;
}
int arrayLength = PArrayDataType.getArrayLength(ptr, baseDataType, arrayExpr.getMaxLength());
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
index 8d13ab6..56f65e0 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ConvertTimezoneFunction.java
@@ -19,6 +19,7 @@ package org.apache.phoenix.expression.function;
import java.sql.SQLException;
import java.util.List;
+
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.cache.JodaTimezoneCache;
import org.apache.phoenix.expression.Expression;
@@ -60,16 +61,25 @@ public class ConvertTimezoneFunction extends ScalarFunction {
if (!children.get(0).evaluate(tuple, ptr)) {
return false;
}
+ if (ptr.getLength() == 0) {
+ return true;
+ }
long date = PDate.INSTANCE.getCodec().decodeLong(ptr, children.get(0).getSortOrder());
if (!children.get(1).evaluate(tuple, ptr)) {
return false;
}
+ if (ptr.getLength() == 0) {
+ return true;
+ }
DateTimeZone timezoneFrom = JodaTimezoneCache.getInstance(ptr);
if (!children.get(2).evaluate(tuple, ptr)) {
return false;
}
+ if (ptr.getLength() == 0) {
+ return true;
+ }
DateTimeZone timezoneTo = JodaTimezoneCache.getInstance(ptr);
long convertedDate = date - timezoneFrom.getOffset(date) + timezoneTo.getOffset(date);
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/expression/function/EncodeFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/EncodeFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/EncodeFunction.java
index ebb4953..1078566 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/EncodeFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/EncodeFunction.java
@@ -23,10 +23,10 @@ import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode.Argument;
import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
import org.apache.phoenix.schema.IllegalDataException;
+import org.apache.phoenix.schema.tuple.Tuple;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PLong;
import org.apache.phoenix.schema.types.PVarchar;
-import org.apache.phoenix.schema.tuple.Tuple;
import org.apache.phoenix.util.Base62Encoder;
/**
@@ -52,6 +52,9 @@ public class EncodeFunction extends ScalarFunction {
if (!numExpr.evaluate(tuple, ptr)) {
return false;
}
+ if (ptr.getLength() == 0) {
+ return true;
+ }
long num = numExpr.getDataType().getCodec().decodeLong(ptr, numExpr.getSortOrder());
Expression encodingExpression = getEncodingExpr();
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/iterate/FilterResultIterator.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/iterate/FilterResultIterator.java b/phoenix-core/src/main/java/org/apache/phoenix/iterate/FilterResultIterator.java
index eaa6956..65bcad2 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/iterate/FilterResultIterator.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/iterate/FilterResultIterator.java
@@ -21,10 +21,9 @@ import java.sql.SQLException;
import java.util.List;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-
import org.apache.phoenix.expression.Expression;
-import org.apache.phoenix.schema.types.PBoolean;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBoolean;
/**
@@ -60,7 +59,7 @@ public class FilterResultIterator extends LookAheadResultIterator {
do {
next = delegate.next();
expression.reset();
- } while (next != null && (!expression.evaluate(next, ptr) || Boolean.FALSE.equals(expression.getDataType().toObject(ptr))));
+ } while (next != null && (!expression.evaluate(next, ptr) || ptr.getLength() == 0 || !Boolean.TRUE.equals(expression.getDataType().toObject(ptr))));
return next;
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
index 2a6d1f8..c06dd1c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
@@ -425,6 +425,27 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
return builder.encode();
}
+ private static byte[] generateEmptyArrayBytes(PDataType baseType, SortOrder sortOrder) {
+ PArrayDataTypeEncoder encoder = new PArrayDataTypeEncoder(baseType, sortOrder);
+ byte[] arrayBytes = encoder.encode();
+ if (arrayBytes == null) {
+ arrayBytes = ByteUtil.EMPTY_BYTE_ARRAY;
+ }
+ return arrayBytes;
+ }
+
+ /**
+ * Appends an item to array. Uses the ptr bytes of item and the array bytes to create new array bytes with appended item bytes,
+ * then sets the new array bytes to ptr.
+ *
+ * @param ptr holds the bytes of the item to be added to array
+ * @param arrayBytes byte [] form of phoenix array
+ * @param length arrayBytes length
+ * @param offset arrayBytes offset
+ * @param arrayLength length of the array
+ * @param maxLength maximum length of the item to be added
+ * @param sortOrder sort order of the elements in array
+ */
public static boolean appendItemToArray(ImmutableBytesWritable ptr, int length, int offset, byte[] arrayBytes,
PDataType baseType, int arrayLength, Integer maxLength, SortOrder sortOrder) {
if (ptr.getLength() == 0) {
@@ -432,6 +453,13 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
return true;
}
+ // If the arrayBytes is null or empty, generate an empty array which will get filled in below
+ if (arrayBytes.length == 0) {
+ arrayBytes = generateEmptyArrayBytes(baseType, sortOrder);
+ offset = 0;
+ length = arrayBytes.length;
+ }
+
int elementLength = maxLength == null ? ptr.getLength() : maxLength;
// padding
@@ -451,7 +479,7 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
- Bytes.SIZEOF_BYTE;
// checks whether offset array consists of shorts or integers
- boolean useInt = offsetArrayLength / Math.abs(arrayLength) == Bytes.SIZEOF_INT;
+ boolean useInt = arrayLength == 0 ? false : offsetArrayLength / Math.abs(arrayLength) == Bytes.SIZEOF_INT;
boolean convertToInt = false;
int newElementPosition = offsetArrayPosition - 2 * Bytes.SIZEOF_BYTE;
@@ -478,7 +506,11 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
newArray[newOffsetArrayPosition-1] = sepByte;
System.arraycopy(elementBytes, elementOffset, newArray, newElementPosition, elementLength);
- arrayLength = (Math.abs(arrayLength) + 1) * (int)Math.signum(arrayLength);
+ int factor = (int)Math.signum(arrayLength);
+ if (factor == 0) {
+ factor = 1;
+ }
+ arrayLength = (Math.abs(arrayLength) + 1) * factor;
if (useInt) {
System.arraycopy(arrayBytes, offset + offsetArrayPosition, newArray, newOffsetArrayPosition,
offsetArrayLength);
@@ -537,6 +569,13 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
if (ptr.getLength() == 0) {
elementLength = 0;
}
+ // If the arrayBytes is null or empty, generate an empty array which will get filled in below
+ if (arrayBytes.length == 0) {
+ arrayBytes = generateEmptyArrayBytes(baseType, sortOrder);
+ offset = 0;
+ length = arrayBytes.length;
+ }
+
// padding
if (elementLength > ptr.getLength()) {
baseType.pad(ptr, elementLength, sortOrder);
@@ -554,7 +593,7 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
arrayLength = Math.abs(arrayLength);
// checks whether offset array consists of shorts or integers
- boolean useInt = offsetArrayLength / arrayLength == Bytes.SIZEOF_INT;
+ boolean useInt = arrayLength == 0 ? false : offsetArrayLength / arrayLength == Bytes.SIZEOF_INT;
boolean convertToInt = false;
int endElementPosition = getOffset(arrayBytes, arrayLength - 1, !useInt, offsetArrayPosition + offset, serializationVersion)
+ elementLength + Bytes.SIZEOF_BYTE;
@@ -1067,6 +1106,9 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
public static int getArrayLength(ImmutableBytesWritable ptr, PDataType baseType, Integer maxLength) {
byte[] bytes = ptr.get();
+ if (ptr.getLength() == 0) {
+ return 0;
+ }
if (baseType.isFixedWidth()) {
int elemLength = maxLength == null ? baseType.getByteSize() : maxLength;
return (ptr.getLength() / elemLength);
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataTypeEncoder.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataTypeEncoder.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataTypeEncoder.java
index b5a7e04..3dad6c3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataTypeEncoder.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataTypeEncoder.java
@@ -153,9 +153,9 @@ public class PArrayDataTypeEncoder implements ColumnValueEncoder {
if (!baseType.isFixedWidth()) {
int noOfElements = offsetPos.size();
int[] offsetPosArray = new int[noOfElements];
- int index = 0;
+ int index = 0, maxOffset = 0;
for (Integer i : offsetPos) {
- offsetPosArray[index] = i;
+ maxOffset = offsetPosArray[index] = i;
++index;
}
if (serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION) {
@@ -163,7 +163,7 @@ public class PArrayDataTypeEncoder implements ColumnValueEncoder {
PArrayDataType.writeEndSeperatorForVarLengthArray(oStream, sortOrder, rowKeyOrderOptimizable);
}
noOfElements = PArrayDataType.serializeOffsetArrayIntoStream(oStream, byteStream, noOfElements,
- offsetPosArray[offsetPosArray.length - 1], offsetPosArray, serializationVersion);
+ maxOffset, offsetPosArray, serializationVersion);
PArrayDataType.serializeHeaderInfoIntoStream(oStream, noOfElements, serializationVersion);
}
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
http://git-wip-us.apache.org/repos/asf/phoenix/blob/c93de246/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConcatFunctionTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConcatFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConcatFunctionTest.java
index f23729e..0317e6f 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConcatFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConcatFunctionTest.java
@@ -31,7 +31,30 @@ import org.apache.phoenix.exception.DataExceedsCapacityException;
import org.apache.phoenix.expression.function.ArrayConcatFunction;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.TypeMismatchException;
-import org.apache.phoenix.schema.types.*;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PBooleanArray;
+import org.apache.phoenix.schema.types.PChar;
+import org.apache.phoenix.schema.types.PCharArray;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDate;
+import org.apache.phoenix.schema.types.PDateArray;
+import org.apache.phoenix.schema.types.PDecimal;
+import org.apache.phoenix.schema.types.PDecimalArray;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.PDoubleArray;
+import org.apache.phoenix.schema.types.PFloat;
+import org.apache.phoenix.schema.types.PFloatArray;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PIntegerArray;
+import org.apache.phoenix.schema.types.PLong;
+import org.apache.phoenix.schema.types.PLongArray;
+import org.apache.phoenix.schema.types.PSmallint;
+import org.apache.phoenix.schema.types.PSmallintArray;
+import org.apache.phoenix.schema.types.PTinyint;
+import org.apache.phoenix.schema.types.PTinyintArray;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.schema.types.PVarcharArray;
+import org.apache.phoenix.schema.types.PhoenixArray;
import org.junit.Test;
import com.google.common.collect.Lists;
@@ -363,7 +386,7 @@ public class ArrayConcatFunctionTest {
PhoenixArray arr1 = null;
PhoenixArray arr2 = new PhoenixArray(base, o2);
- PhoenixArray expected = null;
+ PhoenixArray expected = arr2;
test(arr1, arr2, type, null, null, type, null, null, expected, SortOrder.ASC, SortOrder.ASC);
test(arr1, arr2, type, null, null, type, null, null, expected, SortOrder.ASC, SortOrder.DESC);
test(arr1, arr2, type, null, null, type, null, null, expected, SortOrder.DESC, SortOrder.DESC);
[3/3] phoenix git commit: PHOENIX-3773 Support FIRST_VALUES and
LAST_VALUES aggregate function to retrieve n values (Loknath Priyatham Teja
Singamsetty)
Posted by ja...@apache.org.
PHOENIX-3773 Support FIRST_VALUES and LAST_VALUES aggregate function to retrieve n values (Loknath Priyatham Teja Singamsetty)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/9095b3ec
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/9095b3ec
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/9095b3ec
Branch: refs/heads/master
Commit: 9095b3ec87aef45138d62e5bbffdafee3e934d38
Parents: c93de24
Author: James Taylor <ja...@apache.org>
Authored: Wed Jun 7 10:15:13 2017 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Wed Jun 7 10:15:13 2017 -0700
----------------------------------------------------------------------
.../phoenix/end2end/FirstValuesFunctionIT.java | 638 ++++++++++++++++++
.../phoenix/end2end/LastValuesFunctionIT.java | 640 +++++++++++++++++++
.../phoenix/expression/ExpressionType.java | 6 +-
.../FirstLastValueBaseClientAggregator.java | 65 +-
.../FirstLastValueServerAggregator.java | 13 +-
.../expression/function/FirstValueFunction.java | 2 +-
.../function/FirstValuesFunction.java | 97 +++
.../expression/function/LastValueFunction.java | 2 +-
.../expression/function/LastValuesFunction.java | 97 +++
.../expression/function/NthValueFunction.java | 4 +-
.../parse/FirstValuesAggregateParseNode.java | 38 ++
.../parse/LastValuesAggregateParseNode.java | 38 ++
.../util/FirstLastNthValueDataContainer.java | 7 +-
13 files changed, 1619 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/it/java/org/apache/phoenix/end2end/FirstValuesFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/FirstValuesFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/FirstValuesFunctionIT.java
new file mode 100644
index 0000000..1f0ea69
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/FirstValuesFunctionIT.java
@@ -0,0 +1,638 @@
+/*
+ * 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.end2end;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class FirstValuesFunctionIT extends ParallelStatsDisabledIT {
+
+ //@Test
+ public void simpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 9, 5, 10)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 9, 6, 13)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (8, 9, 7, 8)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (9, 9, 8, 6)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{2, 4, 9}, (int[]) rs.getArray(1).getArray());
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{6, 8, 13}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void varcharDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String table_name = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + table_name + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG, "
+ + "\"DATE\" VARCHAR(3), \"value\" VARCHAR(3))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, '1', '3')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, '2', '7')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, '3', '9')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, '4', '2')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, '5', '4')");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + table_name
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new String[]{"3", "7"}, (String[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void floatDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" FLOAT, \"value\" FLOAT)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 1, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 5, 400)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ assertEquals(((float[])rs.getArray(1).getArray())[0], 300, 0.000001);
+ assertEquals(((float[])rs.getArray(1).getArray())[1], 7, 0.000001);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void doubleDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG, "
+ + "\"DATE\" DOUBLE, \"value\" DOUBLE)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 1, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 5, 400)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ assertEquals(((double[])rs.getArray(1).getArray())[0], 300, 0.000001);
+ assertEquals(((double[])rs.getArray(1).getArray())[1], 7, 0.000001);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void offsetValueAscOrder() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{300, 7}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void simpleTestNoGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName);
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{9, 7}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void rowLessThanOffsetNoGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName);
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{9, 7}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void offsetValueDescOrder() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" DESC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{150, 2}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void offsetValueSubAggregation() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 9, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 9, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 10, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 10, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(SUM_VALUE, 2) WITHIN GROUP (ORDER BY MIN_DATE ASC) FROM (" +
+ "SELECT MIN(\"DATE\") AS MIN_DATE, SUM(\"value\") AS SUM_VALUE FROM "
+ + tableName + " GROUP BY page_id) x");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{307, 13}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void offsetValueLastMismatchByColumn() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 5, 8)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 4, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 3, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 0, 1)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" DESC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{8, 4}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void testSortOrderInDataColWithOffset() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL, page_id UNSIGNED_LONG,"
+ + " dates BIGINT NOT NULL, \"value\" BIGINT NOT NULL CONSTRAINT pk PRIMARY KEY (id, dates, \"value\" DESC))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (1, 8, 1, 3)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (5, 8, 5, 158)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (4, 8, 4, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{3, 7}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void nonUniqueValuesInOrderByAsc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 7);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void nonUniqueValuesInOrderByAscSkipDuplicit() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 5) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 7);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertInIntArray(new int[]{2, 4, 9}, result[3]);
+ assertEquals(result[4], 3);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void nonUniqueValuesInOrderByDesc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 3);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void nonUniqueValuesInOrderNextValueDesc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 8, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertInIntArray(new int[]{3, 5}, result[0]);
+ assertInIntArray(new int[]{3, 5}, result[1]);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void nonUniqueValuesInOrderNextValueAsc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 8, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 5) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 7);
+ assertEquals(result[1], 9);
+ assertInIntArray(new int[]{2, 4}, result[2]);
+ assertInIntArray(new int[]{2, 4}, result[3]);
+ assertInIntArray(new int[]{3, 5}, result[4]);
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void ignoreNullValues() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL, page_id UNSIGNED_LONG,"
+ + " dates BIGINT NOT NULL, \"value\" BIGINT NULL CONSTRAINT pk PRIMARY KEY (id, dates))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (1, 8, 1, 1)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (2, 8, 2, NULL)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (3, 8, 3, NULL)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (5, 8, 4, 4)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (4, 8, 5, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{1, 4}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ //@Test
+ public void rowLessThanOffsetWithGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 9, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT FIRST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{7, 9}, (int[]) rs.getArray(1).getArray());
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{5}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ private void assertInIntArray(int[] should, int actualValue) {
+ ArrayList<Integer> shouldList = new ArrayList<Integer>();
+ for (int i: should) {
+ shouldList.add(i);
+ }
+ assertTrue(shouldList.contains(actualValue));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/it/java/org/apache/phoenix/end2end/LastValuesFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/LastValuesFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LastValuesFunctionIT.java
new file mode 100644
index 0000000..f48e4df
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LastValuesFunctionIT.java
@@ -0,0 +1,640 @@
+/*
+ * 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.end2end;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class LastValuesFunctionIT extends ParallelStatsDisabledIT {
+
+ @Test
+ public void simpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 9, 5, 10)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 9, 6, 13)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (8, 9, 7, 8)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (9, 9, 8, 6)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{7, 9, 4}, (int[]) rs.getArray(1).getArray());
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{10, 13, 8}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void varcharDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String table_name = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + table_name + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG, "
+ + "\"DATE\" VARCHAR(3), \"value\" VARCHAR(3))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, '1', '3')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, '2', '7')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, '3', '9')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, '4', '2')");
+ conn.createStatement().execute(
+ "UPSERT INTO " + table_name + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, '5', '4')");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + table_name
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new String[]{"4", "2"}, (String[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void floatDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" FLOAT, \"value\" FLOAT)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 1, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 5, 400)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ assertEquals(((float[])rs.getArray(1).getArray())[0], 400, 0.000001);
+ assertEquals(((float[])rs.getArray(1).getArray())[1], 2, 0.000001);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void doubleDatatypeSimpleTest() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG, "
+ + "\"DATE\" DOUBLE, \"value\" DOUBLE)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 1, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 5, 400)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ assertEquals(((double[])rs.getArray(1).getArray())[0], 400, 0.000001);
+ assertEquals(((double[])rs.getArray(1).getArray())[1], 2, 0.000001);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void offsetValueAscOrder() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{150, 2}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void simpleTestNoGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName);
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{7, 9}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void rowLessThanOffsetNoGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName);
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{7, 9}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void offsetValueDescOrder() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" DESC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{300, 7}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void offsetValueSubAggregation() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 0, 300)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 9, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 9, 3, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 10, 4, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 10, 5, 150)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(SUM_VALUE, 2) WITHIN GROUP (ORDER BY MIN_DATE ASC) FROM (" +
+ "SELECT MIN(\"DATE\") AS MIN_DATE, SUM(\"value\") AS SUM_VALUE FROM "
+ + tableName + " GROUP BY page_id) x");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{152, 13}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void offsetValueLastMismatchByColumn() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " \"DATE\" INTEGER, \"value\" UNSIGNED_LONG)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (1, 8, 5, 8)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (4, 8, 4, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (5, 8, 3, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, \"DATE\", \"value\") VALUES (6, 8, 0, 1)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY \"DATE\" DESC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{1, 9}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void testSortOrderInDataColWithOffset() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL, page_id UNSIGNED_LONG,"
+ + " dates BIGINT NOT NULL, \"value\" BIGINT NOT NULL CONSTRAINT pk PRIMARY KEY (id, dates, \"value\" DESC))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (1, 8, 1, 3)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (2, 8, 2, 7)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (3, 8, 3, 9)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (5, 8, 5, 158)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (4, 8, 4, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{158, 5}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void nonUniqueValuesInOrderByAsc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 3);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void nonUniqueValuesInOrderByAscSkipDuplicit() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 5) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 3);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertInIntArray(new int[]{2, 4, 9}, result[3]);
+ assertEquals(result[4], 7);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void nonUniqueValuesInOrderByDesc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 1, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 2, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 3) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 7);
+ assertInIntArray(new int[]{2, 4, 9}, result[1]);
+ assertInIntArray(new int[]{2, 4, 9}, result[2]);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void nonUniqueValuesInOrderNextValueDesc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 8, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates DESC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 7);
+ assertEquals(result[1], 9);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void nonUniqueValuesInOrderNextValueAsc() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 8, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 5) WITHIN GROUP (ORDER BY dates ASC) FROM " + tableName
+ + " GROUP BY page_id");
+
+ int[] result = null;
+ assertTrue(rs.next());
+ result = (int[]) rs.getArray(1).getArray();
+ assertInIntArray(new int[]{3, 5}, result[0]);
+ assertInIntArray(new int[]{3, 5}, result[1]);
+ assertInIntArray(new int[]{2, 4}, result[2]);
+ assertInIntArray(new int[]{2, 4}, result[3]);
+ assertEquals(result[4], 9);
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void ignoreNullValues() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL, page_id UNSIGNED_LONG,"
+ + " dates BIGINT NOT NULL, \"value\" BIGINT NULL CONSTRAINT pk PRIMARY KEY (id, dates))";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (1, 8, 1, 1)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (2, 8, 2, NULL)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (3, 8, 3, NULL)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (5, 8, 4, NULL)");
+ conn.createStatement().execute("UPSERT INTO " + tableName
+ + " (id, page_id, dates, \"value\") VALUES (4, 8, 5, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(\"value\", 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new long[]{5, 1}, (long[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void rowLessThanOffsetWithGroupBy() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+
+ String tableName = generateUniqueName();
+ String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " "
+ + "(id INTEGER NOT NULL PRIMARY KEY, page_id UNSIGNED_LONG,"
+ + " dates INTEGER, val INTEGER)";
+ conn.createStatement().execute(ddl);
+
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (2, 8, 0, 7)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (3, 8, 1, 9)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (4, 8, 2, 4)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (5, 8, 2, 2)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (6, 8, 3, 3)");
+ conn.createStatement().execute(
+ "UPSERT INTO " + tableName + " (id, page_id, dates, val) VALUES (7, 9, 3, 5)");
+ conn.commit();
+
+ ResultSet rs = conn.createStatement().executeQuery(
+ "SELECT LAST_VALUES(val, 2) WITHIN GROUP (ORDER BY dates ASC) FROM "
+ + tableName + " GROUP BY page_id");
+
+ assertTrue(rs.next());
+ int[] result = (int[]) rs.getArray(1).getArray();
+ assertEquals(result[0], 3);
+ assertInIntArray(new int[]{2, 4}, result[1]);
+ assertTrue(rs.next());
+ Assert.assertArrayEquals(new int[]{5}, (int[]) rs.getArray(1).getArray());
+ assertFalse(rs.next());
+ }
+
+ private void assertInIntArray(int[] should, int actualValue) {
+ ArrayList<Integer> shouldList = new ArrayList<Integer>();
+ for (int i: should) {
+ shouldList.add(i);
+ }
+ assertTrue(shouldList.contains(actualValue));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
index f5f4a29..b8f68da 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
@@ -53,6 +53,7 @@ import org.apache.phoenix.expression.function.EncodeFunction;
import org.apache.phoenix.expression.function.ExpFunction;
import org.apache.phoenix.expression.function.ExternalSqlTypeIdFunction;
import org.apache.phoenix.expression.function.FirstValueFunction;
+import org.apache.phoenix.expression.function.FirstValuesFunction;
import org.apache.phoenix.expression.function.FloorDateExpression;
import org.apache.phoenix.expression.function.FloorDecimalExpression;
import org.apache.phoenix.expression.function.FloorFunction;
@@ -67,6 +68,7 @@ import org.apache.phoenix.expression.function.InstrFunction;
import org.apache.phoenix.expression.function.InvertFunction;
import org.apache.phoenix.expression.function.LTrimFunction;
import org.apache.phoenix.expression.function.LastValueFunction;
+import org.apache.phoenix.expression.function.LastValuesFunction;
import org.apache.phoenix.expression.function.LengthFunction;
import org.apache.phoenix.expression.function.LnFunction;
import org.apache.phoenix.expression.function.LogFunction;
@@ -288,7 +290,9 @@ public enum ExpressionType {
DayOfWeekFunction(DayOfWeekFunction.class),
DayOfYearFunction(DayOfYearFunction.class),
DefaultValueExpression(DefaultValueExpression.class),
- ArrayColumnExpression(SingleCellColumnExpression.class);
+ ArrayColumnExpression(SingleCellColumnExpression.class),
+ FirstValuesFunction(FirstValuesFunction.class),
+ LastValuesFunction(LastValuesFunction.class);
ExpressionType(Class<? extends Expression> clazz) {
this.clazz = clazz;
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueBaseClientAggregator.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueBaseClientAggregator.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueBaseClientAggregator.java
index 804c38e..5c49312 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueBaseClientAggregator.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueBaseClientAggregator.java
@@ -27,16 +27,17 @@ import java.util.TreeMap;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes.ByteArrayComparator;
-import org.apache.phoenix.schema.types.PDataType;
-import org.apache.phoenix.schema.types.PVarbinary;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.tuple.SingleKeyValueTuple;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PArrayDataType;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PVarbinary;
import org.apache.phoenix.util.ByteUtil;
import org.apache.phoenix.util.FirstLastNthValueDataContainer;
/**
- * Base client aggregator for (FIRST|LAST|NTH)_VALUE functions
+ * Base client aggregator for (FIRST|LAST|NTH)_VALUE and (FIRST|LAST)_VALUES functions
*
*/
public class FirstLastValueBaseClientAggregator extends BaseAggregator {
@@ -47,9 +48,19 @@ public class FirstLastValueBaseClientAggregator extends BaseAggregator {
protected byte[] topValue = null;
protected TreeMap<byte[], LinkedList<byte[]>> topValues = new TreeMap<byte[], LinkedList<byte[]>>(new ByteArrayComparator());
protected boolean isAscending;
+ protected PDataType dataType;
+
+ // Set to true for retrieving multiple top values for FIRST_VALUES or LAST_VALUES
+ protected boolean isArrayReturnType = false;
public FirstLastValueBaseClientAggregator() {
super(SortOrder.getDefault());
+ this.dataType = PVarbinary.INSTANCE;
+ }
+
+ public FirstLastValueBaseClientAggregator(PDataType type) {
+ super(SortOrder.getDefault());
+ this.dataType = (type == null) ? PVarbinary.INSTANCE : type;
}
@Override
@@ -73,20 +84,44 @@ public class FirstLastValueBaseClientAggregator extends BaseAggregator {
entrySet = topValues.descendingMap().entrySet();
}
- int counter = offset;
+ int counter = 0;
+ ImmutableBytesWritable arrPtr = new ImmutableBytesWritable(ByteUtil.EMPTY_BYTE_ARRAY);
for (Map.Entry<byte[], LinkedList<byte[]>> entry : entrySet) {
ListIterator<byte[]> it = entry.getValue().listIterator();
while (it.hasNext()) {
- if (--counter == 0) {
- ptr.set(it.next());
- return true;
+ if (isArrayReturnType) {
+ ImmutableBytesWritable newArrPtr = new ImmutableBytesWritable(it.next());
+ PArrayDataType.appendItemToArray(
+ newArrPtr,
+ arrPtr.getLength(),
+ arrPtr.getOffset(),
+ arrPtr.get(),
+ PDataType.fromTypeId(dataType.getSqlType() - PDataType.ARRAY_TYPE_BASE),
+ counter,
+ null,
+ sortOrder);
+ arrPtr = newArrPtr;
+
+ if (++counter == offset) {
+ break;
+ }
+ } else {
+ if (++counter == offset) {
+ ptr.set(it.next());
+ return true;
+ }
+ it.next();
}
- it.next();
}
}
- //not enought values to return Nth
- return false;
+ if (isArrayReturnType) {
+ ptr.set(arrPtr.get());
+ return true;
+ }
+
+ ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
+ return true;
}
if (topValue == null) {
@@ -100,7 +135,7 @@ public class FirstLastValueBaseClientAggregator extends BaseAggregator {
@Override
public void aggregate(Tuple tuple, ImmutableBytesWritable ptr) {
- //if is called cause aggregation in ORDER BY clausule
+ //if is called cause aggregation in ORDER BY clause
if (tuple instanceof SingleKeyValueTuple) {
topValue = ptr.copyBytes();
return;
@@ -143,13 +178,15 @@ public class FirstLastValueBaseClientAggregator extends BaseAggregator {
@Override
public PDataType getDataType() {
- return PVarbinary.INSTANCE;
+ return dataType;
}
- public void init(int offset) {
- if (offset != 0) {
+ public void init(int offset, boolean isArrayReturnType) {
+ if (offset > 0) {
useOffset = true;
this.offset = offset;
}
+
+ this.isArrayReturnType = isArrayReturnType;
}
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueServerAggregator.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueServerAggregator.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueServerAggregator.java
index 66b38c6..b41c6c6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueServerAggregator.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/aggregator/FirstLastValueServerAggregator.java
@@ -17,21 +17,22 @@
*/
package org.apache.phoenix.expression.aggregator;
-import org.apache.phoenix.expression.Expression;
-import org.apache.phoenix.schema.types.PDataType;
-import org.apache.phoenix.schema.types.PVarbinary;
-import org.apache.phoenix.schema.tuple.Tuple;
-import org.apache.phoenix.util.ByteUtil;
-import org.apache.phoenix.util.SizedUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
+
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.schema.SortOrder;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.apache.phoenix.util.ByteUtil;
import org.apache.phoenix.util.FirstLastNthValueDataContainer;
+import org.apache.phoenix.util.SizedUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValueFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValueFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValueFunction.java
index bed3e7a..420deba 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValueFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValueFunction.java
@@ -66,7 +66,7 @@ public class FirstValueFunction extends FirstLastValueBaseFunction {
@Override
public Aggregator newClientAggregator() {
FirstLastValueBaseClientAggregator aggregator = new FirstLastValueBaseClientAggregator();
- aggregator.init(0);
+ aggregator.init(0, false);
return aggregator;
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValuesFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValuesFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValuesFunction.java
new file mode 100644
index 0000000..fec2eaf
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/FirstValuesFunction.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.FirstValuesAggregateParseNode;
+import org.apache.phoenix.parse.FunctionParseNode;
+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 = FirstValuesFunction.NAME, nodeClass = FirstValuesAggregateParseNode.class, args = {
+ @FunctionParseNode.Argument(),
+ @FunctionParseNode.Argument(allowedTypes = { PBoolean.class }, isConstant = true),
+ @FunctionParseNode.Argument(),
+ @FunctionParseNode.Argument(allowedTypes = { PInteger.class }, isConstant = true)})
+public class FirstValuesFunction extends FirstLastValueBaseFunction {
+ public static final String NAME = "FIRST_VALUES";
+ private int offset;
+
+ public FirstValuesFunction() {
+ }
+
+ public FirstValuesFunction(List<Expression> childExpressions) {
+ this(childExpressions, null);
+ }
+
+ public FirstValuesFunction(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/9095b3ec/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValueFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValueFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValueFunction.java
index fdd6898..f5b3193 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValueFunction.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/LastValueFunction.java
@@ -68,7 +68,7 @@ public class LastValueFunction extends FirstLastValueBaseFunction {
public Aggregator newClientAggregator() {
FirstLastValueBaseClientAggregator aggregator = new FirstLastValueBaseClientAggregator();
- aggregator.init(0);
+ aggregator.init(0, false);
return aggregator;
}
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/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/9095b3ec/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/9095b3ec/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));
+ }
+}
[2/3] phoenix git commit: PHOENIX-3773 Support FIRST_VALUES and
LAST_VALUES aggregate function to retrieve n values (Loknath Priyatham Teja
Singamsetty)
Posted by ja...@apache.org.
http://git-wip-us.apache.org/repos/asf/phoenix/blob/9095b3ec/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/9095b3ec/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