Skip to content

How a query is put together ​

A query is some let names, one source, and a pipeline of operators that shape the rows the source produces.

Once you can see those three parts in any query, the rest of the handbook is detail. This page names the three parts and says what a row is and where its columns come from. It also shows how CQL chooses names when you do not choose them yourself.

The three parts ​

cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;   // 1. names

from stETH at latest-21600..latest every 7200 as s                  // 2. the source
| extend pooled = s.getTotalPooledEther()                            // 3. the pipeline
| where pooled > 0
| project { block: $block, pooled: format(pooled, 18) }

Open in workbench →

Names come first. let binds a name to an address, an ABI, or a plain value, and ends with a semicolon. A let cannot read anything from a row, because CQL settles it before the first row exists. So CQL refuses let x = s.getTotalPooledEther(); with a note to use extend instead.

The source starts with from and says what to read:

  • which contracts
  • whether you want their state or their events
  • at which blocks
  • what to call the result

Everything after the contract is optional. There is exactly one source per pipeline. A second contract set is another source inside a join or a union.

The pipeline is zero or more operators, each starting with |. Rows flow from top to bottom. Every operator takes the rows that the previous operator produced. It then sends what it keeps, adds or combines to the next operator.

What a row is ​

A state row is one contract at one block. from stETH at latest is one row. The same source with at latest-21600..latest every 7200 is one row per sampled block. from [a, b] doubles that. CQL answers every call that you make on the alias, such as s.getTotalPooledEther(), at that row's block. That is what makes a range of state rows a time series.

An event row is one log. from stETH events TokenRebased gives one row for each TokenRebased that the contract emitted in the range, with the event's arguments as columns. The contract emits one a day, when the oracle reports. You can still make a call on an event row's alias. CQL answers it at the block that the event was in.

Both kinds carry system columns: $block, $chain, $address and $timestamp on every row, and $tx, $logIndex, $event and a few more on event rows.

Where columns come from ​

Columns come from three places, and you can tell which place from how you write them:

  • A system column starts with $: $block, $timestamp.
  • A field of the source is written through its alias: e.postTotalEther, t.value. On a state row the alias has no fields of its own, only functions to call: s.getTotalPooledEther().
  • A column you made with extend, project or summarize: pooled, total.

CQL searches for an unqualified name in this order: alias first, then a column, then a let.

After a join, if both sides have a column with the same name, CQL keeps both columns and renames the right one with a number. For example, with extend treasury = … on each side, a bare treasury is the left side's column and treasury1 is the right side's column. You can also name a field of the right source through its alias, so b.value is the same column as value1. A column that you made has no alias to go through. Operators has the whole rule.

How columns get their names ​

When you write project { pooled: format(pooled, 18) } the name is yours. When you do not name a column, CQL derives a name. The rule is short:

  • A path keeps its last segment.
  • A call keeps the function name.
  • An aggregate prefixes the function.
You writeThe column is called
t.fromfrom
s.$address$address
s.getTotalPooledEther()getTotalPooledEther
count()count_
sum(t.value)sum_value

Anything else needs a name, for example t.value * 2. CQL tells you so rather than inventing Column1. To name a column inside project { … }, write name: expr. After extend, inside summarize and after by, write name = expr. Do not write expr as name. In an expression, as is a type cast, and CQL refuses t.value as amount.

The alias ​

as s names the source so you can reach it from the pipeline. Everything you read from the contract goes through that name: s.getTotalPooledEther() calls a function, s.$address reads which contract this row is, s.$storage[0] reads raw storage. The alias is not a column. project { s } is an error.

See also ​