Ramin Gharib created FLINK-41086:
------------------------------------

             Summary: JSON_LENGTH and JSON_TYPE fail the job when a JSON path 
function throws
                 Key: FLINK-41086
                 URL: https://issues.apache.org/jira/browse/FLINK-41086
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Runtime
    Affects Versions: 2.4.0
            Reporter: Ramin Gharib


h3. Problem

{{JSON_LENGTH(json, path)}} and {{JSON_TYPE(json, path)}} should return NULL 
when the path does not locate a value. {{SqlJsonUtils#jsonLength}} and 
{{SqlJsonUtils#jsonType}} only catch {{InvalidPathException}} around 
{{JsonPath#read}}. Path functions such as {{min()}} or {{avg()}} throw the 
parent {{JsonPathException}} on their own, for example over an empty array. 
{{JsonPathInputTypeStrategy}} only rejects the {{lax}}/{{strict}} prefix, so 
these paths pass validation. The exception escapes and fails the job. With 
{{f0}} as a STRING column:

||{{f0}}||Query||Expected||Actual||
|{{[]}}|{{JSON_LENGTH(f0, '$.min()')}}|NULL|{{JsonPathException: Aggregation 
function attempted to calculate value using empty array}}|
|{{\{"a": []\}}}|{{JSON_TYPE(f0, '$.a.avg()')}}|NULL|the same exception|
|{{\{"a": [1, 2]\}}}|{{JSON_LENGTH(f0, '$.a[*].min()')}}|NULL|the same 
exception|

{{JSON_VALUE}}, {{JSON_QUERY}} and {{JSON_EXISTS}} go through 
{{SqlJsonUtils#jsonApiCommonSyntax}}, which catches any {{Exception}} around 
the same call.

h3. Proposal

Catch {{JsonPathException}} in both methods. It is the parent of 
{{InvalidPathException}} and {{PathNotFoundException}}, so the existing cases 
keep returning NULL.

h3. Tests

* The queries above in {{JsonFunctionsITCase}}, for SQL and the Table API.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to