Skip to content

Commit 8346f68

Browse files
committed
HIVE-29277: Invalid index should return null for get_json_object
1 parent 00174fc commit 8346f68

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,12 @@ private Object extract(Object json, String path, boolean skipMapProc) {
239239
indexListCache.put(path, indexList);
240240
}
241241

242-
if (indexList.size() > 0) {
243-
json = extract_json_withindex(json, indexList);
242+
if (indexList.isEmpty()) {
243+
// Return null if index is invalid.
244+
return path.contains("[") ? null : json;
244245
}
245246

246-
return json;
247+
return extract_json_withindex(json, indexList);
247248
}
248249

249250
private transient AddingList jsonList = new AddingList();

ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFJson.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public void testRootArray() throws HiveException {
105105
runTest("[1,2,3]", "$0", null, udf);
106106
runTest("[1,2,3]", "0", null, udf);
107107
runTest("[1,2,3]", "$.", null, udf);
108+
runTest("[1,2,3]", "$[1 ]", null, udf);
109+
runTest("[1,2,3]", "$[ 1]", null, udf);
110+
runTest("[1,2,3]", "$[invalid]", null, udf);
108111

109112
runTest("[1,2,3]", "$", "[1,2,3]", udf);
110113
runTest("{\"a\":4}", "$", "{\"a\":4}", udf);

ql/src/test/queries/clientpositive/udf_get_json_object.q

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ get_json_object('[{"k1":[{"k11":[1,2,3]}]}]', '$[0].k1[0].k11[1]');
6464
SELECT
6565
get_json_object('[1,2,3]', '[2]'),
6666
get_json_object('[1,2,3]', '$0'),
67+
get_json_object('[1,2,3]', '$[0 ]'),
68+
get_json_object('[1,2,3]', '$[ 0]'),
6769
get_json_object('[1,2,3]', '$[3]');

ql/src/test/results/clientpositive/llap/udf_get_json_object.q.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,19 @@ POSTHOOK: Input: _dummy_database@_dummy_table
227227
PREHOOK: query: SELECT
228228
get_json_object('[1,2,3]', '[2]'),
229229
get_json_object('[1,2,3]', '$0'),
230+
get_json_object('[1,2,3]', '$[0 ]'),
231+
get_json_object('[1,2,3]', '$[ 0]'),
230232
get_json_object('[1,2,3]', '$[3]')
231233
PREHOOK: type: QUERY
232234
PREHOOK: Input: _dummy_database@_dummy_table
233235
#### A masked pattern was here ####
234236
POSTHOOK: query: SELECT
235237
get_json_object('[1,2,3]', '[2]'),
236238
get_json_object('[1,2,3]', '$0'),
239+
get_json_object('[1,2,3]', '$[0 ]'),
240+
get_json_object('[1,2,3]', '$[ 0]'),
237241
get_json_object('[1,2,3]', '$[3]')
238242
POSTHOOK: type: QUERY
239243
POSTHOOK: Input: _dummy_database@_dummy_table
240244
#### A masked pattern was here ####
241-
NULL NULL NULL
245+
NULL NULL NULL NULL NULL

0 commit comments

Comments
 (0)