Repository navigation
fix: handle NULL num_rows and empty aggregates in Oracle smart query - #1145
Open
greatbooms wants to merge 1 commit into
Open
greatbooms wants to merge 1 commit into
greatbooms wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
greatbooms
force-pushed
the
fix/oracle-smart-query-null-safety
branch
from
July 20, 2026 08:17
c277cf1 to
7a05d13
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Oracle smart query emits JSON that is syntactically valid but fails ChartDB's own import validation (
DatabaseMetadataSchema), so the user only sees the generic "Invalid JSON. Please correct it or contact us at support@chartdb.io" error with nothing to act on.Two independent causes, both hit by ordinary schemas:
Tables without gathered statistics —
all_tables.num_rowsisNULLuntil statistics are gathered, so the query emits"rows": null.TableInfoSchemadeclaresrows: z.number().optional(), and.optional()acceptsundefined, notnull.Empty aggregates —
JSON_ARRAYAGGover zero rows returnsNULL, not an empty array, so a schema with no views emits"views": null.DatabaseMetadataSchemarequiresz.array(...)for all six aggregates, so the same applies tofk_info,pk_info,columns,indexesandtables.Originally hit on a real Oracle schema of 226 tables, 44 of which had no gathered statistics, and which contained no views.
Fix
NVL(num_rows, 0)for the per-table row countNVL(<aggregate>, TO_CLOB('[]')) FORMAT JSONfor all six top-level aggregatesFORMAT JSONis required on the wrapped aggregates:NVLreturns a plainCLOBand strips Oracle's internal JSON typing, so without it the array is emitted as an escaped string instead of a nested array. The same idiom is already used elsewhere in this script for thenullableanduniqueboolean keys.Verification
Reproduced and verified against Oracle Database 26ai Free 23.26.2.0.0 (
gvenzl/oracle-free), on a schema with two freshly created tables (statistics never gathered,num_rowsNULL) and no views.Before — parses as JSON, fails validation:
After:
Why
FORMAT JSONis needed — same patch withFORMAT JSONremoved from the six aggregates:So dropping
FORMAT JSONwould trade one validation failure for a worse one across every aggregate.No behaviour change when nothing is null — after gathering statistics and adding a view to the same schema, the patched and unpatched queries produce byte-identical output:
npm run lint,tsc --noEmit,prettier --checkandnpm test(833 tests) all pass.