{"id":47355133,"url":"https://github.com/questdb/sql-parser","last_synced_at":"2026-03-18T02:01:15.635Z","repository":{"id":338273038,"uuid":"1157156224","full_name":"questdb/sql-parser","owner":"questdb","description":null,"archived":false,"fork":false,"pushed_at":"2026-03-04T14:57:53.000Z","size":1684,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-03-04T20:29:15.613Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/questdb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-13T13:53:14.000Z","updated_at":"2026-03-04T14:58:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"50714c61-adfe-4bec-a56b-631bd881714f","html_url":"https://github.com/questdb/sql-parser","commit_stats":null,"previous_names":["questdb/questdb-sql-parser","questdb/sql-parser"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/questdb/sql-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/questdb%2Fsql-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/questdb%2Fsql-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/questdb%2Fsql-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/questdb%2Fsql-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/questdb","download_url":"https://codeload.github.com/questdb/sql-parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/questdb%2Fsql-parser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30623653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-17T11:26:08.186Z","status":"ssl_error","status_checked_at":"2026-03-17T11:24:37.311Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-03-18T02:00:46.635Z","updated_at":"2026-03-18T02:01:15.619Z","avatar_url":"https://github.com/questdb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @questdb/sql-parser\n\nAn SQL parser for [QuestDB](https://questdb.io/) syntax, built with [Chevrotain](https://chevrotain.io/). Parses SQL into a fully typed AST, converts AST back to SQL, and provides context-aware autocomplete — all in a single package.\n\n## Features\n\n- **Complete QuestDB SQL coverage** — 45 statement types including SELECT, INSERT, UPDATE, CREATE TABLE, ALTER TABLE, GRANT/REVOKE, COPY, PIVOT, and more\n- **QuestDB-specific syntax** — `SAMPLE BY`, `LATEST ON`, `ASOF JOIN`, `LT JOIN`, `SPLICE JOIN`, `WINDOW JOIN`, implicit SELECT, duration literals, geohash types\n- **Lossless round-trip** — parse SQL to AST and convert back with `toSql()`. Verified against 1,726 real-world queries from QuestDB documentation with full round-trip coverage\n- **Context-aware autocomplete** — schema-driven suggestions for tables, columns, keywords, functions, and operators at any cursor position\n- **Fully typed** — complete TypeScript type definitions for the entire AST\n- **Single dependency** — only Chevrotain at runtime\n\n\n## Quick Start\n\n### Parse SQL to AST\n\n```typescript\nimport { parseToAst } from \"@questdb/sql-parser\";\n\nconst result = parseToAst(\"SELECT * FROM trades WHERE symbol = 'BTC-USD'\");\n\nif (result.errors.length === 0) {\n  console.log(result.ast[0]);\n  // {\n  //   type: \"select\",\n  //   columns: [{ type: \"star\" }],\n  //   from: [{ type: \"tableRef\", table: { type: \"qualifiedName\", parts: [\"trades\"] } }],\n  //   where: {\n  //     type: \"binary\", operator: \"=\",\n  //     left: { type: \"column\", name: { type: \"qualifiedName\", parts: [\"symbol\"] } },\n  //     right: { type: \"literal\", value: \"BTC-USD\", literalType: \"string\" }\n  //   }\n  // }\n}\n```\n\n### Convert AST back to SQL\n\n```typescript\nimport { parseToAst, toSql } from \"@questdb/sql-parser\";\n\nconst result = parseToAst(\"SELECT avg(price) FROM trades SAMPLE BY 1h FILL(PREV)\");\nconst sql = toSql(result.ast);\n// \"SELECT avg(price) FROM trades SAMPLE BY 1h FILL(PREV)\"\n```\n\n### Parse a single statement\n\n```typescript\nimport { parseOne } from \"@questdb/sql-parser\";\n\nconst stmt = parseOne(\"INSERT INTO trades VALUES (now(), 'BTC', 42000.50)\");\nconsole.log(stmt.type); // \"insert\"\n```\n\n### Parse multiple statements\n\n```typescript\nimport { parseStatements } from \"@questdb/sql-parser\";\n\nconst statements = parseStatements(`\n  SELECT * FROM trades\n  INSERT INTO logs VALUES (1)\n  CREATE TABLE orders (id LONG, ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY WAL\n`);\n\nconsole.log(statements.length); // 3\n```\n\n### Autocomplete\n\n```typescript\nimport { createAutocompleteProvider } from \"@questdb/sql-parser\";\n\nconst provider = createAutocompleteProvider({\n  tables: [\n    { name: \"trades\", designatedTimestamp: \"ts\" },\n    { name: \"orders\" },\n  ],\n  columns: {\n    trades: [\n      { name: \"ts\", type: \"TIMESTAMP\" },\n      { name: \"symbol\", type: \"SYMBOL\" },\n      { name: \"price\", type: \"DOUBLE\" },\n    ],\n    orders: [\n      { name: \"id\", type: \"LONG\" },\n      { name: \"created_at\", type: \"TIMESTAMP\" },\n    ],\n  },\n});\n\nconst suggestions = provider.getSuggestions(\"SELECT * FROM t\", 16);\n// [{ label: \"trades\", kind: \"Table\", insertText: \"trades\", ... }, ...]\n```\n\n## Supported Statements\n\n| Category               | Statements                                                                                                                                 |\n| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |\n| **Query**              | `SELECT`, implicit SELECT, `PIVOT`, `EXPLAIN`                                                                                              |\n| **DML**                | `INSERT`, `INSERT ATOMIC`, `INSERT BATCH`, `UPDATE`                                                                                        |\n| **DDL**                | `CREATE TABLE`, `ALTER TABLE`, `DROP TABLE`, `TRUNCATE TABLE`, `RENAME TABLE`                                                              |\n| **Views**              | `CREATE VIEW`, `ALTER VIEW`, `DROP VIEW`, `COMPILE VIEW`                                                                                   |\n| **Materialized Views** | `CREATE MATERIALIZED VIEW`, `ALTER MATERIALIZED VIEW`, `DROP MATERIALIZED VIEW`, `REFRESH MATERIALIZED VIEW`                               |\n| **Access Control**     | `CREATE/ALTER/DROP USER`, `CREATE/ALTER/DROP GROUP`, `CREATE/ALTER/DROP SERVICE ACCOUNT`, `ADD/REMOVE USER`, `ASSUME/EXIT SERVICE ACCOUNT` |\n| **Permissions**        | `GRANT`, `REVOKE`, `GRANT/REVOKE ASSUME SERVICE ACCOUNT`                                                                                   |\n| **Operations**         | `COPY FROM/TO`, `BACKUP`, `VACUUM TABLE`, `REINDEX TABLE`, `CANCEL QUERY`, `CHECKPOINT`, `SNAPSHOT`                                        |\n| **WAL**                | `RESUME WAL`, `SET TYPE WAL`                                                                                                               |\n| **Introspection**      | `SHOW TABLES/COLUMNS/PARTITIONS/CREATE TABLE/PARAMETERS/...`                                                                               |\n\n## QuestDB-Specific Syntax\n\nThe parser handles QuestDB extensions that standard SQL parsers don't support:\n\n```sql\n-- Time-series sampling\nSELECT avg(price) FROM trades SAMPLE BY 1h FILL(PREV) ALIGN TO CALENDAR\n\n-- Latest value per partition\nSELECT * FROM trades LATEST ON ts PARTITION BY symbol\n\n-- Time-ordered joins\nSELECT * FROM trades ASOF JOIN quotes ON (symbol)\nSELECT * FROM trades LT JOIN quotes ON (symbol) TOLERANCE 5s\nSELECT * FROM a WINDOW JOIN b ON a.ts = b.ts\n  RANGE BETWEEN 1h PRECEDING AND CURRENT ROW FOLLOWING\n\n-- Implicit SELECT (table name without SELECT * FROM)\ntrades WHERE symbol = 'BTC-USD'\n\n-- Duration literals\nSELECT * FROM trades WHERE ts \u003e now() - 2d\n\n-- Geohash literals and types\nCREATE TABLE geo (pos GEOHASH(8c), area GEOHASH(4b))\n\n-- Array subscripts and slices\nSELECT arr[2:], data[1:3] FROM t\n\n-- WAL tables with deduplication and TTL\nCREATE TABLE trades (ts TIMESTAMP, price DOUBLE)\n  TIMESTAMP(ts) PARTITION BY DAY WAL\n  DEDUP UPSERT KEYS(ts)\n  TTL 30 DAYS\n```\n\n## Architecture\n\n```\nSQL String\n    |\n    v\n Lexer (tokenize)\n    |\n    v\n Tokens\n    |\n    v\n Parser (Chevrotain CstParser)\n    |\n    v\n Concrete Syntax Tree (CST)\n    |\n    v\n Visitor (CST -\u003e AST)\n    |\n    v\n AST  -----\u003e toSql() -----\u003e SQL String\n```\n\nThe parser uses Chevrotain's [CST pattern](https://chevrotain.io/docs/guide/concrete_syntax_tree.html): the parser produces a lossless CST, then a visitor transforms it into a clean, typed AST. The `toSql()` function serializes any AST back to valid SQL.\n\n## API Reference\n\n### High-Level API\n\n| Function               | Description                                                       |\n| ---------------------- | ----------------------------------------------------------------- |\n| `parseToAst(sql)`      | Parse SQL string into `{ ast: Statement[], errors: Error[] }`     |\n| `parseOne(sql)`        | Parse a single statement, throws on errors or multiple statements |\n| `parseStatements(sql)` | Parse multiple statements, throws on errors                       |\n| `toSql(ast)`           | Convert `Statement[]` back to a SQL string                        |\n\n### Low-Level API\n\n| Function        | Description                                               |\n| --------------- | --------------------------------------------------------- |\n| `tokenize(sql)` | Tokenize SQL into Chevrotain tokens                       |\n| `parse(sql)`    | Parse SQL to CST with lex/parse errors                    |\n| `visitor`       | Chevrotain CST visitor instance for CST-to-AST conversion |\n\n### Autocomplete API\n\n| Function                             | Description                                                  |\n| ------------------------------------ | ------------------------------------------------------------ |\n| `createAutocompleteProvider(schema)` | Create a provider with `getSuggestions(query, cursorOffset)` |\n| `getContentAssist(sql, offset)`      | Low-level: get valid tokens at cursor position               |\n\n### Grammar Exports\n\nArrays of keywords, functions, data types, operators, and constants for syntax highlighting integration:\n\n```typescript\nimport { keywords, functions, dataTypes, operators, constants } from \"@questdb/sql-parser/grammar\";\n```\n\n## Development\n\n```bash\nyarn                # Install dependencies\nyarn build          # Compile TypeScript (tsup + tsc)\nyarn test           # Run all tests (6,100+ tests)\nyarn test:watch     # Run tests in watch mode\nyarn typecheck      # Type-check without emitting\nyarn lint           # Run ESLint\nyarn lint:fix       # Auto-fix lint issues\nyarn generate:cst   # Regenerate CST type definitions from parser grammar\nyarn clean          # Remove dist/ and coverage/\n```\n\nSee [DEVELOPMENT.md](DEVELOPMENT.md) for the full development workflow guide — how to add keywords, statement types, modify autocomplete, and more.\n\n## License\n\nApache-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquestdb%2Fsql-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquestdb%2Fsql-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquestdb%2Fsql-parser/lists"}