Scalar subquery → join then aggregate (limits of the query layer)

A scalar subquery in the select list is answered by the KDBS query layer, which holds whole tables to compute, so it fails at 2 million rows. Rewrite it as a join + GROUP BY that the step chain answers, with the same result.

SQL

Situation

A report lists the order count of the 1,000 VIP customers. It is written as a scalar subquery that counts per customer.

Tables

TableColumnTypeDescription
customersgradevarchar(16)VIP has 1,000 customers
orderscust_idint64Customer who placed the order
IndexColumns
customers_x1grade
orders_x1cust_id, ord_dt

Order count per customer

BEFORE · Scalar subquery

SELECT c.cust_id, c.cust_nm,
       (SELECT COUNT(*) FROM orders AS o WHERE o.cust_id = c.cust_id) AS cnt
FROM customers AS c
WHERE c.grade = 'VIP'

Run it and read the error message.

Approach

1. See which path answers

The KDBS step chain accepts subqueries only in condition position. A subquery in the select list is answered by the query layer. For each table, the layer pushes down the WHERE conditions that refer only to that table, fetches the rows, and computes the subquery on top of them.

2. Count the rows the layer must hold

The subquery's orders has no condition that refers only to that table (o.cust_id = c.cust_id refers to the outer row). So the layer must hold all 2 million orders rows, which exceeds sort_max_rows (1,048,576).

3. Rewrite it in a shape the step chain answers

An inner equi-join of customers and orders, plus a GROUP BY that groups by columns and aggregates without an alias, is answered by the step chain. The inner orders of the join is probed per customer through cust_id of orders_x1.

Explanation

Scalar subquery → join + GROUP BY

AFTER · Join + GROUP BY

SELECT c.cust_id, c.cust_nm, COUNT(*)
FROM customers AS c
JOIN orders AS o ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
GROUP BY c.cust_id, c.cust_nm

Why it works

  • Before fails with this statement's relation walk would hold more than sort_max_rows (1048576) rows; the query layer answers from rows it holds.
  • After is IndexProbe customers AS c (1,000 VIP rows, Buffers 1,001) → IndexProbe orders AS o key=… (opened 1,000 times, 19,971 rows, Buffers 20,971) → AGGREGATE under STEP CHAIN order=captured. Total Buffers 21,972, 1,000 rows.
  • VIP customers with no orders drop out of the inner join. This data has no such customers, but if zero counts must also appear, an outer join is needed, and an outer join goes back to the query layer. The layer must hold all of orders, so on this data it fails for the same reason.

Variation: one alias changes the path

TRY · Alias on the aggregate

SELECT c.cust_id, c.cust_nm, COUNT(*) AS cnt
FROM customers AS c
JOIN orders AS o ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
GROUP BY c.cust_id, c.cust_nm
  • Only AS cnt is added to After. An aggregate with an alias is query layer syntax in this version (ckdbs/manual/sql/sql.md §4, "an aggregate under an alias"), so it moves to the layer and fails with the same error as Before. The column name appears as COUNT(*) on the result screen. If a name is needed, the app assigns it.

Key points

  • In KDBS, two statements with the same result go down different engine paths. The step chain navigates by key; the query layer holds the fetched rows and computes. Small differences such as an alias, an expression, or an outer join change the path.
  • A query layer statement must put conditions that refer only to each table on that table to reduce the rows fetched. When the rows to hold exceed sort_max_rows, the result is an error, not a partial result.
  • Optimizers in other DBs sometimes turn scalar subqueries into joins (unnesting), but KDBS does not rewrite statements. The one who rewrites is the person writing the SQL.