Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ql/src/java/org/apache/hadoop/hive/ql/udf/UDFJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,12 @@ private Object extract(Object json, String path, boolean skipMapProc) {
indexListCache.put(path, indexList);
}

if (indexList.size() > 0) {
json = extract_json_withindex(json, indexList);
if (indexList.isEmpty()) {
// Return null if index is invalid.
return path.contains("[") ? null : json;
Copy link
Member

@deniskuzZ deniskuzZ Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we refactor

  if (indexList.size() > 0) {
    json = extract_json_withindex(json, indexList);
  } else if (path.indexOf('[') >= 0) {
    return null; // index is invalid
  }

}

return json;
return extract_json_withindex(json, indexList);
}

private transient AddingList jsonList = new AddingList();
Expand Down
3 changes: 3 additions & 0 deletions ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public void testRootArray() throws HiveException {
runTest("[1,2,3]", "$0", null, udf);
runTest("[1,2,3]", "0", null, udf);
runTest("[1,2,3]", "$.", null, udf);
runTest("[1,2,3]", "$[1 ]", null, udf);
runTest("[1,2,3]", "$[ 1]", null, udf);
runTest("[1,2,3]", "$[invalid]", null, udf);

runTest("[1,2,3]", "$", "[1,2,3]", udf);
runTest("{\"a\":4}", "$", "{\"a\":4}", udf);
Expand Down
2 changes: 2 additions & 0 deletions ql/src/test/queries/clientpositive/udf_get_json_object.q
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ get_json_object('[{"k1":[{"k11":[1,2,3]}]}]', '$[0].k1[0].k11[1]');
SELECT
get_json_object('[1,2,3]', '[2]'),
get_json_object('[1,2,3]', '$0'),
get_json_object('[1,2,3]', '$[0 ]'),
get_json_object('[1,2,3]', '$[ 0]'),
get_json_object('[1,2,3]', '$[3]');
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,19 @@ POSTHOOK: Input: _dummy_database@_dummy_table
PREHOOK: query: SELECT
get_json_object('[1,2,3]', '[2]'),
get_json_object('[1,2,3]', '$0'),
get_json_object('[1,2,3]', '$[0 ]'),
get_json_object('[1,2,3]', '$[ 0]'),
get_json_object('[1,2,3]', '$[3]')
PREHOOK: type: QUERY
PREHOOK: Input: _dummy_database@_dummy_table
#### A masked pattern was here ####
POSTHOOK: query: SELECT
get_json_object('[1,2,3]', '[2]'),
get_json_object('[1,2,3]', '$0'),
get_json_object('[1,2,3]', '$[0 ]'),
get_json_object('[1,2,3]', '$[ 0]'),
get_json_object('[1,2,3]', '$[3]')
POSTHOOK: type: QUERY
POSTHOOK: Input: _dummy_database@_dummy_table
#### A masked pattern was here ####
NULL NULL NULL
NULL NULL NULL NULL NULL