Skip to content

fix: return actionable errors for invalid native queries - #19775

Merged
AlbericByte merged 3 commits into
apache:masterfrom
FrankChen021:codex/native-query-value-instantiation-response
Jul 28, 2026
Merged

fix: return actionable errors for invalid native queries#19775
AlbericByte merged 3 commits into
apache:masterfrom
FrankChen021:codex/native-query-value-instantiation-response

Conversation

@FrankChen021

@FrankChen021 FrankChen021 commented Jul 28, 2026

Copy link
Copy Markdown
Member

Description

Native query deserialization currently handles malformed JSON as a client error, but query constructor validation failures escape as opaque HTTP 500 responses.

This is particularly problematic for AI agents that generate native queries. The existing response does not explain what is wrong, so the agent cannot reliably correct and retry its query.

This PR converts Jackson ValueInstantiationException failures into HTTP 400 responses and exposes the underlying validation message. For example:

{
  "error": "Json parse failed",
  "errorMessage": "Invalid native query: dataSource can't be null",
  "errorClass": "com.fasterxml.jackson.databind.exc.ValueInstantiationException",
  "host": null
}

The HTTP 400 status identifies the request as invalid rather than a transient server failure, while the actionable errorMessage helps users, programmatic clients, and AI agents identify and repair the generated native query.

If no underlying validation message is available, the response uses:

Invalid native query: the request contains invalid or missing fields

Exceptions occurring after query deserialization are unaffected.

Release note

Fixed: Invalid native queries that fail during object construction now return HTTP 400 with an actionable validation message. This helps users and automated clients, including AI agents, identify and correct invalid generated queries.


Key changed classes in this PR
  • BadJsonQueryException
  • QueryResource
  • QueryResourceTest

This PR has:

  • been self-reviewed.
  • a release note entry in the PR description.
  • added unit tests covering the new error-handling path.

Testing

The local environment does not have JDK 25, so the focused tests were run on the rebased source using JDK 24 with the Java release property overridden to 24:

JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-24.jdk/Contents/Home \
mvn test -pl server -am \
  -Djava.version=24 \
  -Dmaven.compiler.release=24 \
  -Dtest="org.apache.druid.server.QueryResourceTest#testBadQuery+testIncompleteQuery+testBadJsonQueryExceptionUsesDeepestCauseMessage" \
  -Dsurefire.failIfNoSpecifiedTests=false \
  -Pskip-static-checks \
  -Dweb.console.skip=true \
  -T1C

Result: 3 tests run, 0 failures, 0 errors. GitHub CI remains authoritative for the JDK 25 build.

@FrankChen021
FrankChen021 marked this pull request as ready for review July 28, 2026 02:41
Copilot AI review requested due to automatic review settings July 28, 2026 02:41
@FrankChen021 FrankChen021 changed the title Return actionable errors for invalid native queries fix: return actionable errors for invalid native queries Jul 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves native query error handling in Druid’s HTTP query endpoint by converting Jackson construction-time failures (notably ValueInstantiationException) into user-facing HTTP 400 responses with an actionable validation message, instead of allowing them to surface as opaque HTTP 500s.

Changes:

  • Catch Jackson ValueInstantiationException during query deserialization and rethrow as BadJsonQueryException to produce HTTP 400.
  • Extend BadJsonQueryException to generate a clearer “Invalid native query: …” message when object construction fails.
  • Add a unit test covering an incomplete native query payload and asserting the new response shape/message.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
server/src/main/java/org/apache/druid/server/QueryResource.java Converts construction-time Jackson failures into BadJsonQueryException so they return HTTP 400.
processing/src/main/java/org/apache/druid/query/BadJsonQueryException.java Adds handling/message formatting for ValueInstantiationException to produce actionable client error messages.
server/src/test/java/org/apache/druid/server/QueryResourceTest.java Adds test coverage for incomplete native query payloads that fail during object construction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/main/java/org/apache/druid/server/QueryResource.java
Comment thread server/src/test/java/org/apache/druid/server/QueryResourceTest.java
@jtuglu1

jtuglu1 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@FrankChen021 this is related to #19745

@FrankChen021

FrankChen021 commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

@jtuglu1 Thanks for the link. The two PRs improve native-query errors at different stages: #19745 propagates DruidException after deserialization, while this PR handles Jackson construction failures before query lifecycle initialization. They appear complementary. I will rebase this PR after #19745 lands and resolve any shared QueryResource test or constructor changes.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have reviewed the code for correctness, edge cases, concurrency, and integration risks; no issues found.

Since the prior review, production behavior is unchanged. The update moves exception-message tests into the owning processing module and adds safe coverage for empty and absent causes.

Reviewed 4 of 4 changed files.


This is an automated review by Codex GPT-5.6-Sol

@AlbericByte
AlbericByte merged commit 4bb735c into apache:master Jul 28, 2026
28 checks passed
@github-actions github-actions Bot added this to the 39.0.0 milestone Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants