Skip to content

Commit 29dc668

Browse files
committed
doc
1 parent c8daadc commit 29dc668

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ The goal is simple: stream big results with stable memory usage and without forc
2020

2121
## Highlights
2222
- Direct REST calls to Statement Execution API.
23-
- Polls statement execution until completion.
23+
- Optimized polling with server-side wait (up to 50s) before falling back to client polling.
24+
- Query metrics support via Query History API (`enableMetrics` option).
2425
- Efficient external link handling: merge chunks into a single stream.
2526
- `mergeExternalLinks` supports streaming uploads and returns a new StatementResult with a presigned URL.
2627
- `fetchRow`/`fetchAll` support `JSON_OBJECT` (schema-based row mapping).
@@ -98,6 +99,35 @@ const merged = await mergeExternalLinks(result, auth, {
9899
console.log(merged.result?.external_links?.[0].external_link) // Presigned URL to merged CSV
99100
```
100101

102+
## Sample (Progress with Metrics)
103+
Track query progress with execution metrics:
104+
105+
```ts
106+
import { executeStatement } from '@bitofsky/databricks-sql'
107+
108+
const auth = {
109+
token: process.env.DATABRICKS_TOKEN!,
110+
host: process.env.DATABRICKS_HOST!,
111+
httpPath: process.env.DATABRICKS_HTTP_PATH!,
112+
}
113+
114+
const result = await executeStatement(
115+
'SELECT * FROM samples.tpch.lineitem LIMIT 10000',
116+
auth,
117+
{
118+
enableMetrics: true,
119+
onProgress: (status, metrics) => {
120+
console.log(`State: ${status.state}`)
121+
if (metrics) { // metrics is optional, only present when enableMetrics: true
122+
console.log(` Execution time: ${metrics.execution_time_ms}ms`)
123+
console.log(` Rows produced: ${metrics.rows_produced_count}`)
124+
console.log(` Bytes read: ${metrics.read_bytes}`)
125+
}
126+
},
127+
}
128+
)
129+
```
130+
101131
## Sample (Abort)
102132
Cancel a long-running query and stop polling/streaming:
103133

@@ -147,7 +177,9 @@ function executeStatement(
147177
): Promise<StatementResult>
148178
```
149179
- Calls the Databricks Statement Execution API and polls until completion.
150-
- Use `options.onProgress` to receive status updates.
180+
- Server waits up to 50s (`wait_timeout`) before client-side polling begins.
181+
- Use `options.onProgress` to receive status updates with optional metrics.
182+
- Set `enableMetrics: true` to fetch query metrics from Query History API on each poll.
151183
- Throws `DatabricksSqlError` on failure, `StatementCancelledError` on cancel, and `AbortError` on abort.
152184

153185
### fetchRow(statementResult, auth, options?)
@@ -204,17 +236,18 @@ function mergeExternalLinks(
204236
### Options (Summary)
205237
```ts
206238
type ExecuteStatementOptions = {
207-
onProgress?: (status: StatementStatus) => void
239+
onProgress?: (status: StatementStatus, metrics?: QueryMetrics) => void
240+
enableMetrics?: boolean // Fetch metrics from Query History API (default: false)
208241
signal?: AbortSignal
209242
disposition?: 'INLINE' | 'EXTERNAL_LINKS'
210243
format?: 'JSON_ARRAY' | 'ARROW_STREAM' | 'CSV'
211-
wait_timeout?: string
244+
wait_timeout?: string // Server wait time (default: '50s', max: '50s')
212245
row_limit?: number
213246
byte_limit?: number
214247
catalog?: string
215248
schema?: string
216249
parameters?: StatementParameter[]
217-
on_wait_timeout?: 'CONTINUE' | 'CANCEL'
250+
on_wait_timeout?: 'CONTINUE' | 'CANCEL' // Default: 'CONTINUE'
218251
warehouse_id?: string
219252
}
220253
@@ -248,6 +281,8 @@ type MergeExternalLinksOptions = {
248281
## Notes
249282
- Databricks requires `INLINE` results to use `JSON_ARRAY` format. `INLINE + CSV` is rejected by the API.
250283
- `EXTERNAL_LINKS` are merged using `@bitofsky/merge-streams`.
284+
- Query metrics are fetched from `/api/2.0/sql/history/queries/{query_id}?include_metrics=true` when `enableMetrics: true`.
285+
- Metrics may not be immediately available; `is_final: true` indicates complete metrics.
251286
- Requires Node.js >= 20 for global `fetch` and Web streams.
252287

253288
## Development

0 commit comments

Comments
 (0)