You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2014/05/12 22:53:20 UTC
svn commit: r1594090 - in /hive/trunk/ql/src:
java/org/apache/hadoop/hive/ql/udf/UDFJson.java
test/results/clientpositive/udf_get_json_object.q.out
Author: hashutosh
Date: Mon May 12 20:53:19 2014
New Revision: 1594090
URL: http://svn.apache.org/r1594090
Log:
HIVE-7036 : get_json_object bug when extract list of list with index (Navis via Ashutosh Chauhan)
Modified:
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java
hive/trunk/ql/src/test/results/clientpositive/udf_get_json_object.q.out
Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java?rev=1594090&r1=1594089&r2=1594090&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java Mon May 12 20:53:19 2014
@@ -26,6 +26,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import com.google.common.collect.Iterators;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
@@ -215,41 +216,51 @@ public class UDFJson extends UDF {
return json;
}
- List<Object> jsonList = new ArrayList<Object>();
+ private transient AddingList jsonList = new AddingList();
+
+ private static class AddingList extends ArrayList<Object> {
+ @Override
+ public Iterator<Object> iterator() {
+ return Iterators.forArray(toArray());
+ }
+ @Override
+ public void removeRange(int fromIndex, int toIndex) {
+ super.removeRange(fromIndex, toIndex);
+ }
+ };
@SuppressWarnings("unchecked")
private Object extract_json_withindex(Object json, ArrayList<String> indexList) {
jsonList.clear();
jsonList.add(json);
- Iterator<String> itr = indexList.iterator();
- while (itr.hasNext()) {
- String index = itr.next();
- List<Object> tmp_jsonList = new ArrayList<Object>();
+ for (String index : indexList) {
+ int targets = jsonList.size();
if (index.equalsIgnoreCase("*")) {
- for (int i = 0; i < jsonList.size(); i++) {
- Object array = jsonList.get(i);
+ for (Object array : jsonList) {
if (array instanceof List) {
for (int j = 0; j < ((List<Object>)array).size(); j++) {
- tmp_jsonList.add(((List<Object>)array).get(j));
+ jsonList.add(((List<Object>)array).get(j));
}
}
}
- jsonList = tmp_jsonList;
} else {
- for (int i = 0; i < (jsonList).size(); i++) {
- Object array = jsonList.get(i);
+ for (Object array : jsonList) {
int indexValue = Integer.parseInt(index);
if (!(array instanceof List)) {
continue;
}
- if (indexValue >= ((List<Object>)array).size()) {
- return null;
+ List<Object> list = (List<Object>) array;
+ if (indexValue >= list.size()) {
+ continue;
}
- tmp_jsonList.add(((List<Object>)array).get(indexValue));
- jsonList = tmp_jsonList;
+ jsonList.add(list.get(indexValue));
}
}
+ if (jsonList.size() == targets) {
+ return null;
+ }
+ jsonList.removeRange(0, targets);
}
if (jsonList.isEmpty()) {
return null;
Modified: hive/trunk/ql/src/test/results/clientpositive/udf_get_json_object.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientpositive/udf_get_json_object.q.out?rev=1594090&r1=1594089&r2=1594090&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientpositive/udf_get_json_object.q.out (original)
+++ hive/trunk/ql/src/test/results/clientpositive/udf_get_json_object.q.out Mon May 12 20:53:19 2014
@@ -130,7 +130,7 @@ POSTHOOK: type: QUERY
POSTHOOK: Input: default@src_json
#### A masked pattern was here ####
POSTHOOK: Lineage: dest1.c1 SIMPLE []
-2 [[1,2,{"b":"y","a":"x"}],[3,4],[5,6]] 1 [1,2,{"b":"y","a":"x"}] [1,2,{"b":"y","a":"x"},3,4,5,6] y ["y"]
+2 [[1,2,{"b":"y","a":"x"}],[3,4],[5,6]] [1,3,5] [1,2,{"b":"y","a":"x"}] [1,2,{"b":"y","a":"x"},3,4,5,6] y ["y"]
PREHOOK: query: SELECT get_json_object(src_json.json, '$.non_exist_key'), get_json_object(src_json.json, '$..no_recursive'), get_json_object(src_json.json, '$.store.book[10]'), get_json_object(src_json.json, '$.store.book[0].non_exist_key'), get_json_object(src_json.json, '$.store.basket[*].non_exist_key'), get_json_object(src_json.json, '$.store.basket[0][*].non_exist_key') FROM src_json
PREHOOK: type: QUERY
PREHOOK: Input: default@src_json