From ad07daaaf550e05e18de6cf72bb441a6be0a8e53 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 14:40:42 +0000 Subject: [PATCH 1/7] Query every tenant of a storage id with *.table `select _tenant, count(*) from customer.*.orders group by _tenant` reads every tenant of the storage id, and `*.orders` does the same for the session's storage id (in the TUI, the one being browsed). Tenants of one storage id share a schema, so one query over all of them makes sense. Every row gets a `_tenant` field with its tenant's name, usable anywhere a field is. Conditions on `_tenant` never reach the database. They pick which tenants to read from the storage id's tenant list, so `where _tenant = 'acme'` touches no other tenant. `_tenant` outside a query across tenants is refused. Planner.split/1, factored out of the GROUP BY planner, separates what each tenant reads (the filtering) from what runs over all the rows afterwards (grouping, ordering, limit, projection). Efsql.Fanout plans each tenant's read separately, because a tenant not opened since a deploy can lack a newly migrated index. The executor reads every tenant in a single FoundationDB transaction on the database, which ecto_foundationdb now supports: each tenant's async reads are started and then awaited together, so they are pipelined and see one consistent snapshot. The retry limit is 2, so a read that can't finish within the 5-second transaction limit fails with a message saying how to narrow it instead of retrying forever. At most 100 tenants are read by default, adjustable with `:max_tenants`. The TUI session also accepts tenant-qualified queries when no tenant is active. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- README.md | 27 ++++++++ lib/efsql.ex | 78 +++++++++++++++------- lib/efsql/executor.ex | 60 +++++++++++++++++ lib/efsql/fanout.ex | 75 +++++++++++++++++++++ lib/efsql/logical.ex | 9 ++- lib/efsql/parser.ex | 37 +++++++++-- lib/efsql/planner.ex | 71 ++++++++++++++------ lib/efsql/sql/ast.ex | 5 +- lib/efsql/tui/app.ex | 2 +- lib/efsql/tui/help.ex | 7 ++ lib/efsql/tui/session.ex | 33 ++++++---- src/efsql_sql_grammar.yrl | 4 ++ test/cross_tenant_test.exs | 102 +++++++++++++++++++++++++++++ test/parser_test.exs | 37 +++++++++++ test/planner_split_test.exs | 52 +++++++++++++++ test/sql/parser_test.exs | 12 ++++ test/support/efsql_test/sql_gen.ex | 16 ++++- 17 files changed, 556 insertions(+), 71 deletions(-) create mode 100644 lib/efsql/fanout.ex create mode 100644 test/cross_tenant_test.exs create mode 100644 test/planner_split_test.exs diff --git a/README.md b/README.md index 3a77621..c756053 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,33 @@ select * from admins.engineering.users; The two-part `tenant_id.table_name` form continues to use the storage ID set at startup via `--storage-id` (or the default if none was given). +### Query every tenant + +The tenants of one storage ID share a schema, so a query can read all of +them at once. Write `*` for the tenant: + +```sql +select _tenant, count(*) from customer.*.orders group by _tenant; +select id, total, _tenant from *.orders where status = 'paid' order by total desc limit 20; +select * from *.orders where _tenant like 'acme%'; +``` + +`*.table` uses the startup storage ID (in the TUI, the one you're browsing), +and `storage_id.*.table` names one. Every row gets a `_tenant` field with its +tenant's name, which you can select, filter, group and sort by like any +other. + +A condition on `_tenant` decides which tenants are read at all, so +`where _tenant = 'acme'` never touches the others. Everything else runs in +each tenant, with that tenant's own indexes, and grouping, ordering and +`LIMIT` then apply to all the rows together. + +The tenants are read in a single FoundationDB transaction, so the result is +one consistent snapshot. That also means the whole read has to finish within +FoundationDB's five-second transaction limit; a query that reads too much +says so, and a `_tenant` or `WHERE` condition narrows it. One query reads at +most 100 tenants unless the `:efsql, :max_tenants` setting says otherwise. + ### Select rows ```sql diff --git a/lib/efsql.ex b/lib/efsql.ex index 4c9e4b6..109ca7e 100644 --- a/lib/efsql.ex +++ b/lib/efsql.ex @@ -31,7 +31,23 @@ defmodule Efsql do end def qall(sql, options \\ [], tenants \\ %{}) do - {logical, tenants} = sql_to_logical(sql, tenants) + sql |> Efsql.Parser.sql_to_logical() |> run_logical(options, tenants) + end + + @doc """ + Plans and runs a translated query, resolving its tenant (or, for + `*.table`, every tenant of the storage id) through the `tenants` cache. + Returns `{plan, rows, tenants}`. + """ + def run_logical(%Efsql.Logical.Select{prefix: {:all_tenants, _}} = logical, options, tenants) do + {plan, tenants} = + logical |> Efsql.Rewrite.normalize() |> Efsql.Fanout.plan(tenants, options) + + {plan, Efsql.Executor.run(plan), tenants} + end + + def run_logical(%Efsql.Logical.Select{} = logical, options, tenants) do + {logical, tenants} = resolve_tenant(logical, tenants) plan = logical |> Efsql.Rewrite.normalize() |> Efsql.Planner.plan(options) {plan, Efsql.Executor.run(plan), tenants} end @@ -47,33 +63,51 @@ defmodule Efsql do resolve_tenant(logical, tenants) end + def resolve_tenant(%Efsql.Logical.Select{prefix: {:all_tenants, _}}, _tenants) do + raise Efsql.Exception.Unsupported, "a query across tenants can't be streamed" + end + + # Already resolved, e.g. to the TUI session's active tenant. + def resolve_tenant(%Efsql.Logical.Select{prefix: nil, tenant: tenant} = logical, tenants) + when tenant != nil, + do: {logical, tenants} + def resolve_tenant(%Efsql.Logical.Select{} = logical, tenants) do - {tenant_name, open_opts} = + {tenant_name, storage_id} = case logical.prefix do - {storage_id, tenant_name} -> {tenant_name, [storage_id: storage_id]} + {storage_id, tenant_name} -> {tenant_name, storage_id} nil -> raise "Tenant required" - tenant_name -> {tenant_name, []} + tenant_name -> {tenant_name, nil} end - cache_key = {tenant_name, open_opts[:storage_id]} - - {tenant, tenants} = - case Map.fetch(tenants, cache_key) do - {:ok, cached} -> - {cached, tenants} - - :error -> - unless EctoFoundationDB.Tenant.exists?(Efsql.Repo, tenant_name) do - raise Efsql.Exception.Unsupported, "Tenant '#{tenant_name}' does not exist" - end - - # migrate: false keeps this read-only. Tenant.open/3 already requires - # the tenant to exist (only open!/3 creates), so with the migration - # step skipped efsql never writes to the database it is exploring. - t = EctoFoundationDB.Tenant.open(Efsql.Repo, tenant_name, [migrate: false] ++ open_opts) - {t, Map.put(tenants, cache_key, t)} - end + if not Map.has_key?(tenants, {tenant_name, storage_id}) and + not EctoFoundationDB.Tenant.exists?(Efsql.Repo, tenant_name) do + raise Efsql.Exception.Unsupported, "Tenant '#{tenant_name}' does not exist" + end + {tenant, tenants} = open_tenant(tenants, tenant_name, storage_id) {%Efsql.Logical.Select{logical | tenant: tenant}, tenants} end + + @doc """ + Opens a tenant through the session's cache of open tenants, keyed by + name and storage id (nil for the Repo's own). + """ + def open_tenant(tenants, tenant_name, storage_id) do + case Map.fetch(tenants, {tenant_name, storage_id}) do + {:ok, tenant} -> + {tenant, tenants} + + :error -> + open_opts = if storage_id, do: [storage_id: storage_id], else: [] + + # migrate: false keeps this read-only. Tenant.open/3 already requires + # the tenant to exist (only open!/3 creates), so with the migration + # step skipped efsql never writes to the database it is exploring. + tenant = + EctoFoundationDB.Tenant.open(Efsql.Repo, tenant_name, [migrate: false] ++ open_opts) + + {tenant, Map.put(tenants, {tenant_name, storage_id}, tenant)} + end + end end diff --git a/lib/efsql/executor.ex b/lib/efsql/executor.ex index f93ab22..e6b4622 100644 --- a/lib/efsql/executor.ex +++ b/lib/efsql/executor.ex @@ -7,11 +7,21 @@ defmodule Efsql.Executor do false — including NOT LIKE. IS NULL matches a nil or absent field. Sorting places NULLs last ascending and first descending, matching PostgreSQL's defaults. Values compare with `Efsql.Types.compare/2`, so datetimes order chronologically. + + A query across tenants (`{:fan_out, [{tenant_name, plan}]}`) reads every + tenant in one FoundationDB transaction on the database: each tenant's + reads are started, then all awaited together, so they are pipelined and + see one snapshot. Each tenant's own operators run on its rows, which + then get `_tenant`, before the shared operators run on them all. """ + alias Efsql.Exception.Unsupported alias Efsql.Physical.Plan alias Efsql.Types + # transaction_too_old and transaction_timed_out + @too_long [1007, 1031] + @cmp_ops ~w[== > >= < <=]a @cmp_results %{ ==: [:eq], @@ -25,6 +35,9 @@ defmodule Efsql.Executor do Enum.reduce(ops, fetch(access), &apply_op/2) end + @doc "Whether `row` satisfies every one of `predicates`, with SQL NULL semantics." + def matches?(row, predicates), do: Enum.all?(predicates, &eval(&1, row)) + # -- access nodes -- defp fetch({:pk_range, query, id_start, id_end, options}) do @@ -39,6 +52,50 @@ defmodule Efsql.Executor do Efsql.Repo.all_from_source(query, options) end + defp fetch({:fan_out, []}), do: [] + + defp fetch({:fan_out, tenant_plans}) do + db = Ecto.Adapters.FoundationDB.db(Efsql.Repo) + + Ecto.Adapters.FoundationDB.transactional(db, fn tx -> + # transaction_too_old is retryable, but a read too big for one + # transaction would only fail the same way again. + :erlfdb.set_option(tx, :retry_limit, 2) + + started = + for {name, %Plan{access: access} = plan} <- tenant_plans, + do: {name, plan, start(access)} + + results = started |> Enum.flat_map(&elem(&1, 2)) |> Efsql.Repo.await() + + {rows, []} = + Enum.flat_map_reduce(started, results, fn {name, plan, futures}, results -> + {mine, rest} = Enum.split(results, length(futures)) + + rows = + plan.ops + |> Enum.reduce(List.flatten(mine), &apply_op/2) + |> Enum.map(&Map.put(&1, :_tenant, name)) + + {rows, rest} + end) + + rows + end) + rescue + e in ErlangError -> + case e.original do + {:erlfdb_error, code} when code in @too_long -> + raise Unsupported, + "the query read too much to finish in one transaction across " <> + "#{length(tenant_plans)} tenants; narrow it with a condition on _tenant " <> + "or the WHERE clause" + + _ -> + reraise e, __STACKTRACE__ + end + end + defp fetch({:union, nodes}) do nodes |> Enum.map(&async_fetch/1) @@ -46,6 +103,9 @@ defmodule Efsql.Executor do |> List.flatten() end + defp start({:union, nodes}), do: Enum.map(nodes, &async_fetch/1) + defp start(access), do: [async_fetch(access)] + defp async_fetch({:pk_range, query, id_start, id_end, options}) do Efsql.Repo.async_all_range(query, id_start, id_end, options) end diff --git a/lib/efsql/fanout.ex b/lib/efsql/fanout.ex new file mode 100644 index 0000000..53a4750 --- /dev/null +++ b/lib/efsql/fanout.ex @@ -0,0 +1,75 @@ +defmodule Efsql.Fanout do + @moduledoc """ + Plans a query across every tenant of a storage id (`*.table` or + `storage_id.*.table`). Tenants of one storage id share their schema, + which is what makes one query over all of them meaningful. + + Predicates on `_tenant` never reach the database: they pick the tenants + to read, from the storage id's tenant list. Each remaining tenant then + gets its own plan for the rest of the query, since its indexes can + differ from another's (the application migrates a tenant when it opens + it, so one not opened since a deploy may lack a new index). Grouping, + ordering, the limit and the projection run once over all the tenants' + rows; see `Efsql.Planner.split/1`. + + The plan's access node is `{:fan_out, [{tenant_name, plan}]}`, which + `Efsql.Executor` reads in one FoundationDB transaction across the + tenants, so the result is a single consistent snapshot. + """ + + alias Efsql.Exception.Unsupported + alias Efsql.Executor + alias Efsql.Logical + alias Efsql.Physical.Plan + alias Efsql.Planner + + @default_max_tenants 100 + + @doc """ + Returns `{plan, tenants}`, opening tenants through the session's + `tenants` cache (see `Efsql.open_tenant/3`). + + Options: `:max_tenants`, the most tenants one query may read (default + #{@default_max_tenants}, or the `:efsql, :max_tenants` application + setting). The rest are passed to the adapter as for any query. + """ + def plan(%Logical.Select{prefix: {:all_tenants, storage_id}} = logical, tenants, options) do + default = Application.get_env(:efsql, :max_tenants, @default_max_tenants) + {max_tenants, options} = Keyword.pop(options, :max_tenants, default) + + {tenant_preds, row_preds} = + Enum.split_with(logical.predicates, &(Logical.predicate_field(&1) == :_tenant)) + + names = + storage_id + |> list_tenants() + |> Enum.filter(&Executor.matches?(%{_tenant: &1}, tenant_preds)) + |> Enum.sort() + + if length(names) > max_tenants do + raise Unsupported, + "the query would read #{length(names)} tenants, more than the limit of " <> + "#{max_tenants}; narrow it with a condition on _tenant " <> + "(where _tenant like 'acme%')" + end + + {rows, ops} = Planner.split(%Logical.Select{logical | predicates: row_preds}) + + {per_tenant, tenants} = + Enum.map_reduce(names, tenants, fn name, tenants -> + {tenant, tenants} = Efsql.open_tenant(tenants, name, storage_id) + {{name, Planner.plan(%Logical.Select{rows | tenant: tenant}, options)}, tenants} + end) + + plan = %Plan{ + access: {:fan_out, per_tenant}, + ops: ops, + columns: Planner.columns(logical.projection) + } + + {plan, tenants} + end + + defp list_tenants(nil), do: EctoFoundationDB.Tenant.list(Efsql.Repo) + defp list_tenants(storage_id), do: Efsql.Discover.tenants(storage_id) +end diff --git a/lib/efsql/logical.ex b/lib/efsql/logical.ex index 7219823..474227e 100644 --- a/lib/efsql/logical.ex +++ b/lib/efsql/logical.ex @@ -12,7 +12,10 @@ defmodule Efsql.Logical do * `{:in, field, values}` * `{:is_null, field}` / `{:not_null, field}` - The primary key is the pseudo-field `:_`. + The primary key is the pseudo-field `:_`. A query across every tenant of + a storage id (`*.table`) also has `:_tenant`, the tenant's name, which + isn't stored: predicates on it choose the tenants to read, and the + executor adds it to each row. A grouped query (`GROUP BY`, or any aggregate) has `group_by` set to its key fields (`[]` for one group over every row) and `aggregates` to @@ -26,7 +29,9 @@ defmodule Efsql.Logical do defmodule Select do defstruct source: nil, - # tenant name, or {storage_id, tenant_name}, as written in the SQL + # tenant name, or {storage_id, tenant_name}, as written in the SQL; + # {:all_tenants, storage_id | nil} for `*.table`, every tenant of + # a storage id prefix: nil, # the opened EctoFoundationDB.Tenant, resolved before planning tenant: nil, diff --git a/lib/efsql/parser.ex b/lib/efsql/parser.ex index 6d91190..bb51419 100644 --- a/lib/efsql/parser.ex +++ b/lib/efsql/parser.ex @@ -31,13 +31,34 @@ defmodule Efsql.Parser do limit: select.limit } - if grouped?(select), - do: grouped(logical, select), - else: %Logical.Select{ - logical - | projection: projection(select.fields), - order: Enum.map(select.order_by, fn {name, dir} -> {dir, field(name)} end) - } + logical = + if grouped?(select), + do: grouped(logical, select), + else: %Logical.Select{ + logical + | projection: projection(select.fields), + order: Enum.map(select.order_by, fn {name, dir} -> {dir, field(name)} end) + } + + check_tenant_field(logical) + end + + # `_tenant` only exists in a query across tenants. + defp check_tenant_field(%Logical.Select{prefix: {:all_tenants, _}} = logical), do: logical + + defp check_tenant_field(logical) do + used = + if(is_list(logical.projection), do: logical.projection, else: []) ++ + Enum.map(logical.predicates, &Logical.predicate_field/1) ++ + Enum.map(logical.order, &elem(&1, 1)) ++ + (logical.group_by || []) ++ Enum.map(logical.aggregates, &elem(&1, 2)) + + if :_tenant in used do + raise Unsupported, + "_tenant is only available in a query across tenants (from *.table)" + end + + logical end defp projection(:star), do: :star @@ -144,6 +165,8 @@ defmodule Efsql.Parser do {name, {name, String.to_atom(function), arg}} end + defp split_from([:star, table]), do: {{:all_tenants, nil}, table} + defp split_from([storage, :star, table]), do: {{:all_tenants, storage}, table} defp split_from([table]), do: {nil, table} defp split_from([tenant, table]), do: {tenant, table} defp split_from([storage, tenant, table]), do: {{storage, tenant}, table} diff --git a/lib/efsql/planner.ex b/lib/efsql/planner.ex index 6c1c2e0..4f8cb22 100644 --- a/lib/efsql/planner.ex +++ b/lib/efsql/planner.ex @@ -32,24 +32,27 @@ defmodule Efsql.Planner do @pk_field :_ def plan(%Logical.Select{group_by: group_by} = logical, options) when is_list(group_by) do - %Logical.Select{aggregates: aggregates, order: order, limit: limit} = logical - - read = Enum.uniq(group_by ++ for({_name, _fun, f} <- aggregates, f != :star, do: f)) + {rows, ops} = split(logical) + %Plan{} = plan = plan(rows, options) + %Plan{plan | ops: plan.ops ++ ops, columns: columns(logical.projection)} + end - %Plan{} = - plan = - plan( - %Logical.Select{ - logical - | projection: if(read == [], do: :star, else: read), - order: [], - limit: nil, - group_by: nil, - aggregates: [] - }, - options - ) + def plan(%Logical.Select{} = logical, options), + do: %Plan{plan_rows(logical, options) | columns: columns(logical.projection)} + @doc """ + Splits a query into the rows to read and the operators that must then + run over all of them: the read carries only the filtering, and grouping, + ordering, the limit and the final projection come after. A grouped + query is always planned this way; a query across tenants is too, since + its order and limit apply to the tenants' rows together. + + The read never asks for `:_tenant`, which isn't stored; the executor + adds it to each row of a query across tenants. + """ + def split(%Logical.Select{group_by: group_by} = logical) when is_list(group_by) do + %Logical.Select{aggregates: aggregates, order: order, limit: limit} = logical + read = Enum.uniq(group_by ++ for({_name, _fun, f} <- aggregates, f != :star, do: f)) columns = group_by ++ Enum.map(aggregates, &elem(&1, 0)) ops = @@ -61,14 +64,40 @@ defmodule Efsql.Planner do {:project, logical.projection} ) - %Plan{plan | ops: plan.ops ++ ops, columns: columns(logical.projection)} + {rows(logical, read), ops} end - def plan(%Logical.Select{} = logical, options), - do: %Plan{plan_rows(logical, options) | columns: columns(logical.projection)} + def split(%Logical.Select{projection: projection, order: order, limit: limit} = logical) do + read = + if projection == :star, + do: :star, + else: Enum.uniq(projection ++ Enum.map(order, fn {_dir, f} -> f end)) + + ops = + [] + |> append_if(order != [], {:sort, order}) + |> append_if(limit != nil, {:limit, limit}) + |> append_if(projection != :star and read != projection, {:project, projection}) + + {rows(logical, read), ops} + end + + defp rows(logical, read) do + read = if read == :star, do: [], else: read -- [:_tenant] + + %Logical.Select{ + logical + | projection: if(read == [], do: :star, else: read), + order: [], + limit: nil, + group_by: nil, + aggregates: [] + } + end - defp columns(:star), do: nil - defp columns(fields), do: Enum.uniq(fields) + @doc "The result columns in select-list order, nil for `select *`." + def columns(:star), do: nil + def columns(fields), do: Enum.uniq(fields) defp plan_rows(%Logical.Select{} = logical, options) do %Logical.Select{predicates: preds, order: sort, projection: projection} = logical diff --git a/lib/efsql/sql/ast.ex b/lib/efsql/sql/ast.ex index 017fe8d..f14333c 100644 --- a/lib/efsql/sql/ast.ex +++ b/lib/efsql/sql/ast.ex @@ -14,7 +14,7 @@ defmodule Efsql.SQL.AST do @type t :: %__MODULE__{ fields: :star | [String.t() | Efsql.SQL.AST.aggregate()], - from: [String.t(), ...], + from: [String.t() | :star, ...], where: Efsql.SQL.AST.expr() | nil, group_by: [String.t()], order_by: [{String.t() | Efsql.SQL.AST.aggregate(), :asc | :desc}], @@ -22,7 +22,8 @@ defmodule Efsql.SQL.AST do } # `from` holds the dotted name's parts: [table], [tenant, table] or - # [storage_id, tenant, table]. + # [storage_id, tenant, table]. A tenant of `*` means every tenant: + # [:star, table] or [storage_id, :star, table]. defstruct fields: :star, from: [], where: nil, group_by: [], order_by: [], limit: nil end diff --git a/lib/efsql/tui/app.ex b/lib/efsql/tui/app.ex index 28447d0..3893d07 100644 --- a/lib/efsql/tui/app.ex +++ b/lib/efsql/tui/app.ex @@ -456,7 +456,7 @@ defmodule Efsql.Tui.App do defp run_query(model, sql) do sql = if String.ends_with?(sql, ";"), do: sql, else: sql <> ";" - session = %{tenant: model.tenant, tenants: model.tenants} + session = %{tenant: model.tenant, tenants: model.tenants, storage_id: model.storage_id} fun = fn -> started = System.monotonic_time(:millisecond) diff --git a/lib/efsql/tui/help.ex b/lib/efsql/tui/help.ex index cb77878..36ce986 100644 --- a/lib/efsql/tui/help.ex +++ b/lib/efsql/tui/help.ex @@ -77,6 +77,13 @@ defmodule Efsql.Tui.Help do "Aggregates: count(*), count(f), sum, min, max, avg.", {:note, "selected fields must be grouped; having is not supported"} ]}, + {"Every tenant", + [ + "Write * for the tenant to read every tenant of the storage id.", + {:sql, "select _tenant, count(*) from *.users group by _tenant;"}, + {:sql, "select * from *.users where _tenant like 'acme%';"}, + {:note, "a _tenant condition picks the tenants; one snapshot across them"} + ]}, {"How a query runs", [ "Constraints an index or the key can answer are pushed to", diff --git a/lib/efsql/tui/session.ex b/lib/efsql/tui/session.ex index 481aae7..1adb817 100644 --- a/lib/efsql/tui/session.ex +++ b/lib/efsql/tui/session.ex @@ -2,25 +2,30 @@ defmodule Efsql.Tui.Session do @moduledoc """ Session-aware query execution: statements without a tenant qualifier run against the session's active tenant, so `select id from users;` works - once a tenant is activated in the Navigator. Tenant-qualified statements - behave exactly as in the line CLI. + once a tenant is activated in the Navigator, and `*.users` reads every + tenant of the active storage id. Tenant-qualified statements behave + exactly as in the line CLI. """ - def qall(_sql, %{tenant: nil}) do + alias Efsql.Logical + + def qall(sql, session) do + sql + |> Efsql.Parser.sql_to_logical() + |> in_session(session) + |> Efsql.run_logical([], session.tenants) + end + + defp in_session(%Logical.Select{prefix: {:all_tenants, nil}} = logical, session), + do: %Logical.Select{logical | prefix: {:all_tenants, session[:storage_id]}} + + defp in_session(%Logical.Select{prefix: nil}, %{tenant: nil}) do raise Efsql.Exception.Unsupported, "no active tenant — qualify the table (tenant.table) or pick a tenant in the navigator" end - def qall(sql, %{tenant: tenant, tenants: tenants}) do - logical = %Efsql.Logical.Select{} = Efsql.Parser.sql_to_logical(sql) - - {logical, tenants} = - case logical.prefix do - nil -> {%{logical | tenant: tenant}, tenants} - _ -> Efsql.resolve_tenant(logical, tenants) - end + defp in_session(%Logical.Select{prefix: nil} = logical, %{tenant: tenant}), + do: %Logical.Select{logical | tenant: tenant} - plan = logical |> Efsql.Rewrite.normalize() |> Efsql.Planner.plan([]) - {plan, Efsql.Executor.run(plan), tenants} - end + defp in_session(logical, _session), do: logical end diff --git a/src/efsql_sql_grammar.yrl b/src/efsql_sql_grammar.yrl index 887b18a..8f3a558 100644 --- a/src/efsql_sql_grammar.yrl +++ b/src/efsql_sql_grammar.yrl @@ -62,6 +62,10 @@ table -> name : ['$1']. table -> name '.' name : ['$1', '$3']. table -> name '.' name '.' name : ['$1', '$3', '$5']. +%% Every tenant: `*.table`, or `storage_id.*.table`. +table -> '*' '.' name : [star, '$3']. +table -> name '.' '*' '.' name : ['$1', star, '$5']. + where_clause -> '$empty' : nil. where_clause -> 'where' expr : '$2'. diff --git a/test/cross_tenant_test.exs b/test/cross_tenant_test.exs new file mode 100644 index 0000000..91a4dde --- /dev/null +++ b/test/cross_tenant_test.exs @@ -0,0 +1,102 @@ +defmodule EfsqlTest.Integration.CrossTenant do + use EfsqlTest.Case, async: true + + alias Efsql.Exception.Unsupported + alias Efsql.Physical.Plan + alias EctoFoundationDB.Sandbox + alias EfsqlTest.Repo + alias EfsqlTest.User + + # The case's tenant holds Alice, Bob and Charles; this adds a second with + # Dora and Eve. Other tests' tenants share the storage id, so every query + # names its two tenants with a condition on _tenant. + setup context do + other = "t_" <> String.replace(Ecto.UUID.generate(), "-", "") + tenant = Sandbox.checkout(Repo, other, []) + + Repo.transaction( + fn -> + Repo.insert(%User{id: "0004", name: "Dora", notes: "foobar"}) + Repo.insert(%User{id: "0005", name: "Eve"}) + end, + prefix: tenant + ) + + on_exit(fn -> Sandbox.checkin(Repo, other) end) + + both = "_tenant in ('#{context[:tenant_id]}', '#{other}')" + {:ok, other: other, both: both} + end + + test "one row per tenant with GROUP BY _tenant", context do + rows = + Efsql.all( + "select _tenant, count(*) as n from *.users where #{context[:both]} group by _tenant;" + ) + + assert Enum.sort(rows) == + Enum.sort([%{_tenant: context[:tenant_id], n: 3}, %{_tenant: context[:other], n: 2}]) + end + + test "ORDER BY and LIMIT apply to all the tenants' rows together", context do + assert [%{name: "Eve"}, %{name: "Dora"}, %{name: "Charles"}] = + Efsql.all( + "select name, _tenant from *.users where #{context[:both]} " <> + "order by name desc limit 3;" + ) + end + + test "every row says which tenant it came from", context do + rows = Efsql.all("select * from *.users where #{context[:both]};") + + assert rows |> Enum.map(& &1._tenant) |> Enum.frequencies() == %{ + context[:tenant_id] => 3, + context[:other] => 2 + } + end + + test "an aggregate over every tenant", context do + assert [%{n: 5, "max(name)": "Eve"}] = + Efsql.all("select count(*) as n, max(name) from *.users where #{context[:both]};") + end + + test "WHERE applies within each tenant", context do + rows = + Efsql.all("select _tenant, name from *.users where #{context[:both]} and notes = 'foobar';") + + assert Enum.sort_by(rows, & &1.name) == [ + %{_tenant: context[:tenant_id], name: "Bob"}, + %{_tenant: context[:other], name: "Dora"} + ] + end + + test "a condition on _tenant chooses the tenants to read", context do + {%Plan{access: {:fan_out, per_tenant}}, rows, _tenants} = + Efsql.qall("select name from *.users where _tenant = '#{context[:other]}';") + + assert [{other, _plan}] = per_tenant + assert other == context[:other] + assert Enum.sort_by(rows, & &1.name) == [%{name: "Dora"}, %{name: "Eve"}] + + assert {%Plan{access: {:fan_out, []}}, [], _} = + Efsql.qall("select name from *.users where _tenant = 'no-such-tenant';") + end + + test "each tenant keeps its index pushdown", context do + {%Plan{access: {:fan_out, per_tenant}, ops: ops}, rows, _tenants} = + Efsql.qall("select _tenant, name from *.users where #{context[:both]} and name = 'Dora';") + + for {_tenant, %Plan{access: access}} <- per_tenant do + assert {:index_scan, %Ecto.Query{wheres: [_]}, _opts} = access + end + + assert ops == [] + assert rows == [%{_tenant: context[:other], name: "Dora"}] + end + + test "more tenants than the limit is refused", context do + assert_raise Unsupported, ~r/would read 2 tenants, more than the limit of 1/, fn -> + Efsql.all("select name from *.users where #{context[:both]};", max_tenants: 1) + end + end +end diff --git a/test/parser_test.exs b/test/parser_test.exs index 10db5e9..88af595 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -47,6 +47,43 @@ defmodule Efsql.ParserTest do end end + describe "every tenant" do + test "* for the tenant reads every tenant of a storage id" do + assert %Logical.Select{prefix: {:all_tenants, nil}, source: "users"} = + parse("select id from *.users;") + + assert %Logical.Select{prefix: {:all_tenants, "customer"}, source: "users"} = + parse("select id from customer.*.users;") + end + + test "_tenant is a field of a query across tenants" do + assert %Logical.Select{ + projection: [:_tenant, :"count(*)"], + predicates: [{:in, :_tenant, ["a", "b"]}], + group_by: [:_tenant], + order: [asc: :_tenant] + } = + parse( + "select _tenant, count(*) from *.users where _tenant in ('a', 'b') " <> + "group by _tenant order by _tenant;" + ) + end + + test "_tenant anywhere else is unsupported" do + for sql <- [ + "select _tenant from t.users;", + "select id from t.users where _tenant = 'a';", + "select id from t.users order by _tenant;", + "select count(*) from t.users group by _tenant;", + "select count(_tenant) from t.users;" + ] do + assert_raise Unsupported, ~r/_tenant is only available in a query across tenants/, fn -> + parse(sql) + end + end + end + end + describe "where" do test "equality" do assert %Logical.Select{predicates: [{:cmp, :==, :name, "Alice"}]} = diff --git a/test/planner_split_test.exs b/test/planner_split_test.exs new file mode 100644 index 0000000..a92fbfc --- /dev/null +++ b/test/planner_split_test.exs @@ -0,0 +1,52 @@ +defmodule Efsql.PlannerSplitTest do + use ExUnit.Case, async: true + + alias Efsql.Executor + alias Efsql.Logical + alias Efsql.Planner + + defp split(sql), do: sql |> Efsql.Parser.sql_to_logical() |> Planner.split() + + test "select *: read everything, order and limit afterwards" do + assert {%Logical.Select{projection: :star, order: [], limit: nil}, ops} = + split("select * from *.users order by name desc limit 5;") + + assert ops == [{:sort, [desc: :name]}, {:limit, 5}] + end + + test "fields to order by are read too, then projected away" do + assert {%Logical.Select{projection: [:id, :name]}, ops} = + split("select id from *.users order by name;") + + assert ops == [{:sort, [asc: :name]}, {:project, [:id]}] + end + + test "a read never asks for _tenant" do + assert {%Logical.Select{projection: [:id]}, []} = + split("select _tenant, id from *.users;") + + assert {%Logical.Select{projection: :star}, []} = split("select _tenant from *.users;") + end + + test "a grouped query reads its group and aggregate fields, then aggregates" do + assert {%Logical.Select{projection: [:name], group_by: nil, aggregates: []}, ops} = + split("select _tenant, count(name) as n from *.users group by _tenant order by n;") + + assert ops == [ + {:aggregate, [:_tenant], [{:n, :count, :name}]}, + {:sort, [asc: :n]} + ] + end + + test "predicates stay with the read" do + assert {%Logical.Select{predicates: [{:cmp, :==, :name, "Alice"}]}, _ops} = + split("select id from *.users where name = 'Alice';") + end + + test "tenant names are chosen with the usual predicate semantics" do + preds = [{:like, :_tenant, "acme%"}] + assert Executor.matches?(%{_tenant: "acme-eu"}, preds) + refute Executor.matches?(%{_tenant: "globex"}, preds) + assert Executor.matches?(%{_tenant: "anything"}, []) + end +end diff --git a/test/sql/parser_test.exs b/test/sql/parser_test.exs index 505b666..17eaf2c 100644 --- a/test/sql/parser_test.exs +++ b/test/sql/parser_test.exs @@ -74,6 +74,18 @@ defmodule Efsql.SQL.ParserTest do assert %Select{from: ["user", "date"]} = parse("select * from user.date") end + test "* for the tenant means every tenant" do + assert %Select{from: [:star, "t"]} = parse("select * from *.t") + assert %Select{from: ["storage", :star, "t"]} = parse("select a from storage . * . t") + end + + test "* only stands for a tenant" do + assert reason("select * from *") == "expected '.', got the end of the statement" + assert reason("select * from t.*") == "expected '.', got the end of the statement" + assert reason("select * from *.*.t") == "expected a name, got '*'" + assert reason("select * from s.t.*") == "expected a name, got '*'" + end + test "four parts is too many" do assert reason("select * from a.b.c.d") =~ "at most three parts" end diff --git a/test/support/efsql_test/sql_gen.ex b/test/support/efsql_test/sql_gen.ex index 96708f9..8b119b5 100644 --- a/test/support/efsql_test/sql_gen.ex +++ b/test/support/efsql_test/sql_gen.ex @@ -20,7 +20,7 @@ defmodule EfsqlTest.SQLGen do def select() do %Select{ fields: if(one_in(4), do: :star, else: list(1..4, &field/0)), - from: list(1..3, &name/0), + from: from(), where: maybe(fn -> expr(3) end), group_by: if(one_in(3), do: list(1..3, &name/0), else: []), order_by: list(0..3, fn -> {order_key(), Enum.random([:asc, :desc])} end), @@ -30,6 +30,15 @@ defmodule EfsqlTest.SQLGen do # -- trees -- + # A table name, sometimes across every tenant: *.t or s.*.t. + defp from() do + case Enum.random(1..5) do + 1 -> [:star, name()] + 2 -> [name(), :star, name()] + _ -> list(1..3, &name/0) + end + end + # Function names must lex as plain words that aren't keywords. @functions ~w[count sum min max avg lower] @@ -101,7 +110,7 @@ defmodule EfsqlTest.SQLGen do kw("select"), fields(select.fields), kw("from"), - Enum.intersperse(Enum.map(select.from, &print_name/1), p(".")), + Enum.intersperse(Enum.map(select.from, &print_from_part/1), p(".")), if(select.where, do: [kw("where"), print(select.where)], else: []), group_by(select.group_by), order_by(select.order_by), @@ -125,6 +134,9 @@ defmodule EfsqlTest.SQLGen do defp print_aggregate({:aggregate, function, arg, _alias}), do: [kw(function), p("("), if(arg == :star, do: p("*"), else: print_name(arg)), p(")")] + defp print_from_part(:star), do: p("*") + defp print_from_part(name), do: print_name(name) + defp group_by([]), do: [] defp group_by(names), do: [kw("group"), kw("by"), comma(Enum.map(names, &print_name/1))] From 59f520874faa24c7d13cf7e30006326383f24e35 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 14:42:15 +0000 Subject: [PATCH 2/7] Fail cross-tenant reads at once on transaction_too_old transaction_too_old is retryable, so erlfdb's transactional loop retries it. A read too big for one transaction would run again, just as slowly, and fail the same way. The retry limit of 2 still meant three full reads before the error, and it also capped retries for genuinely transient errors. The reads now run in a function that rescues transaction_too_old (and transaction_timed_out) inside the transaction and re-raises it as Unsupported. erlfdb's loop only retries erlfdb errors, so the query fails on its first attempt, and other errors keep FoundationDB's normal retries. The retry limit is gone. A new test opens a transaction with an ancient read version, which forces transaction_too_old, runs a cross-tenant query inside it, and checks that the transaction function ran only once. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- lib/efsql/executor.ex | 56 ++++++++++++++++++++------------------ test/cross_tenant_test.exs | 26 ++++++++++++++++++ 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/lib/efsql/executor.ex b/lib/efsql/executor.ex index e6b4622..4bc61fa 100644 --- a/lib/efsql/executor.ex +++ b/lib/efsql/executor.ex @@ -19,7 +19,7 @@ defmodule Efsql.Executor do alias Efsql.Physical.Plan alias Efsql.Types - # transaction_too_old and transaction_timed_out + # transaction_too_old (retryable) and transaction_timed_out @too_long [1007, 1031] @cmp_ops ~w[== > >= < <=]a @@ -56,32 +56,41 @@ defmodule Efsql.Executor do defp fetch({:fan_out, tenant_plans}) do db = Ecto.Adapters.FoundationDB.db(Efsql.Repo) + Ecto.Adapters.FoundationDB.transactional(db, fn -> read_tenants(tenant_plans) end) + end - Ecto.Adapters.FoundationDB.transactional(db, fn tx -> - # transaction_too_old is retryable, but a read too big for one - # transaction would only fail the same way again. - :erlfdb.set_option(tx, :retry_limit, 2) + defp fetch({:union, nodes}) do + nodes + |> Enum.map(&async_fetch/1) + |> Efsql.Repo.await() + |> List.flatten() + end - started = - for {name, %Plan{access: access} = plan} <- tenant_plans, - do: {name, plan, start(access)} + # Runs inside the transaction. A read too big for one transaction fails + # with transaction_too_old, which erlfdb's transactional loop would retry, + # forever and each time as slowly. So that error is turned into one the + # loop doesn't catch (it only retries erlfdb errors) and the query fails + # at once; any other error still retries as usual. + defp read_tenants(tenant_plans) do + started = + for {name, %Plan{access: access} = plan} <- tenant_plans, + do: {name, plan, start(access)} - results = started |> Enum.flat_map(&elem(&1, 2)) |> Efsql.Repo.await() + results = started |> Enum.flat_map(&elem(&1, 2)) |> Efsql.Repo.await() - {rows, []} = - Enum.flat_map_reduce(started, results, fn {name, plan, futures}, results -> - {mine, rest} = Enum.split(results, length(futures)) + {rows, []} = + Enum.flat_map_reduce(started, results, fn {name, plan, futures}, results -> + {mine, rest} = Enum.split(results, length(futures)) - rows = - plan.ops - |> Enum.reduce(List.flatten(mine), &apply_op/2) - |> Enum.map(&Map.put(&1, :_tenant, name)) + rows = + plan.ops + |> Enum.reduce(List.flatten(mine), &apply_op/2) + |> Enum.map(&Map.put(&1, :_tenant, name)) - {rows, rest} - end) + {rows, rest} + end) - rows - end) + rows rescue e in ErlangError -> case e.original do @@ -96,13 +105,6 @@ defmodule Efsql.Executor do end end - defp fetch({:union, nodes}) do - nodes - |> Enum.map(&async_fetch/1) - |> Efsql.Repo.await() - |> List.flatten() - end - defp start({:union, nodes}), do: Enum.map(nodes, &async_fetch/1) defp start(access), do: [async_fetch(access)] diff --git a/test/cross_tenant_test.exs b/test/cross_tenant_test.exs index 91a4dde..2a54214 100644 --- a/test/cross_tenant_test.exs +++ b/test/cross_tenant_test.exs @@ -99,4 +99,30 @@ defmodule EfsqlTest.Integration.CrossTenant do Efsql.all("select name from *.users where #{context[:both]};", max_tenants: 1) end end + + test "a read too old for its transaction fails at once instead of retrying", context do + sql = "select name from *.users where #{context[:both]};" + + # Open the tenants first, so that only the reads run in the transaction below. + {_plan, _rows, tenants} = Efsql.qall(sql) + + db = Ecto.Adapters.FoundationDB.db(Efsql.Repo) + attempts = :counters.new(1, []) + + # A read version far older than FoundationDB keeps makes every read fail + # with transaction_too_old, which erlfdb would retry. The query joins this + # transaction; if the error reached the retry loop, this function would + # run again (and again). + assert_raise Unsupported, + ~r/read too much to finish in one transaction across 2 tenants/, + fn -> + Ecto.Adapters.FoundationDB.transactional(db, fn tx -> + :counters.add(attempts, 1, 1) + :erlfdb.set_read_version(tx, 1) + Efsql.qall(sql, [], tenants) + end) + end + + assert :counters.get(attempts, 1) == 1 + end end From 151a49787d4443d7eb14c26e72b2d0ec35be4078 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:29:24 +0000 Subject: [PATCH 3/7] Read tenants in batches with tenant_batch A query across tenants reads them all in one FoundationDB transaction, which has to finish within five seconds. With `\set tenant_batch 25` in the CLI or TUI, or `tenant_batch: 25` for Efsql.qall/3, the tenants are read 25 at a time, one transaction per batch, and the tenant limit no longer applies. The plan's access node becomes {:batches, [fan_out]}, and the executor runs the batches in turn. Grouping, ordering and the limit still apply to all the rows together. Batching is opt-in because the result is no longer one snapshot: each batch sees the database at a different moment. The CLI and TUI footers say how many transactions a result came from (Fanout.transactions/1). The too-many-tenants and too-big-for-one-transaction errors now point to the setting. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- README.md | 18 +++++++++++++-- lib/efsql/cli.ex | 30 ++++++++++++++++++++++-- lib/efsql/executor.ex | 6 ++++- lib/efsql/fanout.ex | 47 ++++++++++++++++++++++++++++++++------ lib/efsql/tui/app.ex | 29 ++++++++++++++++++++++- lib/efsql/tui/help.ex | 1 + lib/efsql/tui/session.ex | 5 +++- lib/efsql/tui/view.ex | 14 ++++++++++-- test/cross_tenant_test.exs | 23 +++++++++++++++++++ test/tui_app_test.exs | 29 +++++++++++++++++++++++ 10 files changed, 186 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c756053..237da5d 100644 --- a/README.md +++ b/README.md @@ -209,8 +209,22 @@ each tenant, with that tenant's own indexes, and grouping, ordering and The tenants are read in a single FoundationDB transaction, so the result is one consistent snapshot. That also means the whole read has to finish within FoundationDB's five-second transaction limit; a query that reads too much -says so, and a `_tenant` or `WHERE` condition narrows it. One query reads at -most 100 tenants unless the `:efsql, :max_tenants` setting says otherwise. +says so, and a `_tenant` or `WHERE` condition narrows it. One transaction +reads at most 100 tenants unless the `:efsql, :max_tenants` setting says +otherwise. + +When the tenants don't fit in one transaction, read them in batches: + +``` +\set tenant_batch 25 +``` + +Each batch of 25 tenants is then its own transaction, and there is no +tenant limit. Grouping, ordering and `LIMIT` still apply to all the rows +together, but the result is no longer one snapshot: each batch sees the +database at a slightly different moment, and the result says how many +transactions it came from. `\set tenant_batch off` goes back to one +transaction. (From Elixir, pass `tenant_batch: 25` to `Efsql.qall/3`.) ### Select rows diff --git a/lib/efsql/cli.ex b/lib/efsql/cli.ex index a3495b4..89617ea 100644 --- a/lib/efsql/cli.ex +++ b/lib/efsql/cli.ex @@ -1,5 +1,5 @@ defmodule Efsql.Cli do - defstruct args: [], history: [], debug: false, tenants: %{}, limit: 15 + defstruct args: [], history: [], debug: false, tenants: %{}, limit: 15, tenant_batch: nil use GenServer @@ -93,6 +93,8 @@ defmodule Efsql.Cli do Meta-commands: \\tenants [storage_id] list tenants (optionally for a specific storage id) \\set limit N set the default row limit (currently #{state.limit}) + \\set tenant_batch N|off read *.table queries N tenants per transaction + (currently #{state.tenant_batch || "off"}) \\? show this help """, :light_black @@ -150,6 +152,22 @@ defmodule Efsql.Cli do end end + defp handle_input("\\set tenant_batch " <> rest, state = %__MODULE__{}) do + case {String.trim(rest), Integer.parse(String.trim(rest))} do + {"off", _} -> + Owl.IO.puts(Owl.Data.tag("tenant_batch off: one transaction", :light_black)) + %__MODULE__{state | tenant_batch: nil} + + {_, {n, ""}} when n > 0 -> + Owl.IO.puts(Owl.Data.tag("tenant_batch set to #{n}", :light_black)) + %__MODULE__{state | tenant_batch: n} + + _ -> + print_error("Usage: \\set tenant_batch | off") + state + end + end + defp handle_input(data, state = %__MODULE__{}) do limit_sql = "limit #{state.limit + 1}" @@ -160,9 +178,11 @@ defmodule Efsql.Cli do do: {data, :all}, else: {String.replace(data, ~r/;\s*$/, " #{limit_sql};"), state.limit} - {call, rows, tenants} = Efsql.qall(sql, [], state.tenants) + options = if state.tenant_batch, do: [tenant_batch: state.tenant_batch], else: [] + {call, rows, tenants} = Efsql.qall(sql, options, state.tenants) if state.debug, do: print_debug(call) print_table(rows, display_limit, call.columns) + print_snapshots(Efsql.Fanout.transactions(call)) {tenants} rescue e -> @@ -257,6 +277,12 @@ defmodule Efsql.Cli do Owl.IO.puts(Owl.Data.tag(label, :light_black)) end + defp print_snapshots(1), do: :ok + + defp print_snapshots(n) do + Owl.IO.puts(Owl.Data.tag("(read in #{n} transactions, not one snapshot)", :yellow)) + end + defp column_sorter(nil), do: :asc defp column_sorter(columns) do diff --git a/lib/efsql/executor.ex b/lib/efsql/executor.ex index 4bc61fa..22d0a67 100644 --- a/lib/efsql/executor.ex +++ b/lib/efsql/executor.ex @@ -52,6 +52,9 @@ defmodule Efsql.Executor do Efsql.Repo.all_from_source(query, options) end + # One transaction per batch, one after another. + defp fetch({:batches, batches}), do: Enum.flat_map(batches, &fetch/1) + defp fetch({:fan_out, []}), do: [] defp fetch({:fan_out, tenant_plans}) do @@ -98,7 +101,8 @@ defmodule Efsql.Executor do raise Unsupported, "the query read too much to finish in one transaction across " <> "#{length(tenant_plans)} tenants; narrow it with a condition on _tenant " <> - "or the WHERE clause" + "or the WHERE clause, or read fewer tenants per transaction " <> + "(\\set tenant_batch N)" _ -> reraise e, __STACKTRACE__ diff --git a/lib/efsql/fanout.ex b/lib/efsql/fanout.ex index 53a4750..d0de6df 100644 --- a/lib/efsql/fanout.ex +++ b/lib/efsql/fanout.ex @@ -15,6 +15,14 @@ defmodule Efsql.Fanout do The plan's access node is `{:fan_out, [{tenant_name, plan}]}`, which `Efsql.Executor` reads in one FoundationDB transaction across the tenants, so the result is a single consistent snapshot. + + One transaction has to finish within FoundationDB's five seconds, so a + query over many or large tenants may not fit. With `:tenant_batch` set, + the tenants are read in batches of that many, one transaction each: + `{:batches, [{:fan_out, ...}]}`. Each batch is a snapshot of its own, + so the result as a whole is not one; `transactions/1` says how many + there were, for the result to say so. Grouping, ordering and the limit + still apply to all the rows together. """ alias Efsql.Exception.Unsupported @@ -29,13 +37,25 @@ defmodule Efsql.Fanout do Returns `{plan, tenants}`, opening tenants through the session's `tenants` cache (see `Efsql.open_tenant/3`). - Options: `:max_tenants`, the most tenants one query may read (default - #{@default_max_tenants}, or the `:efsql, :max_tenants` application - setting). The rest are passed to the adapter as for any query. + Options: + + * `:tenant_batch` - read the tenants in batches of this many, one + transaction each, instead of all in one. Default nil (one). + * `:max_tenants` - the most tenants one transaction may read (default + #{@default_max_tenants}, or the `:efsql, :max_tenants` application + setting). It doesn't apply with `:tenant_batch`, which bounds each + transaction itself. + + The rest are passed to the adapter as for any query. """ def plan(%Logical.Select{prefix: {:all_tenants, storage_id}} = logical, tenants, options) do default = Application.get_env(:efsql, :max_tenants, @default_max_tenants) {max_tenants, options} = Keyword.pop(options, :max_tenants, default) + {batch, options} = Keyword.pop(options, :tenant_batch) + + unless batch == nil or (is_integer(batch) and batch > 0) do + raise ArgumentError, "tenant_batch must be a positive integer, got #{inspect(batch)}" + end {tenant_preds, row_preds} = Enum.split_with(logical.predicates, &(Logical.predicate_field(&1) == :_tenant)) @@ -46,11 +66,12 @@ defmodule Efsql.Fanout do |> Enum.filter(&Executor.matches?(%{_tenant: &1}, tenant_preds)) |> Enum.sort() - if length(names) > max_tenants do + if batch == nil and length(names) > max_tenants do raise Unsupported, "the query would read #{length(names)} tenants, more than the limit of " <> - "#{max_tenants}; narrow it with a condition on _tenant " <> - "(where _tenant like 'acme%')" + "#{max_tenants} in one transaction; narrow it with a condition on _tenant " <> + "(where _tenant like 'acme%'), or read them in batches " <> + "(\\set tenant_batch 25), giving up the single snapshot" end {rows, ops} = Planner.split(%Logical.Select{logical | predicates: row_preds}) @@ -61,8 +82,13 @@ defmodule Efsql.Fanout do {{name, Planner.plan(%Logical.Select{rows | tenant: tenant}, options)}, tenants} end) + access = + if batch, + do: {:batches, per_tenant |> Enum.chunk_every(batch) |> Enum.map(&{:fan_out, &1})}, + else: {:fan_out, per_tenant} + plan = %Plan{ - access: {:fan_out, per_tenant}, + access: access, ops: ops, columns: Planner.columns(logical.projection) } @@ -70,6 +96,13 @@ defmodule Efsql.Fanout do {plan, tenants} end + @doc """ + How many transactions, so snapshots, a plan's result comes from: more + than one only for a query across tenants read in batches. + """ + def transactions(%Plan{access: {:batches, batches}}), do: length(batches) + def transactions(%Plan{}), do: 1 + defp list_tenants(nil), do: EctoFoundationDB.Tenant.list(Efsql.Repo) defp list_tenants(storage_id), do: Efsql.Discover.tenants(storage_id) end diff --git a/lib/efsql/tui/app.ex b/lib/efsql/tui/app.ex index 3893d07..42b1f95 100644 --- a/lib/efsql/tui/app.ex +++ b/lib/efsql/tui/app.ex @@ -27,6 +27,8 @@ defmodule Efsql.Tui.App do flash: nil, busy: nil, limit: 15, + # tenants per transaction for *.table queries; nil is one transaction + tenant_batch: nil, cluster_file: nil, # navigator nav_path: [], @@ -437,6 +439,9 @@ defmodule Efsql.Tui.App do "\\set limit " <> n -> set_limit(model, n) + "\\set tenant_batch " <> n -> + set_tenant_batch(model, n) + sql -> run_query(model, sql) end @@ -454,9 +459,31 @@ defmodule Efsql.Tui.App do end end + defp set_tenant_batch(model, n) do + case {String.trim(n), Integer.parse(String.trim(n))} do + {"off", _} -> + {%{model | tenant_batch: nil, input: "", qcursor: 0, flash: {:info, "tenant_batch off"}}, + []} + + {_, {n, ""}} when n > 0 -> + flash = {:info, "tenant_batch set to #{n}"} + {%{model | tenant_batch: n, input: "", qcursor: 0, flash: flash}, []} + + _ -> + flash = {:error, "usage: \\set tenant_batch N|off"} + {%{model | flash: flash, input: "", qcursor: 0}, []} + end + end + defp run_query(model, sql) do sql = if String.ends_with?(sql, ";"), do: sql, else: sql <> ";" - session = %{tenant: model.tenant, tenants: model.tenants, storage_id: model.storage_id} + + session = %{ + tenant: model.tenant, + tenants: model.tenants, + storage_id: model.storage_id, + tenant_batch: model.tenant_batch + } fun = fn -> started = System.monotonic_time(:millisecond) diff --git a/lib/efsql/tui/help.ex b/lib/efsql/tui/help.ex index 36ce986..505479c 100644 --- a/lib/efsql/tui/help.ex +++ b/lib/efsql/tui/help.ex @@ -96,6 +96,7 @@ defmodule Efsql.Tui.Help do [ {:sql, "\\? this help \\plan toggle plan"}, {:sql, "\\set limit N default row limit"}, + {:sql, "\\set tenant_batch N|off tenants per transaction for *.table"}, "? help t tenants q query editor", "r resample Enter open/run Esc back", "Tab complete, or with empty input jump to results", diff --git a/lib/efsql/tui/session.ex b/lib/efsql/tui/session.ex index 1adb817..95e0960 100644 --- a/lib/efsql/tui/session.ex +++ b/lib/efsql/tui/session.ex @@ -13,9 +13,12 @@ defmodule Efsql.Tui.Session do sql |> Efsql.Parser.sql_to_logical() |> in_session(session) - |> Efsql.run_logical([], session.tenants) + |> Efsql.run_logical(options(session), session.tenants) end + defp options(%{tenant_batch: n}) when is_integer(n), do: [tenant_batch: n] + defp options(_session), do: [] + defp in_session(%Logical.Select{prefix: {:all_tenants, nil}} = logical, session), do: %Logical.Select{logical | prefix: {:all_tenants, session[:storage_id]}} diff --git a/lib/efsql/tui/view.ex b/lib/efsql/tui/view.ex index 6f64923..2482408 100644 --- a/lib/efsql/tui/view.ex +++ b/lib/efsql/tui/view.ex @@ -292,7 +292,7 @@ defmodule Efsql.Tui.View do end defp results_lines(%Model{rows: []} = model, _height, _cols) do - [[{:dim, " (0 rows, #{model.elapsed_ms} ms)"}]] + [[{:dim, " (0 rows, #{model.elapsed_ms} ms)" <> snapshots_note(model)}]] end defp results_lines(model, height, cols) do @@ -318,12 +318,22 @@ defmodule Efsql.Tui.View do [ {:dim, " (#{count} rows, #{model.elapsed_ms} ms)" <> - hidden_note(model, column_map) <> browse_hint(model, count)} + snapshots_note(model) <> hidden_note(model, column_map) <> browse_hint(model, count)} ] [header] ++ rows ++ [footer] end + # A query across tenants read in batches is not one snapshot; say so. + defp snapshots_note(%Model{plan: %Efsql.Physical.Plan{} = plan}) do + case Efsql.Fanout.transactions(plan) do + 1 -> "" + n -> " · #{n} transactions, not one snapshot" + end + end + + defp snapshots_note(_model), do: "" + defp header_of(_model, :split), do: "…" defp header_of(model, {:col, ix}), do: model.columns |> Enum.at(ix) |> to_string() diff --git a/test/cross_tenant_test.exs b/test/cross_tenant_test.exs index 2a54214..f706a83 100644 --- a/test/cross_tenant_test.exs +++ b/test/cross_tenant_test.exs @@ -100,6 +100,29 @@ defmodule EfsqlTest.Integration.CrossTenant do end end + test "tenants can be read in batches, one transaction each", context do + sql = + "select _tenant, count(*) as n, max(name) as last from *.users " <> + "where #{context[:both]} group by _tenant order by last desc;" + + # One tenant per batch, and no tenant limit: each transaction reads one. + {%Plan{access: {:batches, batches}} = plan, rows, _tenants} = + Efsql.qall(sql, tenant_batch: 1, max_tenants: 1) + + assert [{:fan_out, [_]}, {:fan_out, [_]}] = batches + assert Efsql.Fanout.transactions(plan) == 2 + + # grouping and ordering still see every tenant's rows together + assert rows == [ + %{_tenant: context[:other], n: 2, last: "Eve"}, + %{_tenant: context[:tenant_id], n: 3, last: "Charles"} + ] + + # the same result as one transaction + {plan, ^rows, _tenants} = Efsql.qall(sql) + assert Efsql.Fanout.transactions(plan) == 1 + end + test "a read too old for its transaction fails at once instead of retrying", context do sql = "select name from *.users where #{context[:both]};" diff --git a/test/tui_app_test.exs b/test/tui_app_test.exs index 168dc33..9355eb1 100644 --- a/test/tui_app_test.exs +++ b/test/tui_app_test.exs @@ -202,6 +202,35 @@ defmodule Efsql.Tui.AppTest do assert model.columns == [:id, :age, :name] end + test "\\set tenant_batch sets and clears the tenants per transaction" do + model = %{activated() | mode: :query} + + {model, _} = feed(model, chars("\\set tenant_batch 25") ++ [{:key, :enter}]) + assert model.tenant_batch == 25 + assert model.flash == {:info, "tenant_batch set to 25"} + + {model, _} = feed(model, chars("\\set tenant_batch off") ++ [{:key, :enter}]) + assert model.tenant_batch == nil + + {model, _} = feed(model, chars("\\set tenant_batch 0") ++ [{:key, :enter}]) + assert model.tenant_batch == nil + assert {:error, "usage: " <> _} = model.flash + end + + test "results read in batches say they are not one snapshot" do + model = %{activated() | mode: :query} + rows = [%{name: "Alice", _tenant: "a"}, %{name: "Dora", _tenant: "b"}] + batches = {:batches, [{:fan_out, []}, {:fan_out, []}]} + + plan = %Efsql.Physical.Plan{access: batches, ops: [], columns: [:name, :_tenant]} + {batched, _} = feed(model, [{:done, :query, {:ok, {plan, rows, %{}, 1}}}]) + assert frame_text(batched) =~ "(2 rows, 1 ms) · 2 transactions, not one snapshot" + + plan = %Efsql.Physical.Plan{plan | access: {:fan_out, []}} + {single, _} = feed(model, [{:done, :query, {:ok, {plan, rows, %{}, 1}}}]) + refute frame_text(single) =~ "transactions" + end + test "uuid primary keys are not truncated in results" do model = %{activated() | mode: :query} uuid = "00ab2aa8-2bba-4102-bf8c-ced5d3142f8a" From f6309b63c66702207688a98e15419e0455253fd9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:31:04 +0000 Subject: [PATCH 4/7] Show only the transaction count for batched results The count already says the result spans several snapshots. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- lib/efsql/cli.ex | 2 +- lib/efsql/tui/view.ex | 4 ++-- test/tui_app_test.exs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/efsql/cli.ex b/lib/efsql/cli.ex index 89617ea..ddaab66 100644 --- a/lib/efsql/cli.ex +++ b/lib/efsql/cli.ex @@ -280,7 +280,7 @@ defmodule Efsql.Cli do defp print_snapshots(1), do: :ok defp print_snapshots(n) do - Owl.IO.puts(Owl.Data.tag("(read in #{n} transactions, not one snapshot)", :yellow)) + Owl.IO.puts(Owl.Data.tag("(read in #{n} transactions)", :yellow)) end defp column_sorter(nil), do: :asc diff --git a/lib/efsql/tui/view.ex b/lib/efsql/tui/view.ex index 2482408..033dd37 100644 --- a/lib/efsql/tui/view.ex +++ b/lib/efsql/tui/view.ex @@ -324,11 +324,11 @@ defmodule Efsql.Tui.View do [header] ++ rows ++ [footer] end - # A query across tenants read in batches is not one snapshot; say so. + # A query across tenants read in batches says how many transactions it took. defp snapshots_note(%Model{plan: %Efsql.Physical.Plan{} = plan}) do case Efsql.Fanout.transactions(plan) do 1 -> "" - n -> " · #{n} transactions, not one snapshot" + n -> " · #{n} transactions" end end diff --git a/test/tui_app_test.exs b/test/tui_app_test.exs index 9355eb1..3f1f287 100644 --- a/test/tui_app_test.exs +++ b/test/tui_app_test.exs @@ -217,14 +217,14 @@ defmodule Efsql.Tui.AppTest do assert {:error, "usage: " <> _} = model.flash end - test "results read in batches say they are not one snapshot" do + test "results read in batches say how many transactions they took" do model = %{activated() | mode: :query} rows = [%{name: "Alice", _tenant: "a"}, %{name: "Dora", _tenant: "b"}] batches = {:batches, [{:fan_out, []}, {:fan_out, []}]} plan = %Efsql.Physical.Plan{access: batches, ops: [], columns: [:name, :_tenant]} {batched, _} = feed(model, [{:done, :query, {:ok, {plan, rows, %{}, 1}}}]) - assert frame_text(batched) =~ "(2 rows, 1 ms) · 2 transactions, not one snapshot" + assert frame_text(batched) =~ "(2 rows, 1 ms) · 2 transactions" plan = %Efsql.Physical.Plan{plan | access: {:fan_out, []}} {single, _} = feed(model, [{:done, :query, {:ok, {plan, rows, %{}, 1}}}]) From 9cf91227fab60bffd69879f1fb97355d1d9203bb Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:37:29 +0000 Subject: [PATCH 5/7] Read tenant batches concurrently With tenant_batch set, up to four batches are now read at once. Each batch runs in its own process and transaction, and results come back in batch order. The number is configurable with `batch_concurrency:` or the `:efsql, :batch_concurrency` setting. The batches node carries it: {:batches, batches, concurrency}. Executor.map_concurrently/3 runs the batches. The first failure stops the remaining work and is re-raised in the caller as the original exception, throw or exit. A plain Task.async_stream would turn it into a linked exit, which the CLI's rescue can't catch, so a "too big" batch would have crashed the session instead of printing its message. With a concurrency of 1, everything runs in the calling process as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- README.md | 3 +- lib/efsql/executor.ex | 38 ++++++++++++++++++-- lib/efsql/fanout.ex | 30 +++++++++++----- test/cross_tenant_test.exs | 31 +++++++++------- test/executor_concurrency_test.exs | 57 ++++++++++++++++++++++++++++++ test/tui_app_test.exs | 2 +- 6 files changed, 136 insertions(+), 25 deletions(-) create mode 100644 test/executor_concurrency_test.exs diff --git a/README.md b/README.md index 237da5d..b49ede9 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,8 @@ When the tenants don't fit in one transaction, read them in batches: ``` Each batch of 25 tenants is then its own transaction, and there is no -tenant limit. Grouping, ordering and `LIMIT` still apply to all the rows +tenant limit. Up to four batches are read at once (the +`:efsql, :batch_concurrency` setting, or `batch_concurrency:` from Elixir). Grouping, ordering and `LIMIT` still apply to all the rows together, but the result is no longer one snapshot: each batch sees the database at a slightly different moment, and the result says how many transactions it came from. `\set tenant_batch off` goes back to one diff --git a/lib/efsql/executor.ex b/lib/efsql/executor.ex index 22d0a67..ff36a6d 100644 --- a/lib/efsql/executor.ex +++ b/lib/efsql/executor.ex @@ -35,6 +35,39 @@ defmodule Efsql.Executor do Enum.reduce(ops, fetch(access), &apply_op/2) end + @doc """ + Maps `fun` over `items`, up to `max` at a time, each in a process of its + own, and returns the results in order. The first failure stops the rest + and is raised (or thrown, or exited) here as it was there, so a caller's + `rescue` sees the original exception rather than a linked process's + exit. With `max` 1 it all runs in the calling process. + """ + def map_concurrently(items, 1, fun), do: Enum.map(items, fun) + + def map_concurrently(items, max, fun) do + items + |> Task.async_stream( + fn item -> + try do + {:ok, fun.(item)} + catch + kind, reason -> {:failed, kind, reason, __STACKTRACE__} + end + end, + max_concurrency: max, + ordered: true, + timeout: :infinity + ) + |> Enum.reduce_while([], fn + {:ok, {:ok, result}}, results -> {:cont, [result | results]} + {:ok, failed}, _results -> {:halt, failed} + end) + |> case do + {:failed, kind, reason, stacktrace} -> :erlang.raise(kind, reason, stacktrace) + results -> Enum.reverse(results) + end + end + @doc "Whether `row` satisfies every one of `predicates`, with SQL NULL semantics." def matches?(row, predicates), do: Enum.all?(predicates, &eval(&1, row)) @@ -52,8 +85,9 @@ defmodule Efsql.Executor do Efsql.Repo.all_from_source(query, options) end - # One transaction per batch, one after another. - defp fetch({:batches, batches}), do: Enum.flat_map(batches, &fetch/1) + # One transaction per batch, up to `concurrency` at a time. + defp fetch({:batches, batches, concurrency}), + do: batches |> map_concurrently(concurrency, &fetch/1) |> Enum.concat() defp fetch({:fan_out, []}), do: [] diff --git a/lib/efsql/fanout.ex b/lib/efsql/fanout.ex index d0de6df..28a484d 100644 --- a/lib/efsql/fanout.ex +++ b/lib/efsql/fanout.ex @@ -19,10 +19,12 @@ defmodule Efsql.Fanout do One transaction has to finish within FoundationDB's five seconds, so a query over many or large tenants may not fit. With `:tenant_batch` set, the tenants are read in batches of that many, one transaction each: - `{:batches, [{:fan_out, ...}]}`. Each batch is a snapshot of its own, - so the result as a whole is not one; `transactions/1` says how many - there were, for the result to say so. Grouping, ordering and the limit - still apply to all the rows together. + `{:batches, [{:fan_out, ...}], concurrency}`. Up to `concurrency` + batches are read at once, each in a process and transaction of its own. + Each batch is a snapshot of its own, so the result as a whole is not + one; `transactions/1` says how many there were, for the result to say + so. Grouping, ordering and the limit still apply to all the rows + together. """ alias Efsql.Exception.Unsupported @@ -32,6 +34,7 @@ defmodule Efsql.Fanout do alias Efsql.Planner @default_max_tenants 100 + @default_batch_concurrency 4 @doc """ Returns `{plan, tenants}`, opening tenants through the session's @@ -41,6 +44,9 @@ defmodule Efsql.Fanout do * `:tenant_batch` - read the tenants in batches of this many, one transaction each, instead of all in one. Default nil (one). + * `:batch_concurrency` - how many batches to read at once (default + #{@default_batch_concurrency}, or the `:efsql, :batch_concurrency` + application setting). * `:max_tenants` - the most tenants one transaction may read (default #{@default_max_tenants}, or the `:efsql, :max_tenants` application setting). It doesn't apply with `:tenant_batch`, which bounds each @@ -53,8 +59,14 @@ defmodule Efsql.Fanout do {max_tenants, options} = Keyword.pop(options, :max_tenants, default) {batch, options} = Keyword.pop(options, :tenant_batch) - unless batch == nil or (is_integer(batch) and batch > 0) do - raise ArgumentError, "tenant_batch must be a positive integer, got #{inspect(batch)}" + concurrency_default = + Application.get_env(:efsql, :batch_concurrency, @default_batch_concurrency) + + {concurrency, options} = Keyword.pop(options, :batch_concurrency, concurrency_default) + + for {name, value} <- [tenant_batch: batch, batch_concurrency: concurrency], + value != nil and not (is_integer(value) and value > 0) do + raise ArgumentError, "#{name} must be a positive integer, got #{inspect(value)}" end {tenant_preds, row_preds} = @@ -84,7 +96,9 @@ defmodule Efsql.Fanout do access = if batch, - do: {:batches, per_tenant |> Enum.chunk_every(batch) |> Enum.map(&{:fan_out, &1})}, + do: + {:batches, per_tenant |> Enum.chunk_every(batch) |> Enum.map(&{:fan_out, &1}), + concurrency}, else: {:fan_out, per_tenant} plan = %Plan{ @@ -100,7 +114,7 @@ defmodule Efsql.Fanout do How many transactions, so snapshots, a plan's result comes from: more than one only for a query across tenants read in batches. """ - def transactions(%Plan{access: {:batches, batches}}), do: length(batches) + def transactions(%Plan{access: {:batches, batches, _concurrency}}), do: length(batches) def transactions(%Plan{}), do: 1 defp list_tenants(nil), do: EctoFoundationDB.Tenant.list(Efsql.Repo) diff --git a/test/cross_tenant_test.exs b/test/cross_tenant_test.exs index f706a83..8ac8a8a 100644 --- a/test/cross_tenant_test.exs +++ b/test/cross_tenant_test.exs @@ -105,22 +105,27 @@ defmodule EfsqlTest.Integration.CrossTenant do "select _tenant, count(*) as n, max(name) as last from *.users " <> "where #{context[:both]} group by _tenant order by last desc;" - # One tenant per batch, and no tenant limit: each transaction reads one. - {%Plan{access: {:batches, batches}} = plan, rows, _tenants} = - Efsql.qall(sql, tenant_batch: 1, max_tenants: 1) + # grouping and ordering see every tenant's rows together + expected = [ + %{_tenant: context[:other], n: 2, last: "Eve"}, + %{_tenant: context[:tenant_id], n: 3, last: "Charles"} + ] - assert [{:fan_out, [_]}, {:fan_out, [_]}] = batches - assert Efsql.Fanout.transactions(plan) == 2 + {plan, ^expected, _tenants} = Efsql.qall(sql) + assert Efsql.Fanout.transactions(plan) == 1 - # grouping and ordering still see every tenant's rows together - assert rows == [ - %{_tenant: context[:other], n: 2, last: "Eve"}, - %{_tenant: context[:tenant_id], n: 3, last: "Charles"} - ] + # One tenant per batch, and no tenant limit: each transaction reads one. + # The same result whether the batches are read one at a time or together. + for concurrency <- [1, 2] do + options = [tenant_batch: 1, max_tenants: 1, batch_concurrency: concurrency] - # the same result as one transaction - {plan, ^rows, _tenants} = Efsql.qall(sql) - assert Efsql.Fanout.transactions(plan) == 1 + {%Plan{access: {:batches, batches, ^concurrency}} = plan, rows, _tenants} = + Efsql.qall(sql, options) + + assert [{:fan_out, [_]}, {:fan_out, [_]}] = batches + assert Efsql.Fanout.transactions(plan) == 2 + assert rows == expected + end end test "a read too old for its transaction fails at once instead of retrying", context do diff --git a/test/executor_concurrency_test.exs b/test/executor_concurrency_test.exs new file mode 100644 index 0000000..c18b414 --- /dev/null +++ b/test/executor_concurrency_test.exs @@ -0,0 +1,57 @@ +defmodule Efsql.ExecutorConcurrencyTest do + use ExUnit.Case, async: true + + alias Efsql.Exception.Unsupported + alias Efsql.Executor + + test "results come back in order, whatever order the work finishes in" do + assert Executor.map_concurrently([30, 10, 20, 0], 4, fn ms -> + Process.sleep(ms) + ms + end) == [30, 10, 20, 0] + end + + test "at most max run at once, and more than one does" do + running = :counters.new(2, []) + + Executor.map_concurrently(1..12, 3, fn _ -> + :counters.add(running, 1, 1) + now = :counters.get(running, 1) + if now > :counters.get(running, 2), do: :counters.put(running, 2, now) + Process.sleep(20) + :counters.sub(running, 1, 1) + end) + + assert :counters.get(running, 2) in 2..3 + end + + test "a failure reaches the caller as the original exception" do + assert_raise Unsupported, "too big", fn -> + Executor.map_concurrently([1, 2, 3], 2, fn + 2 -> raise Unsupported, "too big" + n -> n + end) + end + + assert catch_throw(Executor.map_concurrently([1], 2, fn _ -> throw(:stop) end)) == :stop + assert catch_exit(Executor.map_concurrently([1], 2, fn _ -> exit(:gone) end)) == :gone + end + + test "a failure stops the work not yet done" do + started = :counters.new(1, []) + + assert_raise Unsupported, fn -> + Executor.map_concurrently(1..50, 2, fn n -> + :counters.add(started, 1, 1) + if n == 1, do: raise(Unsupported, "stop"), else: Process.sleep(50) + end) + end + + assert :counters.get(started, 1) < 50 + end + + test "with max 1 everything runs in the calling process" do + me = self() + assert Executor.map_concurrently([1, 2], 1, fn _ -> self() end) == [me, me] + end +end diff --git a/test/tui_app_test.exs b/test/tui_app_test.exs index 3f1f287..ccbd976 100644 --- a/test/tui_app_test.exs +++ b/test/tui_app_test.exs @@ -220,7 +220,7 @@ defmodule Efsql.Tui.AppTest do test "results read in batches say how many transactions they took" do model = %{activated() | mode: :query} rows = [%{name: "Alice", _tenant: "a"}, %{name: "Dora", _tenant: "b"}] - batches = {:batches, [{:fan_out, []}, {:fan_out, []}]} + batches = {:batches, [{:fan_out, []}, {:fan_out, []}], 4} plan = %Efsql.Physical.Plan{access: batches, ops: [], columns: [:name, :_tenant]} {batched, _} = feed(model, [{:done, :query, {:ok, {plan, rows, %{}, 1}}}]) From ba696c5be8d025ad77117cd0f8ab365e78ff0fe9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:43:11 +0000 Subject: [PATCH 6/7] Drop _tenant from cross-tenant rows unless it's selected The executor adds _tenant to every row of a query across tenants, and nothing removed it when the select list didn't name it. So `select name from *.users` returned _tenant as well. Fanout now appends a projection for ungrouped queries that don't select it. Grouped queries only keep _tenant as a group field, which split/1 already projects away. Also matches %Logical.Select{} on three struct updates that Elixir 1.19's type checker warned about: Planner.rows/2, Fanout.plan/3 and Parser.grouped/2. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- lib/efsql/fanout.ex | 13 ++++++++++++- lib/efsql/parser.ex | 2 +- lib/efsql/planner.ex | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/efsql/fanout.ex b/lib/efsql/fanout.ex index 28a484d..cc6eda9 100644 --- a/lib/efsql/fanout.ex +++ b/lib/efsql/fanout.ex @@ -86,7 +86,18 @@ defmodule Efsql.Fanout do "(\\set tenant_batch 25), giving up the single snapshot" end - {rows, ops} = Planner.split(%Logical.Select{logical | predicates: row_preds}) + {%Logical.Select{} = rows, ops} = + Planner.split(%Logical.Select{logical | predicates: row_preds}) + + # The executor gives every row its _tenant; drop it again unless selected. + # (A grouped query's rows only keep it as a group field, which split/1 + # already projects away when it isn't selected.) + ops = + if logical.group_by == nil and is_list(logical.projection) and + :_tenant not in logical.projection and + not match?([{:project, _} | _], Enum.reverse(ops)), + do: ops ++ [{:project, logical.projection}], + else: ops {per_tenant, tenants} = Enum.map_reduce(names, tenants, fn name, tenants -> diff --git a/lib/efsql/parser.ex b/lib/efsql/parser.ex index bb51419..3d4637b 100644 --- a/lib/efsql/parser.ex +++ b/lib/efsql/parser.ex @@ -83,7 +83,7 @@ defmodule Efsql.Parser do raise Unsupported, "SELECT * can't be used with GROUP BY or aggregates; name the fields" end - defp grouped(logical, select) do + defp grouped(%Logical.Select{} = logical, select) do group_by = Enum.map(select.group_by, &group_field/1) {columns, aggregates} = diff --git a/lib/efsql/planner.ex b/lib/efsql/planner.ex index 4f8cb22..a18923b 100644 --- a/lib/efsql/planner.ex +++ b/lib/efsql/planner.ex @@ -82,7 +82,7 @@ defmodule Efsql.Planner do {rows(logical, read), ops} end - defp rows(logical, read) do + defp rows(%Logical.Select{} = logical, read) do read = if read == :star, do: [], else: read -- [:_tenant] %Logical.Select{ From 599d0c3e23b550f79169125a268432fe3e23bc6c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:51:51 +0000 Subject: [PATCH 7/7] Add the yecc compiler to mix.exs Elixir 1.19 no longer compiles .yrl files by default and warns that the project must list :yecc. It still compiled the grammar, but with a warning on every build. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- mix.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mix.exs b/mix.exs index c58219a..5aa849a 100644 --- a/mix.exs +++ b/mix.exs @@ -8,6 +8,8 @@ defmodule Efsql.MixProject do description: "SQL frontend and data explorer for FoundationDB", elixir: "~> 1.17", start_permanent: Mix.env() == :prod, + # src/efsql_sql_grammar.yrl, the SQL grammar + compilers: [:yecc] ++ Mix.compilers(), deps: deps(), elixirc_paths: elixirc_paths(Mix.env()), releases: releases(),