SQL language overview
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
SELECTthe 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 BYover expressions andHAVING, outer, cross and comma joins,UNION,INTERSECTandEXCEPT, subqueries inFROMand subqueries as values. Every row set it holds is bounded bysort_max_rows(groups byaggregate_max_groups); a statement past a bound fails instead of being truncated.ANALYZEdoes 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
| Category | Statements |
|---|---|
| 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 |
| Queries | SELECT, ANALYZE |
| Transactions | BEGIN, COMMIT, ROLLBACK, SET ISOLATION LEVEL, SET DURABILITY |
| Introspection | SHOW, DESCRIBE |
| Session | PING, SYNC, STOP |
Lexical rules
- Keywords and column names are case-insensitive:
SELECT OWNER FROM accountsreads the columnowner. 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'. Adecimalcolumn 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, notqty = '2';name = '5', notname = 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, includingSELECT,COUNTandCHECK, 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:
| Error | Meaning | What to do |
|---|---|---|
InvalidArgument | The statement is wrong, for example an unknown column. | Fix the statement. |
NOT_IMPLEMENTED | The form fits the design but this release has not built it, for example WITH or LIKE ... ESCAPE. | Rewrite it, or check a later release. |
UNSUPPORTED | The 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 inFROMis accepted instead. - Window functions (
OVER),DISTINCT ON,NATURAL JOIN,LIKE ... ESCAPEandNULLS 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_INCREMENTandIF NOT EXISTS. Every primary key is already issued automatically whenINSERTleaves 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.