SQL language overview

v3.2.0

The statements KDBS accepts, how they are written, and what the language leaves out on purpose.

In this article

KDBS speaks a SQL dialect built for transaction processing. Every statement it accepts runs in a predictable way: joins run in the order you write them, and nothing reorders or rewrites a query. Forms the engine does not run are refused with a clear message, not accepted and handled differently.

Two paths for a query

A SELECT runs on one of two paths. The reply does not depend on which one it took.

  • The step chain. Most OLTP statements compile, when they are parsed, into a fixed chain of steps: key lookups, index and Cabin probes, scans and keyed joins. Its plan does not change with table size, it can be inspected with ANALYZE, and repeated statements record Waystone trails.
  • The query layer. A SELECT the step chain cannot carry is parsed again by a wider grammar and run over rows the step chain reads, one table at a time, at the statement’s snapshot. It answers expressions in the select list, SELECT DISTINCT, GROUP BY over expressions and HAVING, outer, cross and comma joins, UNION, INTERSECT and EXCEPT, subqueries in FROM and subqueries as values. Every row set it holds is bounded by sort_max_rows (groups by aggregate_max_groups); a statement past a bound fails instead of being truncated. ANALYZE does not reach it, and it records no trails.
kdbs> SELECT a.owner, COUNT(*) FROM accounts AS a JOIN purchases AS p ON p.account_id = a.id GROUP BY a.owner HAVING COUNT(*) > 1
a.owner  count(*)
-------  --------
  alice         3

Statements

CategoryStatements
Schema (DDL)CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE NAMESPACE, CREATE INDEX, CREATE ASSERTION, CREATE TRANSITION, CREATE CABIN, and the matching DROP statements
Data (DML)INSERT, UPDATE, DELETE, PURGE
QueriesSELECT, ANALYZE
TransactionsBEGIN, COMMIT, ROLLBACK, SET ISOLATION LEVEL, SET DURABILITY
IntrospectionSHOW, DESCRIBE
SessionPING, SYNC, STOP

Lexical rules

  • Keywords and column names are case-insensitive: SELECT OWNER FROM accounts reads the column owner. Column names are unique ignoring case. Table names are matched as written.
  • A trailing ; is accepted and ignored. Send one statement at a time.
  • String literals use single quotes: 'Mechanical keyboard'. There is no escape for a quote inside a string.
  • A bare number with a fraction, such as 12.34, means the same as the string '12.34'. A decimal column accepts it; an integer column refuses it.
  • No type is converted implicitly. A literal must be written in the form of the column it is compared with: qty = 2, not qty = '2'; name = '5', not name = 5. A mismatch is refused when the statement is compiled. See Data types.
  • Only eleven words are reserved: JOIN, ON, AS, IN, EXISTS, NOT, BETWEEN, LEFT, RIGHT, FULL, OUTER. Every other word, including SELECT, COUNT and CHECK, can be used as a column or table name.

Names

A table can be written as <namespace>.<table>, such as shop.items. Table names are unique across the whole instance, so a namespace qualifier never changes which table a name refers to: at CREATE TABLE it chooses the namespace, and everywhere else it is checked. See CREATE NAMESPACE.

The catalog is readable as views in the sys namespace, for example SELECT * FROM sys.tables. See SHOW and DESCRIBE.

Refusals

When KDBS does not accept a statement, the error says why and, where it can, gives the byte position of the part it refused. The error also says which kind of refusal it is:

ErrorMeaningWhat to do
InvalidArgumentThe statement is wrong, for example an unknown column.Fix the statement.
NOT_IMPLEMENTEDThe form fits the design but this release has not built it, for example WITH or LIKE ... ESCAPE.Rewrite it, or check a later release.
UNSUPPORTEDThe engine’s design rules the form out, for example updating a primary key or SAVEPOINT.Rewrite the statement.

Only TXN_CONFLICT is retryable. Every other error fails the same way if you send the same statement again.

kdbs> WITH x AS (SELECT * FROM accounts) SELECT * FROM x
      ^
ERR NOT_IMPLEMENTED retryable=0 common table expressions (WITH) are not supported (byte 0); subqueries are allowed in predicate position only

What the language leaves out

Each of these is refused:

  • Common table expressions (WITH). A subquery in FROM is accepted instead.
  • Window functions (OVER), DISTINCT ON, NATURAL JOIN, LIKE ... ESCAPE and NULLS FIRST / NULLS LAST.
  • A catalog view (sys.*) in a query-layer statement, such as a join with an ordinary table.
  • Savepoints and chained transactions. A transaction commits or rolls back whole.
  • Dialect aliases such as SERIAL, AUTO_INCREMENT and IF NOT EXISTS. Every primary key is already issued automatically when INSERT leaves it out.

OR, NOT, parentheses, IN (<value list>), NOT BETWEEN, LIKE and a literal on the left of a comparison are accepted in WHERE. A condition built from them never becomes a key or an index range: it is checked against the rows a step reads. See SELECT.