Repository navigation
fix(sql): reconcile LAG/LEAD default values into a common result type with the value argument - #30115
Conversation
… with the value argument LAG/LEAD previously narrowed the default value (3rd arg) to the value type (1st arg), truncating longer string defaults and rounding decimal defaults; the narrowed result was persisted by CTAS and exposed by views. Reconcile the value argument and the default value into one common result type, mirroring MySQL's window function type aggregation: - a decimal value keeps its declared precision when the default is an integer (LAG(decimal(10,4),1,-1) stays decimal(10,4)); - the remaining combinations follow the COALESCE type reconciliation (varchar width widening, integer + decimal common type); - a domainless NULL default, fewer-argument forms, and irreconcilable types keep the historical behavior. Update the affected BVT results (int column + int64 default now yields int64, matching MySQL bigint) and add coverage for string widening, decimal defaults, CTAS/View metadata, and prepared default reuse.
Keep TEXT capacity when reconciling numeric defaults, widen decimal integer capacity using actual literal digits or the full integer domain, and promote the physical decimal family when required. Preserve string quoting in semantic AST cache keys without changing display names. Add typed planner regressions and SQL metadata/value controls for reversed projections, large text, and decimal defaults.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 2446a89c315e7bf503bd38045143cfa3fac25b39
Assessment: request_changes
Reviewed the complete eight-file delta, callers, metadata consumers, tests and supplied review concerns. The binder is the appropriate owner for common-type reconciliation, but COALESCE overload selection still permits a lossy temporal conversion. A public SQL regression reproduces for both LAG and LEAD and passes with the production changes reverted to the exact merge-base.
Model: gpt-6-astra / xhigh
Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1
- P1
pkg/sql/plan/base_binder.go:8440: Use a TIME value with a DATE default: CREATE TABLE t(v TIME(6), d DATE); INSERT INTO t VALUES ('12:34:56.123456','2024-01-02'); SELECT LAG(v,0,d) OVER (), LEAD(v,0,d) OVER () FROM t;
Impact: Both functions silently discard the source time and return the current date, even though offset zero never selects the default. Common-type reconciliation must preserve the time component instead of narrowing the source to DATE.
Evidence: The COALESCE resolver selects DATE; this target is accepted here and applied to argument zero at line 8214. Time.ToDate() returns Today(time.UTC). The retained SQL frontend test returned '2026-10-15' for both functions while source and no-default controls returned '12:34:56.123456'. Identical assertions pass with both changed production files restored to their merge-base blobs. See temporal-head.log (receipt 1792057578568479997-2, exit 1) and temporal-base.log (receipt 1792057778568636375-2, exit 0). Use temporal domain aggregation that preserves fractional precision and add regressions for both functions and operand roles.
Validation:
-
Artifacts are retained under /home/xupeng/.local/state/mo-bot/jobs/f7a31e5ea46b83dc8ad35f3b/attempt-1. review-evidence-manifest.json correlates commands, source fingerprints, terminal receipts, logs and overlay hashes.
-
Isolation preflight passed. Recovered repository-owned native build after independently verifying 192 matching current-source inputs and 62 artifact hashes.
-
Focused planner selection passed all 42 top-level tests, including the new LAG/LEAD tests: planner-normal.log, receipt 1792057468541686122-2.
-
Public SQL domain exploration exposed the temporal defect; focused candidate and merge-base controls confirmed it independently for LAG and LEAD.
-
Final evidence audit passed and source remained unchanged: evidence-audit-normalized.log, receipt 1792057869499117042-2. Initial audit mismatch was solely the supplied patch's missing final newline.
-
Frozen exact-head evidence reports successful build, Ubuntu unit tests, SCA, coverage and two BVT configurations. Historical review concerns were checked against current code and regression coverage.
Limitations:
-
The supplied local_validation inventory is empty; CI evidence contains statuses rather than per-case logs. Full BVT, race tests and upstream MySQL comparisons were not rerun locally.
-
No performance improvement is claimed or benchmarked. Widened window types can increase conversion work and buffer sizes; shared semantic keys retain additional literal text. Source review found no added I/O, locks or goroutines.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 0a071fc1a3a4e01576b8aa683db8b55b031bdc0f
Assessment: request_changes
Reviewed the complete eight-file delta and affected callers. Binder-owned type reconciliation and literal-aware expression keys are appropriate, but the string fallback drops collation metadata, changing query results and persisted schemas.
Model: gpt-6-astra / xhigh
Session: 01a13dcf-1586-7a01-9000-d45e20995cf8 · slot 1 · generation 1
- P2
pkg/sql/plan/base_binder.go:8478: Use LAG/LEAD with a VARCHAR column declared COLLATE utf8mb4_unicode_ci and a BIT default, or a similarly collated TEXT column and Boolean default—even with offset 0.
Impact: The reconstructed string type replaces utf8mb4_unicode_ci with utf8mb4_general_ci. Comparisons change despite the default being unused, and CTAS/views persist the incorrect collation. Merge the established string collation metadata, including its version, into the fallback type before inserting casts.
Evidence: Embedded SQL with v='ß' shows SELECT x='ss' FROM (SELECT LAG(v,0,CAST(7 AS BIT(3))) OVER () x FROM t) q returning 0 on the candidate versus 1 with the merge-base production files. LEAD and TEXT/Boolean cases reproduce this. All four CTAS/view probes persist utf8mb4_general_ci on the candidate; the merge-base retains utf8mb4_unicode_ci. Candidate receipt 1792126454086850454-2.json exits 1; identical baseline probes pass under receipt 1792126408913855195-2.json.
Validation:
-
Evidence, scripts, overlays, logs and receipt mappings are retained in /home/xupeng/.local/state/mo-bot/jobs/68ddbaa3e00aba4d3e14c14b/attempt-1/review-evidence.json.
-
Isolation preflight passed. Native prerequisites were recovered and built through the repository owner; the final audit verified 192 source inputs, 62 artifacts and an unchanged checkout.
-
Ten targeted planner tests passed (planner.log; receipt 1792126198189246491-2.json). Seven shared expression-key/prepared-binding tests passed (semantic-fixed.log; receipt 1792126487131967688-2.json).
-
Production SQL reproduced four comparison regressions and four persisted-collation failures. Identical probes passed with exact merge-base versions of both changed production files; see collation-persist.log and collation-base.log.
-
Final receipt/log/script/overlay audit passed: audit-validation.log, receipt 1792126589973195720-2.json. Exploratory failures and the corrected reviewer-script attempt are separately classified in the evidence report.
-
The frozen head's CI snapshot reports successful Ubuntu UT, shared build, SCA and substantive BVT jobs.
Limitations:
-
Full distributed BVT and live MySQL comparison were not repeated locally; underlying CI logs were not supplied.
-
Performance assessment is source-based: mixed-domain windows add required casts, and expression keys retain additional literal identity bytes. No benchmark claim is made.
|
mo-bot review · Commit: "interrupted" |
aptend
left a comment
There was a problem hiding this comment.
Deep re-review of exact head 3b3f10d947954d7469f98959c1653f9c5f0f2bcc against merge-base c2b990cd7122c30f3adf7f9a5c20d798c18f3989. No blocking finding remains.
Read all six historical reviews, eight conversation comments, the inline comment and its resolved/outdated thread. Compared the increment since my previous a0739a4 review and latest external 0a071fc review, accounted for upstream base drift, and reviewed the complete eight-file diff plus affected common-type, cast, semantic-key, window-result and persisted-metadata consumers. This is a focused compatibility bug fix; it does not require a separate feature-design approval.
Closure verified in running SQL, not inferred from thread resolution: JSON/VARCHAR(255) retains all 309 bytes; DECIMAL/BIT retains 12.34; VARCHAR/TINYTEXT retains 300-byte Unicode; character/BIT, TEXT/BOOL, binary data and TEXT/DATE preserve selected source values; TIME/DATE retains the negative fractional duration; utf8mb4_unicode_ci comparisons and persisted CTAS/view metadata retain their identity. Also exercised default-selected/reversed operands, binary NUL bytes, wide numeric/binary mixtures, NULL and binary-protocol prepared reuse (short/long/NULL/Unicode/short), and literal-sensitive window/aggregate identities.
Fresh local evidence on this head:
- Repository-owned
make -j4 cgocompleted, followed by full normal planner/window/aggexec tests: 4,514 passing root tests, 19,307 passing named tests, two existing skips. - Independent public-SQL probe passed. All three committed BVT scripts ran twice: new default-type script 197/197 per round (metadata enabled), negative-offset script 50/50 per round (metadata enabled), existing value-window script 49/49 per round (standard non-metadata mode). The changed INT32/INT64 result contract is covered by binder tests and the metadata-enabled negative-offset script.
- The exploratory metadata-forced run of the existing value-window script had seven unchanged legacy header mismatches (VARCHAR width and FLOAT precision/scale), with identical values; this is not counted as a full metadata pass. Earlier harness-only BIT wire-format expectations and missing runner configuration were corrected, and final runs completed successfully. Logs retained under
/tmp/review30115-pRYSPx.
No new concurrency/lifecycle mechanism is introduced by these query-local binder changes, so no fresh race or throughput benchmark is claimed. Reviewed source casts remain linear in evaluated input; no performance improvement is claimed. Temporary probe source is removed after validation. State and exact head are checked again immediately before submission.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Approved after reviewing the complete 8-file delta at 10c5bb0fda13bc7a9b66b63434e322256f2e5d6b against a322466757b9b0165c59fbda565608653d923b2a, including every previous review and the resolved JSON-capacity thread.
The design keeps one owner for expression binding and reuses COALESCE overload resolution, exact-decimal capacity, string/collation metadata, the shared semantic AST key, and the existing window executor. The additional policy is justified by LAG/LEAD's requirement to preserve either selected value rather than truth-/date-convert it according to overload cost. No duplicate execution path, persistent state, scheduler, or cleanup owner is introduced. Unknown string bounds remain SQL-persistable LOB domains; exact decimals exceeding SQL precision 65 reject before DDL side effects.
Historical blockers are addressed: unknown JSON display size, DECIMAL/BIT scale, multibyte character-to-LOB capacity, string/Boolean/BIT preservation, TIME/date domain separation, and collation identity. I also checked the shared semantic-key consumers and the interaction with the newer main-side explicit-COLLATE/provenance handling.
Independent local evidence on this head (Go 1.27.1, darwin/arm64, verified native build):
- Native build passed.
- Focused
Test(BindLagLead|LagLead)planner tests: passed, 502 named pass events, package 1.868s. - Real SQL/frontend probe
TestReviewLagLeadAdjacent: passed all 6 subcases, package 6.094s. Covers historical source/default controls, native Unicode versus explicit binary COLLATE, JSON 309-byte preservation, BIT64/DECIMAL and BIT/Boolean, negative long TIME, emoji versus VARBINARY byte capacity, CTAS/view metadata and values, literal identity, binary prepared reuse (short/long/Unicode/NULL/short), and precision-rejection cleanup with continued connection use. The smaller preceding probe also passed; final evidence uses the complete probe's own source fingerprint. - Current-head Ubuntu UT, Linux SCA and both selected SQL BVT jobs are green. Prior detailed public BVT execution was reviewed as historical evidence, not relabeled as a new local run; main-side collation integration was independently exercised above.
Increment reviewed separately: implementation 2 files +263/-14; tests/results 6 files +1465/-9; docs 0. The type tables are lightweight; SQL cases distinguish runtime values, metadata persistence, and failures without per-permutation clusters. Existing result changes are width metadata, with values preserved.
Performance: type reconciliation is planning work. Widened mixed-type windows can add vector casts and wider result/storage representations; those are real costs of preserving values. No TP throughput benchmark or full local repository run was performed, and this approval does not claim zero runtime cost for affected windows. No blocking finding remains.
Merge Queue Status
This pull request spent 2 hours 41 minutes 5 seconds in the queue, including 50 minutes 52 seconds running CI. Required conditions to merge
|
|
Independent post-merge verification at main 4.2-dev ac27db3 still truncates the first default to "long defau" and rounds 0.5 to 1 through actual SQL. Old clean main d00ece1 independently reproduces that failure. The issue remains open for release scope and has been returned from slyunnn to ck89119, the developer immediately before QA according to assignment events. Full commands, exact rows, branch scope, successful-run evidence and environment limits: #29974 (comment) No new main production regression was demonstrated by these checks; this does not claim all-branch completion. |
What type of PR is this?
Which issue(s) this PR fixes:
issue #29974
What this PR does / why we need it:
LAG/LEAD previously narrowed the default value (3rd arg) to the value type (1st arg), truncating longer string defaults (
LAG(s,1,'long default')returnedlong defau) and rounding decimal defaults (LAG(v,1,0.5)on an INT column returned1). The narrowed result was also persisted by CTAS and exposed by views with too-narrow metadata.This PR reconciles the value argument and the default value into one common result type, mirroring MySQL's window function type aggregation:
LAG(decimal(10,4),1,-1)staysdecimal(10,4)), matching MySQL;INT + 0.5 -> decimal(11,1));Verified against MySQL 8.4.11: string widening, decimal defaults, CTAS/View metadata (
varchar(12)/decimal(11,1)), and prepared-statement default reuse all match.Two BVT result files are updated for the MySQL-consistent metadata change (an INT column with an INT64 default now yields INT64 instead of narrowing the default to INT32); a new BVT case covers the issue scenarios.