Skip to content

[Performance]: JSON_TABLE converts unselected columns after projection pruning #33697

Todo

Description

@Ariznawlll

Is there an existing issue for the same bug?

Branch Name

main

Commit ID

98b2097

Other Environment Information

  • Isolated single-node runtime; every formal run returned git_version()='98b20975' and SELECT 42=42.
  • GOMAXPROCS=4, GOMEMLIMIT=4GiB, max_dop=1; arms ran serially in owned disposable schemas.
  • Two formal scales, N=8,192 and N=16,384; 500 result-checked statements per arm under a 20-second symbolized CPU profile at each scale.
  • Exact results, empty immediate SHOW WARNINGS, executed physical plans, planner serialized-spec inspection, and schema cleanup passed at both scales.
  • This is a structural executor-path and sampled-CPU report, not a wall-clock-speedup claim.

Actual Behavior

A JSON_TABLE query whose only consumer-visible column is n FOR ORDINALITY still converts every column in the serialized table spec. In the target, the planner output attributes are exactly ["n"], but the spec is ["waste0","waste1","waste2","waste3","n"]; selected column n is at spec position 4. Four unselected conversions therefore remain in jsonTableState.nextRow.

The exact narrow control serializes only n. Both arms execute a single-worker physical tablefunction 0→N → group N→1 and return COUNT=N, SUM(n)=N(N+1)/2 with identical empty warnings.

Direct symbolized CPU attribution:

N Target json_table.go:296 (convertColumn call) Target json_table.go:239 (ConvertTablePathMatchesWithOptions) Control ordinary conversion
8,192 6.68s cumulative 5.75s cumulative no samples
16,384 6.20s cumulative 5.46s cumulative no samples

The target's conversion work includes four columns that its own pruned output cannot expose. The control is a fixed-input upper bound, not a generally semantics-preserving rewrite for arbitrary JSON_TABLE error/warning policies.

Expected Behavior

When projection pruning proves that table-function outputs are unobservable, JSON_TABLE should avoid their ordinary conversion work where exact SQL semantics permit. Possible implementations include pruning the serialized spec during planning or lazily converting only selected outputs during execution.

The implementation must explicitly preserve column-position mapping, nested/ordinality behavior, ON EMPTY/ON ERROR actions, conversion errors and warnings, truncation policy, and cases where evaluating an unselected expression is observably required. This report does not request blindly dropping fallible conversions.

Steps to Reproduce

Use N=8,192 and then N=16,384. The following Python helper generates the two SQL statements; each JSON object has a 2 KiB waste string, keeping N=16,384 below the 64 MiB blob limit:

def json_query(arm, n):
    waste = "" if arm == "control" else (
        "waste0 VARCHAR(2048) PATH '$.waste',"
        "waste1 TEXT PATH '$.waste',"
        "waste2 BIGINT PATH '$.number',"
        "waste3 DOUBLE PATH '$.number',"
    )
    obj = '{"number":1,"waste":"' + "x" * 2048 + '"}'
    source = "CONCAT('[',REPEAT('" + obj + ",'," + str(n - 1) + "),'" + obj + "]')"
    return (
        "SELECT COUNT(*),SUM(jt.n) FROM JSON_TABLE(" + source +
        ",'$[*]' COLUMNS(" + waste + "n FOR ORDINALITY)) jt"
    )

Run both statements with SET max_dop=1, check the exact aggregate and immediate SHOW WARNINGS, and inspect:

EXPLAIN <query>;
EXPLAIN PHYPLAN ANALYZE <query>;

For the planner gate, inspect the FUNCTION_SCAN node's TableDef.Cols and unmarshal TableDef.TblFunc.Param as jsonvalue.TableSpec. Require output columns ["n"] for both arms, target spec columns ["waste0","waste1","waste2","waste3","n"], and control spec columns ["n"]. An EXPLAIN-only projection check is insufficient because an earlier correlated fixture retained all five TableDef columns despite displaying only jt.n.

Profile each arm separately for 20 seconds while repeating the statement 500 times with exact result checks:

curl --fail --silent --show-error \
  'http://127.0.0.1:16011/debug/pprof/profile?seconds=20' \
  --output json-target.cpu.pprof

go tool pprof -list='table_function\.\(\*jsonTableState\)\.nextRow' \
  ./mo-service json-target.cpu.pprof
go tool pprof -list='table_function\.\(\*jsonTableState\)\.convertColumn' \
  ./mo-service json-target.cpu.pprof

At this commit, nextRow lines 292–296 iterates every non-nested spec column, and convertColumn line 239 performs path conversion.

Additional information

Evidence includes probe_day12_json_pruned.py, a read-only Go-overlay planner test, and complete plans/profiles/receipts under json_pruned_verified_n8192/ and json_pruned_verified_n16384/ in the local Day-12 investigation artifacts. No product source change was used for the runtime measurements.

Activity

  1. added
    kind/bug
    on Dec 3, 2026
  2. added this to the MO-202612 milestone on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions