From 70406ed49f93bfb92d2361a71fe173c4384064e2 Mon Sep 17 00:00:00 2001 From: Data Magician <53010250+Datamagician10@users.noreply.github.com> Date: Wed, 4 Nov 2026 18:16:11 +0000 Subject: [PATCH 1/7] window: reject DISTINCT on STRING_AGG and IGNORE NULLS on ARRAY_AGG as analytic functions (#41) BigQuery refuses to plan STRING_AGG(DISTINCT x) OVER () and ARRAY_AGG(x IGNORE NULLS) OVER (); this engine returned a value for both. The rejection is per-function on the analytic path only: the GROUP BY aggregates keep DISTINCT and IGNORE NULLS, and other analytic aggregates (MIN, SUM, COUNT) keep DISTINCT. RESPECT NULLS emits no marker on the analytic path, so an explicit RESPECT NULLS cannot be distinguished from the default and is left alone. --- src/internal/function/fn_window.zig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/internal/function/fn_window.zig b/src/internal/function/fn_window.zig index 724ee7e..2d4004f 100644 --- a/src/internal/function/fn_window.zig +++ b/src/internal/function/fn_window.zig @@ -161,6 +161,17 @@ pub const WINDOW_ARRAY_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { + // BigQuery rejects this combination for the analytic function only: + // the GROUP BY aggregate keeps IGNORE NULLS. RESPECT NULLS emits no + // marker on the analytic path (it is the default), so an explicit + // RESPECT NULLS is indistinguishable here and is not rejected. + if (agg.opt.ignore_nulls) { + return bind.fail( + "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + .{}, + error.UnsupportedOperation, + ); + } return value_mod.arrayValue(alloc, try agg.relevantValues()); } }; @@ -294,6 +305,17 @@ pub const WINDOW_STRING_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { + // BigQuery rejects DISTINCT for the analytic function; the GROUP BY + // aggregate keeps it, and other analytic aggregates (MIN, SUM, + // COUNT) keep DISTINCT too, so this is per-function, not a blanket + // ban on DISTINCT in a window. + if (agg.opt.distinct) { + return bind.fail( + "Analytic function string_agg does not support DISTINCT.", + .{}, + error.UnsupportedOperation, + ); + } var out: std.ArrayList(u8) = .empty; var wrote = false; for (try agg.relevantValues()) |v| { From 6d2cc0e2048513449f3fc0b1a298b8272e997552 Mon Sep 17 00:00:00 2001 From: Data Magician <53010250+Datamagician10@users.noreply.github.com> Date: Wed, 4 Nov 2026 18:18:38 +0000 Subject: [PATCH 2/7] window: cover analytic option rejections in unit tests (#41) Direct done() tests: STRING_AGG with DISTINCT errors with BigQuery's message, and the same frame without DISTINCT still aggregates duplicates; ARRAY_AGG with IGNORE NULLS errors, and without it the NULL stays in the array. SQL-level coverage via the new spec cases is left to CI: no system sqlite3 here, so the window.zig TestEnv suite cannot link locally. --- src/internal/function/fn_window.zig | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/internal/function/fn_window.zig b/src/internal/function/fn_window.zig index 2d4004f..2101886 100644 --- a/src/internal/function/fn_window.zig +++ b/src/internal/function/fn_window.zig @@ -1021,6 +1021,53 @@ test "relevantValues honors ignore_nulls and distinct" { try testing.expectEqual(@as(usize, 3), (try agg.relevantValues()).len); } +test "analytic string_agg rejects DISTINCT, array_agg rejects IGNORE NULLS" { + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + // STRING_AGG(DISTINCT x) OVER (): BigQuery refuses to plan it. + var agg = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; + try agg.step(alloc, .{ .string = "a" }); + try agg.step(alloc, .{ .string = "b" }); + try agg.step(alloc, .{ .string = "a" }); + var opt = agg.opt; + opt.distinct = true; + agg.setOption(opt); + var f = WINDOW_STRING_AGG{}; + try testing.expectError(error.UnsupportedOperation, f.done(alloc, &agg)); + try testing.expectEqualStrings( + "Analytic function string_agg does not support DISTINCT.", + bind.message.?, + ); + // Without DISTINCT the same frame still aggregates, duplicates included. + opt.distinct = false; + agg.setOption(opt); + try value_mod.expectValueEqual(.{ .string = "a,b,a" }, try f.done(alloc, &agg)); + + // ARRAY_AGG(x IGNORE NULLS) OVER (): BigQuery refuses to plan it. + var agg2 = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; + try agg2.step(alloc, .{ .int = 1 }); + try agg2.step(alloc, .null); + try agg2.step(alloc, .{ .int = 2 }); + var opt2 = agg2.opt; + opt2.ignore_nulls = true; + agg2.setOption(opt2); + var g = WINDOW_ARRAY_AGG{}; + try testing.expectError(error.UnsupportedOperation, g.done(alloc, &agg2)); + try testing.expectEqualStrings( + "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + bind.message.?, + ); + // Without IGNORE NULLS the NULL stays in the array. + opt2.ignore_nulls = false; + agg2.setOption(opt2); + try value_mod.expectValueEqual( + try value_mod.arrayValue(alloc, &.{ .{ .int = 1 }, .null, .{ .int = 2 } }), + try g.done(alloc, &agg2), + ); +} + test "inverse removes the oldest matching scalar" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); From f4cbe24b875c0461aa4f3852ae0ae89823ace8db Mon Sep 17 00:00:00 2001 From: Data Magician <53010250+Datamagician10@users.noreply.github.com> Date: Wed, 4 Nov 2026 18:19:55 +0000 Subject: [PATCH 3/7] specs: add analytic option rejection cases for #41 Two error cases (STRING_AGG DISTINCT, ARRAY_AGG IGNORE NULLS over a window, with BigQuery's messages as expect_error substrings) plus controls showing both functions still work over a window without the rejected option. New cases are not in spec_baseline.txt; regenerate with --update-baseline once they pass in CI. --- .../window/analytic_option_rejections.zon | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 testdata/specs/zigquery/functions/window/analytic_option_rejections.zon diff --git a/testdata/specs/zigquery/functions/window/analytic_option_rejections.zon b/testdata/specs/zigquery/functions/window/analytic_option_rejections.zon new file mode 100644 index 0000000..8cfaf40 --- /dev/null +++ b/testdata/specs/zigquery/functions/window/analytic_option_rejections.zon @@ -0,0 +1,44 @@ +.{ + .spec = "zigquery/functions/window/analytic_option_rejections", + .dialect = .zigquery, + .typed = true, + .cases = .{ + // BigQuery refuses to plan these two combinations for the analytic + // function (measured on recidiviz-bigquery-sandbox, 2026-08-08, in + // issue #41). The GROUP BY aggregates keep both options, and other + // analytic aggregates keep DISTINCT (see window/distinct), so the + // rejection is per-function, not a blanket ban. + .{ + .desc = "string_agg with distinct over a window is rejected", + .sql = "SELECT STRING_AGG(DISTINCT x) OVER () AS s FROM UNNEST(['a', 'b', 'a']) AS x", + .expect_error = "Analytic function string_agg does not support DISTINCT.", + .rows = .{}, + }, + .{ + .desc = "array_agg with ignore nulls over a window is rejected", + .sql = "SELECT ARRAY_AGG(x IGNORE NULLS) OVER () AS a FROM UNNEST([1, 2, 3]) AS x", + .expect_error = "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + .rows = .{}, + }, + // Controls: the same functions without the rejected option still work + // over the whole partition, one row per input row. + .{ + .desc = "string_agg over a window without distinct", + .sql = "SELECT STRING_AGG(x) OVER () AS s FROM UNNEST(['a', 'b', 'a']) AS x", + .rows = .{ + .{"a,b,a"}, + .{"a,b,a"}, + .{"a,b,a"}, + }, + }, + .{ + .desc = "array_agg over a window without ignore nulls", + .sql = "SELECT ARRAY_AGG(x) OVER () AS a FROM UNNEST([1, 2, 3]) AS x", + .rows = .{ + .{.{ 1, 2, 3 }}, + .{.{ 1, 2, 3 }}, + .{.{ 1, 2, 3 }}, + }, + }, + }, +} From 67a2197dd5600f2e1ad4a8fac0f3e971dda94ed5 Mon Sep 17 00:00:00 2001 From: Datamagician10 <53010250+Datamagician10@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:24:36 -0800 Subject: [PATCH 4/7] transformer: move analytic option rejections to transform time (#41) Review feedback on PR #136: the STRING_AGG DISTINCT and ARRAY_AGG IGNORE NULLS rejections sat in each window function's runtime done(). They now live in one map in transformer/scan_analytic.zig (function -> rejected option pseudo-arg -> BigQuery message) and are checked while the analytic scan is transformed, so a rejected query fails translation with BigQuery's message and never reaches the runtime. The fn_window done() implementations keep honoring the options if a call does reach them; their unit test asserts that instead of the rejection. New transformer tests cover both rejections at translate time plus controls (plain STRING_AGG / ARRAY_AGG, ARRAY_AGG DISTINCT, SUM DISTINCT). Checks: zig build test 592/592, zig build test-server pass, spec-runner analytic_option_rejections 4/4 PASS, zig fmt --check clean. --- src/internal/function/fn_window.zig | 52 +++------ src/internal/transformer/scan_analytic.zig | 118 +++++++++++++++++++++ 2 files changed, 135 insertions(+), 35 deletions(-) diff --git a/src/internal/function/fn_window.zig b/src/internal/function/fn_window.zig index 2101886..31e2996 100644 --- a/src/internal/function/fn_window.zig +++ b/src/internal/function/fn_window.zig @@ -161,17 +161,9 @@ pub const WINDOW_ARRAY_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { - // BigQuery rejects this combination for the analytic function only: - // the GROUP BY aggregate keeps IGNORE NULLS. RESPECT NULLS emits no - // marker on the analytic path (it is the default), so an explicit - // RESPECT NULLS is indistinguishable here and is not rejected. - if (agg.opt.ignore_nulls) { - return bind.fail( - "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", - .{}, - error.UnsupportedOperation, - ); - } + // ARRAY_AGG(x IGNORE NULLS) OVER () never reaches here: the + // transformer rejects that combination at analyze time (issue #41; + // see transformer/scan_analytic.zig). return value_mod.arrayValue(alloc, try agg.relevantValues()); } }; @@ -305,17 +297,9 @@ pub const WINDOW_STRING_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { - // BigQuery rejects DISTINCT for the analytic function; the GROUP BY - // aggregate keeps it, and other analytic aggregates (MIN, SUM, - // COUNT) keep DISTINCT too, so this is per-function, not a blanket - // ban on DISTINCT in a window. - if (agg.opt.distinct) { - return bind.fail( - "Analytic function string_agg does not support DISTINCT.", - .{}, - error.UnsupportedOperation, - ); - } + // STRING_AGG(DISTINCT x) OVER () never reaches here: the transformer + // rejects that combination at analyze time (issue #41; see + // transformer/scan_analytic.zig). var out: std.ArrayList(u8) = .empty; var wrote = false; for (try agg.relevantValues()) |v| { @@ -1021,12 +1005,16 @@ test "relevantValues honors ignore_nulls and distinct" { try testing.expectEqual(@as(usize, 3), (try agg.relevantValues()).len); } -test "analytic string_agg rejects DISTINCT, array_agg rejects IGNORE NULLS" { +test "window done() honors options; the #41 rejections live in the transformer" { + // STRING_AGG(DISTINCT x) OVER () and ARRAY_AGG(x IGNORE NULLS) OVER () + // are rejected at analyze/transform time (transformer/scan_analytic.zig), + // never reaching these runtime implementations. If a call does reach + // done(), the options are honored like any other frame: DISTINCT + // dedupes and IGNORE NULLS drops the NULL. var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); - // STRING_AGG(DISTINCT x) OVER (): BigQuery refuses to plan it. var agg = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; try agg.step(alloc, .{ .string = "a" }); try agg.step(alloc, .{ .string = "b" }); @@ -1035,17 +1023,12 @@ test "analytic string_agg rejects DISTINCT, array_agg rejects IGNORE NULLS" { opt.distinct = true; agg.setOption(opt); var f = WINDOW_STRING_AGG{}; - try testing.expectError(error.UnsupportedOperation, f.done(alloc, &agg)); - try testing.expectEqualStrings( - "Analytic function string_agg does not support DISTINCT.", - bind.message.?, - ); - // Without DISTINCT the same frame still aggregates, duplicates included. + try value_mod.expectValueEqual(.{ .string = "a,b" }, try f.done(alloc, &agg)); + // Without DISTINCT the same frame aggregates duplicates too. opt.distinct = false; agg.setOption(opt); try value_mod.expectValueEqual(.{ .string = "a,b,a" }, try f.done(alloc, &agg)); - // ARRAY_AGG(x IGNORE NULLS) OVER (): BigQuery refuses to plan it. var agg2 = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; try agg2.step(alloc, .{ .int = 1 }); try agg2.step(alloc, .null); @@ -1054,10 +1037,9 @@ test "analytic string_agg rejects DISTINCT, array_agg rejects IGNORE NULLS" { opt2.ignore_nulls = true; agg2.setOption(opt2); var g = WINDOW_ARRAY_AGG{}; - try testing.expectError(error.UnsupportedOperation, g.done(alloc, &agg2)); - try testing.expectEqualStrings( - "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", - bind.message.?, + try value_mod.expectValueEqual( + try value_mod.arrayValue(alloc, &.{ .{ .int = 1 }, .{ .int = 2 } }), + try g.done(alloc, &agg2), ); // Without IGNORE NULLS the NULL stays in the array. opt2.ignore_nulls = false; diff --git a/src/internal/transformer/scan_analytic.zig b/src/internal/transformer/scan_analytic.zig index 29deff3..b9c1de0 100644 --- a/src/internal/transformer/scan_analytic.zig +++ b/src/internal/transformer/scan_analytic.zig @@ -13,10 +13,64 @@ const coordinator = @import("../coordinator/coordinator.zig"); const context = @import("../coordinator/context.zig"); const types_data = @import("../coordinator/types_data.zig"); const sqlbuilder = @import("../sqlbuilder/sqlbuilder.zig"); +const value_mod = @import("../value.zig"); const Error = types_data.Error; const SQLExpression = sqlbuilder.SQLExpression; +// ---------------------------------------------------------------------- +// Analytic option rejections (issue #41): the handful of window functions +// BigQuery refuses to plan when a given aggregate option is present. The +// map lives here, in one place, and the check runs at transform time — +// before any SQL is generated or executed — rather than inside each +// window function's runtime implementation. +// +// Options reach this layer as trailing pseudo-args on the extracted call +// (zigquery_distinct / zigquery_ignore_nulls; see extractor.zig). RESPECT +// NULLS emits no marker (it is the default), so an explicit RESPECT NULLS +// is indistinguishable here; BigQuery's own message names both options. +// The GROUP BY aggregates are a different scan and keep both options, and +// analytic aggregates not in this map (MIN, SUM, COUNT, ...) keep them +// too — the rejection is per-function, not a blanket ban. +// ---------------------------------------------------------------------- + +const AnalyticOptionRejection = struct { + /// Window runtime name of the function, e.g. "zigquery_window_string_agg". + function: []const u8, + /// Pseudo-arg encoding the rejected option, e.g. "zigquery_distinct". + option: []const u8, + /// BigQuery's error message for the combination. + message: []const u8, +}; + +const analytic_option_rejections = [_]AnalyticOptionRejection{ + .{ + .function = "zigquery_window_string_agg", + .option = "zigquery_distinct", + .message = "Analytic function string_agg does not support DISTINCT.", + }, + .{ + .function = "zigquery_window_array_agg", + .option = "zigquery_ignore_nulls", + .message = "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + }, +}; + +fn checkAnalyticOptionRejections(func_data: *const types_data.ComputedColumnData) Error!void { + if (func_data.expression.type != .function) return; + const function = func_data.expression.function orelse return; + for (analytic_option_rejections) |rejection| { + if (!std.mem.eql(u8, function.name, rejection.function)) continue; + for (function.arguments) |arg| { + if (arg.type != .function) continue; + const arg_function = arg.function orelse continue; + if (!std.mem.eql(u8, arg_function.name, rejection.option)) continue; + value_mod.setDiagnostic("{s}", .{rejection.message}); + return error.UnsupportedFunction; + } + } +} + pub fn transform( coord: *coordinator.QueryCoordinator, data: types_data.ScanData, @@ -36,6 +90,9 @@ pub fn transform( // reference inside one resolves against the input scan's columns. var transformed_functions = std.AutoHashMap(i64, *SQLExpression).init(alloc); for (analytic_data.function_list) |func_data| { + // Reject the function/option combinations BigQuery refuses to plan + // (issue #41) before transforming anything. + try checkAnalyticOptionRejections(func_data); const col = func_data.column orelse return error.InvalidAST; const expr = try coord.transformExpression(func_data.expression, ctx); try transformed_functions.put(col.id, expr); @@ -205,3 +262,64 @@ test "golden SQL: DISTINCT and IGNORE NULLS become trailing pseudo-args" { try expectContains(stmts[0], ", zigquery_ignore_nulls()) OVER ("); } } + +test "analytic option rejections fail at translate time (issue #41)" { + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + // STRING_AGG(DISTINCT x) OVER (): BigQuery refuses to plan it, so + // translation itself fails with BigQuery's message — no SQL is produced + // and nothing reaches the window runtime. + try testing.expectError(error.UnsupportedFunction, driver.translate( + alloc, + "SELECT STRING_AGG(DISTINCT x) OVER () FROM UNNEST(['a', 'b', 'a']) AS x", + .{}, + )); + try testing.expectEqualStrings( + "Analytic function string_agg does not support DISTINCT.", + value_mod.diagnostic.?, + ); + + // ARRAY_AGG(x IGNORE NULLS) OVER (): same, with its own message. + try testing.expectError(error.UnsupportedFunction, driver.translate( + alloc, + "SELECT ARRAY_AGG(x IGNORE NULLS) OVER () FROM UNNEST([1, NULL, 2]) AS x", + .{}, + )); + try testing.expectEqualStrings( + "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + value_mod.diagnostic.?, + ); +} + +test "analytic option rejection controls still translate (issue #41)" { + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + // The same two functions without the rejected option. + _ = try driver.translate( + alloc, + "SELECT STRING_AGG(x) OVER () FROM UNNEST(['a', 'b', 'a']) AS x", + .{}, + ); + _ = try driver.translate( + alloc, + "SELECT ARRAY_AGG(x) OVER () FROM UNNEST([1, NULL, 2]) AS x", + .{}, + ); + // Options not in the map for that function: ARRAY_AGG keeps DISTINCT, + // and other analytic aggregates (SUM here) keep DISTINCT too — the + // rejection is per-function, not a blanket ban on the option. + _ = try driver.translate( + alloc, + "SELECT ARRAY_AGG(DISTINCT x) OVER () FROM UNNEST([1, 1, 2]) AS x", + .{}, + ); + _ = try driver.translate( + alloc, + "SELECT SUM(DISTINCT x) OVER () FROM UNNEST([1, 1, 2]) AS x", + .{}, + ); +} From babd30b5ce23685b0a2d078d872641ddd3adf880 Mon Sep 17 00:00:00 2001 From: Datamagician10 <53010250+Datamagician10@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:52:23 -0800 Subject: [PATCH 5/7] transformer: test analytic option rejections through direct calls Follow-up on the #136 review: fn_window.zig drops out of the change entirely (the window .zon specs cover that behavior), the scan_analytic tests call checkAnalyticOptionRejections directly instead of translating whole queries, and the comments state the rule briefly per AGENTS.md. Checks: zig build test, zig build test-server, spec-runner analytic_option_rejections 4/4 PASS, zig fmt clean. --- src/internal/function/fn_window.zig | 51 --------- src/internal/transformer/scan_analytic.zig | 124 +++++++++------------ 2 files changed, 50 insertions(+), 125 deletions(-) diff --git a/src/internal/function/fn_window.zig b/src/internal/function/fn_window.zig index 31e2996..724ee7e 100644 --- a/src/internal/function/fn_window.zig +++ b/src/internal/function/fn_window.zig @@ -161,9 +161,6 @@ pub const WINDOW_ARRAY_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { - // ARRAY_AGG(x IGNORE NULLS) OVER () never reaches here: the - // transformer rejects that combination at analyze time (issue #41; - // see transformer/scan_analytic.zig). return value_mod.arrayValue(alloc, try agg.relevantValues()); } }; @@ -297,9 +294,6 @@ pub const WINDOW_STRING_AGG = struct { alloc: std.mem.Allocator, agg: *WindowFuncAggregatedStatus, ) Error!Value { - // STRING_AGG(DISTINCT x) OVER () never reaches here: the transformer - // rejects that combination at analyze time (issue #41; see - // transformer/scan_analytic.zig). var out: std.ArrayList(u8) = .empty; var wrote = false; for (try agg.relevantValues()) |v| { @@ -1005,51 +999,6 @@ test "relevantValues honors ignore_nulls and distinct" { try testing.expectEqual(@as(usize, 3), (try agg.relevantValues()).len); } -test "window done() honors options; the #41 rejections live in the transformer" { - // STRING_AGG(DISTINCT x) OVER () and ARRAY_AGG(x IGNORE NULLS) OVER () - // are rejected at analyze/transform time (transformer/scan_analytic.zig), - // never reaching these runtime implementations. If a call does reach - // done(), the options are honored like any other frame: DISTINCT - // dedupes and IGNORE NULLS drops the NULL. - var arena = std.heap.ArenaAllocator.init(testing.allocator); - defer arena.deinit(); - const alloc = arena.allocator(); - - var agg = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; - try agg.step(alloc, .{ .string = "a" }); - try agg.step(alloc, .{ .string = "b" }); - try agg.step(alloc, .{ .string = "a" }); - var opt = agg.opt; - opt.distinct = true; - agg.setOption(opt); - var f = WINDOW_STRING_AGG{}; - try value_mod.expectValueEqual(.{ .string = "a,b" }, try f.done(alloc, &agg)); - // Without DISTINCT the same frame aggregates duplicates too. - opt.distinct = false; - agg.setOption(opt); - try value_mod.expectValueEqual(.{ .string = "a,b,a" }, try f.done(alloc, &agg)); - - var agg2 = WindowFuncAggregatedStatus{ .cache_alloc = alloc }; - try agg2.step(alloc, .{ .int = 1 }); - try agg2.step(alloc, .null); - try agg2.step(alloc, .{ .int = 2 }); - var opt2 = agg2.opt; - opt2.ignore_nulls = true; - agg2.setOption(opt2); - var g = WINDOW_ARRAY_AGG{}; - try value_mod.expectValueEqual( - try value_mod.arrayValue(alloc, &.{ .{ .int = 1 }, .{ .int = 2 } }), - try g.done(alloc, &agg2), - ); - // Without IGNORE NULLS the NULL stays in the array. - opt2.ignore_nulls = false; - agg2.setOption(opt2); - try value_mod.expectValueEqual( - try value_mod.arrayValue(alloc, &.{ .{ .int = 1 }, .null, .{ .int = 2 } }), - try g.done(alloc, &agg2), - ); -} - test "inverse removes the oldest matching scalar" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); diff --git a/src/internal/transformer/scan_analytic.zig b/src/internal/transformer/scan_analytic.zig index b9c1de0..b026531 100644 --- a/src/internal/transformer/scan_analytic.zig +++ b/src/internal/transformer/scan_analytic.zig @@ -18,28 +18,15 @@ const value_mod = @import("../value.zig"); const Error = types_data.Error; const SQLExpression = sqlbuilder.SQLExpression; -// ---------------------------------------------------------------------- -// Analytic option rejections (issue #41): the handful of window functions -// BigQuery refuses to plan when a given aggregate option is present. The -// map lives here, in one place, and the check runs at transform time — -// before any SQL is generated or executed — rather than inside each -// window function's runtime implementation. -// -// Options reach this layer as trailing pseudo-args on the extracted call -// (zigquery_distinct / zigquery_ignore_nulls; see extractor.zig). RESPECT -// NULLS emits no marker (it is the default), so an explicit RESPECT NULLS -// is indistinguishable here; BigQuery's own message names both options. -// The GROUP BY aggregates are a different scan and keep both options, and -// analytic aggregates not in this map (MIN, SUM, COUNT, ...) keep them -// too — the rejection is per-function, not a blanket ban. -// ---------------------------------------------------------------------- +// Analytic calls BigQuery refuses to plan, by function and option. Options +// arrive as trailing pseudo-args (zigquery_distinct, zigquery_ignore_nulls). +// RESPECT NULLS has no pseudo-arg, so the array_agg message names it +// alongside IGNORE NULLS. Expectations: +// testdata/specs/zigquery/functions/window/analytic_option_rejections.zon const AnalyticOptionRejection = struct { - /// Window runtime name of the function, e.g. "zigquery_window_string_agg". function: []const u8, - /// Pseudo-arg encoding the rejected option, e.g. "zigquery_distinct". option: []const u8, - /// BigQuery's error message for the combination. message: []const u8, }; @@ -90,8 +77,6 @@ pub fn transform( // reference inside one resolves against the input scan's columns. var transformed_functions = std.AutoHashMap(i64, *SQLExpression).init(alloc); for (analytic_data.function_list) |func_data| { - // Reject the function/option combinations BigQuery refuses to plan - // (issue #41) before transforming anything. try checkAnalyticOptionRejections(func_data); const col = func_data.column orelse return error.InvalidAST; const expr = try coord.transformExpression(func_data.expression, ctx); @@ -263,63 +248,54 @@ test "golden SQL: DISTINCT and IGNORE NULLS become trailing pseudo-args" { } } -test "analytic option rejections fail at translate time (issue #41)" { +test "checkAnalyticOptionRejections" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); - // STRING_AGG(DISTINCT x) OVER (): BigQuery refuses to plan it, so - // translation itself fails with BigQuery's message — no SQL is produced - // and nothing reaches the window runtime. - try testing.expectError(error.UnsupportedFunction, driver.translate( - alloc, - "SELECT STRING_AGG(DISTINCT x) OVER () FROM UNNEST(['a', 'b', 'a']) AS x", - .{}, - )); - try testing.expectEqualStrings( - "Analytic function string_agg does not support DISTINCT.", - value_mod.diagnostic.?, - ); - - // ARRAY_AGG(x IGNORE NULLS) OVER (): same, with its own message. - try testing.expectError(error.UnsupportedFunction, driver.translate( - alloc, - "SELECT ARRAY_AGG(x IGNORE NULLS) OVER () FROM UNNEST([1, NULL, 2]) AS x", - .{}, - )); - try testing.expectEqualStrings( - "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", - value_mod.diagnostic.?, - ); -} - -test "analytic option rejection controls still translate (issue #41)" { - var arena = std.heap.ArenaAllocator.init(testing.allocator); - defer arena.deinit(); - const alloc = arena.allocator(); + const Case = struct { + function: []const u8, + options: []const []const u8 = &.{}, + message: ?[]const u8 = null, + }; + const cases = [_]Case{ + .{ + .function = "zigquery_window_string_agg", + .options = &.{"zigquery_distinct"}, + .message = "Analytic function string_agg does not support DISTINCT.", + }, + .{ + .function = "zigquery_window_array_agg", + .options = &.{"zigquery_ignore_nulls"}, + .message = "Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS.", + }, + .{ .function = "zigquery_window_string_agg" }, + .{ .function = "zigquery_window_array_agg", .options = &.{"zigquery_distinct"} }, + .{ .function = "zigquery_window_sum", .options = &.{"zigquery_distinct"} }, + }; + for (cases) |case| { + var args: std.ArrayList(types_data.ExpressionData) = .empty; + try args.append(alloc, .{}); + for (case.options) |option| { + try args.append(alloc, try types_data.newFunctionCallExpressionData(alloc, option, &.{})); + } + const call = try types_data.newFunctionCallExpressionData( + alloc, + case.function, + try args.toOwnedSlice(alloc), + ); + const computed = types_data.ComputedColumnData{ .expression = call }; + if (case.message) |message| { + try testing.expectError( + error.UnsupportedFunction, + checkAnalyticOptionRejections(&computed), + ); + try testing.expectEqualStrings(message, value_mod.diagnostic.?); + } else { + try checkAnalyticOptionRejections(&computed); + } + } - // The same two functions without the rejected option. - _ = try driver.translate( - alloc, - "SELECT STRING_AGG(x) OVER () FROM UNNEST(['a', 'b', 'a']) AS x", - .{}, - ); - _ = try driver.translate( - alloc, - "SELECT ARRAY_AGG(x) OVER () FROM UNNEST([1, NULL, 2]) AS x", - .{}, - ); - // Options not in the map for that function: ARRAY_AGG keeps DISTINCT, - // and other analytic aggregates (SUM here) keep DISTINCT too — the - // rejection is per-function, not a blanket ban on the option. - _ = try driver.translate( - alloc, - "SELECT ARRAY_AGG(DISTINCT x) OVER () FROM UNNEST([1, 1, 2]) AS x", - .{}, - ); - _ = try driver.translate( - alloc, - "SELECT SUM(DISTINCT x) OVER () FROM UNNEST([1, 1, 2]) AS x", - .{}, - ); + const plain = types_data.ComputedColumnData{}; + try checkAnalyticOptionRejections(&plain); } From 2009077cfd6cb910984dd79bab582d1f901a295c Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Thu, 12 Nov 2026 22:54:45 -0800 Subject: [PATCH 6/7] fmt --- src/internal/transformer/scan_analytic.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/internal/transformer/scan_analytic.zig b/src/internal/transformer/scan_analytic.zig index b026531..722c9c0 100644 --- a/src/internal/transformer/scan_analytic.zig +++ b/src/internal/transformer/scan_analytic.zig @@ -231,7 +231,10 @@ test "golden SQL: DISTINCT and IGNORE NULLS become trailing pseudo-args" { .{}, ); try expectContains(stmts[0], "zigquery_window_count(`x__"); - try expectContains(stmts[0], ", zigquery_distinct()) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)"); + try expectContains( + stmts[0], + ", zigquery_distinct()) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)", + ); } // FIRST_VALUE(x IGNORE NULLS) OVER (ORDER BY x) -> trailing From 58be80587daaa02dcdd19e1f8d655a6ce0f53eab Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Fri, 13 Nov 2026 09:25:38 -0800 Subject: [PATCH 7/7] style(transformer): wrap analytic option test line at 100 columns Co-Authored-By: Claude Fable 5.2 --- src/internal/transformer/scan_analytic.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/internal/transformer/scan_analytic.zig b/src/internal/transformer/scan_analytic.zig index 722c9c0..f9496e8 100644 --- a/src/internal/transformer/scan_analytic.zig +++ b/src/internal/transformer/scan_analytic.zig @@ -280,7 +280,8 @@ test "checkAnalyticOptionRejections" { var args: std.ArrayList(types_data.ExpressionData) = .empty; try args.append(alloc, .{}); for (case.options) |option| { - try args.append(alloc, try types_data.newFunctionCallExpressionData(alloc, option, &.{})); + const marker = try types_data.newFunctionCallExpressionData(alloc, option, &.{}); + try args.append(alloc, marker); } const call = try types_data.newFunctionCallExpressionData( alloc,