← Back to Pull requests

fix: return 400 for native queries that fail JSON binding #20830

Merged
FrankChen021 merged 4 files (225 lines) on Nov 07, 2026 03:08 UTC
View on GitHub
FrankChen021 FrankChen021 commented on Nov 04, 2026 07:24 UTC
Author

Description

#19775 made QueryResource return a 400 for native queries that fail with a ValueInstantiationException. Other JsonMappingExceptions raised while binding well-formed JSON to a Query still fall through to the catch-all in QueryResource#doPost. For example:

  • an unknown queryType (InvalidTypeIdException)

  • a field of the wrong type, e.g. "aggregations": "rows" (MismatchedInputException)
  • For each such request, the broker logs a full stack trace at WARN ("Uncaught exception from query processing. This should be caught and handled directly.") and returns a 500 Unhandled exception made it to the top that names only the exception class. These are client errors, so they flood the broker log and don't tell the caller what is wrong.

    Fix

    readQuery now also catches JsonMappingException, after the existing ValueInstantiationException catch. It is converted into a BadJsonQueryException, so the request goes through the existing catch (QueryException e) path: a 400 Json parse failed response with no stack trace logged. The error message is Invalid native query: <original message> (through reference chain: <path>), for example:

    Invalid native query: Cannot deserialize value of type `java.util.ArrayList<...>` from String value ('rows') ... (through reference chain: org.apache.druid.query.timeseries.TimeseriesQuery["aggregations"])
    

    errorClass is the actual Jackson exception class, consistent with the ValueInstantiationException handling from #19775.

    Not every JsonMappingException is a client error, so two cases still go to the catch-all and return 500 with a logged stack trace:

  • InvalidDefinitionException: Jackson cannot bind to the query class itself, such as a registered subtype with no usable creator. This is a server-side defect and does not depend on the request.

  • A plain JsonMappingException that wraps an exception thrown by a deserializer or setter, unless the wrapped exception points at bad input. Jackson wraps such exceptions with the original as the cause. The request returns 400 only when that cause is an IllegalArgumentException (including IAE), a DruidException targeted at Persona.USER (such as InvalidInput), or a JsonProcessingException. Any other cause, such as a NullPointerException or a defensive DruidException, is treated as a bug and returns 500.
  • Before this PR all of these returned 500, so a case that falls outside the 400 rules behaves as it did before. The custom deserializers in processing and server that can run on query input all reject bad values with IAE/InvalidInput or a Jackson exception, so their errors now return 400.

    Release note

    Native queries that are valid JSON but cannot be deserialized into a query, such as an unknown queryType or a field of the wrong type, now return HTTP 400 with a descriptive error instead of HTTP 500, and no longer log a stack trace on the Broker.

    <hr>

    ##### Key changed/added classes in this PR
    QueryResource
    QueryResource#isServerErrorDuringBinding
    BadJsonQueryException
    QueryResourceTest

    <hr>

    This PR has:

  • been self-reviewed.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • Diff: 225 lines (+225 / -0) across 4 files Reviewer: GWphua Review duration: 4063m
    All Vouch review confidence checks passed
    Residual risk 0.06 = Change Risk (0.40) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 225 lines diff across 4 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.85 · 3 substantive comment(s)
    0.85
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.324
    Real PR Conversation Timeline
    8 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 04, 2026 07:26 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟢 Approval recommended

    No active review issues were identified in the query-binding error-handling changes.

    No open issues 🎉

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Extends native-query error handling so well-formed JSON that cannot bind to a query returns HTTP 400 with an actionable message instead of HTTP 500, building on the constructor-validation handling from #19775.

    Changes:

  • Routes additional Jackson mapping exceptions through the existing client-error handler, avoiding catch-all warning stack traces.

  • Preserves the Jackson exception class and includes the binding message and reference path when available.

  • Adds regression tests for unknown query types and incorrectly typed fields.
  • | File | Description |
    | ---- | ----------- |
    | server/​src/​test/​java/​org/​apache/​druid/​server/​QueryResourceTest.java | Tests HTTP 400 responses and error details for query-binding failures. |
    | server/​src/​main/​java/​org/​apache/​druid/​server/​QueryResource.java | Converts additional JSON mapping failures into client errors. |
    | processing/​src/​main/​java/​org/​apache/​druid/​query/​BadJsonQueryException.java | Formats mapping-error messages with reference paths when available. |
    </details>

    <sub>Review effort: Balanced</sub>

    github-advanced-security[bot] github-advanced-security[bot] commented on code on Nov 04, 2026 07:54 UTC
    Code Review
    processing/src/main/java/org/apache/druid/query/BadJsonQueryException.java:1
    @@ -75,4 +81,13 @@
         }
         return "Invalid native query: " + errorMessage;
       }
    +
    +  private static String getErrorMessage(JsonMappingException e)

    CodeQL / Confusing overloading of methods

    Method BadJsonQueryException.getErrorMessage(..) could be confused with overloaded method [getErrorMessage](1), since dispatch depends on static types.

    Show more details

    FrankChen021 FrankChen021 commented on Nov 04, 2026 12:20 UTC
    Reviewer

    🟡 Changes recommended

    Do not merge until the new mapping catch excludes server-side mapping-definition failures. JsonMappingException includes InvalidDefinitionException, so a broken registered query subtype can now become a 400 BadJsonQueryException instead of following the existing 500 path; ordinary input-side failures are otherwise routed correctly.

    Reviewed 4 of 4 changed files; findings in 1 file.

    Validation: git diff --check 7e300b9ccf6cca26d7bbe632f790415b31f123c1 fa8e5a08c4e3656e2ee8a3ab768225924e99fdc8 passed; no build or tests run for this static review.

    | Severity | Findings |
    | --- | ---: |
    | P0 | 0 |
    | P1 | 0 |
    | P2 | 1 |
    | P3 | 0 |
    | Total | 1 |

    <!-- mergelens:review -->

    ---

    This is an automated review by Codex GPT-5.6-Luna(max)

    After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

    FrankChen021 FrankChen021 commented on code on Nov 04, 2026 12:20 UTC
    Code Review
    server/src/main/java/org/apache/druid/server/QueryResource.java:1
    @@ -300,6 +301,10 @@ private Query<?> readQuery(
         catch (ValueInstantiationException e) {
           throw new BadJsonQueryException(e);
         }
    +    catch (JsonMappingException e) {

    [P2] Broad mapping catch hides definition failures

    Finding: JsonMappingException also covers InvalidDefinitionException, which Jackson uses when the target query type itself is invalid, such as a registered subtype with no usable creator. Those failures are independent of request JSON, but this catch wraps them in BadJsonQueryException; doPost then returns HTTP 400 and skips the catch-all warning/500 path, making a broker-side configuration or code defect look like a client error.

    Suggestion: Limit this conversion to input-side mapping exceptions such as MismatchedInputException and InvalidTypeIdException, while allowing InvalidDefinitionException to reach the server-error path.

    FrankChen021 FrankChen021 commented on code on Nov 05, 2026 03:27 UTC
    Code Review
    server/src/main/java/org/apache/druid/server/QueryResource.java:1
    @@ -300,6 +301,10 @@ private Query<?> readQuery(
         catch (ValueInstantiationException e) {
           throw new BadJsonQueryException(e);
         }
    +    catch (JsonMappingException e) {

    Fixed in 0f3d5a5254: InvalidDefinitionException is now rethrown before the JsonMappingException catch, so it still goes to the catch-all 500 path. testQueryWithBrokenDefinitionIsServerError covers it. I kept the broad catch so other input-side subclasses, such as UnrecognizedPropertyException and InvalidFormatException, still return 400.

    FrankChen021 FrankChen021 commented on Nov 05, 2026 12:41 UTC
    Reviewer

    🟢 Approval recommended

    The current head fixes the prior mapping-definition issue while preserving 400 responses for ordinary JSON mapping failures and user-facing deserializer validation. No actionable issues remain in the reviewed changes.

    Reviewed 4 of 4 changed files.

    Validation: git diff --check 7e300b9ccf6cca26d7bbe632f790415b31f123c1 53f0c71e23714ca85b46923ed3961a852d37f358 passed; no build or tests run for this static review.

    <!-- mergelens:review -->

    ---

    This is an automated review by Codex GPT-5.6-Luna(max)

    GWphua GWphua approved these changes on Nov 06, 2026 15:13 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    FrankChen021 FrankChen021
    Labels
    risk: low confidence: 84.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.8% (MEDIUM) · Higher = larger blast radius
    39.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.6% · Lower = rushed or shallow review
    84.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.1% · Safe to merge
    6.1%
    Repository