PHOENIX-1875 implement ARRAY_PREPEND built in function (Dumindu)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/1e955650 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/1e955650 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/1e955650 Branch: refs/heads/4.x-HBase-0.98 Commit: 1e955650fc3578e3a8bdd50790fe5a9ba48dfdc3 Parents: e1b7a55 Author: ramkrishna <ramkrishna.s.vasude...@gmail.com> Authored: Wed May 13 10:50:06 2015 +0530 Committer: ramkrishna <ramkrishna.s.vasude...@gmail.com> Committed: Wed May 13 10:50:06 2015 +0530 ---------------------------------------------------------------------- .../phoenix/end2end/ArrayPrependFunctionIT.java | 652 +++++++++++++++++++ .../phoenix/expression/ExpressionType.java | 4 +- .../phoenix/expression/ExpressionType.java.orig | 282 ++++++++ .../function/ArrayAppendFunction.java | 35 +- .../function/ArrayModifierFunction.java | 75 +++ .../function/ArrayPrependFunction.java | 96 +++ .../phoenix/schema/types/PArrayDataType.java | 161 ++++- .../expression/ArrayPrependFunctionTest.java | 552 ++++++++++++++++ 8 files changed, 1823 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayPrependFunctionIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayPrependFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayPrependFunctionIT.java new file mode 100644 index 0000000..3145d95 --- /dev/null +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayPrependFunctionIT.java @@ -0,0 +1,652 @@ +/* + * 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.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.sql.*; + +import org.apache.phoenix.schema.TypeMismatchException; +import org.apache.phoenix.schema.types.PhoenixArray; +import org.junit.Test; + +public class ArrayPrependFunctionIT extends BaseHBaseManagedTimeIT { + + private void initTableWithVarArray(Connection conn, String type, Object[] objectArray, String value) throws SQLException { + conn.createStatement().execute("CREATE TABLE t ( k VARCHAR PRIMARY KEY, a " + type + "[],b " + type + ")"); + conn.commit(); + PreparedStatement stmt = conn.prepareStatement("UPSERT INTO t VALUES(?,?," + value + ")"); + PhoenixArray array = (PhoenixArray) conn.createArrayOf(type, objectArray); + stmt.setString(1, "a"); + stmt.setArray(2, array); + stmt.execute(); + conn.commit(); + + } + + private void initTables(Connection conn) throws Exception { + String ddl = "CREATE TABLE regions (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)[])"; + conn.createStatement().execute(ddl); + String dml = "UPSERT INTO regions(region_name,varchars,integers,doubles,bigints,chars,double1,char1,nullcheck,chars2) VALUES('SF Bay Area'," + + "ARRAY['2345','46345','23234']," + + "ARRAY[2345,46345,23234,456]," + + "ARRAY[23.45,46.345,23.234,45.6,5.78]," + + "ARRAY[12,34,56,78,910]," + + "ARRAY['a','bbbb','c','ddd','e']," + + "23.45," + + "'wert'," + + "NULL," + + "ARRAY['foo','a','bbbb','c','ddd','e']" + + ")"; + PreparedStatement stmt = conn.prepareStatement(dml); + stmt.execute(); + conn.commit(); + } + + private void initTablesDesc(Connection conn, String type, String val) throws Exception { + String ddl = "CREATE TABLE regions (pk " + type + " PRIMARY KEY DESC,varchars VARCHAR[],integers INTEGER[],doubles DOUBLE[],bigints BIGINT[],chars CHAR(15)[],chars2 CHAR(15)[], bools BOOLEAN[])"; + conn.createStatement().execute(ddl); + String dml = "UPSERT INTO regions(pk,varchars,integers,doubles,bigints,chars,chars2,bools) VALUES(" + val + "," + + "ARRAY['2345','46345','23234']," + + "ARRAY[2345,46345,23234,456]," + + "ARRAY[23.45,46.345,23.234,45.6,5.78]," + + "ARRAY[12,34,56,78,910]," + + "ARRAY['a','bbbb','c','ddd','e']," + + "ARRAY['a','bbbb','c','ddd','e','foo']," + + "ARRAY[true,false]" + + ")"; + PreparedStatement stmt = conn.prepareStatement(dml); + stmt.execute(); + conn.commit(); + } + + @Test + public void testArrayPrependFunctionInteger() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(1234,integers) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Integer[] integers = new Integer[]{1234, 2345, 46345, 23234, 456}; + + Array array = conn.createArrayOf("INTEGER", integers); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionVarchar() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND('34567',varchars) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + String[] strings = new String[]{"34567", "2345", "46345", "23234"}; + + Array array = conn.createArrayOf("VARCHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionNulls1() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String[] s = new String[]{null, null, "1", "2"}; + initTableWithVarArray(conn, "VARCHAR", s, null); + String[] s2 = new String[]{null, null, null, "1", "2"}; + PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2); + conn = DriverManager.getConnection(getUrl()); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM t WHERE k = 'a'"); + assertTrue(rs.next()); + assertEquals(array2, rs.getArray(1)); + } + + @Test + public void testArrayPrependFunctionNulls2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String[] s = new String[]{"1", "2"}; + initTableWithVarArray(conn, "VARCHAR", s, null); + String[] s2 = new String[]{null, "1", "2"}; + PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2); + conn = DriverManager.getConnection(getUrl()); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM t WHERE k = 'a'"); + assertTrue(rs.next()); + assertEquals(array2, rs.getArray(1)); + } + + @Test + public void testArrayPrependFunctionNulls3() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String[] s = new String[]{"176", null, "212"}; + initTableWithVarArray(conn, "VARCHAR", s, null); + String[] s2 = new String[]{null, "176", null, "212"}; + PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2); + conn = DriverManager.getConnection(getUrl()); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM t WHERE k = 'a'"); + assertTrue(rs.next()); + assertEquals(array2, rs.getArray(1)); + } + + @Test + public void testArrayPrependFunctionNulls4() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String[] s = new String[]{"176", null, "212"}; + initTableWithVarArray(conn, "VARCHAR", s, "'foo'"); + String[] s2 = new String[]{"foo", "176", null, "212"}; + PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2); + conn = DriverManager.getConnection(getUrl()); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM t WHERE k = 'a'"); + assertTrue(rs.next()); + assertEquals(array2, rs.getArray(1)); + } + + @Test + public void testArrayPrependFunctionDouble() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(double1,doubles) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{23.45, 23.45, 46.345, 23.234, 45.6, 5.78}; + + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionDouble2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(23,doubles) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{new Double(23), 23.45, 46.345, 23.234, 45.6, 5.78}; + + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionBigint() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(1112,bigints) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Long[] longs = new Long[]{1112l, 12l, 34l, 56l, 78l, 910l}; + + Array array = conn.createArrayOf("BIGINT", longs); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionChar() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND('fac',chars) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + String[] strings = new String[]{"fac", "a", "bbbb", "c", "ddd", "e"}; + + Array array = conn.createArrayOf("CHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test(expected = TypeMismatchException.class) + public void testArrayPrependFunctionIntToCharArray() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(234,varchars) FROM regions WHERE region_name = 'SF Bay Area'"); + } + + @Test(expected = TypeMismatchException.class) + public void testArrayPrependFunctionVarcharToIntegerArray() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND('234',integers) FROM regions WHERE region_name = 'SF Bay Area'"); + + } + + @Test(expected = SQLException.class) + public void testArrayPrependFunctionChar2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND('facfacfacfacfacfacfac',chars) FROM regions WHERE region_name = 'SF Bay Area'"); + rs.next(); + rs.getArray(1); + } + + @Test + public void testArrayPrependFunctionIntegerToDoubleArray() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(45,doubles) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{45.0, 23.45, 46.345, 23.234, 45.6, 5.78}; + + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithNestedFunctions1() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(integers[1],ARRAY[23,45]) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Integer[] integers = new Integer[]{2345, 23, 45}; + + Array array = conn.createArrayOf("INTEGER", integers); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithNestedFunctions2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(ARRAY_ELEM(ARRAY[2,4],1),integers) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Integer[] integers = new Integer[]{2, 2345, 46345, 23234, 456}; + + Array array = conn.createArrayOf("INTEGER", integers); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithNestedFunctions3() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(ARRAY_ELEM(doubles,2),doubles) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{46.345, 23.45, 46.345, 23.234, 45.6, 5.78}; + + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithUpsert1() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + + String ddl = "CREATE TABLE regions (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[])"; + conn.createStatement().execute(ddl); + + String dml = "UPSERT INTO regions(region_name,varchars) VALUES('SF Bay Area',ARRAY_PREPEND(':-)',ARRAY['hello','world']))"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT varchars FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + String[] strings = new String[]{":-)", "hello", "world"}; + + Array array = conn.createArrayOf("VARCHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithUpsert2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + + String ddl = "CREATE TABLE regions (region_name VARCHAR PRIMARY KEY,integers INTEGER[])"; + conn.createStatement().execute(ddl); + + String dml = "UPSERT INTO regions(region_name,integers) VALUES('SF Bay Area',ARRAY_PREPEND(6,ARRAY[4,5]))"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT integers FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Integer[] integers = new Integer[]{6, 4, 5}; + + Array array = conn.createArrayOf("INTEGER", integers); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithUpsert3() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + + String ddl = "CREATE TABLE regions (region_name VARCHAR PRIMARY KEY,doubles DOUBLE[])"; + conn.createStatement().execute(ddl); + + String dml = "UPSERT INTO regions(region_name,doubles) VALUES('SF Bay Area',ARRAY_PREPEND(9.0,ARRAY[5.67,7.87]))"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT doubles FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{new Double(9), 5.67, 7.87}; + + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithUpsertSelect1() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + + String ddl = "CREATE TABLE source (region_name VARCHAR PRIMARY KEY,doubles DOUBLE[])"; + conn.createStatement().execute(ddl); + + ddl = "CREATE TABLE target (region_name VARCHAR PRIMARY KEY,doubles DOUBLE[])"; + conn.createStatement().execute(ddl); + + String dml = "UPSERT INTO source(region_name,doubles) VALUES('SF Bay Area',ARRAY_PREPEND(9.0,ARRAY[5.67,7.87]))"; + conn.createStatement().execute(dml); + + dml = "UPSERT INTO source(region_name,doubles) VALUES('SF Bay Area2',ARRAY_PREPEND(9.2,ARRAY[56.7,7.87]))"; + conn.createStatement().execute(dml); + conn.commit(); + + dml = "UPSERT INTO target(region_name, doubles) SELECT region_name, ARRAY_PREPEND(5,doubles) FROM source"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT doubles FROM target"); + assertTrue(rs.next()); + + Double[] doubles = new Double[]{new Double(5), new Double(9), 5.67, 7.87}; + Array array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertTrue(rs.next()); + + doubles = new Double[]{new Double(5), new Double(9.2), 56.7, 7.87}; + array = conn.createArrayOf("DOUBLE", doubles); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionWithUpsertSelect2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + + String ddl = "CREATE TABLE source (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[])"; + conn.createStatement().execute(ddl); + + ddl = "CREATE TABLE target (region_name VARCHAR PRIMARY KEY,varchars VARCHAR[])"; + conn.createStatement().execute(ddl); + + String dml = "UPSERT INTO source(region_name,varchars) VALUES('SF Bay Area',ARRAY_PREPEND('c',ARRAY['abcd','b']))"; + conn.createStatement().execute(dml); + + dml = "UPSERT INTO source(region_name,varchars) VALUES('SF Bay Area2',ARRAY_PREPEND('something',ARRAY['d','fgh']))"; + conn.createStatement().execute(dml); + conn.commit(); + + dml = "UPSERT INTO target(region_name, varchars) SELECT region_name, ARRAY_PREPEND('stu',varchars) FROM source"; + conn.createStatement().execute(dml); + conn.commit(); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT varchars FROM target"); + assertTrue(rs.next()); + + String[] strings = new String[]{"stu", "c", "abcd", "b"}; + Array array = conn.createArrayOf("VARCHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertTrue(rs.next()); + + strings = new String[]{"stu", "something", "d", "fgh"}; + array = conn.createArrayOf("VARCHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere1() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE ARRAY[123,2345,46345,23234,456]=ARRAY_PREPEND(123,integers)"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere2() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE varchars[1]=ANY(ARRAY_PREPEND('1234',ARRAY['2345','46345','23234']))"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere3() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE ARRAY['1234','2345','46345','23234']=ARRAY_PREPEND('1234',ARRAY['2345','46345','23234'])"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere4() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE ARRAY[123.4,23.45,4634.5,2.3234]=ARRAY_PREPEND(123.4,ARRAY[23.45,4634.5,2.3234])"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere5() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE ARRAY['foo','2345','46345','23234']=ARRAY_PREPEND('foo',varchars)"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere6() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE chars2=ARRAY_PREPEND('foo',chars)"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionInWhere7() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT region_name FROM regions WHERE ARRAY[4,2,3]=ARRAY_PREPEND(4,ARRAY[2,3])"); + assertTrue(rs.next()); + + assertEquals("SF Bay Area", rs.getString(1)); + assertFalse(rs.next()); + } + + @Test(expected = SQLException.class) + public void testArrayPrependFunctionCharLimitCheck() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTables(conn); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(char1,chars) FROM regions WHERE region_name = 'SF Bay Area'"); + assertTrue(rs.next()); + + String[] strings = new String[]{"wert", "a", "bbbb", "c", "ddd", "e"}; + + Array array = conn.createArrayOf("CHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionIntegerDesc() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTablesDesc(conn, "INTEGER", "23"); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(pk,integers) FROM regions"); + assertTrue(rs.next()); + + Integer[] integers = new Integer[]{23, 2345, 46345, 23234, 456}; + + Array array = conn.createArrayOf("INTEGER", integers); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + + } + + @Test + public void testArrayPrependFunctionVarcharDesc() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTablesDesc(conn, "VARCHAR", "'e'"); + + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(pk,varchars) FROM regions"); + assertTrue(rs.next()); + + String[] strings = new String[]{"e", "2345", "46345", "23234"}; + + Array array = conn.createArrayOf("VARCHAR", strings); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionBigIntDesc() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTablesDesc(conn, "BIGINT", "1112"); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(pk,bigints) FROM regions"); + assertTrue(rs.next()); + + Long[] longs = new Long[]{1112l, 12l, 34l, 56l, 78l, 910l}; + + Array array = conn.createArrayOf("BIGINT", longs); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } + + @Test + public void testArrayPrependFunctionBooleanDesc() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + initTablesDesc(conn, "BOOLEAN", "false"); + ResultSet rs; + rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(pk,bools) FROM regions"); + assertTrue(rs.next()); + + Boolean[] booleans = new Boolean[]{false, true, false}; + + Array array = conn.createArrayOf("BOOLEAN", booleans); + + assertEquals(array, rs.getArray(1)); + assertFalse(rs.next()); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/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 c054f6a..a490a6b 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 @@ -25,6 +25,7 @@ import org.apache.phoenix.expression.function.ArrayAppendFunction; import org.apache.phoenix.expression.function.ArrayElemRefExpression; import org.apache.phoenix.expression.function.ArrayIndexFunction; import org.apache.phoenix.expression.function.ArrayLengthFunction; +import org.apache.phoenix.expression.function.ArrayPrependFunction; import org.apache.phoenix.expression.function.ByteBasedRegexpReplaceFunction; import org.apache.phoenix.expression.function.ByteBasedRegexpSplitFunction; import org.apache.phoenix.expression.function.ByteBasedRegexpSubstrFunction; @@ -228,7 +229,8 @@ public enum ExpressionType { MinuteFunction(MinuteFunction.class), DayOfMonthFunction(DayOfMonthFunction.class), ArrayAppendFunction(ArrayAppendFunction.class), - UDFExpression(UDFExpression.class) + UDFExpression(UDFExpression.class), + ArrayPrependFunction(ArrayPrependFunction.class) ; ExpressionType(Class<? extends Expression> clazz) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java.orig ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java.orig b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java.orig new file mode 100644 index 0000000..c054f6a --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java.orig @@ -0,0 +1,282 @@ +/* + * 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; + +import java.util.Map; + +import org.apache.phoenix.expression.function.ArrayAllComparisonExpression; +import org.apache.phoenix.expression.function.ArrayAnyComparisonExpression; +import org.apache.phoenix.expression.function.ArrayAppendFunction; +import org.apache.phoenix.expression.function.ArrayElemRefExpression; +import org.apache.phoenix.expression.function.ArrayIndexFunction; +import org.apache.phoenix.expression.function.ArrayLengthFunction; +import org.apache.phoenix.expression.function.ByteBasedRegexpReplaceFunction; +import org.apache.phoenix.expression.function.ByteBasedRegexpSplitFunction; +import org.apache.phoenix.expression.function.ByteBasedRegexpSubstrFunction; +import org.apache.phoenix.expression.function.CeilDateExpression; +import org.apache.phoenix.expression.function.CeilDecimalExpression; +import org.apache.phoenix.expression.function.CeilFunction; +import org.apache.phoenix.expression.function.CeilTimestampExpression; +import org.apache.phoenix.expression.function.CoalesceFunction; +import org.apache.phoenix.expression.function.ConvertTimezoneFunction; +import org.apache.phoenix.expression.function.CountAggregateFunction; +import org.apache.phoenix.expression.function.DayOfMonthFunction; +import org.apache.phoenix.expression.function.DecodeFunction; +import org.apache.phoenix.expression.function.DistinctCountAggregateFunction; +import org.apache.phoenix.expression.function.DistinctValueAggregateFunction; +import org.apache.phoenix.expression.function.EncodeFunction; +import org.apache.phoenix.expression.function.ExternalSqlTypeIdFunction; +import org.apache.phoenix.expression.function.FirstValueFunction; +import org.apache.phoenix.expression.function.FloorDateExpression; +import org.apache.phoenix.expression.function.FloorDecimalExpression; +import org.apache.phoenix.expression.function.FloorFunction; +import org.apache.phoenix.expression.function.HourFunction; +import org.apache.phoenix.expression.function.IndexStateNameFunction; +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.LengthFunction; +import org.apache.phoenix.expression.function.LowerFunction; +import org.apache.phoenix.expression.function.LpadFunction; +import org.apache.phoenix.expression.function.MD5Function; +import org.apache.phoenix.expression.function.MaxAggregateFunction; +import org.apache.phoenix.expression.function.MinAggregateFunction; +import org.apache.phoenix.expression.function.MinuteFunction; +import org.apache.phoenix.expression.function.MonthFunction; +import org.apache.phoenix.expression.function.NowFunction; +import org.apache.phoenix.expression.function.NthValueFunction; +import org.apache.phoenix.expression.function.PercentRankAggregateFunction; +import org.apache.phoenix.expression.function.PercentileContAggregateFunction; +import org.apache.phoenix.expression.function.PercentileDiscAggregateFunction; +import org.apache.phoenix.expression.function.RTrimFunction; +import org.apache.phoenix.expression.function.RandomFunction; +import org.apache.phoenix.expression.function.RegexpReplaceFunction; +import org.apache.phoenix.expression.function.RegexpSplitFunction; +import org.apache.phoenix.expression.function.RegexpSubstrFunction; +import org.apache.phoenix.expression.function.ReverseFunction; +import org.apache.phoenix.expression.function.RoundDateExpression; +import org.apache.phoenix.expression.function.RoundDecimalExpression; +import org.apache.phoenix.expression.function.RoundFunction; +import org.apache.phoenix.expression.function.RoundTimestampExpression; +import org.apache.phoenix.expression.function.SQLIndexTypeFunction; +import org.apache.phoenix.expression.function.SQLTableTypeFunction; +import org.apache.phoenix.expression.function.SQLViewTypeFunction; +import org.apache.phoenix.expression.function.SecondFunction; +import org.apache.phoenix.expression.function.SignFunction; +import org.apache.phoenix.expression.function.SqlTypeNameFunction; +import org.apache.phoenix.expression.function.StddevPopFunction; +import org.apache.phoenix.expression.function.StddevSampFunction; +import org.apache.phoenix.expression.function.StringBasedRegexpReplaceFunction; +import org.apache.phoenix.expression.function.StringBasedRegexpSplitFunction; +import org.apache.phoenix.expression.function.StringBasedRegexpSubstrFunction; +import org.apache.phoenix.expression.function.SubstrFunction; +import org.apache.phoenix.expression.function.SumAggregateFunction; +import org.apache.phoenix.expression.function.TimezoneOffsetFunction; +import org.apache.phoenix.expression.function.ToCharFunction; +import org.apache.phoenix.expression.function.ToDateFunction; +import org.apache.phoenix.expression.function.ToNumberFunction; +import org.apache.phoenix.expression.function.ToTimeFunction; +import org.apache.phoenix.expression.function.ToTimestampFunction; +import org.apache.phoenix.expression.function.TrimFunction; +import org.apache.phoenix.expression.function.TruncFunction; +import org.apache.phoenix.expression.function.UDFExpression; +import org.apache.phoenix.expression.function.UpperFunction; +import org.apache.phoenix.expression.function.WeekFunction; +import org.apache.phoenix.expression.function.YearFunction; + +import com.google.common.collect.Maps; + +/** + * + * Enumeration of all Expression types that will be looked up. They may be evaluated on the server-side. + * Used during serialization and deserialization to pass Expression between client + * and server. + * + * + * @since 0.1 + */ +//Important : When you want to add new Types make sure to add those towards the end, not changing the existing type's +//ordinal +public enum ExpressionType { + ReverseFunction(ReverseFunction.class), + RowKey(RowKeyColumnExpression.class), + KeyValue(KeyValueColumnExpression.class), + LiteralValue(LiteralExpression.class), + RoundFunction(RoundFunction.class), + FloorFunction(FloorFunction.class), + CeilFunction(CeilFunction.class), + RoundDateExpression(RoundDateExpression.class), + FloorDateExpression(FloorDateExpression.class), + CeilDateExpression(CeilDateExpression.class), + RoundTimestampExpression(RoundTimestampExpression.class), + CeilTimestampExpression(CeilTimestampExpression.class), + RoundDecimalExpression(RoundDecimalExpression.class), + FloorDecimalExpression(FloorDecimalExpression.class), + CeilDecimalExpression(CeilDecimalExpression.class), + TruncFunction(TruncFunction.class), + ToDateFunction(ToDateFunction.class), + ToCharFunction(ToCharFunction.class), + ToNumberFunction(ToNumberFunction.class), + CoerceFunction(CoerceExpression.class), + SubstrFunction(SubstrFunction.class), + AndExpression(AndExpression.class), + OrExpression(OrExpression.class), + ComparisonExpression(ComparisonExpression.class), + CountAggregateFunction(CountAggregateFunction.class), + SumAggregateFunction(SumAggregateFunction.class), + MinAggregateFunction(MinAggregateFunction.class), + MaxAggregateFunction(MaxAggregateFunction.class), + StringBasedLikeExpression(StringBasedLikeExpression.class), + NotExpression(NotExpression.class), + CaseExpression(CaseExpression.class), + InListExpression(InListExpression.class), + IsNullExpression(IsNullExpression.class), + LongSubtractExpression(LongSubtractExpression.class), + DateSubtractExpression(DateSubtractExpression.class), + DecimalSubtractExpression(DecimalSubtractExpression.class), + LongAddExpression(LongAddExpression.class), + DecimalAddExpression(DecimalAddExpression.class), + DateAddExpression(DateAddExpression.class), + LongMultiplyExpression(LongMultiplyExpression.class), + DecimalMultiplyExpression(DecimalMultiplyExpression.class), + LongDivideExpression(LongDivideExpression.class), + DecimalDivideExpression(DecimalDivideExpression.class), + CoalesceFunction(CoalesceFunction.class), + StringBasedRegexpReplaceFunction(StringBasedRegexpReplaceFunction.class), + SQLTypeNameFunction(SqlTypeNameFunction.class), + StringBasedRegexpSubstrFunction(StringBasedRegexpSubstrFunction.class), + StringConcatExpression(StringConcatExpression.class), + LengthFunction(LengthFunction.class), + LTrimFunction(LTrimFunction.class), + RTrimFunction(RTrimFunction.class), + UpperFunction(UpperFunction.class), + LowerFunction(LowerFunction.class), + TrimFunction(TrimFunction.class), + DistinctCountAggregateFunction(DistinctCountAggregateFunction.class), + PercentileContAggregateFunction(PercentileContAggregateFunction.class), + PercentRankAggregateFunction(PercentRankAggregateFunction.class), + StddevPopFunction(StddevPopFunction.class), + StddevSampFunction(StddevSampFunction.class), + PercentileDiscAggregateFunction(PercentileDiscAggregateFunction.class), + DoubleAddExpression(DoubleAddExpression.class), + DoubleSubtractExpression(DoubleSubtractExpression.class), + DoubleMultiplyExpression(DoubleMultiplyExpression.class), + DoubleDivideExpression(DoubleDivideExpression.class), + RowValueConstructorExpression(RowValueConstructorExpression.class), + MD5Function(MD5Function.class), + SQLTableTypeFunction(SQLTableTypeFunction.class), + IndexStateName(IndexStateNameFunction.class), + InvertFunction(InvertFunction.class), + ProjectedColumnExpression(ProjectedColumnExpression.class), + TimestampAddExpression(TimestampAddExpression.class), + TimestampSubtractExpression(TimestampSubtractExpression.class), + ArrayIndexFunction(ArrayIndexFunction.class), + ArrayLengthFunction(ArrayLengthFunction.class), + ArrayConstructorExpression(ArrayConstructorExpression.class), + SQLViewTypeFunction(SQLViewTypeFunction.class), + ExternalSqlTypeIdFunction(ExternalSqlTypeIdFunction.class), + ConvertTimezoneFunction(ConvertTimezoneFunction.class), + DecodeFunction(DecodeFunction.class), + TimezoneOffsetFunction(TimezoneOffsetFunction.class), + EncodeFunction(EncodeFunction.class), + LpadFunction(LpadFunction.class), + NthValueFunction(NthValueFunction.class), + FirstValueFunction(FirstValueFunction.class), + LastValueFunction(LastValueFunction.class), + ArrayAnyComparisonExpression(ArrayAnyComparisonExpression.class), + ArrayAllComparisonExpression(ArrayAllComparisonExpression.class), + InlineArrayElemRefExpression(ArrayElemRefExpression.class), + SQLIndexTypeFunction(SQLIndexTypeFunction.class), + ModulusExpression(ModulusExpression.class), + DistinctValueAggregateFunction(DistinctValueAggregateFunction.class), + StringBasedRegexpSplitFunction(StringBasedRegexpSplitFunction.class), + RandomFunction(RandomFunction.class), + ToTimeFunction(ToTimeFunction.class), + ToTimestampFunction(ToTimestampFunction.class), + ByteBasedLikeExpression(ByteBasedLikeExpression.class), + ByteBasedRegexpReplaceFunction(ByteBasedRegexpReplaceFunction.class), + ByteBasedRegexpSubstrFunction(ByteBasedRegexpSubstrFunction.class), + ByteBasedRegexpSplitFunction(ByteBasedRegexpSplitFunction.class), + LikeExpression(LikeExpression.class), + RegexpReplaceFunction(RegexpReplaceFunction.class), + RegexpSubstrFunction(RegexpSubstrFunction.class), + RegexpSplitFunction(RegexpSplitFunction.class), + SignFunction(SignFunction.class), + YearFunction(YearFunction.class), + MonthFunction(MonthFunction.class), + SecondFunction(SecondFunction.class), + WeekFunction(WeekFunction.class), + HourFunction(HourFunction.class), + NowFunction(NowFunction.class), + InstrFunction(InstrFunction.class), + MinuteFunction(MinuteFunction.class), + DayOfMonthFunction(DayOfMonthFunction.class), + ArrayAppendFunction(ArrayAppendFunction.class), + UDFExpression(UDFExpression.class) + ; + + ExpressionType(Class<? extends Expression> clazz) { + this.clazz = clazz; + } + + public Class<? extends Expression> getExpressionClass() { + return clazz; + } + + private final Class<? extends Expression> clazz; + + private static final Map<Class<? extends Expression>,ExpressionType> classToEnumMap = Maps.newHashMapWithExpectedSize(3); + static { + for (ExpressionType type : ExpressionType.values()) { + classToEnumMap.put(type.clazz, type); + } + } + + /** + * Return the ExpressionType for a given Expression instance + */ + public static ExpressionType valueOf(Expression expression) { + ExpressionType type = classToEnumMap.get(expression.getClass()); + if (type == null) { // FIXME: this exception gets swallowed and retries happen + throw new IllegalArgumentException("No ExpressionType for " + expression.getClass()); + } + return type; + } + + /** + * Return the ExpressionType for a given Expression instance + * or null if none exists. + */ + public static ExpressionType valueOfOrNull(Expression expression) { + return classToEnumMap.get(expression.getClass()); + } + + /** + * Instantiates a DataAccessor based on its DataAccessorType + */ + public Expression newInstance() { + try { + return clazz.newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayAppendFunction.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayAppendFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayAppendFunction.java index db92d61..bf6c29f 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayAppendFunction.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayAppendFunction.java @@ -33,7 +33,7 @@ import org.apache.phoenix.schema.tuple.Tuple; @FunctionParseNode.Argument(allowedTypes = {PBinaryArray.class, PVarbinaryArray.class}), @FunctionParseNode.Argument(allowedTypes = {PVarbinary.class}, defaultValue = "null")}) -public class ArrayAppendFunction extends ScalarFunction { +public class ArrayAppendFunction extends ArrayModifierFunction { public static final String NAME = "ARRAY_APPEND"; @@ -42,21 +42,6 @@ public class ArrayAppendFunction extends ScalarFunction { public ArrayAppendFunction(List<Expression> children) throws TypeMismatchException { super(children); - - if (getDataType() != null && !(getElementExpr() instanceof LiteralExpression && getElementExpr().isNullable()) && !getElementDataType().isCoercibleTo(getBaseType())) { - throw TypeMismatchException.newException(getBaseType(), getElementDataType()); - } - - // If the base type of an element is fixed width, make sure the element being appended will fit - if (getDataType() != null && getElementExpr().getDataType().getByteSize() == null && getElementDataType() != null && getBaseType().isFixedWidth() && getElementDataType().isFixedWidth() && getArrayExpr().getMaxLength() != null && - getElementExpr().getMaxLength() != null && getElementExpr().getMaxLength() > getArrayExpr().getMaxLength()) { - throw new DataExceedsCapacityException(""); - } - // If the base type has a scale, make sure the element being appended has a scale less than or equal to it - if (getDataType() != null && getArrayExpr().getScale() != null && getElementExpr().getScale() != null && - getElementExpr().getScale() > getArrayExpr().getScale()) { - throw new DataExceedsCapacityException(getBaseType(), getArrayExpr().getMaxLength(), getArrayExpr().getScale()); - } } @Override @@ -78,12 +63,8 @@ public class ArrayAppendFunction extends ScalarFunction { return true; } - if (!getBaseType().isSizeCompatible(ptr, null, getElementDataType(), getElementExpr().getMaxLength(), getElementExpr().getScale(), getArrayExpr().getMaxLength(), getArrayExpr().getScale())) { - throw new DataExceedsCapacityException(""); - } - - getBaseType().coerceBytes(ptr, null, getElementDataType(), getElementExpr().getMaxLength(), getElementExpr().getScale(), getElementExpr().getSortOrder(), getArrayExpr().getMaxLength(), getArrayExpr().getScale(), getArrayExpr().getSortOrder()); - + checkSizeCompatibility(ptr); + coerceBytes(ptr); return PArrayDataType.appendItemToArray(ptr, length, offset, arrayBytes, getBaseType(), arrayLength, getMaxLength(), getArrayExpr().getSortOrder()); } @@ -114,14 +95,4 @@ public class ArrayAppendFunction extends ScalarFunction { public Expression getElementExpr() { return getChildren().get(1); } - - public PDataType getBaseType() { - return PDataType.arrayBaseType(getArrayExpr().getDataType()); - } - - public PDataType getElementDataType() { - return getElementExpr().getDataType(); - } - - } http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/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 new file mode 100644 index 0000000..afd10e5 --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayModifierFunction.java @@ -0,0 +1,75 @@ +/* + * 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.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.exception.DataExceedsCapacityException; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.expression.LiteralExpression; +import org.apache.phoenix.schema.TypeMismatchException; +import org.apache.phoenix.schema.types.*; + +public abstract class ArrayModifierFunction extends ScalarFunction { + + public ArrayModifierFunction() { + } + + public ArrayModifierFunction(List<Expression> children) throws TypeMismatchException { + super(children); + + if (getDataType() != null && !(getElementExpr() instanceof LiteralExpression && getElementExpr().isNullable()) && !getElementDataType().isCoercibleTo(getBaseType())) { + throw TypeMismatchException.newException(getBaseType(), getElementDataType()); + } + + // If the base type of an element is fixed width, make sure the element being appended will fit + if (getDataType() != null && getElementExpr().getDataType().getByteSize() == null && getElementDataType() != null && getBaseType().isFixedWidth() && getElementDataType().isFixedWidth() && getArrayExpr().getMaxLength() != null && + getElementExpr().getMaxLength() != null && getElementExpr().getMaxLength() > getArrayExpr().getMaxLength()) { + throw new DataExceedsCapacityException(""); + } + // If the base type has a scale, make sure the element being appended has a scale less than or equal to it + if (getDataType() != null && getArrayExpr().getScale() != null && getElementExpr().getScale() != null && + getElementExpr().getScale() > getArrayExpr().getScale()) { + throw new DataExceedsCapacityException(getBaseType(), getArrayExpr().getMaxLength(), getArrayExpr().getScale()); + } + } + + protected void checkSizeCompatibility(ImmutableBytesWritable ptr) { + if (!getBaseType().isSizeCompatible(ptr, null, getElementDataType(), getElementExpr().getMaxLength(), getElementExpr().getScale(), getArrayExpr().getMaxLength(), getArrayExpr().getScale())) { + throw new DataExceedsCapacityException(""); + } + } + + protected void coerceBytes(ImmutableBytesWritable ptr) { + getBaseType().coerceBytes(ptr, null, getElementDataType(), getElementExpr().getMaxLength(), getElementExpr().getScale(), getElementExpr().getSortOrder(), getArrayExpr().getMaxLength(), getArrayExpr().getScale(), getArrayExpr().getSortOrder()); + } + + public abstract Expression getArrayExpr(); + + public abstract Expression getElementExpr(); + + public PDataType getBaseType() { + return PDataType.arrayBaseType(getArrayExpr().getDataType()); + } + + public PDataType getElementDataType() { + return getElementExpr().getDataType(); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayPrependFunction.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayPrependFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayPrependFunction.java new file mode 100644 index 0000000..3cea4df --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayPrependFunction.java @@ -0,0 +1,96 @@ +/* + * 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.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.FunctionParseNode; +import org.apache.phoenix.schema.SortOrder; +import org.apache.phoenix.schema.TypeMismatchException; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.*; + +@FunctionParseNode.BuiltInFunction(name = ArrayPrependFunction.NAME, args = { + @FunctionParseNode.Argument(allowedTypes = {PVarbinary.class}), + @FunctionParseNode.Argument(allowedTypes = {PBinaryArray.class, + PVarbinaryArray.class})}) +public class ArrayPrependFunction extends ArrayModifierFunction { + + public static final String NAME = "ARRAY_PREPEND"; + + public ArrayPrependFunction() { + } + + public ArrayPrependFunction(List<Expression> children) throws TypeMismatchException { + super(children); + } + + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + + if (!getArrayExpr().evaluate(tuple, ptr)) { + return false; + } else if (ptr.getLength() == 0) { + return true; + } + int arrayLength = PArrayDataType.getArrayLength(ptr, getBaseType(), getArrayExpr().getMaxLength()); + + int length = ptr.getLength(); + int offset = ptr.getOffset(); + byte[] arrayBytes = ptr.get(); + + getElementExpr().evaluate(tuple, ptr); + + checkSizeCompatibility(ptr); + coerceBytes(ptr); + return PArrayDataType.prependItemToArray(ptr, length, offset, arrayBytes, getBaseType(), arrayLength, getMaxLength(), getArrayExpr().getSortOrder()); + } + + @Override + public PDataType getDataType() { + return children.get(1).getDataType(); + } + + @Override + public Integer getMaxLength() { + return this.children.get(1).getMaxLength(); + } + + @Override + public SortOrder getSortOrder() { + return getChildren().get(1).getSortOrder(); + } + + @Override + public String getName() { + return NAME; + } + + @Override + public Expression getArrayExpr() { + return getChildren().get(1); + } + + @Override + public Expression getElementExpr() { + return getChildren().get(0); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e955650/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 c6861f7..86f22f7 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 @@ -21,6 +21,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.text.Format; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -512,7 +513,7 @@ public abstract class PArrayDataType<T> extends PDataType<T> { } else { int off = newOffsetArrayPosition; for (int arrayIndex = 0; arrayIndex < Math.abs(arrayLength) - 1; arrayIndex++) { - Bytes.putInt(newArray, off, getOffset(arrayBytes, arrayIndex, true, offsetArrayPosition)); + Bytes.putInt(newArray, off, getOffset(arrayBytes, arrayIndex, true, offsetArrayPosition + offset)); off += Bytes.SIZEOF_INT; } @@ -543,6 +544,164 @@ public abstract class PArrayDataType<T> extends PDataType<T> { Bytes.putByte(array, newOffsetArrayPosition + offsetArrayLength + byteSize + 2 * Bytes.SIZEOF_INT, header); } + public static boolean prependItemToArray(ImmutableBytesWritable ptr, int length, int offset, byte[] arrayBytes, PDataType baseType, int arrayLength, Integer maxLength, SortOrder sortOrder) { + int elementLength = maxLength == null ? ptr.getLength() : maxLength; + if (ptr.getLength() == 0) { + elementLength = 0; + } + //padding + if (elementLength > ptr.getLength()) { + baseType.pad(ptr, elementLength, sortOrder); + } + int elementOffset = ptr.getOffset(); + byte[] elementBytes = ptr.get(); + + byte[] newArray; + if (!baseType.isFixedWidth()) { + int offsetArrayPosition = Bytes.toInt(arrayBytes, offset + length - Bytes.SIZEOF_INT - Bytes.SIZEOF_INT - Bytes.SIZEOF_BYTE, Bytes.SIZEOF_INT); + int offsetArrayLength = length - offsetArrayPosition - Bytes.SIZEOF_INT - Bytes.SIZEOF_INT - Bytes.SIZEOF_BYTE; + arrayLength = Math.abs(arrayLength); + + //checks whether offset array consists of shorts or integers + boolean useInt = offsetArrayLength / arrayLength == Bytes.SIZEOF_INT; + boolean convertToInt = false; + int endElementPosition = getOffset(arrayBytes, arrayLength - 1, !useInt, offsetArrayPosition + offset) + elementLength + Bytes.SIZEOF_BYTE; + int newOffsetArrayPosition; + int lengthIncrease; + int firstNonNullElementPosition = 0; + int currentPosition = 0; + //handle the case where prepended element is null + if (elementLength == 0) { + int nulls = 1; + //counts the number of nulls which are already at the beginning of the array + for (int index = 0; index < arrayLength; index++) { + int currOffset = getOffset(arrayBytes, index, !useInt, offsetArrayPosition + offset); + if (arrayBytes[offset + currOffset] == QueryConstants.SEPARATOR_BYTE) { + nulls++; + } else { + //gets the offset of the first element after nulls at the beginning + firstNonNullElementPosition = currOffset; + break; + } + } + + int nMultiplesOver255 = nulls / 255; + int nRemainingNulls = nulls % 255; + + //Calculates the increase in length due to prepending the null + //There is a length increase only when nRemainingNulls == 1 + //nRemainingNulls == 1 and nMultiplesOver255 == 0 means there were no nulls at the beginning previously. + //At that case we need to increase the length by two bytes, one for separator byte and one for null count. + //ex: initial array - 65 0 66 0 0 0 after prepending null - 0 1(inverted) 65 0 66 0 0 0 + //nRemainingNulls == 1 and nMultiplesOver255 != 0 means there were null at the beginning previously. + //In this case due to prepending nMultiplesOver255 is increased by 1. + //We need to increase the length by one byte to store increased that. + //ex: initial array - 0 1 65 0 66 0 0 0 after prepending null - 0 1 1(inverted) 65 0 66 0 0 0 + //nRemainingNulls == 0 case. + //ex: initial array - 0 254(inverted) 65 0 66 0 0 0 after prepending null - 0 1 65 0 66 0 0 0 + //nRemainingNulls > 1 case. + //ex: initial array - 0 45(inverted) 65 0 66 0 0 0 after prepending null - 0 46(inverted) 65 0 66 0 0 0 + lengthIncrease = nRemainingNulls == 1 ? (nMultiplesOver255 == 0 ? 2 * Bytes.SIZEOF_BYTE : Bytes.SIZEOF_BYTE) : 0; + endElementPosition = getOffset(arrayBytes, arrayLength - 1, !useInt, offsetArrayPosition + offset) + lengthIncrease; + if (!useInt) { + if (PArrayDataType.useShortForOffsetArray(endElementPosition)) { + newArray = new byte[length + Bytes.SIZEOF_SHORT + lengthIncrease]; + } else { + newArray = new byte[length + arrayLength * Bytes.SIZEOF_SHORT + Bytes.SIZEOF_INT + lengthIncrease]; + convertToInt = true; + } + } else { + newArray = new byte[length + Bytes.SIZEOF_INT + lengthIncrease]; + } + newArray[currentPosition] = QueryConstants.SEPARATOR_BYTE; + currentPosition++; + + newOffsetArrayPosition = offsetArrayPosition + lengthIncrease; + while (nMultiplesOver255-- > 0) { + newArray[currentPosition] = (byte) 1; + currentPosition++; + } + // Write a byte for the remaining null elements + if (nRemainingNulls > 0) { + byte nNullByte = SortOrder.invert((byte) (nRemainingNulls - 1)); + newArray[currentPosition] = nNullByte; // Single byte for repeating nulls + currentPosition++; + } + } else { + if (!useInt) { + if (PArrayDataType.useShortForOffsetArray(endElementPosition)) { + newArray = new byte[length + elementLength + Bytes.SIZEOF_SHORT + Bytes.SIZEOF_BYTE]; + } else { + newArray = new byte[length + elementLength + arrayLength * Bytes.SIZEOF_SHORT + Bytes.SIZEOF_INT + Bytes.SIZEOF_BYTE]; + convertToInt = true; + } + } else { + newArray = new byte[length + elementLength + Bytes.SIZEOF_INT + Bytes.SIZEOF_BYTE]; + } + newOffsetArrayPosition = offsetArrayPosition + Bytes.SIZEOF_BYTE + elementLength; + + lengthIncrease = elementLength + Bytes.SIZEOF_BYTE; + System.arraycopy(elementBytes, elementOffset, newArray, 0, elementLength); + currentPosition += elementLength + Bytes.SIZEOF_BYTE; + } + + System.arraycopy(arrayBytes, firstNonNullElementPosition + offset, newArray, currentPosition, offsetArrayPosition); + + arrayLength = arrayLength + 1; + //writes the new offset and changes the previous offsets + if (useInt || convertToInt) { + writeNewOffsets(arrayBytes, newArray, false, !useInt, newOffsetArrayPosition, arrayLength, offsetArrayPosition, offset, lengthIncrease, length); + } else { + writeNewOffsets(arrayBytes, newArray, true, true, newOffsetArrayPosition, arrayLength, offsetArrayPosition, offset, lengthIncrease, length); + } + } else { + newArray = new byte[length + elementLength]; + + System.arraycopy(elementBytes, elementOffset, newArray, 0, elementLength); + System.arraycopy(arrayBytes, offset, newArray, elementLength, length); + } + + ptr.set(newArray); + return true; + } + + private static void writeNewOffsets(byte[] arrayBytes, byte[] newArray, boolean useShortNew, boolean useShortPrevious, int newOffsetArrayPosition, int arrayLength, int offsetArrayPosition, int offset, int offsetShift, int length) { + int currentPosition = newOffsetArrayPosition; + int offsetArrayElementSize = useShortNew ? Bytes.SIZEOF_SHORT : Bytes.SIZEOF_INT; + if (useShortNew) { + Bytes.putShort(newArray, currentPosition, (short) (0 - Short.MAX_VALUE)); + } else { + Bytes.putInt(newArray, currentPosition, 0); + } + + currentPosition += offsetArrayElementSize; + boolean nullsAtBeginning = true; + for (int arrayIndex = 0; arrayIndex < arrayLength - 1; arrayIndex++) { + int oldOffset = getOffset(arrayBytes, arrayIndex, useShortPrevious, offsetArrayPosition + offset); + if (arrayBytes[offset + oldOffset] == QueryConstants.SEPARATOR_BYTE && nullsAtBeginning) { + if (useShortNew) { + Bytes.putShort(newArray, currentPosition, (short) (oldOffset - Short.MAX_VALUE)); + } else { + Bytes.putInt(newArray, currentPosition, oldOffset); + } + } else { + if (useShortNew) { + Bytes.putShort(newArray, currentPosition, (short) (oldOffset + offsetShift - Short.MAX_VALUE)); + } else { + Bytes.putInt(newArray, currentPosition, oldOffset + offsetShift); + } + nullsAtBeginning = false; + } + currentPosition += offsetArrayElementSize; + } + + Bytes.putInt(newArray, currentPosition, newOffsetArrayPosition); + currentPosition += Bytes.SIZEOF_INT; + Bytes.putInt(newArray, currentPosition, useShortNew ? arrayLength : -arrayLength); + currentPosition += Bytes.SIZEOF_INT; + Bytes.putByte(newArray, currentPosition, arrayBytes[offset + length - 1]); + } + public static int serailizeOffsetArrayIntoStream(DataOutputStream oStream, TrustedByteArrayOutputStream byteStream, int noOfElements, int maxOffset, int[] offsetPos) throws IOException { int offsetPosition = (byteStream.size());