mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-07 07:55:13 +02:00
* docs: rewrite Semantic Querying concept with imperative-vs-declarative diagram
Reframe semantic-layer-internals.mdx around the contract the semantic
layer offers an agent: declare what you want (a Semantic Query), KTX
figures out how to compute it. Replaces the old "Context-Aware SQL"
framing with a clear imperative-vs-declarative narrative.
Adds a React Flow component (semantic-layer-flow.tsx) that contrasts a
buggy 4-table agent-authored SQL (chasm trap, LEFT-JOIN-in-WHERE,
hardcoded DATE_TRUNC) against the chasm-safe per-fact CTE SQL the
planner actually emits, including the outer GROUP BY over the requested
dimensions. Both lanes converge into a shared warehouse node and each
SQL card now has parallel bullet notes (failures on the left, KTX
behavior on the right).
Side fixes bundled in:
- include the /ktx basePath in the favicon metadata so the icon resolves
under the production prefix
- migrate docs-site/middleware.ts to docs-site/proxy.ts (Next 16 rename)
- redirect / to /ktx/docs/getting-started/introduction so the apex docs
URL works
- add tests covering the apex redirect, the favicon basePath, and the
middleware-to-proxy rename
- propagate the Semantic Query terminology across the ktx-sl CLI
reference, the context-layer concept page, and the agent-clients /
primary-sources integration pages
* Fix CI dead-code failures
* docs-site: polish semantic-layer-internals code blocks and flow diagram
- Make CodeBlock a server component so children traverse synchronously
under React 19 RSC streaming; previously extractText returned "" in
dev SSR, leaving code blocks empty.
- Add custom JSON/YAML/SQL/code-like tokenizers with theme-aware token
classes; drop the colored file-glyph dot and gradient tab-head.
- Tighten tab-head: subtle grey background, smaller monospace filename
in muted grey, smaller rectangular language pill placed to the left
of the filename.
- Polish the React Flow semantic-layer diagram (controls, fit-view
padding, edge types).
* docs-site: annotate imperative SQL, add section anchor, drop ClickHouse
- Wire numbered red badges to each problematic span in the "Without KTX"
SQL with hover sync between SQL gutter, lines, and the notes list.
- Add #imperative-vs-declarative anchor on the flow section header so
the eyebrow link is shareable; reveals a # glyph on hover/focus.
- Align the compiled-SQL note dots to the first-line midpoint
(mt-[6px] instead of mt-1) so 4px dots sit at y=8 in a 16px line.
- Remove all ClickHouse references from docs-site (primary-sources,
quickstart, ktx-setup, contributing, agents-setup, mechanics test,
warehouse drivers in the flow diagram).
* test: drop ClickHouse contributing-docs assertion
Align the workspace-package mirror test with the ClickHouse removal
from docs-site (75907eb). The connector-clickhouse package still
exists in packages/, but contributing.mdx no longer lists it, so the
test that mirrored docs against the workspace was failing.
168 lines
5.6 KiB
Text
168 lines
5.6 KiB
Text
---
|
|
title: "ktx sl"
|
|
description: "List, search, validate, or query semantic-layer sources."
|
|
---
|
|
|
|
Interact with your project's semantic layer. Semantic sources are YAML
|
|
definitions that describe tables, columns, measures, joins, segments, and grain:
|
|
the vocabulary agents use to generate correct SQL.
|
|
|
|
## Command signature
|
|
|
|
```bash
|
|
ktx sl <subcommand> [options]
|
|
```
|
|
|
|
## Subcommands
|
|
|
|
| Subcommand | Description |
|
|
|-----------|-------------|
|
|
| `list` | List semantic-layer sources |
|
|
| `search <query>` | Search semantic-layer sources |
|
|
| `validate <sourceName>` | Validate a semantic-layer source against the database schema |
|
|
| `query` | Compile or execute a Semantic Query |
|
|
|
|
## Options
|
|
|
|
### `sl list`
|
|
|
|
| Flag | Description | Default |
|
|
|------|-------------|---------|
|
|
| `--connection-id <id>` | Filter by KTX connection id | - |
|
|
| `--output <mode>` | Output mode: `pretty` (default in TTY), `plain` (TSV), or `json` | `pretty` |
|
|
| `--json` | Shortcut for `--output=json` (overrides `--output`) | `false` |
|
|
|
|
### `sl search`
|
|
|
|
| Flag | Description | Default |
|
|
|------|-------------|---------|
|
|
| `--connection-id <id>` | Filter by KTX connection id | - |
|
|
| `--limit <number>` | Maximum search results | - |
|
|
| `--output <mode>` | Output mode: `pretty` (default in TTY), `plain` (TSV), or `json` | `pretty` |
|
|
| `--json` | Shortcut for `--output=json` (overrides `--output`) | `false` |
|
|
|
|
### `sl validate`
|
|
|
|
| Flag | Description | Default |
|
|
|------|-------------|---------|
|
|
| `--connection-id <id>` | KTX connection id (required) | - |
|
|
|
|
### `sl query`
|
|
|
|
| Flag | Description | Default |
|
|
|------|-------------|---------|
|
|
| `--connection-id <id>` | KTX connection id | - |
|
|
| `--query-file <path>` | JSON Semantic Query file | - |
|
|
| `--measure <measure>` | Measure to query; repeatable (at least one required) | - |
|
|
| `--dimension <dimension>` | Dimension to include; repeatable | - |
|
|
| `--filter <filter>` | Filter expression; repeatable | - |
|
|
| `--segment <segment>` | Segment to include; repeatable | - |
|
|
| `--order-by <field[:direction]>` | Order field, optionally suffixed with `:asc` or `:desc`; repeatable | - |
|
|
| `--limit <n>` | Query limit | - |
|
|
| `--include-empty` | Include empty rows | `false` |
|
|
| `--format <format>` | Output format: `json` or `sql` | `json` |
|
|
| `--execute` | Execute the compiled query against the database | `false` |
|
|
| `--yes` | Install the managed Python runtime without prompting when required | `false` |
|
|
| `--no-input` | Disable interactive managed runtime installation | - |
|
|
| `--max-rows <n>` | Maximum rows to return when executing | - |
|
|
|
|
`sl query` requires at least one `--measure` unless `--query-file` is set.
|
|
`--query-file` should point to a JSON Semantic Query object.
|
|
|
|
## Examples
|
|
|
|
```bash
|
|
# List all semantic sources
|
|
ktx sl list
|
|
|
|
# List sources for a specific connection
|
|
ktx sl list --connection-id my-warehouse
|
|
|
|
# List sources as JSON
|
|
ktx sl list --json
|
|
|
|
# Search sources as JSON
|
|
ktx sl search "revenue" --json
|
|
|
|
# Validate a source against the live schema
|
|
ktx sl validate orders --connection-id my-warehouse
|
|
|
|
# Compile a query and view the generated SQL
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--measure orders.total_revenue \
|
|
--dimension orders.created_date \
|
|
--format sql
|
|
|
|
# Execute a query with filters
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--measure orders.total_revenue \
|
|
--dimension orders.status \
|
|
--filter "orders.created_date >= '2024-01-01'" \
|
|
--execute \
|
|
--max-rows 100
|
|
|
|
# Query with ordering and limit
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--measure orders.total_revenue \
|
|
--dimension customers.country \
|
|
--order-by total_revenue:desc \
|
|
--limit 10 \
|
|
--execute
|
|
|
|
# Execute and cap the result set
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--measure orders.count \
|
|
--dimension orders.created_date \
|
|
--execute \
|
|
--max-rows 1000
|
|
|
|
# Compile or execute without prompting for runtime installation
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--measure orders.count \
|
|
--execute \
|
|
--yes
|
|
|
|
# Execute a query from a JSON file
|
|
ktx sl query \
|
|
--connection-id my-warehouse \
|
|
--query-file query.json \
|
|
--execute \
|
|
--max-rows 100
|
|
```
|
|
|
|
## Output
|
|
|
|
Semantic-layer list and search commands return human-readable output by
|
|
default. Use `--json` on `list` or `search` when an agent needs structured
|
|
output. Use `--format sql` on `query` to inspect generated SQL before
|
|
execution, or leave `--format json` for the compiled query and optional rows.
|
|
Pretty `sl search` output shows `#1`, `#2`, and later rank badges for the
|
|
displayed results. Plain and JSON output keep the raw `score` value, which is a
|
|
ranking score rather than a percentage.
|
|
|
|
```json
|
|
{
|
|
"sql": "SELECT orders.status, SUM(orders.total_amount) AS total_revenue FROM public.orders GROUP BY orders.status",
|
|
"rows": [
|
|
{
|
|
"orders.status": "completed",
|
|
"total_revenue": 125000
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
## Common errors
|
|
|
|
| Error | Cause | Recovery |
|
|
|-------|-------|----------|
|
|
| Source not found | Source name or connection id is wrong | Run `ktx sl list --json` and retry with an exact source name and connection id |
|
|
| Validation fails | YAML references missing columns, invalid joins, or invalid SQL expressions | Fix the source YAML and rerun `ktx sl validate` |
|
|
| Query compile fails | Measure, dimension, filter, or segment name is invalid | Search sources with `ktx sl search`, inspect the source YAML in your project files, then retry using declared fields |
|
|
| Execution returns too many rows | `--max-rows` is missing or too high | Add `--max-rows` with a bounded value before executing |
|
|
| Runtime install is blocked | Query execution needs the managed Python runtime and prompts are disabled | Run `ktx dev runtime install --feature core --yes`, or rerun `ktx sl query --yes` |
|