HTTP: queries
POST /v1/query with a JSON filter or a SQL SELECT - request fields, counts, paging with cursors, sorting, errors with positions.
POST /v1/query runs one statement and answers a count or a page of rows. GET on this path is 405.
Request#
| Field | Meaning | Default |
|---|---|---|
db | database | main |
table | table | documents |
language | "filter" (JSON filter) or "sql" | "filter" |
text | the filter JSON text (a string holding JSON) or the SQL statement | required |
pageSize | rows per page | server default 100, at most 10,000 |
cursor | the next of the previous page | start |
count | true for a count only | false |
Answer#
- count:
{"count": 250} - page:
{"rows":[...], "keys":[...], "next":"<cursor>"|null, "done": bool}; with a SQLLIMIT nthe page is cut tonrows and the answer also has"limit": n.
rows are documents as JSON objects; keys[i] is the key of rows[i]. Cursors are consistent only while the data does not change.
Examples (test queries_count_page_and_report_errors)#
500 documents {"n":i,"even":bool} were written. A count:
POST /v1/query
{"language":"filter","text":"{\"even\":true}","count":true}
-> 200 {"count":250}Paging, 60 rows at a time, until next is null and done is true:
POST /v1/query
{"text":"{\"even\":true}","pageSize":60}
POST /v1/query
{"text":"{\"even\":true}","pageSize":60,"cursor":"<next of the last answer>"}The test collects 250 distinct rows in total. Another table:
{"db":"shop","table":"orders","text":"{}","count":true} -> {"count":7}SQL#
language: "sql" compiles the statement into the same plan as the JSON filter. From sql_runs_over_http_and_the_binary_protocol, with 300 documents {"id","age","city"}:
{"language":"sql","text":"SELECT COUNT(*) FROM documents WHERE age < 25"}
-> {"count":40}
{"language":"sql","text":"SELECT * FROM documents WHERE id BETWEEN 10 AND 14 LIMIT 3"}
-> 3 rows, "limit":3, "done":trueFROM accepts table or db.table. Conditions: AND OR NOT, parentheses, = != <> < <= > >=, IN, NOT IN, BETWEEN, IS [NOT] NULL, dotted and "quoted" column names, 'text', integers and doubles, TRUE FALSE NULL. ORDER BY <column> [ASC|DESC], ... [LIMIT n] [OFFSET m] is supported; _key is the primary key. For a sorted query the cursor is the number of rows already returned (a string), so a client can jump to any row; OFFSET alone orders by key. A column list (SELECT name, address.city FROM t) returns only those columns, in JSON output. Not supported yet: GROUP BY and LIKE in this subset, joins and writes through /v1/query (the MySQL and MariaDB front-ends cover more, see the SQL reference).
Sorted queries read every match: measured 40 ms for the top 100 of 1 M rows and about 0.9 s for OFFSET 500000.
Errors#
| Request | Answer |
|---|---|
filter with an unknown operator ({"n":{"$bogus":1}}) | 400, code syntax_error |
| SQL that does not parse | 400, code syntax_error, with position: {line, column} |
| body that is not JSON | 400, the body contains requestId |
Example: the statement SELECT *\nFROM documents\nWHERE age == answers 400 with error.code "syntax_error" and error.position.line 3.
Not supported yet#
Cursors as a separate resource (/v1/cursors), named parameters, explain, a server-side time limit, a read-only flag per request: all appear in the design draft of the API and are not implemented.