Do Firebird 4.0 JSON_QUERY DEFAULT clauses conform to SQL/JSON standard for missing keys?
0 reputation · 04 Apr 2023, 09:34 UTC
0 reputation · 04 Apr 2023, 09:34 UTC
Firebird 4.0 introduced native JSON support, including the JSON_QUERY function to extract values from JSON columns. By default, the function returns NULL when the specified key or path does not exist. A DEFAULT clause can be supplied to provide a user‑defined fallback value. It remains unclear whether this default behavior fully conforms to the SQL/JSON standard’s error‑handling rules for missing keys.
To verify the behavior of JSON_QUERY in a controlled environment that does not rely on production credentials, a dedicated test database or a local Firebird instance should be used. The test should insert JSON data where the target key is absent, then execute queries both with and without a DEFAULT clause.
JSON_QUERY in Firebird 4.0 treat missing keys as defined by the SQL/JSON standard when a DEFAULT clause is supplied?Firebird 4.0's JSON_QUERY implementation provides a DEFAULT clause to handle missing keys, but it does not strictly conform to the full SQL/JSON standard syntax. While the standard specifies ON EMPTY and ON ERROR clauses to define behavior for missing data or malformed JSON, Firebird uses a simplified DEFAULT clause to provide a fallback value when a path evaluation results in an empty set.
In the SQL/JSON standard, the distinction between an "empty" result (the path exists but is empty) and an "error" (the path is invalid or the key is missing) is critical. Firebird 4.0 generally operates in a "lax" mode by default. In this mode, missing object keys or out-of-bounds array indices are treated as empty results rather than errors. Consequently, the DEFAULT clause triggers for both missing keys and empty values, collapsing two distinct standard states into a single fallback mechanism.
To determine if specific edge cases (such as nested paths or array indices) diverge from your required standard, execute the following tests in a local Firebird 4.0 instance:
{'user': {'id': 1, 'tags': ['sql']}}).DEFAULT clause.$.user.email).$.user.tags[5]).-- Example Test Query
SELECT JSON_QUERY(json_column, '$.non_existent_key' DEFAULT 'FallbackValue')
FROM test_table;
This analysis assumes the use of Firebird 4.0.x. It is important to note that JSON_QUERY is intended to return a JSON fragment (object or array). If you are attempting to extract a scalar value (string, number), JSON_VALUE should be used instead, as the two functions have different return type requirements and may handle defaults differently.
Diagnostic Requirement: To refine this recommendation, please specify if you are utilizing strict mode path evaluation, as this changes whether a missing key triggers an error state versus an empty state.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.