[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r37821600 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonEachFunctionIT.java --- @@ -0,0 +1,107 @@ +/* + * 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 org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonEachFunction}. + * + */ +public class JsonEachFunctionIT extends BaseHBaseManagedTimeIT { --- End diff -- Rename this to json_each_text (do not implement json_each) --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r37822168 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonPopulateRecordFunctionIT.java --- @@ -0,0 +1,101 @@ +/* + * 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 org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonPopulateRecordFunction}. + * + */ +public class JsonPopulateRecordFunctionIT extends BaseHBaseManagedTimeIT { + +@Test +public void testJsonPopulateRecord() throws Exception { +Connection conn = getConnection(); + +try{ +String json = [{\a\:1,\b\:2}]; +populateJsonTable(conn, json, valueOne); + +String selectQuery = SELECT json_populate_record(ARRAY['a','b'],col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); --- End diff -- This should be implemented so that rs.getInt(a)=1 rs.getInt(b)=2 --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r37821173 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayElementsFunctionIT.java --- @@ -0,0 +1,175 @@ +/* + * 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 org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonArrayElementsFunction}. + * + */ +public class JsonArrayElementsFunctionIT extends BaseHBaseManagedTimeIT { + +@Test +public void testJsonArrayElementsWithSameType() throws Exception { +Connection conn = getConnection(); + +try { +String json = [25.343,36.763,37.56,386.63]; +String pk = valueOne; + +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_elements(col1) FROM testJson WHERE pk = 'valueOne'; --- End diff -- This is ok because Phoenix does not have a SETOF. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r37821754 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonEachFunctionIT.java --- @@ -0,0 +1,107 @@ +/* + * 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 org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonEachFunction}. + * + */ +public class JsonEachFunctionIT extends BaseHBaseManagedTimeIT { + +@Test + public void testJsonEach() throws Exception { +Connection conn = getConnection(); + +try{ +String json = + {\f2\:{\f3\:\value\},\f4\:{\f5\:99,\f6\:[1,true,\foo\]},\f7\:true}; +populateJsonTable(conn, json, valueOne); + +String selectQuery = SELECT json_each(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +String[] strArr = new String[]{ +f2,{\f3\:\value\}, +f4,{\f5\:99,\f6\:[1,true,\foo\]}, +f7,true}; +Array array = conn.createArrayOf(VARCHAR, strArr); +PhoenixArray resultArray = (PhoenixArray) rs.getArray(1); +assertEquals(JsonEach return data is not as expected., resultArray, +array); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + +@Test +public void testJsonEachWithNullKey() throws Exception { +Connection conn = getConnection(); + +try{ +String json = +100; --- End diff -- This should throw an exception because 100 is not valid json. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r37822337 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonPopulateRecordSetFunctionIT.java --- @@ -0,0 +1,139 @@ +/* + * 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 org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonPopulateRecordSetFunction}. + * + */ +public class JsonPopulateRecordSetFunctionIT extends BaseHBaseManagedTimeIT { --- End diff -- See above for record set. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user ictwanglei closed the pull request at: https://github.com/apache/phoenix/pull/93 --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36014571 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayElementsFunctionIT.java --- @@ -0,0 +1,175 @@ +/* + * 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 org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonArrayElementsFunction}. + * + */ +public class JsonArrayElementsFunctionIT extends BaseHBaseManagedTimeIT { + +@Test +public void testJsonArrayElementsWithSameType() throws Exception { +Connection conn = getConnection(); + +try { +String json = [25.343,36.763,37.56,386.63]; +String pk = valueOne; + +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_elements(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +String[] strArr = new String[4]; +strArr[0] = 25.343; +strArr[1] = 36.763; +strArr[2] = 37.56; +strArr[3] = 386.63; +Array array = conn.createArrayOf(VARCHAR, strArr); +PhoenixArray resultArray = (PhoenixArray) rs.getArray(1); +assertEquals(Json array elements is not as expected., resultArray, +array); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} +@Test +public void testJsonArrayElementsWithDifferentDataTypes() throws Exception { +Connection conn = getConnection(); + +try { +String json = [1,36.763,false,\string\]; +String pk = valueOne; + +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_elements(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +String[] strArr = new String[4]; +strArr[0] = 1; +strArr[1] = 36.763; +strArr[2] = false; +strArr[3] = \string\; + +Array array = conn.createArrayOf(VARCHAR, strArr); +PhoenixArray resultArray = (PhoenixArray) rs.getArray(1); + +assertEquals(Json array elements is not as expected., resultArray, +array); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} +@Test +public void testJsonArrayElementsWithNestJson() throws Exception { +Connection conn = getConnection(); + +try { +String json = [1,[1,true,\string\]]; +String pk = valueOne; + +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_elements(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +String[] strArr = new String[2]; +strArr[0] = 1; +strArr[1] = [1,true,\string\]; +Array array = conn.createArrayOf(VARCHAR, strArr);
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36014695 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayLengthFunctionIT.java --- @@ -0,0 +1,177 @@ +/* + * 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.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.expression.function.JsonArrayLengthFunction; +import org.apache.phoenix.schema.IllegalDataException; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link JsonArrayLengthFunction}. + * + */ +public class JsonArrayLengthFunctionIT extends BaseHBaseManagedTimeIT { + + @Test + public void testJsonArrayLengthWithIntTypeInWhereClause() throws Exception { + Connection conn = getConnection(); + String json = [1,2,3]; + String pk = valueOne; + try { + populateJsonTable(conn, json, pk); + + String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 3; + PreparedStatement stmt = conn.prepareStatement(selectQuery); + ResultSet rs = stmt.executeQuery(); + assertTrue(rs.next()); + assertEquals(Json data is not as expected., json, + rs.getString(1)); + assertFalse(rs.next()); + + } finally { + conn.close(); + } + } + +@Test +public void testJsonArrayLengthWithDoubleType() throws Exception { +Connection conn = getConnection(); +String json = [1.23,2.34,3.56,54.3]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_length(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json array length is not as expected., 4, +rs.getInt(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + +@Test +public void testJsonArrayLengthWithDifferentDataTypes() +throws Exception { +Connection conn = getConnection(); +String json = [1,2.3,true,\f1\,[\string\,3]]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_length(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json array length is not as expected., +5, rs.getInt(1)); + +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} +@Test +public void testJsonArrayLengthWithNestedJson() throws Exception { +Connection conn = getConnection(); +String json = [1,\string\,false,[1.23,[true,\ok\]]]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 4; +
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36013361 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayToJsonFunctionIT.java --- @@ -0,0 +1,150 @@ +/* + * 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.apache.phoenix.util.TestUtil.ROW1; +import static org.apache.phoenix.util.TestUtil.TABLE_WITH_ARRAY; +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.sql.*; +import java.util.Properties; +import org.apache.phoenix.expression.function.ArrayToJsonFunction; +import org.apache.phoenix.query.BaseTest; +import org.apache.phoenix.util.PhoenixRuntime; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.ArrayToJsonFunction}. + * + */ +public class ArrayToJsonFunctionIT extends BaseHBaseManagedTimeIT { +private static final String TABLE_WITH_ALL_ARRAY_TYPES = TABLE_WITH_ALL_ARRAY_TYPES; + +@Test +public void testArrayToJsonWithAllArrayTypes() throws Exception { +// create the table +createTableWithAllArrayTypes(getUrl()); + +Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); +Connection conn = DriverManager.getConnection(getUrl(), props); +conn.setAutoCommit(false); + +try{ +// populate the table with data +PreparedStatement stmt = +conn.prepareStatement(UPSERT INTO ++ TABLE_WITH_ALL_ARRAY_TYPES ++ (pk, BOOLEAN_ARRAY, BYTE_ARRAY, DOUBLE_ARRAY, FLOAT_ARRAY, INT_ARRAY, LONG_ARRAY, SHORT_ARRAY, STRING_ARRAY)\n ++ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)); + +stmt.setString(1, valueOne); + +// boolean array +Array boolArray = conn.createArrayOf(BOOLEAN, new Boolean[] { true,false }); +int boolIndex = 2; --- End diff -- Please rename boolIndex as boolColumnIndex and so for other index variables. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36014881 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayLengthFunctionIT.java --- @@ -0,0 +1,177 @@ +/* + * 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.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.expression.function.JsonArrayLengthFunction; +import org.apache.phoenix.schema.IllegalDataException; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link JsonArrayLengthFunction}. + * + */ +public class JsonArrayLengthFunctionIT extends BaseHBaseManagedTimeIT { + + @Test + public void testJsonArrayLengthWithIntTypeInWhereClause() throws Exception { + Connection conn = getConnection(); + String json = [1,2,3]; + String pk = valueOne; + try { + populateJsonTable(conn, json, pk); + + String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 3; + PreparedStatement stmt = conn.prepareStatement(selectQuery); + ResultSet rs = stmt.executeQuery(); + assertTrue(rs.next()); + assertEquals(Json data is not as expected., json, + rs.getString(1)); + assertFalse(rs.next()); + + } finally { + conn.close(); + } + } + +@Test +public void testJsonArrayLengthWithDoubleType() throws Exception { +Connection conn = getConnection(); +String json = [1.23,2.34,3.56,54.3]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_length(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json array length is not as expected., 4, +rs.getInt(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + +@Test +public void testJsonArrayLengthWithDifferentDataTypes() +throws Exception { +Connection conn = getConnection(); +String json = [1,2.3,true,\f1\,[\string\,3]]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_length(col1) FROM testJson WHERE pk = 'valueOne'; +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json array length is not as expected., +5, rs.getInt(1)); + +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} +@Test +public void testJsonArrayLengthWithNestedJson() throws Exception { +Connection conn = getConnection(); +String json = [1,\string\,false,[1.23,[true,\ok\]]]; +String pk = valueOne; +try { +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 4; +
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36013900 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayToJsonFunctionIT.java --- @@ -0,0 +1,150 @@ +/* + * 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.apache.phoenix.util.TestUtil.ROW1; +import static org.apache.phoenix.util.TestUtil.TABLE_WITH_ARRAY; +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.sql.*; +import java.util.Properties; +import org.apache.phoenix.expression.function.ArrayToJsonFunction; +import org.apache.phoenix.query.BaseTest; +import org.apache.phoenix.util.PhoenixRuntime; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.ArrayToJsonFunction}. + * + */ +public class ArrayToJsonFunctionIT extends BaseHBaseManagedTimeIT { +private static final String TABLE_WITH_ALL_ARRAY_TYPES = TABLE_WITH_ALL_ARRAY_TYPES; + +@Test +public void testArrayToJsonWithAllArrayTypes() throws Exception { +// create the table +createTableWithAllArrayTypes(getUrl()); + +Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); +Connection conn = DriverManager.getConnection(getUrl(), props); +conn.setAutoCommit(false); + +try{ +// populate the table with data +PreparedStatement stmt = +conn.prepareStatement(UPSERT INTO ++ TABLE_WITH_ALL_ARRAY_TYPES ++ (pk, BOOLEAN_ARRAY, BYTE_ARRAY, DOUBLE_ARRAY, FLOAT_ARRAY, INT_ARRAY, LONG_ARRAY, SHORT_ARRAY, STRING_ARRAY)\n ++ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)); + +stmt.setString(1, valueOne); + +// boolean array +Array boolArray = conn.createArrayOf(BOOLEAN, new Boolean[] { true,false }); +int boolIndex = 2; +stmt.setArray(boolIndex, boolArray); +// byte array +Array byteArray = conn.createArrayOf(TINYINT, new Byte[] { 11, 22 }); +int byteIndex = 3; +stmt.setArray(byteIndex, byteArray); +// double array +Array doubleArray = conn.createArrayOf(DOUBLE, new Double[] { 67.78, 78.89 }); +int doubleIndex = 4; +stmt.setArray(doubleIndex, doubleArray); +// float array +Array floatArray = conn.createArrayOf(FLOAT, new Float[] { 12.23f, 45.56f }); +int floatIndex = 5; +stmt.setArray(floatIndex, floatArray); +// int array +Array intArray = conn.createArrayOf(INTEGER, new Integer[] { , }); +int intIndex = 6; +stmt.setArray(intIndex, intArray); +// long array +Array longArray = conn.createArrayOf(BIGINT, new Long[] { 777L, 888L }); +int longIndex = 7; +stmt.setArray(longIndex, longArray); +// short array +Array shortArray = conn.createArrayOf(SMALLINT, new Short[] { 333, 444 }); +int shortIndex = 8; +stmt.setArray(shortIndex, shortArray); +// create character array +Array stringArray = conn.createArrayOf(VARCHAR, new String[] { a, b }); +int stringIndex = 9; +stmt.setArray(stringIndex, stringArray); +stmt.execute(); +conn.commit(); + +stmt = +conn.prepareStatement(SELECT pk, + +
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/102#discussion_r36014459 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayElementsFunctionIT.java --- @@ -0,0 +1,175 @@ +/* + * 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 org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.schema.types.PhoenixArray; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.*; + +/** + * End to end test for {@link org.apache.phoenix.expression.function.JsonArrayElementsFunction}. + * + */ +public class JsonArrayElementsFunctionIT extends BaseHBaseManagedTimeIT { + +@Test +public void testJsonArrayElementsWithSameType() throws Exception { +Connection conn = getConnection(); + +try { +String json = [25.343,36.763,37.56,386.63]; +String pk = valueOne; + +populateJsonTable(conn, json, pk); + +String selectQuery = SELECT json_array_elements(col1) FROM testJson WHERE pk = 'valueOne'; --- End diff -- @JamesRTaylor the postgress implementation returns a SETOF json, while this returns a varchar array. Do you think this is OK? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
GitHub user ictwanglei opened a pull request: https://github.com/apache/phoenix/pull/102 PHOENIX-1661 Implement built-in functions for JSON Add some built-in functions for JSON : JsonArrayElementsFunction.java JsonArrayLengthFunction.java JsonEachFunction.java JsonObjectKeysFunction.java JsonPopulateRecordFunction.java JsonPopulateRecordSetFunction.java. ArrayToJsonFunction.java Add end2end test files. Modify file PhoenixJson.java to which I added some helper methods. @JamesRTaylor @twdsilva @AakashPradeep You can merge this pull request into a Git repository by running: $ git pull https://github.com/ictwanglei/phoenix json Alternatively you can review and apply these changes as the patch at: https://github.com/apache/phoenix/pull/102.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #102 commit 26383070e5980697a0de1193c069692a8107b7c2 Author: ictwanglei 597316...@qq.com Date: 2015-03-21T15:32:40Z Create ArrayToJsonFunction.java add a function to support ArrayToJson function as a build-in function for JSON commit 2c63494f99267fbcd0b7e52be76a1dc0154e306a Author: ictwanglei 597316...@qq.com Date: 2015-03-21T15:34:57Z Create ArrayToJsonFunctionTest.java a simple test for testing ArrayToJsonFunction commit 241f4c3186a9f70f057e9196f962a0ed1b2f593b Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:26:56Z Delete ArrayToJsonFunctionTest.java commit f66ba90f92a667405759fceaf1ee74c27ef1d1c8 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:35:37Z Create JsonFunctionTest.java commit 7f2a6cf0c3d56b563a528036844dd5096f98ab6a Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:53:44Z PHOENIX-1661 Implement built-in functions for JSON commit db5c9e1cfbb1f3145a992efef13a8dca7cee0ec7 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:54:53Z Delete ArrayToJsonFunction.java commit a02b954453ff67220c08801e0eafd1c23bdd4b9c Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:55:10Z Rename ArrayToJsonFunction1.java to ArrayToJsonFunction.java commit eb76667b0c61ea646a63c1b46b41f418c6b43239 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:55:33Z Update ArrayToJsonFunction.java commit 6546bc5fb0620b954e31e54e0c71671c54a8f9dc Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:56:50Z PHOENIX-1661 Implement built-in functions for JSON commit 6bb8b116db3dd46434f2020a0ca2f46545b0ac05 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:58:22Z PHOENIX-1661 Implement built-in functions for JSON commit 3782d70ce1c0fedbcea48e3df968564311c06a2a Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:59:25Z PHOENIX-1661 Implement built-in functions for JSON commit 5ff61520d2aa351ea4dab18ec131c74f34b1b463 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:00:08Z PHOENIX-1661 Implement built-in functions for JSON commit d21c22b3697b64b920cc22a33e32d76b2b8eb074 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:01:14Z PHOENIX-1661 Implement built-in functions for JSON commit 541697d72d27a809cb8172efd58d83960b79f362 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:01:50Z PHOENIX-1661 Implement built-in functions for JSON commit 7e0dbaf950b4292bf673bb64367e57085ada25f8 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:03:16Z PHOENIX-1661 Implement built-in functions for JSON commit 331e09b9df48611421830b3c52afc80fd75099d5 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:09:54Z PHOENIX-1661 Implement built-in functions for JSON PHOENIX-1661 Implement built-in functions for JSON commit 9f2c22df19c3a9cacd8a942f9c57baedcbc1f697 Author: ictwanglei ictwang...@gmail.com Date: 2015-06-29T03:28:22Z Merge branch 'master' into json Conflicts: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java commit 1646b8ac56b5bfdbdf637dbdbd47499219ee53b3 Author: ictwanglei ictwang...@gmail.com Date: 2015-06-29T03:35:04Z Merge branch 'json' commit c8eb37c5e2a9df5b35bc92a169deba56f25599fe Author: ictwanglei ictwang...@gmail.com Date: 2015-06-30T11:35:16Z 1 add apache license 2 add some javadocs and commemts in the code 3 define variables only at the place where it is used 4 use StringBuilder replace + operator 5 rename some method name in PhoenixJson 6 change some helper methods level in JsonFunctionTest commit d99e8b613a92c855e6997211ded3e4e2f390db57 Author: ictwanglei ictwang...@gmail.com Date: 2015-07-01T00:59:53Z add apache license commit 684ed5264aabaad8afc8a261e09cabeacf214ee1 Author: ictwanglei ictwang...@gmail.com Date: 2015-07-20T03:51:28Z fix bugs add End2End Test commit 5c3359c02725f6833c51a8fe1cf9f3a3551e6414 Author: ictwanglei 597316...@qq.com Date:
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user petercdc commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35485180 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java --- @@ -200,12 +224,12 @@ public Expression visitLeave(ComparisonParseNode node, ListExpression children Expression lhsExpr = children.get(0); Expression rhsExpr = children.get(1); PDataType dataTypeOfLHSExpr = lhsExpr.getDataType(); -if (dataTypeOfLHSExpr != null !dataTypeOfLHSExpr.isEqualitySupported()) { +if (!(lhsExpr instanceof BaseJSONExpression) dataTypeOfLHSExpr != null !dataTypeOfLHSExpr.isEqualitySupported()) { --- End diff -- some operators will return data of other type,and compare to other value. for example: {k1:10}-'k1'1 so I think we need skip checking here. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35461438 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' = 2; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeFloat() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k4' = 2.5; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeVarchar() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k1' LIKE 'val'; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsText() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' LIKE '2'; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next());
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35464834 --- Diff: phoenix-core/src/main/antlr3/PhoenixSQL.g --- @@ -1149,6 +1175,31 @@ PERCENT OUTER_JOIN : '(' '+' ')' ; + // this is json support +POINT_E +: '-''' +; +PATH_E +: '#''' +; +POINT_T +: '-' +; +PATH_T +: '#' +; +SUPERSET +: '''@' +; +SUBSET +: '@''' +; +M_SERRCH_OR --- End diff -- minor nit : typo --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35472793 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/JsonPointAsElementExpression.java --- @@ -0,0 +1,104 @@ +/* + * 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.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PJson; +import org.apache.phoenix.schema.types.PVarchar; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class JsonPointAsElementExpression extends BaseJSONExpression{ + + private static final Logger logger = LoggerFactory.getLogger(JsonPointAsElementExpression.class); + + public JsonPointAsElementExpression(ListExpression children) + { + super(children); + } + public JsonPointAsElementExpression() + { + + } + private static PDataType defaultType=PJson.INSTANCE; + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + if (!children.get(0).evaluate(tuple, ptr)) { +if (logger.isDebugEnabled()) { --- End diff -- remove the logger.debug() for null arguments --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35474263 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java --- @@ -1339,4 +1363,188 @@ public Expression visit(SubqueryParseNode node) throws SQLException { public int getTotalNodeCount() { return totalNodeCount; } + //JSON node +@Override +public boolean visitEnter(JsonSingleKeySearchParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonSubsetParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonSupersetParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonMultiKeySearchOrParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonMultiKeySeatchAndParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPathAsTextParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPathAsElementParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPointAsElementParseNode node) throws SQLException { +return true; +} + +@Override +public boolean visitEnter(JsonPointAsTextParseNode node){ + return true; +} + + +@Override +public Expression visitLeave(JsonSingleKeySearchParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PVarchar.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } +Expression expression = new JsonSingleKeySearchExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonSubsetParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PJson.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + LiteralExpression rhs=(LiteralExpression)children.get(1); +Expression expression = new JsonSubsetExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonSupersetParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PVarchar.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + LiteralExpression rhs=(LiteralExpression)children.get(1); +Expression expression = new JsonSupersetExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonMultiKeySearchOrParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(children.get(1).getDataType()!=PVarcharArray.INSTANCE){ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } +Expression expression = new JsonMultiKeySearchOrExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonMultiKeySeatchAndParseNode node, ListExpression children)
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35472668 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/JsonPathAsTextExpression.java --- @@ -0,0 +1,82 @@ +/* + * 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.sql.SQLException; +import java.util.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PJson; +import org.apache.phoenix.schema.types.PVarchar; + + +public class JsonPathAsTextExpression extends BaseJSONExpression{ + public JsonPathAsTextExpression(ListExpression children) { +super(children); +} + public JsonPathAsTextExpression() { +} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + if (!children.get(1).evaluate(tuple, ptr)) { +return false; +} + String[] pattern =decodePath((String) PVarchar.INSTANCE.toObject(ptr)); + if (!children.get(0).evaluate(tuple, ptr)) { + return false; + } + PhoenixJson value = (PhoenixJson) PJson.INSTANCE.toObject(ptr, children.get(0).getSortOrder()); + try{ + PhoenixJson jsonValue=value.getPhoenixJson(pattern); + ptr.set(jsonValue.toBytes()); + return true; + }catch(SQLException e) + { + return false; + } + + } + private String[] decodePath(String path) --- End diff -- Change the rhs to be an array. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35475066 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/JsonPathAsElementExpression.java --- @@ -0,0 +1,85 @@ +/* + * 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.sql.SQLException; +import java.util.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PJson; +import org.apache.phoenix.schema.types.PVarchar; + + +public class JsonPathAsElementExpression extends BaseJSONExpression{ + private PDataType datatype=null; + public JsonPathAsElementExpression(ListExpression children) { +super(children); +} + public JsonPathAsElementExpression() { +} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + if (!children.get(1).evaluate(tuple, ptr)) { +return false; +} + String[] pattern =decodePath((String)PVarchar.INSTANCE.toObject(ptr)); + if (!children.get(0).evaluate(tuple, ptr)) { + return false; + } + PhoenixJson value = (PhoenixJson) PJson.INSTANCE.toObject(ptr); + try{ + PhoenixJson jsonValue=value.getPhoenixJson(pattern); --- End diff -- Do you need to add a null check whereever you can getPhoenixJson()? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35473132 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/BaseJSONExpression.java --- @@ -0,0 +1,40 @@ +/* + * 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.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; + +public abstract class BaseJSONExpression extends BaseCompoundExpression { + public BaseJSONExpression(ListExpression children) { +super(children); +} + public BaseJSONExpression() { +} + public abstract boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr); + public abstract T T accept(ExpressionVisitorT visitor); + public abstract PDataType getDataType(); + public PDataType getRealDataType(){ + return null; --- End diff -- Do you need to add this method, why can't you just use getDataType? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35459730 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' = 2; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeFloat() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k4' = 2.5; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeVarchar() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k1' LIKE 'val'; --- End diff -- You should be able to just use = here instead of LIKE --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35459614 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { --- End diff -- Rename to testJsonGetIntegerFieldByKey --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35459640 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' = 2; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeFloat() throws Exception { --- End diff -- Rename to testJsonGetFloatFieldByKey --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35459820 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' = 2; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeFloat() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k4' = 2.5; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeVarchar() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k1' LIKE 'val'; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsText() throws Exception { --- End diff -- Rename to testGetJsonFieldAsTextByKey --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35459677 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JSONOperationT.java --- @@ -0,0 +1,285 @@ +/* + * 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 java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.phoenix.util.PropertiesUtil; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class JSONOperationT extends BaseHBaseManagedTimeIT{ + private String json = {\k1\:\val\,+\k2\:true, +\k3\:2 , +\k4\:2.5 + + ,\k5\:[1,\val2\,false,3.5]+ + ,\k6\:{\nestk1\:\nestval\,\nestk2\:77,\nestk3\:2.1,\nestk4\:[9,8.4,\nestarrayval\]}}; + @Test +public void testJsonPointAsElementForTypeInteger() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k3' = 2; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeFloat() throws Exception { +String selectQuery = SELECT col1 FROM testJson WHERE col1 - 'k4' = 2.5; +String pk = valueOne; +Connection conn = getConnection(); +try { + +createTableAndUpsertRecord(json, pk, conn); + +PreparedStatement stmt = conn.prepareStatement(selectQuery); +ResultSet rs = stmt.executeQuery(); +assertTrue(rs.next()); +assertEquals(Json data read from DB is not as expected for query: + selectQuery ++ , json, rs.getString(1)); +assertFalse(rs.next()); + +} finally { +conn.close(); +} +} + @Test +public void testJsonPointAsElementForTypeVarchar() throws Exception { --- End diff -- Rename to testJsonGetVarcharFieldByKey --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35471677 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/parse/JsonMultiKeySeatchAndParseNode.java --- @@ -0,0 +1,51 @@ +/* + * 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.Collections; +import java.util.List; + +import org.apache.phoenix.compile.ColumnResolver; + +public class JsonMultiKeySeatchAndParseNode extends BinaryParseNode{ --- End diff -- minor nit: typo --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35472618 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/JsonPathAsElementExpression.java --- @@ -0,0 +1,85 @@ +/* + * 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.sql.SQLException; +import java.util.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PJson; +import org.apache.phoenix.schema.types.PVarchar; + + +public class JsonPathAsElementExpression extends BaseJSONExpression{ + private PDataType datatype=null; + public JsonPathAsElementExpression(ListExpression children) { +super(children); +} + public JsonPathAsElementExpression() { +} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + if (!children.get(1).evaluate(tuple, ptr)) { +return false; +} + String[] pattern =decodePath((String)PVarchar.INSTANCE.toObject(ptr)); + if (!children.get(0).evaluate(tuple, ptr)) { + return false; + } + PhoenixJson value = (PhoenixJson) PJson.INSTANCE.toObject(ptr); --- End diff -- Can you change the rhs to be array. This will be consistent with how you implemented ?| and ? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35473956 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java --- @@ -200,12 +224,12 @@ public Expression visitLeave(ComparisonParseNode node, ListExpression children Expression lhsExpr = children.get(0); Expression rhsExpr = children.get(1); PDataType dataTypeOfLHSExpr = lhsExpr.getDataType(); -if (dataTypeOfLHSExpr != null !dataTypeOfLHSExpr.isEqualitySupported()) { +if (!(lhsExpr instanceof BaseJSONExpression) dataTypeOfLHSExpr != null !dataTypeOfLHSExpr.isEqualitySupported()) { --- End diff -- Why do you need to add the checks for BaseJSONExpression here? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35474234 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java --- @@ -1339,4 +1363,188 @@ public Expression visit(SubqueryParseNode node) throws SQLException { public int getTotalNodeCount() { return totalNodeCount; } + //JSON node +@Override +public boolean visitEnter(JsonSingleKeySearchParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonSubsetParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonSupersetParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonMultiKeySearchOrParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonMultiKeySeatchAndParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPathAsTextParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPathAsElementParseNode node) { +return true; +} + +@Override +public boolean visitEnter(JsonPointAsElementParseNode node) throws SQLException { +return true; +} + +@Override +public boolean visitEnter(JsonPointAsTextParseNode node){ + return true; +} + + +@Override +public Expression visitLeave(JsonSingleKeySearchParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PVarchar.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } +Expression expression = new JsonSingleKeySearchExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonSubsetParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PJson.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + LiteralExpression rhs=(LiteralExpression)children.get(1); +Expression expression = new JsonSubsetExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonSupersetParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(!(PVarchar.INSTANCE.isComparableTo(children.get(1).getDataType({ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + LiteralExpression rhs=(LiteralExpression)children.get(1); +Expression expression = new JsonSupersetExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonMultiKeySearchOrParseNode node, ListExpression children) throws SQLException { + if(!(PJson.INSTANCE.isComparableTo(children.get(0).getDataType({ + throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } + if(children.get(1).getDataType()!=PVarcharArray.INSTANCE){ +throw TypeMismatchException.newException(children.get(0).getDataType(), children.get(1).getDataType()); + } +Expression expression = new JsonMultiKeySearchOrExpression(children); +return wrapGroupByExpression(expression); +} + +@Override +public Expression visitLeave(JsonMultiKeySeatchAndParseNode node, ListExpression children)
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/101#discussion_r35477420 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/JsonSubsetExpression.java --- @@ -0,0 +1,102 @@ +/* + * 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.Iterator; +import java.util.List; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.expression.visitor.ExpressionVisitor; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.PBoolean; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.schema.types.PJson; + +public class JsonSubsetExpression extends BaseCompoundExpression{ + public JsonSubsetExpression(ListExpression children) { +super(children); +} + public JsonSubsetExpression() { +} + @Override + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { + if(children.get(1) instanceof BaseJSONExpression){ + if(((BaseJSONExpression)children.get(1)).getRealDataType()!=PJson.INSTANCE) + { + ptr.set(PDataType.FALSE_BYTES); + return true; + } + } + if (!children.get(1).evaluate(tuple, ptr)) { +return false; +} + PhoenixJson pattern = (PhoenixJson) PJson.INSTANCE.toObject(ptr, children.get(1).getSortOrder()); + if(children.get(0) instanceof BaseJSONExpression){ + if(((BaseJSONExpression)children.get(0)).getRealDataType()!=PJson.INSTANCE) + { + ptr.set(PDataType.FALSE_BYTES); + return true; + } + } + if (!children.get(0).evaluate(tuple, ptr)) { + return false; + } + PhoenixJson value = (PhoenixJson) PJson.INSTANCE.toObject(ptr, children.get(0).getSortOrder()); + //null value + if(value==null){ + ptr.set(PDataType.FALSE_BYTES); + return true; + } + //empty set + if(pattern.getNodeSize()==0){ + ptr.set(PDataType.TRUE_BYTES); + return true; + } + if(pattern.getNodeSize()value.getNodeSize()){ + ptr.set(PDataType.FALSE_BYTES); + return true; + } + IteratorString fildnames=pattern.getFieldNames(); --- End diff -- minor nit: typo --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on the pull request: https://github.com/apache/phoenix/pull/101#issuecomment-124781109 Nice work @petercdc Can you please use this JIRA for the pull request https://issues.apache.org/jira/browse/PHOENIX-2144 ? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on the pull request: https://github.com/apache/phoenix/pull/93#issuecomment-124247112 @ictwanglei Can you please rebase and upload new patch, this one does not apply cleanly to the json branch? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35392010 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -231,4 +236,197 @@ private PhoenixJson getPhoenixJsonInternal(String[] paths) { } return new PhoenixJson(node, node.toString()); } + +/** + * If the current {@link PhoenixJson} is a JsonArray,then it returns the length of the JsonArray. + * pFor example:[1,2,3] ,it will return 3 + * pIts required for json_array_length(). + * @throws SQLException + */ +public int getJsonArrayLength() throws SQLException { + if(this.rootNode.isArray()){ + return this.rootNode.size(); + }else{ + throw new SQLException(The JsonNode should be an Array); --- End diff -- Add a new SQLExceptionCode and use SQLExceptionInfo.Builder --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394674 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/function/JsonEachFunction.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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.compile.KeyPart; +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.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.*; + +import java.util.List; + +@BuiltInFunction(name = JsonEachFunction.NAME, args = { +@Argument(allowedTypes = { PJson.class })}) +public class JsonEachFunction extends ScalarFunction { +public static final String NAME = JSON_EACH; --- End diff -- Add some tests for this function. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35392777 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/function/JsonArrayElementsFunction.java --- @@ -0,0 +1,107 @@ +/* + * 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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.compile.KeyPart; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +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.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.*; +import org.apache.phoenix.util.ByteUtil; + +import java.sql.SQLException; +import java.util.List; + +@BuiltInFunction(name = JsonArrayElementsFunction.NAME, args = { +@Argument(allowedTypes = { PJson.class })}) +public class JsonArrayElementsFunction extends ScalarFunction { --- End diff -- The corresponding postgress function returns a SETOF json. Is there a way that can be supported? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35388305 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayLengthFunctionIT.java --- @@ -0,0 +1,114 @@ +/** + * + */ +package org.apache.phoenix.end2end; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +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.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import org.apache.phoenix.expression.function.JsonArrayLengthFunction; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link JsonArrayLengthFunction}. + * + */ +public class JsonArrayLengthFunctionIT extends BaseHBaseManagedTimeIT { + + @Test + public void testJsonArrayLengthWithWhereClause() throws Exception { --- End diff -- Can you test with more complex json (like nested json and the different data types) --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35392706 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -231,4 +236,197 @@ private PhoenixJson getPhoenixJsonInternal(String[] paths) { } return new PhoenixJson(node, node.toString()); } + +/** + * If the current {@link PhoenixJson} is a JsonArray,then it returns the length of the JsonArray. + * pFor example:[1,2,3] ,it will return 3 + * pIts required for json_array_length(). + * @throws SQLException + */ +public int getJsonArrayLength() throws SQLException { + if(this.rootNode.isArray()){ + return this.rootNode.size(); + }else{ + throw new SQLException(The JsonNode should be an Array); + } +} + +/** + * If the current {@link PhoenixJson} is a JsonArray,then it returns the set of array elements. + * pFor example:[1,false,[2,string]] + * it will return (new Object[]{1,false,[2,\string\]}) + * pIts required for json_array_elements(). + * @return {@link String []} as the set of JSON elements + * @throws SQLException + */ +public Object[] getJsonArrayElements() throws SQLException { +if(this.rootNode.isArray()) { +ListString elementlist = new ArrayList(); +IteratorJsonNode elements = this.rootNode.getElements(); +while (elements.hasNext()) { +JsonNode e = elements.next(); +elementlist.add(e.toString()); +} +return elementlist.toArray(); +}else{ +throw new SQLException(The JsonNode should be an Array); --- End diff -- Add a new SQLExceptionCode and use SQLExceptionInfo.Builder --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394793 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -231,4 +236,197 @@ private PhoenixJson getPhoenixJsonInternal(String[] paths) { } return new PhoenixJson(node, node.toString()); } + +/** + * If the current {@link PhoenixJson} is a JsonArray,then it returns the length of the JsonArray. + * pFor example:[1,2,3] ,it will return 3 + * pIts required for json_array_length(). + * @throws SQLException + */ +public int getJsonArrayLength() throws SQLException { + if(this.rootNode.isArray()){ + return this.rootNode.size(); + }else{ + throw new SQLException(The JsonNode should be an Array); + } +} + +/** + * If the current {@link PhoenixJson} is a JsonArray,then it returns the set of array elements. + * pFor example:[1,false,[2,string]] + * it will return (new Object[]{1,false,[2,\string\]}) + * pIts required for json_array_elements(). + * @return {@link String []} as the set of JSON elements + * @throws SQLException + */ +public Object[] getJsonArrayElements() throws SQLException { +if(this.rootNode.isArray()) { +ListString elementlist = new ArrayList(); +IteratorJsonNode elements = this.rootNode.getElements(); +while (elements.hasNext()) { +JsonNode e = elements.next(); +elementlist.add(e.toString()); +} +return elementlist.toArray(); +}else{ +throw new SQLException(The JsonNode should be an Array); +} +} +/** + * It returns the set of JSON keys for the current {@link PhoenixJson}.Only the outermost keys will be generated + * pFor example:{f1:abc,f2:{f3:a, f4:b}} + * it will return (new Object[]{f1,f2}) + * pIts required for json_object_keys(). + * @return {@link String []} as the set of JSON keys + */ +public Object[] getJsonObjectKeys() { +ListString elementlist = new ArrayList(); +IteratorString fieldnames = this.rootNode.getFieldNames(); +while(fieldnames.hasNext()){ +elementlist.add(fieldnames.next()); +} +return elementlist.toArray(); +} + +/** + * It returns the SET of JSON key/value pairs for the current {@link PhoenixJson}.Only the outermost key/value will be generated + * Probably it seems we should use a special SET class which can hold different types of elements. + * but we use the {@link org.apache.phoenix.schema.types.PVarcharArray} as an alternative of SET TYPE + * when implementing the build-in function ,so we use {@link String} directly to store the pair + * and in this case we use , to separate key and value + * pFor example:{f1:abc,f2:edf} + * it will return (new Object[]{f1,abc,f2,edf}) + * + * pIts required for json_each(). + * @return {@link String []} as the SET of JSON key/value pairs + */ +public Object[] getJsonFields() { +ListString elementlist = new ArrayList(); +IteratorMap.EntryString, JsonNode fields = this.rootNode.getFields(); +while(fields.hasNext()){ +Map.EntryString, JsonNode entry = fields.next(); +StringBuilder fieldBuilder = new StringBuilder(); +fieldBuilder.append(entry.getKey()); +fieldBuilder.append(,); +fieldBuilder.append(entry.getValue().toString()); +elementlist.add(fieldBuilder.toString()); +} +return elementlist.toArray(); +} +/** + * Expands the object in current {@link PhoenixJson} to a record whose columns match the record type defined by base. + * Conversion will be best effort; columns in base with no corresponding key will be left null. + * If a column is specified more than once, the last value is used. + * Also use , to separate each columns + * pFor example:types :{a,b} json: {a:1,b:2} + * it will return new String(1,2) + * + * pIts required for json_populate_record(). + * @param types {@link String} the record type + * @return {@link String} as the result record + */ +public String jsonPopulateRecord(String [] types) { --- End diff -- Maybe add a helper function that can be used by both jsonPopulateRecord and jsonPopulateRecordSet? --- If your project is set up for it, you can reply to this email and have your reply appear
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35388212 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayLengthFunctionIT.java --- @@ -0,0 +1,114 @@ +/** + * + */ +package org.apache.phoenix.end2end; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +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.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import org.apache.phoenix.expression.function.JsonArrayLengthFunction; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link JsonArrayLengthFunction}. + * + */ +public class JsonArrayLengthFunctionIT extends BaseHBaseManagedTimeIT { + + @Test + public void testJsonArrayLengthWithWhereClause() throws Exception { + Connection conn = getConnection(); + String json = [1,2,3]; + String pk = valueOne; + try { + populateJsonTable(conn, json, pk); + + String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 3; + PreparedStatement stmt = conn.prepareStatement(selectQuery); + ResultSet rs = stmt.executeQuery(); + assertTrue(rs.next()); + assertEquals(Json data read from DB is not as expected., json, + rs.getString(1)); + assertFalse(rs.next()); + + } finally { + conn.close(); + } + } + + @Test + public void testJsonArrayLengthWhenFirstArgumentIsJsonString() --- End diff -- Change test name to testJsonArrayWithDifferentDataTypes --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35388362 --- Diff: phoenix-core/src/it/java/org/apache/phoenix/end2end/JsonArrayLengthFunctionIT.java --- @@ -0,0 +1,114 @@ +/** + * + */ +package org.apache.phoenix.end2end; + +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; +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.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import org.apache.phoenix.expression.function.JsonArrayLengthFunction; +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; + +/** + * End to end test for {@link JsonArrayLengthFunction}. + * + */ +public class JsonArrayLengthFunctionIT extends BaseHBaseManagedTimeIT { + + @Test + public void testJsonArrayLengthWithWhereClause() throws Exception { + Connection conn = getConnection(); + String json = [1,2,3]; + String pk = valueOne; + try { + populateJsonTable(conn, json, pk); + + String selectQuery = SELECT col1 FROM testJson WHERE json_array_length(col1) = 3; + PreparedStatement stmt = conn.prepareStatement(selectQuery); + ResultSet rs = stmt.executeQuery(); + assertTrue(rs.next()); + assertEquals(Json data read from DB is not as expected., json, + rs.getString(1)); + assertFalse(rs.next()); + + } finally { + conn.close(); + } + } + + @Test + public void testJsonArrayLengthWhenFirstArgumentIsJsonString() + throws Exception { + Connection conn = getConnection(); + String json = [1,2,true,[\string\,3]]; + String pk = valueOne; + try { + populateJsonTable(conn, json, pk); + + String selectQuery = SELECT json_array_length(col1) FROM testJson WHERE pk = 'valueOne'; + PreparedStatement stmt = conn.prepareStatement(selectQuery); + ResultSet rs = stmt.executeQuery(); + assertTrue(rs.next()); + assertEquals(Json data read from DB is not as expected., + 4, rs.getInt(1)); + + assertFalse(rs.next()); + + } finally { + conn.close(); + } + } + +@Test +public void testJsonArrayElementsWithWhereClause() throws Exception { --- End diff -- Can you add a test that verifies json_array_elements returns an array correctly ? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394937 --- Diff: phoenix-core/src/test/java/org/apache/phoenix/expression/function/JsonFunctionTest.java --- @@ -0,0 +1,284 @@ +/* + * 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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.expression.LiteralExpression; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.types.*; +import org.junit.Test; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + + +/** + * Unit tests for JSON build-in function. + * Testing function includes below: + * {@link ArrayToJsonFunction} + * {@link JsonArrayElementsFunction} + * {@link JsonPopulateRecordFunction} + * {@link JsonPopulateRecordSetFunction} + * {@link JsonArrayLengthFunction} + * {@link JsonObjectKeysFunction} + * {@link ToJsonFunction} + * {@link JsonEachFunction} + * + */ +public class JsonFunctionTest { +public static final String TEST_JSON_STR = + {\f2\:{\f3\:\value\},\f4\:{\f5\:99,\f6\:[1,true,\foo\]},\f7\:true}; + +private static PhoenixJson testArrayToJson (Object[] array,PDataType datatype,PArrayDataType arraydatatype) throws Exception { +PhoenixArray pa = PArrayDataType.instantiatePhoenixArray( datatype,array); +LiteralExpression arrayExpr = LiteralExpression.newConstant(pa,arraydatatype ); +ListExpression children = Arrays.ExpressionasList(arrayExpr); +ArrayToJsonFunction e = new ArrayToJsonFunction(children); +ImmutableBytesWritable ptr = new ImmutableBytesWritable(); +boolean evaluated = e.evaluate(null, ptr); +PhoenixJson result = (PhoenixJson)e.getDataType().toObject(ptr); +return result; +} + +@Test +public void testNumberArrayToJson() throws Exception { +Object[] testarray = new Object[]{1,12,32,432}; +PhoenixJson result = testArrayToJson(testarray, PInteger.INSTANCE, PIntegerArray.INSTANCE); +String expected = [1,12,32,432]; +assertEquals(result.serializeToString(), expected); +Object[] testarray2 = new Object[]{1.12,12.34,32.45,432.78}; +PhoenixJson result2 = testArrayToJson(testarray2, PDouble.INSTANCE, PDoubleArray.INSTANCE); +String expected2 = [1.12,12.34,32.45,432.78]; +assertEquals(result2.serializeToString(), expected2); +} +@Test +public void testBooleanArrayToJson() throws Exception { +Object[] testarray = new Object[]{false,true}; +PhoenixJson result = testArrayToJson(testarray, PBoolean.INSTANCE, PBooleanArray.INSTANCE); +String expected = [false,true]; +assertEquals(result.toString(), expected); +} + +@Test +public void testStringArrayToJson() throws Exception { +Object[] testarray = new Object[]{abc123,12.3,string,æ±å}; +PhoenixJson result = testArrayToJson(testarray, PVarchar.INSTANCE, PVarcharArray.INSTANCE); +String expected =[\abc123\,\12.3\,\string\,\æ±å\]; +assertEquals(result.serializeToString(), expected); +} +@Test +public void testDateArrayToJson() throws Exception { +SimpleDateFormat myFormatter = new SimpleDateFormat(-MM-dd kk:mm:ss.SSS); +Date date1= myFormatter.parse(1990-12-01 11:01:45.0); +Date date2 = myFormatter.parse(1989-03-12 13:01:45.0); +Date date3 = myFormatter.parse(1974-06-06 12:01:45.0); +Object[] testarray = new
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394919 --- Diff: phoenix-core/src/test/java/org/apache/phoenix/expression/function/JsonFunctionTest.java --- @@ -0,0 +1,284 @@ +/* + * 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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.expression.LiteralExpression; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.types.*; +import org.junit.Test; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + + +/** + * Unit tests for JSON build-in function. + * Testing function includes below: + * {@link ArrayToJsonFunction} + * {@link JsonArrayElementsFunction} + * {@link JsonPopulateRecordFunction} + * {@link JsonPopulateRecordSetFunction} + * {@link JsonArrayLengthFunction} + * {@link JsonObjectKeysFunction} + * {@link ToJsonFunction} + * {@link JsonEachFunction} + * + */ +public class JsonFunctionTest { +public static final String TEST_JSON_STR = + {\f2\:{\f3\:\value\},\f4\:{\f5\:99,\f6\:[1,true,\foo\]},\f7\:true}; + +private static PhoenixJson testArrayToJson (Object[] array,PDataType datatype,PArrayDataType arraydatatype) throws Exception { +PhoenixArray pa = PArrayDataType.instantiatePhoenixArray( datatype,array); +LiteralExpression arrayExpr = LiteralExpression.newConstant(pa,arraydatatype ); +ListExpression children = Arrays.ExpressionasList(arrayExpr); +ArrayToJsonFunction e = new ArrayToJsonFunction(children); +ImmutableBytesWritable ptr = new ImmutableBytesWritable(); +boolean evaluated = e.evaluate(null, ptr); +PhoenixJson result = (PhoenixJson)e.getDataType().toObject(ptr); +return result; +} + +@Test +public void testNumberArrayToJson() throws Exception { +Object[] testarray = new Object[]{1,12,32,432}; +PhoenixJson result = testArrayToJson(testarray, PInteger.INSTANCE, PIntegerArray.INSTANCE); --- End diff -- Just add test that executes this function as a query. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394623 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayToJsonFunction.java --- @@ -0,0 +1,105 @@ +/* + * 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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.FunctionParseNode; +import org.apache.phoenix.schema.IllegalDataException; +import org.apache.phoenix.schema.SortOrder; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.*; +import org.apache.phoenix.util.ByteUtil; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + + +@FunctionParseNode.BuiltInFunction(name=ArrayToJsonFunction.NAME, args={ +@FunctionParseNode.Argument(allowedTypes={PVarchar.class})} ) +public class ArrayToJsonFunction extends ScalarFunction { --- End diff -- Please add tests for this function --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user twdsilva commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r35394964 --- Diff: phoenix-core/src/test/java/org/apache/phoenix/expression/function/JsonFunctionTest.java --- @@ -0,0 +1,284 @@ +/* + * 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 org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.expression.LiteralExpression; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.types.*; +import org.junit.Test; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + + +/** + * Unit tests for JSON build-in function. + * Testing function includes below: + * {@link ArrayToJsonFunction} + * {@link JsonArrayElementsFunction} + * {@link JsonPopulateRecordFunction} + * {@link JsonPopulateRecordSetFunction} + * {@link JsonArrayLengthFunction} + * {@link JsonObjectKeysFunction} + * {@link ToJsonFunction} + * {@link JsonEachFunction} + * + */ +public class JsonFunctionTest { +public static final String TEST_JSON_STR = + {\f2\:{\f3\:\value\},\f4\:{\f5\:99,\f6\:[1,true,\foo\]},\f7\:true}; + +private static PhoenixJson testArrayToJson (Object[] array,PDataType datatype,PArrayDataType arraydatatype) throws Exception { +PhoenixArray pa = PArrayDataType.instantiatePhoenixArray( datatype,array); +LiteralExpression arrayExpr = LiteralExpression.newConstant(pa,arraydatatype ); +ListExpression children = Arrays.ExpressionasList(arrayExpr); +ArrayToJsonFunction e = new ArrayToJsonFunction(children); +ImmutableBytesWritable ptr = new ImmutableBytesWritable(); +boolean evaluated = e.evaluate(null, ptr); +PhoenixJson result = (PhoenixJson)e.getDataType().toObject(ptr); +return result; +} + +@Test +public void testNumberArrayToJson() throws Exception { +Object[] testarray = new Object[]{1,12,32,432}; +PhoenixJson result = testArrayToJson(testarray, PInteger.INSTANCE, PIntegerArray.INSTANCE); +String expected = [1,12,32,432]; +assertEquals(result.serializeToString(), expected); +Object[] testarray2 = new Object[]{1.12,12.34,32.45,432.78}; +PhoenixJson result2 = testArrayToJson(testarray2, PDouble.INSTANCE, PDoubleArray.INSTANCE); +String expected2 = [1.12,12.34,32.45,432.78]; +assertEquals(result2.serializeToString(), expected2); +} +@Test +public void testBooleanArrayToJson() throws Exception { +Object[] testarray = new Object[]{false,true}; +PhoenixJson result = testArrayToJson(testarray, PBoolean.INSTANCE, PBooleanArray.INSTANCE); +String expected = [false,true]; +assertEquals(result.toString(), expected); +} + +@Test +public void testStringArrayToJson() throws Exception { +Object[] testarray = new Object[]{abc123,12.3,string,æ±å}; +PhoenixJson result = testArrayToJson(testarray, PVarchar.INSTANCE, PVarcharArray.INSTANCE); +String expected =[\abc123\,\12.3\,\string\,\æ±å\]; +assertEquals(result.serializeToString(), expected); +} +@Test +public void testDateArrayToJson() throws Exception { +SimpleDateFormat myFormatter = new SimpleDateFormat(-MM-dd kk:mm:ss.SSS); +Date date1= myFormatter.parse(1990-12-01 11:01:45.0); +Date date2 = myFormatter.parse(1989-03-12 13:01:45.0); +Date date3 = myFormatter.parse(1974-06-06 12:01:45.0); +Object[] testarray = new
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user petercdc closed the pull request at: https://github.com/apache/phoenix/pull/99 --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
GitHub user petercdc opened a pull request: https://github.com/apache/phoenix/pull/101 PHOENIX-1661 Implement built-in functions for JSON 1.Adding following JSON operators: nbsp;nbsp;JSON get data Operator : - - # # nbsp;nbsp;JSON Boolean Operactor : ? ?| ? @ @ 2.add a unit test file: JSONOperationT.java 3.Adding functions for some JSON operators in PhoenixJson.java You can merge this pull request into a Git repository by running: $ git pull https://github.com/petercdc/phoenix jsoncdc Alternatively you can review and apply these changes as the patch at: https://github.com/apache/phoenix/pull/101.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #101 commit a131aa3664d18a048a41b0dab9155d90b17103f5 Author: LiChiachi t...@example.com Date: 2015-07-01T10:55:39Z rebase from json brance (but just recreate new branch base on json branch) fixed: All ParseNode and Visiter , All ExpressionVisiter , PhoenixJson method to be used for Expression method Not fixed yet: Expression method exclusive JsonPonit Operator commit 8babfb5fba20d24a1c65b02fdd13075b0632d4fc Author: LiChiachi t...@example.com Date: 2015-07-02T08:48:36Z rebase on json branch (but create new branch base on json branch) fixed: All ParseNode and All ExpressionNode exclusive Superset and Subset not fixed yet: Type Checking for As Element Node , Superset and Subset Node commit 077d5f41da0c22bcafd9387df90c60ea0be659ed Author: LiChiachi t...@example.com Date: 2015-07-06T09:56:47Z rebase on json branch (but recreate new branch based on json branch) fixed: All ParseNode and ExpressionNode are done not fixed yet: TypeChecking for Skipping checking type on JsongetElementExpression commit 5b8a43e09202b04e3371ff8cfd8451b68baa2759 Author: LiChiachi t...@example.com Date: 2015-07-10T01:52:53Z rebase on json branch (but recreate new branch based on json branch) fix bug wronging with superset and subset node commit 7793e94cf27d4057b633b7c67234f6e451d5577c Author: Andy ex2s62026...@gmail.com Date: 2015-07-11T09:37:09Z First Version commit 229939099154042af98b24839cfb483624400fb0 Author: Andy ex2s62026...@gmail.com Date: 2015-07-11T11:15:14Z fix type checking bug commit ff7e9edd126b65796a41d5bf8e567d50729e3989 Author: Andy ex2s62026...@gmail.com Date: 2015-07-11T14:30:56Z add Apache license commit fd4604cbb799600a87da0647671e8dbad6a8c6a7 Author: Andy ex2s62026...@gmail.com Date: 2015-07-13T12:22:08Z fix some expressions bugs --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user ictwanglei commented on the pull request: https://github.com/apache/phoenix/pull/93#issuecomment-117210029 @AakashPradeep sorry for the missing of comments and other coding mistakes and thanks for your patient guidance. I have modified the code according to your suggestion. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
GitHub user ictwanglei opened a pull request: https://github.com/apache/phoenix/pull/93 PHOENIX-1661 Implement built-in functions for JSON 1. Add some functional files: JsonArrayElementsFunction.java JsonArrayLengthFunction.java JsonEachFunction.java JsonObjectKeysFunction.java JsonPopulateRecordFunction.java JsonPopulateRecordSetFunction.java. ArrayToJsonFunction.java 2. Add a unit test file: JsonFunctionTest.java 3. Modify file PhoenixJson.java to which I added some assistant functions. @JamesRTaylor @twdsilva @AakashPradeep please review the changelist. You can merge this pull request into a Git repository by running: $ git pull https://github.com/ictwanglei/phoenix master Alternatively you can review and apply these changes as the patch at: https://github.com/apache/phoenix/pull/93.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #93 commit 26383070e5980697a0de1193c069692a8107b7c2 Author: ictwanglei 597316...@qq.com Date: 2015-03-21T15:32:40Z Create ArrayToJsonFunction.java add a function to support ArrayToJson function as a build-in function for JSON commit 2c63494f99267fbcd0b7e52be76a1dc0154e306a Author: ictwanglei 597316...@qq.com Date: 2015-03-21T15:34:57Z Create ArrayToJsonFunctionTest.java a simple test for testing ArrayToJsonFunction commit 241f4c3186a9f70f057e9196f962a0ed1b2f593b Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:26:56Z Delete ArrayToJsonFunctionTest.java commit f66ba90f92a667405759fceaf1ee74c27ef1d1c8 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:35:37Z Create JsonFunctionTest.java commit 7f2a6cf0c3d56b563a528036844dd5096f98ab6a Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:53:44Z PHOENIX-1661 Implement built-in functions for JSON commit db5c9e1cfbb1f3145a992efef13a8dca7cee0ec7 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:54:53Z Delete ArrayToJsonFunction.java commit a02b954453ff67220c08801e0eafd1c23bdd4b9c Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:55:10Z Rename ArrayToJsonFunction1.java to ArrayToJsonFunction.java commit eb76667b0c61ea646a63c1b46b41f418c6b43239 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:55:33Z Update ArrayToJsonFunction.java commit 6546bc5fb0620b954e31e54e0c71671c54a8f9dc Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:56:50Z PHOENIX-1661 Implement built-in functions for JSON commit 6bb8b116db3dd46434f2020a0ca2f46545b0ac05 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:58:22Z PHOENIX-1661 Implement built-in functions for JSON commit 3782d70ce1c0fedbcea48e3df968564311c06a2a Author: ictwanglei 597316...@qq.com Date: 2015-06-28T11:59:25Z PHOENIX-1661 Implement built-in functions for JSON commit 5ff61520d2aa351ea4dab18ec131c74f34b1b463 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:00:08Z PHOENIX-1661 Implement built-in functions for JSON commit d21c22b3697b64b920cc22a33e32d76b2b8eb074 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:01:14Z PHOENIX-1661 Implement built-in functions for JSON commit 541697d72d27a809cb8172efd58d83960b79f362 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:01:50Z PHOENIX-1661 Implement built-in functions for JSON commit 7e0dbaf950b4292bf673bb64367e57085ada25f8 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:03:16Z PHOENIX-1661 Implement built-in functions for JSON commit 331e09b9df48611421830b3c52afc80fd75099d5 Author: ictwanglei 597316...@qq.com Date: 2015-06-28T12:09:54Z PHOENIX-1661 Implement built-in functions for JSON PHOENIX-1661 Implement built-in functions for JSON --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33425159 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -0,0 +1,355 @@ +/* + * 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.schema.json; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.EqualityNotSupportedException; +import org.apache.phoenix.schema.types.*; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonParser.Feature; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ValueNode; + +import com.google.common.base.Preconditions; + +/** + * The {@link PhoenixJson} wraps json and uses Jackson library to parse and traverse the json. It + * should be used to represent the JSON data type and also should be used to parse Json data and + * read the value from it. It always conside the last value if same key exist more than once. + */ +public class PhoenixJson implements ComparablePhoenixJson { +private final JsonNode rootNode; +/* + * input data has been stored as it is, since some data is lost when json parser runs, for + * example if a JSON object within the value contains the same key more than once then only last + * one is stored rest all of them are ignored, which will defy the contract of PJsonDataType of + * keeping user data as it is. + */ +private final String jsonAsString; + +/** + * Static Factory method to get an {@link PhoenixJson} object. It also validates the json and + * throws {@link SQLException} if it is invalid with line number and character. + * @param jsonData Json data as {@link String}. + * @return {@link PhoenixJson}. + * @throws SQLException + */ +public static PhoenixJson getInstance(String jsonData) throws SQLException { +if (jsonData == null) { + return null; +} +try { +JsonFactory jsonFactory = new JsonFactory(); +JsonParser jsonParser = jsonFactory.createJsonParser(jsonData); +JsonNode jsonNode = getRootJsonNode(jsonParser); +return new PhoenixJson(jsonNode, jsonData); +} catch (IOException x) { +throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_JSON_DATA).setRootCause(x) +.setMessage(x.getMessage()).build().buildException(); +} + +} + +/** + * Returns the root of the resulting {@link JsonNode} tree. + */ +private static JsonNode getRootJsonNode(JsonParser jsonParser) throws IOException, +JsonProcessingException { +jsonParser.configure(Feature.ALLOW_COMMENTS, true); +ObjectMapper objectMapper = new ObjectMapper(); +try { +return objectMapper.readTree(jsonParser); +} finally { +jsonParser.close(); +} +} + +/* Default for unit testing */PhoenixJson(final JsonNode node, final String jsonData) { +Preconditions.checkNotNull(node, root node cannot be null for json); +this.rootNode = node; +this.jsonAsString = jsonData; +} + +/** + * Get {@link PhoenixJson} for a given json paths. For example : + * p + * code + *
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33424657 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/expression/function/ArrayToJsonFunction.java --- @@ -0,0 +1,87 @@ +package org.apache.phoenix.expression.function; + +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.FunctionParseNode; +import org.apache.phoenix.schema.IllegalDataException; +import org.apache.phoenix.schema.SortOrder; +import org.apache.phoenix.schema.json.PhoenixJson; +import org.apache.phoenix.schema.tuple.Tuple; +import org.apache.phoenix.schema.types.*; +import org.apache.phoenix.util.ByteUtil; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + + +@FunctionParseNode.BuiltInFunction(name=ArrayToJsonFunction.NAME, args={ +@FunctionParseNode.Argument(allowedTypes={PVarchar.class})} ) +public class ArrayToJsonFunction extends ScalarFunction { +public static final String NAME = Array_To_Json; --- End diff -- Please use only capital letters for function name. I guess thats the general convention we are following for other functions though I dont know whether it has any functional significance or not. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33425152 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -0,0 +1,355 @@ +/* + * 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.schema.json; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.EqualityNotSupportedException; +import org.apache.phoenix.schema.types.*; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonParser.Feature; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ValueNode; + +import com.google.common.base.Preconditions; + +/** + * The {@link PhoenixJson} wraps json and uses Jackson library to parse and traverse the json. It + * should be used to represent the JSON data type and also should be used to parse Json data and + * read the value from it. It always conside the last value if same key exist more than once. + */ +public class PhoenixJson implements ComparablePhoenixJson { +private final JsonNode rootNode; +/* + * input data has been stored as it is, since some data is lost when json parser runs, for + * example if a JSON object within the value contains the same key more than once then only last + * one is stored rest all of them are ignored, which will defy the contract of PJsonDataType of + * keeping user data as it is. + */ +private final String jsonAsString; + +/** + * Static Factory method to get an {@link PhoenixJson} object. It also validates the json and + * throws {@link SQLException} if it is invalid with line number and character. + * @param jsonData Json data as {@link String}. + * @return {@link PhoenixJson}. + * @throws SQLException + */ +public static PhoenixJson getInstance(String jsonData) throws SQLException { +if (jsonData == null) { + return null; +} +try { +JsonFactory jsonFactory = new JsonFactory(); +JsonParser jsonParser = jsonFactory.createJsonParser(jsonData); +JsonNode jsonNode = getRootJsonNode(jsonParser); +return new PhoenixJson(jsonNode, jsonData); +} catch (IOException x) { +throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_JSON_DATA).setRootCause(x) +.setMessage(x.getMessage()).build().buildException(); +} + +} + +/** + * Returns the root of the resulting {@link JsonNode} tree. + */ +private static JsonNode getRootJsonNode(JsonParser jsonParser) throws IOException, +JsonProcessingException { +jsonParser.configure(Feature.ALLOW_COMMENTS, true); +ObjectMapper objectMapper = new ObjectMapper(); +try { +return objectMapper.readTree(jsonParser); +} finally { +jsonParser.close(); +} +} + +/* Default for unit testing */PhoenixJson(final JsonNode node, final String jsonData) { +Preconditions.checkNotNull(node, root node cannot be null for json); +this.rootNode = node; +this.jsonAsString = jsonData; +} + +/** + * Get {@link PhoenixJson} for a given json paths. For example : + * p + * code + *
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33424861 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -0,0 +1,355 @@ +/* + * 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.schema.json; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.EqualityNotSupportedException; +import org.apache.phoenix.schema.types.*; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonParser.Feature; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ValueNode; + +import com.google.common.base.Preconditions; + +/** + * The {@link PhoenixJson} wraps json and uses Jackson library to parse and traverse the json. It + * should be used to represent the JSON data type and also should be used to parse Json data and + * read the value from it. It always conside the last value if same key exist more than once. + */ +public class PhoenixJson implements ComparablePhoenixJson { +private final JsonNode rootNode; +/* + * input data has been stored as it is, since some data is lost when json parser runs, for + * example if a JSON object within the value contains the same key more than once then only last + * one is stored rest all of them are ignored, which will defy the contract of PJsonDataType of + * keeping user data as it is. + */ +private final String jsonAsString; + +/** + * Static Factory method to get an {@link PhoenixJson} object. It also validates the json and + * throws {@link SQLException} if it is invalid with line number and character. + * @param jsonData Json data as {@link String}. + * @return {@link PhoenixJson}. + * @throws SQLException + */ +public static PhoenixJson getInstance(String jsonData) throws SQLException { +if (jsonData == null) { + return null; +} +try { +JsonFactory jsonFactory = new JsonFactory(); +JsonParser jsonParser = jsonFactory.createJsonParser(jsonData); +JsonNode jsonNode = getRootJsonNode(jsonParser); +return new PhoenixJson(jsonNode, jsonData); +} catch (IOException x) { +throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_JSON_DATA).setRootCause(x) +.setMessage(x.getMessage()).build().buildException(); +} + +} + +/** + * Returns the root of the resulting {@link JsonNode} tree. + */ +private static JsonNode getRootJsonNode(JsonParser jsonParser) throws IOException, +JsonProcessingException { +jsonParser.configure(Feature.ALLOW_COMMENTS, true); +ObjectMapper objectMapper = new ObjectMapper(); +try { +return objectMapper.readTree(jsonParser); +} finally { +jsonParser.close(); +} +} + +/* Default for unit testing */PhoenixJson(final JsonNode node, final String jsonData) { +Preconditions.checkNotNull(node, root node cannot be null for json); +this.rootNode = node; +this.jsonAsString = jsonData; +} + +/** + * Get {@link PhoenixJson} for a given json paths. For example : + * p + * code + *
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33425136 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -0,0 +1,355 @@ +/* + * 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.schema.json; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.EqualityNotSupportedException; +import org.apache.phoenix.schema.types.*; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonParser.Feature; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ValueNode; + +import com.google.common.base.Preconditions; + +/** + * The {@link PhoenixJson} wraps json and uses Jackson library to parse and traverse the json. It + * should be used to represent the JSON data type and also should be used to parse Json data and + * read the value from it. It always conside the last value if same key exist more than once. + */ +public class PhoenixJson implements ComparablePhoenixJson { +private final JsonNode rootNode; +/* + * input data has been stored as it is, since some data is lost when json parser runs, for + * example if a JSON object within the value contains the same key more than once then only last + * one is stored rest all of them are ignored, which will defy the contract of PJsonDataType of + * keeping user data as it is. + */ +private final String jsonAsString; + +/** + * Static Factory method to get an {@link PhoenixJson} object. It also validates the json and + * throws {@link SQLException} if it is invalid with line number and character. + * @param jsonData Json data as {@link String}. + * @return {@link PhoenixJson}. + * @throws SQLException + */ +public static PhoenixJson getInstance(String jsonData) throws SQLException { +if (jsonData == null) { + return null; +} +try { +JsonFactory jsonFactory = new JsonFactory(); +JsonParser jsonParser = jsonFactory.createJsonParser(jsonData); +JsonNode jsonNode = getRootJsonNode(jsonParser); +return new PhoenixJson(jsonNode, jsonData); +} catch (IOException x) { +throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_JSON_DATA).setRootCause(x) +.setMessage(x.getMessage()).build().buildException(); +} + +} + +/** + * Returns the root of the resulting {@link JsonNode} tree. + */ +private static JsonNode getRootJsonNode(JsonParser jsonParser) throws IOException, +JsonProcessingException { +jsonParser.configure(Feature.ALLOW_COMMENTS, true); +ObjectMapper objectMapper = new ObjectMapper(); +try { +return objectMapper.readTree(jsonParser); +} finally { +jsonParser.close(); +} +} + +/* Default for unit testing */PhoenixJson(final JsonNode node, final String jsonData) { +Preconditions.checkNotNull(node, root node cannot be null for json); +this.rootNode = node; +this.jsonAsString = jsonData; +} + +/** + * Get {@link PhoenixJson} for a given json paths. For example : + * p + * code + *
[GitHub] phoenix pull request: PHOENIX-1661 Implement built-in functions fo...
Github user AakashPradeep commented on a diff in the pull request: https://github.com/apache/phoenix/pull/93#discussion_r33425135 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/json/PhoenixJson.java --- @@ -0,0 +1,355 @@ +/* + * 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.schema.json; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.schema.EqualityNotSupportedException; +import org.apache.phoenix.schema.types.*; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonParser.Feature; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ValueNode; + +import com.google.common.base.Preconditions; + +/** + * The {@link PhoenixJson} wraps json and uses Jackson library to parse and traverse the json. It + * should be used to represent the JSON data type and also should be used to parse Json data and + * read the value from it. It always conside the last value if same key exist more than once. + */ +public class PhoenixJson implements ComparablePhoenixJson { +private final JsonNode rootNode; +/* + * input data has been stored as it is, since some data is lost when json parser runs, for + * example if a JSON object within the value contains the same key more than once then only last + * one is stored rest all of them are ignored, which will defy the contract of PJsonDataType of + * keeping user data as it is. + */ +private final String jsonAsString; + +/** + * Static Factory method to get an {@link PhoenixJson} object. It also validates the json and + * throws {@link SQLException} if it is invalid with line number and character. + * @param jsonData Json data as {@link String}. + * @return {@link PhoenixJson}. + * @throws SQLException + */ +public static PhoenixJson getInstance(String jsonData) throws SQLException { +if (jsonData == null) { + return null; +} +try { +JsonFactory jsonFactory = new JsonFactory(); +JsonParser jsonParser = jsonFactory.createJsonParser(jsonData); +JsonNode jsonNode = getRootJsonNode(jsonParser); +return new PhoenixJson(jsonNode, jsonData); +} catch (IOException x) { +throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_JSON_DATA).setRootCause(x) +.setMessage(x.getMessage()).build().buildException(); +} + +} + +/** + * Returns the root of the resulting {@link JsonNode} tree. + */ +private static JsonNode getRootJsonNode(JsonParser jsonParser) throws IOException, +JsonProcessingException { +jsonParser.configure(Feature.ALLOW_COMMENTS, true); +ObjectMapper objectMapper = new ObjectMapper(); +try { +return objectMapper.readTree(jsonParser); +} finally { +jsonParser.close(); +} +} + +/* Default for unit testing */PhoenixJson(final JsonNode node, final String jsonData) { +Preconditions.checkNotNull(node, root node cannot be null for json); +this.rootNode = node; +this.jsonAsString = jsonData; +} + +/** + * Get {@link PhoenixJson} for a given json paths. For example : + * p + * code + *