Table of contents
Open Table of contents
A query with nothing to scan
A developer runs:
SELECT 1 + 2 AS answer;
-- answer: 3
There is no table after FROM, yet the expression needs to be evaluated once. TiDB’s planner can start with a small synthetic input called TableDual. A projection above it evaluates the expression for that input row. If the input had zero rows, the projection would have no row on which to run.
This walkthrough follows four code units in pingcap/tidb at commit da76b944: LogicalTableDual, PhysicalTableDual, executorBuilder.buildTableDual, and TableDualExec. Source behavior and plans can change between versions; a particular SQL statement is not guaranteed to display the same plan in every release.
The journey from SQL to an executor
SELECT 1 + 2
|
v
PlanBuilder: no FROM -> LogicalTableDual(RowCount=1, no columns)
|
v
physical planning -> PhysicalTableDual(RowCount=1, same schema)
|
v
executorBuilder.buildTableDual -> TableDualExec(numDualRows=1)
|
v
TableDualExec.Next -> one virtual row -> projection -> answer 3
PlanBuilder.buildTableRefs handles a missing FROM by calling buildTableDual, which creates a logical dual with RowCount: 1. This is a planner’s synthetic input, not a stored row in TiKV.
LogicalTableDual represents a result with zero or one row. Its schema can have zero columns: row count and column count are separate concepts. Its DeriveStats supplies the row count to the optimizer. Its column-pruning method can remove unused columns, while preserving the row count.
Physical planning’s findBestTask4LogicalTableDual copies the count and schema into PhysicalTableDual, then places it in a root task. Zero or one row is already ordered for any requested sort order; the function rejects an index-join property. The physical operator’s ExplainInfo reports rows: followed by the count.
The executor builder checks the count: any value other than zero or one is an error. It then passes the schema and row count to TableDualExec, also setting the executor’s initial chunk capacity to that count.
What happens on each pull?
TiDB executors exchange batches called chunks. A parent asks a child for more rows with Next. TableDualExec.Open and Next keep a numReturned counter and follow this sequence:
Openresets the counter, so the executor is ready for a new run.Nextresets the caller’s chunk. If the configured count has already been returned, it leaves the chunk empty.- For a one-row dual with no columns, it sets the chunk’s virtual row count to one. There are no column values to append, but the parent still sees one row.
- For a one-row dual with columns, it appends a
NULLto each output column. It marks the row as returned, so the next pull is empty.
first Next second Next
RowCount = 0 -> empty chunk -> empty chunk
RowCount = 1,
zero columns -> 1 virtual row -> empty chunk
RowCount = 1,
N columns -> 1 row of N NULLs -> empty chunk
For SELECT 1 + 2, the 3 comes from evaluating the projection, not from a column in the dual. A zero-row dual can also represent an empty intermediate result in a plan. The existence of that operator does not mean every unsatisfiable query takes the same optimization path, nor that all operators around it do no work.
Inspecting a plan and the relevant settings
Try EXPLAIN SELECT 1 + 2 AS answer; on your TiDB version and read the operator tree. A TableDual entry may report rows:1; a projection can sit above it. Operator IDs and plan shape can differ with optimizer changes. EXPLAIN shows the chosen plan without executing the query; EXPLAIN ANALYZE executes it and also reports runtime information.
Two general chunk variables are useful context: tidb_init_chunk_size controls the normal initial chunk size, and tidb_max_chunk_size controls the general maximum. At the cited source revision, the dual builder explicitly sets its own initial capacity to RowCount. Neither variable changes the dual’s zero-or-one-row semantics. Their documented defaults and ranges can vary by TiDB version, so consult the documentation for the version you run before tuning a broader workload.