Skip to content

feat: concretize json type from query#8081

Merged
MichaelScofield merged 3 commits intomainfrom
feat/query-driven-json-type-concretize
May 9, 2026
Merged

feat: concretize json type from query#8081
MichaelScofield merged 3 commits intomainfrom
feat/query-driven-json-type-concretize

Conversation

@MichaelScofield
Copy link
Copy Markdown
Collaborator

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

as title

PR Checklist

Please convert it to a draft if some of the following conditions are not met.

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.
  • API changes are backward compatible.
  • Schema or data changes are backward compatible.

@github-actions github-actions Bot added size/M docs-not-required This change does not impact docs. labels May 8, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a query-driven JSON type concretization mechanism to improve JSON data handling. A new optimizer rule, JsonTypeConcretizeRule, deduces expected JSON structures from json_get calls and passes these as hints to the storage engine via the ScanRequest. The FlatProjectionMapper and JsonArray have been updated to use these hints for aligning and casting JSON fields during scans, with JsonArray::try_align now discarding extra fields instead of failing. Feedback identifies a high-severity issue where the optimizer rule fails to trim the $ prefix from JSON paths, leading to incorrect type mapping. Additionally, there is a concern that removing explicit schema enforcement in the Parquet reader could negatively impact performance and pushdown capabilities.

Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/mito2/src/sst/parquet/reader.rs Outdated
@MichaelScofield MichaelScofield force-pushed the feat/query-driven-json-type-concretize branch 2 times, most recently from 2b2861f to 9453567 Compare May 8, 2026 09:11
@MichaelScofield MichaelScofield marked this pull request as ready for review May 8, 2026 09:11
@MichaelScofield
Copy link
Copy Markdown
Collaborator Author

@codex review
@gemini-code-assist review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to deduce and concretize JSON types during query optimization to improve data alignment in the storage layer. It adds a new optimizer rule, JsonTypeConcretizeRule, which extracts expected JSON schemas from json_get calls and passes them as hints to the scan request. The storage engine uses these hints to align JSON arrays by casting fields or discarding extra data. Feedback identifies critical issues in the deduction logic, specifically regarding potential data loss when columns are accessed both directly and via json_get, the lack of support for the $ path prefix, and non-defensive error handling that could cause queries to fail. Additionally, the reviewer pointed out that modifying TableProvider state within an optimizer rule introduces side effects and concurrency risks.

Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/query/src/optimizer/json_type_concretize.rs
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 94535676fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/query/src/optimizer/json_type_concretize.rs
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds query-driven JSON type concretization so JSON extension columns can be read/converted using the types implied by query expressions (e.g., casts on json_get / j.a.b::Int64), improving correctness/consistency of JSON projections across read paths.

Changes:

  • Introduces a DataFusion optimizer rule (JsonTypeConcretizeRule) to deduce JSON native types from query expressions and attach them as scan hints.
  • Plumbs a json_type_hint through ScanRequest into mito2 scan/projection logic, and aligns JSON arrays against the concretized schema during conversion.
  • Updates JSON type merge/Arrow-type conversion utilities and adjusts SQLness golden tests/output.

Reviewed changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/cases/standalone/common/types/json/json2.sql Adds result queries to validate JSON projections after concretization.
tests/cases/standalone/common/types/json/json2.result Updates expected outputs for added JSON queries.
tests/cases/standalone/common/tql-explain-analyze/explain.result Accounts for the new optimizer rule in verbose explain output.
src/store-api/src/storage/requests.rs Adds json_type_hint to ScanRequest and prints it in Display.
src/query/src/query_engine/state.rs Registers JsonTypeConcretizeRule in the logical optimizer pipeline.
src/query/src/optimizer/json_type_concretize.rs New optimizer rule that deduces JSON types from json_get calls and sets scan hints.
src/query/src/optimizer.rs Exposes the new optimizer module.
src/query/src/dummy_catalog.rs Adds a setter on DummyTableProvider to store JSON type hints into the scan request.
src/mito2/src/sst/parquet/reader.rs Adjusts Parquet Arrow reader options when JSON extension fields exist.
src/mito2/src/read/scan_region.rs Applies query-driven JSON type hints by rewriting the projection output schema.
src/mito2/src/read/flat_projection.rs Aligns JSON arrays to the (possibly concretized) JSON schema during conversion.
src/mito2/Cargo.toml Adds arrow-schema dependency.
src/datatypes/src/vectors/json/array.rs Changes JSON alignment to discard extra fields rather than error.
src/datatypes/src/types/json_type.rs Adds JsonNativeType::merge and Arrow-type conversion; updates merge logic.
src/common/function/src/scalars/json/json_get.rs Makes with_type non-optional internally (defaulting to Utf8View) and simplifies builders.
Cargo.lock Records the new dependency resolution (arrow-schema).

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

Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/store-api/src/storage/requests.rs
@MichaelScofield MichaelScofield force-pushed the feat/query-driven-json-type-concretize branch 2 times, most recently from 602435a to 03deae7 Compare May 8, 2026 12:15
@MichaelScofield MichaelScofield marked this pull request as draft May 8, 2026 12:30
@MichaelScofield MichaelScofield force-pushed the feat/query-driven-json-type-concretize branch from 03deae7 to 7deb711 Compare May 8, 2026 12:32
Comment thread src/datatypes/src/types/json_type.rs
Comment thread src/mito2/src/read/scan_region.rs
@MichaelScofield MichaelScofield marked this pull request as ready for review May 9, 2026 01:04
Signed-off-by: luofucong <luofc@foxmail.com>
Signed-off-by: luofucong <luofc@foxmail.com>
Comment thread src/query/src/optimizer/json_type_concretize.rs
Comment thread src/mito2/src/read/flat_projection.rs
Signed-off-by: luofucong <luofc@foxmail.com>
@MichaelScofield MichaelScofield force-pushed the feat/query-driven-json-type-concretize branch from 7deb711 to ace3629 Compare May 9, 2026 04:04
@MichaelScofield
Copy link
Copy Markdown
Collaborator Author

@sunng87 @fengys1996 PTAL

Copy link
Copy Markdown
Contributor

@fengys1996 fengys1996 left a comment

Choose a reason for hiding this comment

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

LGTM

@MichaelScofield MichaelScofield added this pull request to the merge queue May 9, 2026
Merged via the queue into main with commit 7a285c2 May 9, 2026
47 checks passed
@MichaelScofield MichaelScofield deleted the feat/query-driven-json-type-concretize branch May 9, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-not-required This change does not impact docs. size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants