diff --git a/.oxlintrc.json b/.oxlintrc.json index e45ab67fc..3e66f570e 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -26,11 +26,8 @@ { "patterns": [ { - "group": [ - "@databricks/sdk-experimental", - "@databricks/sdk-experimental/**" - ], - "message": "Import the Databricks SDK only through the wrapper in packages/shared/src/workspace-client. Add a re-export there if you need a new symbol." + "group": ["@databricks/sdk-*", "@databricks/sdk-*/**"], + "message": "Import the Databricks SDK only through the wrapper in packages/shared/src/workspace-client (legacy.ts for @databricks/sdk-experimental, modular.ts for the modular @databricks/sdk-* packages). Add a re-export there if you need a new symbol." } ] } diff --git a/docs/docs/api/appkit/Interface.WorkspaceClient.md b/docs/docs/api/appkit/Interface.WorkspaceClient.md index bf508bbe4..26a680581 100644 --- a/docs/docs/api/appkit/Interface.WorkspaceClient.md +++ b/docs/docs/api/appkit/Interface.WorkspaceClient.md @@ -86,20 +86,20 @@ Serving Endpoints. ### statementExecution ```ts -readonly statementExecution: StatementExecutionService; +readonly statementExecution: StatementExecutionClient; ``` -Statement Execution. +Statement Execution (modular SDK). *** ### warehouses ```ts -readonly warehouses: WarehousesService; +readonly warehouses: WarehousesClient; ``` -SQL Warehouses. +SQL Warehouses (modular SDK). ## Methods diff --git a/knip.json b/knip.json index e605829f5..5c5682bfb 100644 --- a/knip.json +++ b/knip.json @@ -8,7 +8,15 @@ ], "workspaces": { "packages/appkit": { - "ignoreDependencies": ["vitest", "@databricks/sdk-experimental"] + "ignoreDependencies": [ + "vitest", + "@databricks/sdk-auth", + "@databricks/sdk-core", + "@databricks/sdk-experimental", + "@databricks/sdk-options", + "@databricks/sdk-statementexecution", + "@databricks/sdk-warehouses" + ] }, "packages/appkit-ui": { "ignoreDependencies": ["tailwindcss", "tw-animate-css"] diff --git a/package.json b/package.json index 54c217804..0bb6b5b80 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,9 @@ "protobufjs@<7.6.2": "7.6.2", "qs@<6.15.2": "6.15.2", "size-sensor": "1.0.3" + }, + "patchedDependencies": { + "@databricks/sdk-statementexecution@0.46.0": "patches/@databricks__sdk-statementexecution@0.46.0.patch" } } } diff --git a/packages/appkit/package.json b/packages/appkit/package.json index 165211c4a..d7b06bfa2 100644 --- a/packages/appkit/package.json +++ b/packages/appkit/package.json @@ -71,7 +71,12 @@ "dependencies": { "@ast-grep/napi": "0.37.0", "@databricks/lakebase": "workspace:*", + "@databricks/sdk-auth": "0.46.0", + "@databricks/sdk-core": "0.46.0", "@databricks/sdk-experimental": "0.17.0", + "@databricks/sdk-options": "0.46.0", + "@databricks/sdk-statementexecution": "0.46.0", + "@databricks/sdk-warehouses": "0.46.0", "@mlflow/core": "0.4.0", "@opentelemetry/api": "1.9.0", "@opentelemetry/api-logs": "0.219.0", diff --git a/packages/appkit/src/connectors/sql-warehouse/arrow-schema.ts b/packages/appkit/src/connectors/sql-warehouse/arrow-schema.ts index 17d099e37..af5bfbb18 100644 --- a/packages/appkit/src/connectors/sql-warehouse/arrow-schema.ts +++ b/packages/appkit/src/connectors/sql-warehouse/arrow-schema.ts @@ -54,12 +54,12 @@ export function parseDatabricksType(typeText: string): DataType { export function buildEmptyArrowIPCBase64( columns: Array<{ name?: string; - type_text?: string; - type_name?: string; + typeText?: string; + typeName?: string; }>, ): string { const fields = columns.map((col, index) => { - const typeText = col.type_text ?? col.type_name ?? "STRING"; + const typeText = col.typeText ?? col.typeName ?? "STRING"; let dataType: DataType; try { dataType = parseDatabricksType(typeText); diff --git a/packages/appkit/src/connectors/sql-warehouse/client.ts b/packages/appkit/src/connectors/sql-warehouse/client.ts index 439658cbd..6e131c6e1 100644 --- a/packages/appkit/src/connectors/sql-warehouse/client.ts +++ b/packages/appkit/src/connectors/sql-warehouse/client.ts @@ -21,10 +21,14 @@ import { SpanStatusCode, TelemetryManager, } from "../../telemetry"; -import { - Context, - type sql, - type WorkspaceClient, +import type { + EndpointState, + ExecuteStatementRequest, + ExternalLink, + ResultData, + StatementResponse, + StatementStatus, + WorkspaceClient, } from "../../workspace-client"; import { buildEmptyArrowIPCBase64 } from "./arrow-schema"; import { executeStatementDefaults } from "./defaults"; @@ -40,9 +44,7 @@ const logger = createLogger("connectors:sql-warehouse"); * Arrow result to match the JSON path. Returns `undefined` when the manifest * carries no columns. */ -function arrowColumnNames( - response: sql.StatementResponse, -): string[] | undefined { +function arrowColumnNames(response: StatementResponse): string[] | undefined { const cols = response.manifest?.schema?.columns; if (!cols || cols.length === 0) return undefined; return cols.map((c, i) => @@ -50,6 +52,46 @@ function arrowColumnNames( ); } +/** + * Coerce the modular SDK's `bigint` row/byte counts back to `number` (the type + * the legacy SDK used). AppKit never does arithmetic on these — they are purely + * informational — but a stray `bigint` makes `JSON.stringify` throw ("Do not + * know how to serialize a BigInt") the instant the result is cached or written + * to an SSE frame. Reyden's INLINE + ARROW_STREAM result — which the analytics + * arrow path caches — carries them on `result`/`manifest`, so normalize every + * statement response at the SDK boundary. Mutates in place (the response is a + * fresh unmarshalled object, owned by the caller). + */ +const BIGINT_COUNT_FIELDS = ["rowOffset", "rowCount", "byteCount"] as const; + +function normalizeResultCounts(result: unknown): void { + if (!result || typeof result !== "object") return; + const r = result as Record; + for (const key of BIGINT_COUNT_FIELDS) { + if (typeof r[key] === "bigint") r[key] = Number(r[key]); + } + // EXTERNAL_LINKS entries carry the same count fields. + if (Array.isArray(r.externalLinks)) { + for (const link of r.externalLinks) normalizeResultCounts(link); + } +} + +function normalizeStatementCounts(response: T): T { + const manifest = response?.manifest as Record | undefined; + if (manifest) { + for (const key of ["totalRowCount", "totalByteCount"] as const) { + if (typeof manifest[key] === "bigint") + manifest[key] = Number(manifest[key]); + } + // Per-chunk `BaseChunkInfo` entries carry the same bigint count fields. + if (Array.isArray(manifest.chunks)) { + for (const chunk of manifest.chunks) normalizeResultCounts(chunk); + } + } + normalizeResultCounts(response?.result); + return response; +} + /** * Maximum size for inline Arrow IPC attachments (25 MiB decoded — the * Databricks Statement Execution API hard cap on INLINE responses). @@ -64,8 +106,8 @@ const MAX_INLINE_ATTACHMENT_BYTES = 25 * 1024 * 1024; /** * Safety cap on how many additional EXTERNAL_LINKS chunks * {@link SQLWarehouseConnector._resolveAllExternalLinks} will follow when the - * manifest omits `total_chunk_count`. High enough to cover any real result; - * only bounds a misbehaving warehouse with a cyclic `next_chunk_index`. + * manifest omits `totalChunkCount`. High enough to cover any real result; + * only bounds a misbehaving warehouse with a cyclic `nextChunkIndex`. */ const MAX_EXTERNAL_CHUNK_FOLLOWS = 10_000; @@ -105,7 +147,7 @@ const WAREHOUSE_RUNNING_CACHE_TTL_MS = 30_000; */ export interface WarehouseStatusUpdate { /** Current state from the SDK (RUNNING | STARTING | STOPPED | STOPPING | DELETED | DELETING). */ - state: sql.State; + state: EndpointState; /** Milliseconds elapsed since `ensureWarehouseRunning` was called. */ elapsedMs: number; /** 1-based attempt counter — useful for tests and telemetry. */ @@ -203,7 +245,7 @@ export class SQLWarehouseConnector { async executeStatement( workspaceClient: WorkspaceClient, - input: sql.ExecuteStatementRequest, + input: ExecuteStatementRequest, signal?: AbortSignal, ) { const startTime = Date.now(); @@ -220,7 +262,7 @@ export class SQLWarehouseConnector { kind: SpanKind.CLIENT, attributes: { "db.system": "databricks", - "db.warehouse_id": input.warehouse_id || "", + "db.warehouse_id": input.warehouseId || "", "db.catalog": input.catalog ?? "", "db.schema": input.schema ?? "", "db.statement": input.statement?.substring(0, 500) || "", @@ -252,52 +294,52 @@ export class SQLWarehouseConnector { throw ValidationError.missingField("statement"); } - if (!input.warehouse_id) { + if (!input.warehouseId) { throw ValidationError.missingField("warehouse_id"); } - const body: sql.ExecuteStatementRequest = { + const body: ExecuteStatementRequest = { statement: input.statement, parameters: input.parameters, - warehouse_id: input.warehouse_id, + warehouseId: input.warehouseId, catalog: input.catalog, schema: input.schema, - wait_timeout: - input.wait_timeout || executeStatementDefaults.wait_timeout, + waitTimeout: + input.waitTimeout || executeStatementDefaults.waitTimeout, disposition: input.disposition || executeStatementDefaults.disposition, format: input.format || executeStatementDefaults.format, - byte_limit: input.byte_limit, - row_limit: input.row_limit, - on_wait_timeout: - input.on_wait_timeout || executeStatementDefaults.on_wait_timeout, + byteLimit: input.byteLimit, + rowLimit: input.rowLimit, + onWaitTimeout: + input.onWaitTimeout || executeStatementDefaults.onWaitTimeout, }; span.addEvent("statement.submitting", { - "db.warehouse_id": input.warehouse_id, + "db.warehouse_id": input.warehouseId, }); const response = - await workspaceClient.statementExecution.executeStatement( - body, - this._createContext(signal), - ); + await workspaceClient.statementExecution.executeStatement(body, { + signal, + }); if (!response) { throw ConnectionError.apiFailure("SQL Warehouse"); } + normalizeStatementCounts(response); const status = response.status; - const statementId = response.statement_id as string; + const statementId = response.statementId as string; span.setAttribute("db.statement_id", statementId); span.addEvent("statement.submitted", { - "db.statement_id": response.statement_id, + "db.statement_id": response.statementId, "db.status": status?.state, }); let result: - | sql.StatementResponse - | { result: { statement_id: string; status: sql.StatementStatus } }; + | StatementResponse + | { result: { statement_id: string; status: StatementStatus } }; switch (status?.state) { case "RUNNING": @@ -322,7 +364,7 @@ export class SQLWarehouseConnector { case "FAILED": throw ExecutionError.statementFailed( status.error?.message, - status.error?.error_code, + status.error?.errorCode, ); case "CANCELED": throw ExecutionError.canceled(); @@ -336,7 +378,7 @@ export class SQLWarehouseConnector { const resultData = result.result as any; const rowCount = - resultData?.data?.length ?? resultData?.data_array?.length ?? 0; + resultData?.data?.length ?? resultData?.dataArray?.length ?? 0; if (rowCount > 0) { span.setAttribute("db.result.row_count", rowCount); @@ -344,7 +386,7 @@ export class SQLWarehouseConnector { const duration = Date.now() - startTime; logger.event()?.setContext("sql-warehouse", { - warehouse_id: input.warehouse_id, + warehouse_id: input.warehouseId, rows_returned: rowCount, query_duration_ms: duration, }); @@ -385,7 +427,7 @@ export class SQLWarehouseConnector { } const attributes = { - "db.warehouse_id": input.warehouse_id, + "db.warehouse_id": input.warehouseId, "db.catalog": input.catalog ?? "", "db.schema": input.schema ?? "", "db.statement": input.statement?.substring(0, 500) || "", @@ -622,9 +664,9 @@ export class SQLWarehouseConnector { ); } - const info = await workspaceClient.warehouses.get( + const info = await workspaceClient.warehouses.getWarehouse( { id: warehouseId }, - this._createContext(signal), + { signal }, ); const state = info?.state; const summary = info?.health?.summary; @@ -650,9 +692,9 @@ export class SQLWarehouseConnector { if (!didStart) { emitter.emit("STARTING", summary); onWarehouseStartIssued?.(); - await workspaceClient.warehouses.start( + await workspaceClient.warehouses.startWarehouse( { id: warehouseId }, - this._createContext(signal), + { signal }, ); didStart = true; } else { @@ -799,15 +841,14 @@ export class SQLWarehouseConnector { }); const response = - await workspaceClient.statementExecution.getStatement( - { - statement_id: statementId, - }, - this._createContext(signal), + await workspaceClient.statementExecution.getStatementResult( + { statementId }, + { signal }, ); if (!response) { throw ConnectionError.apiFailure("SQL Warehouse"); } + normalizeStatementCounts(response); const status = response.status; @@ -837,7 +878,7 @@ export class SQLWarehouseConnector { case "FAILED": throw ExecutionError.statementFailed( status.error?.message, - status.error?.error_code, + status.error?.errorCode, ); case "CANCELED": throw ExecutionError.canceled(); @@ -871,13 +912,13 @@ export class SQLWarehouseConnector { } private async _transformDataArray( - response: sql.StatementResponse, + response: StatementResponse, workspaceClient: WorkspaceClient, signal?: AbortSignal, ) { if (response.manifest?.format === "ARROW_STREAM") { const result = response.result as - | (sql.ResultData & { attachment?: string }) + | (ResultData & { attachment?: string }) | undefined; // Inline Arrow: pass the base64 IPC attachment through unmodified so @@ -893,20 +934,20 @@ export class SQLWarehouseConnector { // rather than omitting it) — it must NOT go down the streaming path // (`streamChunks([])` rejects), so fall through to synthesize an empty // Arrow table below. - if (result?.external_links && result.external_links.length > 0) { + if (result?.externalLinks && result.externalLinks.length > 0) { return this.updateWithArrowStatus(response, workspaceClient, signal); } // Empty result with a known schema: synthesize a zero-row Arrow IPC // attachment so the client always receives an Arrow Table for // ARROW_STREAM, regardless of whether the warehouse returned data. - // Note: an empty array (`data_array: []`) is truthy, so length-check + // Note: an empty array (`dataArray: []`) is truthy, so length-check // explicitly — otherwise zero-row responses fall through to the JSON // row transform below and return `[]` JSON rows instead of an Arrow // table. const hasNoRows = - !result?.data_array || - (Array.isArray(result.data_array) && result.data_array.length === 0); + !result?.dataArray || + (Array.isArray(result.dataArray) && result.dataArray.length === 0); if (hasNoRows && response.manifest?.schema?.columns) { const synthesized = buildEmptyArrowIPCBase64( response.manifest.schema.columns, @@ -917,19 +958,19 @@ export class SQLWarehouseConnector { }; } - // Inline data_array under ARROW_STREAM (rare): fall through to the + // Inline dataArray under ARROW_STREAM (rare): fall through to the // row transform below. The hook will receive `type: "result"` rows; // callers asking for ARROW_STREAM should not hit this path with // current Databricks warehouses. } - if (!response.result?.data_array || !response.manifest?.schema?.columns) { + if (!response.result?.dataArray || !response.manifest?.schema?.columns) { return response; } const columns = response.manifest.schema.columns; - const transformedData = response.result.data_array.map((row) => { + const transformedData = response.result.dataArray.map((row) => { const obj: Record = {}; row.forEach((value, index) => { const column = columns[index]; @@ -937,7 +978,7 @@ export class SQLWarehouseConnector { // attempt to parse JSON strings for string columns if ( - column?.type_name === "STRING" && + column?.typeName === "STRING" && typeof value === "string" && value && (value[0] === "{" || value[0] === "[") @@ -955,8 +996,8 @@ export class SQLWarehouseConnector { return obj; }); - // remove data_array - const { data_array: _data_array, ...restResult } = response.result; + // remove dataArray + const { dataArray: _dataArray, ...restResult } = response.result; return { ...response, result: { @@ -978,7 +1019,7 @@ export class SQLWarehouseConnector { * mechanism used for both INLINE and EXTERNAL_LINKS. */ private _validateArrowAttachment( - response: sql.StatementResponse, + response: StatementResponse, attachment: string, ) { // Cap the size to protect against unbounded inline payloads from @@ -1006,14 +1047,16 @@ export class SQLWarehouseConnector { return { ...response, result: { - ...(response.result as sql.ResultData & { + ...(response.result as ResultData & { attachment?: string; columnNames?: string[]; }), - // `statement_id` is a top-level field, not on `ResultData` — carry it + // `statementId` is a top-level field, not on `ResultData` — carry it // onto the result (as the EXTERNAL_LINKS path does) so the route can // advertise it in `X-Appkit-Arrow-Columns-Ref` for wide inline schemas. - statement_id: response.statement_id, + // Kept as the synthetic `statement_id` key (the connector→route wire + // contract), sourced from the modular SDK's camelCase `statementId`. + statement_id: response.statementId, columnNames, }, }; @@ -1023,26 +1066,26 @@ export class SQLWarehouseConnector { } private async updateWithArrowStatus( - response: sql.StatementResponse, + response: StatementResponse, workspaceClient: WorkspaceClient, signal?: AbortSignal, ): Promise<{ result: { statement_id: string; - status: sql.StatementStatus; + status: StatementStatus; columnNames?: string[]; - external_links?: sql.ExternalLink[]; + external_links?: ExternalLink[]; refreshChunkLink?: RefreshChunkLink; }; }> { - const statementId = response.statement_id as string; + const statementId = response.statementId as string; return { result: { statement_id: statementId, status: { state: response.status?.state, error: response.status?.error, - } as sql.StatementStatus, + } as StatementStatus, columnNames: arrowColumnNames(response), // Resolve the pre-signed links for EVERY chunk in the caller's own // execution context. Streaming these directly (see @@ -1069,9 +1112,9 @@ export class SQLWarehouseConnector { /** * Resolve pre-signed links for EVERY chunk of an EXTERNAL_LINKS result. * - * The execute/getStatement response carries only the first chunk's links - * (each link, except the last, exposes `next_chunk_index`); the remaining - * chunks are fetched with `getStatementResultChunkN`. Runs in the caller's + * The execute/getStatementResult response carries only the first chunk's links + * (each link, except the last, exposes `nextChunkIndex`); the remaining + * chunks are fetched with `getResultData`. Runs in the caller's * identity context (user creds for `.obo.sql`), so there is no cross-identity * fetch. Only the tiny link metadata is resolved eagerly — the bytes still * stream one chunk at a time downstream. Without this a multi-chunk result @@ -1080,29 +1123,29 @@ export class SQLWarehouseConnector { private async _resolveAllExternalLinks( workspaceClient: WorkspaceClient, statementId: string, - response: sql.StatementResponse, + response: StatementResponse, signal?: AbortSignal, - ): Promise { - const first = response.result?.external_links; + ): Promise { + const first = response.result?.externalLinks; if (!first || first.length === 0) return first; - const links: sql.ExternalLink[] = [...first]; + const links: ExternalLink[] = [...first]; // Bound the follow loop so a warehouse returning a cyclic/never-ending // `next_chunk_index` can't spin forever. The manifest's chunk count is the // natural bound; fall back to a generous safety cap if it's absent (real // results still terminate earlier when `next_chunk_index` becomes null) so // a missing count doesn't silently truncate a genuine multi-chunk result. const maxFetches = - response.manifest?.total_chunk_count ?? MAX_EXTERNAL_CHUNK_FOLLOWS; + response.manifest?.totalChunkCount ?? MAX_EXTERNAL_CHUNK_FOLLOWS; let next = this._nextChunkIndex(first); for (let fetches = 0; next != null && fetches < maxFetches; fetches++) { if (signal?.aborted) throw ExecutionError.canceled(); - const chunk = - await workspaceClient.statementExecution.getStatementResultChunkN( - { statement_id: statementId, chunk_index: next }, - this._createContext(signal), - ); - const chunkLinks = chunk.external_links ?? []; + const chunk = await workspaceClient.statementExecution.getResultData( + { statementId, chunkIndex: next }, + { signal }, + ); + normalizeResultCounts(chunk); + const chunkLinks = chunk.externalLinks ?? []; if (chunkLinks.length === 0) break; links.push(...chunkLinks); next = this._nextChunkIndex(chunkLinks); @@ -1110,32 +1153,32 @@ export class SQLWarehouseConnector { return links; } - /** The `next_chunk_index` advertised by a chunk's links, if any. */ - private _nextChunkIndex(links: sql.ExternalLink[]): number | undefined { + /** The `nextChunkIndex` advertised by a chunk's links, if any. */ + private _nextChunkIndex(links: ExternalLink[]): number | undefined { for (const link of links) { - if (link.next_chunk_index != null) return link.next_chunk_index; + if (link.nextChunkIndex != null) return link.nextChunkIndex; } return undefined; } /** * A closure that re-mints a single chunk's pre-signed link via - * `getStatementResultChunkN`, bound to the caller's workspace client + + * `getResultData`, bound to the caller's workspace client + * statement id. Created here (in the caller's identity context) so the * streamer — which runs outside that context — can refresh an expired link - * for `.obo.sql` statements without a cross-identity `getStatement`. + * for `.obo.sql` statements without a cross-identity `getStatementResult`. */ private _makeChunkLinkRefresher( workspaceClient: WorkspaceClient, statementId: string, ): RefreshChunkLink { return async (chunkIndex, signal) => { - const chunk = - await workspaceClient.statementExecution.getStatementResultChunkN( - { statement_id: statementId, chunk_index: chunkIndex }, - this._createContext(signal), - ); - return chunk.external_links?.find((l) => l.chunk_index === chunkIndex); + const chunk = await workspaceClient.statementExecution.getResultData( + { statementId, chunkIndex }, + { signal }, + ); + normalizeResultCounts(chunk); + return chunk.externalLinks?.find((l) => l.chunkIndex === chunkIndex); }; } @@ -1147,7 +1190,7 @@ export class SQLWarehouseConnector { * the pre-signed URLs need no auth to download. */ streamExternalLinks( - chunks: sql.ExternalLink[], + chunks: ExternalLink[], signal?: AbortSignal, refresh?: RefreshChunkLink, ): AsyncGenerator { @@ -1165,10 +1208,12 @@ export class SQLWarehouseConnector { jobId: string, signal?: AbortSignal, ): Promise { - const response = await workspaceClient.statementExecution.getStatement( - { statement_id: jobId }, - this._createContext(signal), - ); + const response = + await workspaceClient.statementExecution.getStatementResult( + { statementId: jobId }, + { signal }, + ); + normalizeStatementCounts(response); return arrowColumnNames(response); } @@ -1187,25 +1232,19 @@ export class SQLWarehouseConnector { if (error instanceof AppKitError) { throw error; } + // The legacy SDK exposed the Databricks error code as `errorCode`; the + // modular SDK's `ApiError` carries it as `code` (e.g. "INVALID_PARAMETER_VALUE"). + // Read either, so callers can still branch on the stable code — notably the + // analytics arrow disposition/format fallback, which keys on + // INVALID_PARAMETER_VALUE / NOT_IMPLEMENTED to switch INLINE↔EXTERNAL_LINKS. const sdkErrorCode = - error && typeof error === "object" && "errorCode" in error - ? (error as { errorCode?: unknown }).errorCode + error && typeof error === "object" + ? ((error as { errorCode?: unknown }).errorCode ?? + (error as { code?: unknown }).code) : undefined; throw ExecutionError.statementFailed( error instanceof Error ? error.message : String(error), typeof sdkErrorCode === "string" ? sdkErrorCode : undefined, ); } - - // create context for cancellation token - private _createContext(signal?: AbortSignal) { - return new Context({ - cancellationToken: { - isCancellationRequested: signal?.aborted ?? false, - onCancellationRequested: (cb: () => void) => { - signal?.addEventListener("abort", cb, { once: true }); - }, - }, - }); - } } diff --git a/packages/appkit/src/connectors/sql-warehouse/defaults.ts b/packages/appkit/src/connectors/sql-warehouse/defaults.ts index b046a5c4a..3a57c8058 100644 --- a/packages/appkit/src/connectors/sql-warehouse/defaults.ts +++ b/packages/appkit/src/connectors/sql-warehouse/defaults.ts @@ -1,18 +1,18 @@ -import type { sql } from "../../workspace-client"; +import type { ExecuteStatementRequest } from "../../workspace-client"; interface ExecuteStatementDefaults { - wait_timeout: string; - disposition: sql.ExecuteStatementRequest["disposition"]; - format: sql.ExecuteStatementRequest["format"]; - on_wait_timeout: sql.ExecuteStatementRequest["on_wait_timeout"]; + waitTimeout: string; + disposition: ExecuteStatementRequest["disposition"]; + format: ExecuteStatementRequest["format"]; + onWaitTimeout: ExecuteStatementRequest["onWaitTimeout"]; timeout: number; } // @TODO: Make these configurable globally and validate right values export const executeStatementDefaults: ExecuteStatementDefaults = { - wait_timeout: "30s", + waitTimeout: "30s", disposition: "INLINE", format: "JSON_ARRAY", - on_wait_timeout: "CONTINUE", + onWaitTimeout: "CONTINUE", timeout: 60000, }; diff --git a/packages/appkit/src/connectors/sql-warehouse/tests/arrow-schema.test.ts b/packages/appkit/src/connectors/sql-warehouse/tests/arrow-schema.test.ts index d8f52f016..b7826e87e 100644 --- a/packages/appkit/src/connectors/sql-warehouse/tests/arrow-schema.test.ts +++ b/packages/appkit/src/connectors/sql-warehouse/tests/arrow-schema.test.ts @@ -428,11 +428,11 @@ describe("parseDatabricksType — error / robustness", () => { describe("buildEmptyArrowIPCBase64", () => { test("produces a decodable empty Arrow Table with the right schema", () => { const columns = [ - { name: "user_id", type_text: "BIGINT" }, - { name: "name", type_text: "STRING" }, - { name: "created_at", type_text: "TIMESTAMP" }, - { name: "balance", type_text: "DECIMAL(10,2)" }, - { name: "active", type_text: "BOOLEAN" }, + { name: "user_id", typeText: "BIGINT" }, + { name: "name", typeText: "STRING" }, + { name: "created_at", typeText: "TIMESTAMP" }, + { name: "balance", typeText: "DECIMAL(10,2)" }, + { name: "active", typeText: "BOOLEAN" }, ]; const b64 = buildEmptyArrowIPCBase64(columns); const buf = Buffer.from(b64, "base64"); @@ -463,9 +463,9 @@ describe("buildEmptyArrowIPCBase64", () => { test("round-trips nested types end-to-end", () => { const columns = [ - { name: "tags", type_text: "ARRAY" }, - { name: "meta", type_text: "STRUCT" }, - { name: "counts", type_text: "MAP" }, + { name: "tags", typeText: "ARRAY" }, + { name: "meta", typeText: "STRUCT" }, + { name: "counts", typeText: "MAP" }, ]; const buf = Buffer.from(buildEmptyArrowIPCBase64(columns), "base64"); const table = tableFromIPC(buf); @@ -476,8 +476,8 @@ describe("buildEmptyArrowIPCBase64", () => { expect(table.schema.fields[2]?.type).toBeInstanceOf(Map_); }); - test("falls back from type_text to type_name when type_text missing", () => { - const columns = [{ name: "id", type_name: "BIGINT" }]; + test("falls back from typeText to typeName when typeText missing", () => { + const columns = [{ name: "id", typeName: "BIGINT" }]; const buf = Buffer.from(buildEmptyArrowIPCBase64(columns), "base64"); const table = tableFromIPC(buf); expect( @@ -487,8 +487,8 @@ describe("buildEmptyArrowIPCBase64", () => { test("unknown type degrades to Utf8 without throwing", () => { const columns = [ - { name: "id", type_text: "BIGINT" }, - { name: "weird", type_text: "FUTURE_TYPE_NOT_YET_SUPPORTED" }, + { name: "id", typeText: "BIGINT" }, + { name: "weird", typeText: "FUTURE_TYPE_NOT_YET_SUPPORTED" }, ]; const buf = Buffer.from(buildEmptyArrowIPCBase64(columns), "base64"); const table = tableFromIPC(buf); @@ -499,7 +499,7 @@ describe("buildEmptyArrowIPCBase64", () => { }); test("missing column name gets a synthesized placeholder", () => { - const columns = [{ type_text: "STRING" }, { name: "", type_text: "INT" }]; + const columns = [{ typeText: "STRING" }, { name: "", typeText: "INT" }]; const buf = Buffer.from(buildEmptyArrowIPCBase64(columns), "base64"); const table = tableFromIPC(buf); expect(table.schema.fields[0]?.name).toBe("column_0"); diff --git a/packages/appkit/src/connectors/sql-warehouse/tests/client.test.ts b/packages/appkit/src/connectors/sql-warehouse/tests/client.test.ts index 5a945b0cc..8344df2fc 100644 --- a/packages/appkit/src/connectors/sql-warehouse/tests/client.test.ts +++ b/packages/appkit/src/connectors/sql-warehouse/tests/client.test.ts @@ -1,7 +1,10 @@ import { tableFromIPC } from "apache-arrow"; import { describe, expect, test, vi } from "vitest"; -import type { sql } from "../../../workspace-client"; +import type { + ExternalLink, + StatementResponse, +} from "../../../workspace-client"; vi.mock("../../../telemetry", () => { const mockMeter = { @@ -40,11 +43,11 @@ function createConnector() { // `_transformDataArray` is async — it paginates multi-chunk EXTERNAL_LINKS // results. The workspace client is only touched when following -// `next_chunk_index`, so a bare stub suffices for the inline / JSON / +// `nextChunkIndex`, so a bare stub suffices for the inline / JSON / // single-chunk cases; the multi-chunk tests pass a real mock. function transform( connector: SQLWarehouseConnector, - response: sql.StatementResponse, + response: StatementResponse, workspaceClient: unknown = {}, ) { return (connector as any)._transformDataArray(response, workspaceClient); @@ -58,11 +61,11 @@ const REAL_ARROW_ATTACHMENT = describe("SQLWarehouseConnector._transformDataArray", () => { describe("classic warehouse (JSON_ARRAY + INLINE)", () => { - test("transforms data_array rows into named objects", async () => { + test("transforms dataArray rows into named objects", async () => { const connector = createConnector(); // Real response shape from classic warehouse: INLINE + JSON_ARRAY const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "JSON_ARRAY", @@ -71,14 +74,14 @@ describe("SQLWarehouseConnector._transformDataArray", () => { columns: [ { name: "test_col", - type_text: "INT", - type_name: "INT", + typeText: "INT", + typeName: "INT", position: 0, }, { name: "test_col2", - type_text: "INT", - type_name: "INT", + typeText: "INT", + typeName: "INT", position: 1, }, ], @@ -87,33 +90,33 @@ describe("SQLWarehouseConnector._transformDataArray", () => { truncated: false, }, result: { - data_array: [["1", "2"]], + dataArray: [["1", "2"]], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result.result.data).toEqual([{ test_col: "1", test_col2: "2" }]); - expect(result.result.data_array).toBeUndefined(); + expect(result.result.dataArray).toBeUndefined(); }); test("parses JSON strings in STRING columns", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "JSON_ARRAY", schema: { columns: [ - { name: "id", type_name: "INT" }, - { name: "metadata", type_name: "STRING" }, + { name: "id", typeName: "INT" }, + { name: "metadata", typeName: "STRING" }, ], }, }, result: { - data_array: [["1", '{"key":"value"}']], + dataArray: [["1", '{"key":"value"}']], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result.result.data[0].metadata).toEqual({ key: "value" }); @@ -125,26 +128,26 @@ describe("SQLWarehouseConnector._transformDataArray", () => { const connector = createConnector(); // Real response shape from classic warehouse: EXTERNAL_LINKS + ARROW_STREAM const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { columns: [ - { name: "test_col", type_name: "INT" }, - { name: "test_col2", type_name: "INT" }, + { name: "test_col", typeName: "INT" }, + { name: "test_col2", typeName: "INT" }, ], }, }, result: { - external_links: [ + externalLinks: [ { - external_link: "https://storage.example.com/chunk0", + externalLink: "https://storage.example.com/chunk0", expiration: "2026-04-15T00:00:00Z", }, ], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result.result.statement_id).toBe("stmt-1"); @@ -156,9 +159,9 @@ describe("SQLWarehouseConnector._transformDataArray", () => { test("passes attachment through unchanged for client-side decoding", async () => { const connector = createConnector(); // Real response shape from serverless warehouse: INLINE + ARROW_STREAM - // Data arrives in result.attachment as base64-encoded Arrow IPC, not data_array. + // Data arrives in result.attachment as base64-encoded Arrow IPC, not dataArray. const response = { - statement_id: "00000001-test-stmt", + statementId: "00000001-test-stmt", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", @@ -167,30 +170,30 @@ describe("SQLWarehouseConnector._transformDataArray", () => { columns: [ { name: "test_col", - type_text: "INT", - type_name: "INT", + typeText: "INT", + typeName: "INT", position: 0, }, { name: "test_col2", - type_text: "INT", - type_name: "INT", + typeText: "INT", + typeName: "INT", position: 1, }, ], - total_chunk_count: 1, - chunks: [{ chunk_index: 0, row_offset: 0, row_count: 1 }], + totalChunkCount: 1, + chunks: [{ chunkIndex: 0, row_offset: 0, row_count: 1 }], total_row_count: 1, }, truncated: false, }, result: { - chunk_index: 0, + chunkIndex: 0, row_offset: 0, row_count: 1, attachment: REAL_ARROW_ATTACHMENT, }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result.result.attachment).toBe(REAL_ARROW_ATTACHMENT); @@ -206,56 +209,56 @@ describe("SQLWarehouseConnector._transformDataArray", () => { test("preserves manifest and status alongside attachment", async () => { const connector = createConnector(); const response = { - statement_id: "00000001-test-stmt", + statementId: "00000001-test-stmt", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { columns: [ - { name: "test_col", type_name: "INT" }, - { name: "test_col2", type_name: "INT" }, + { name: "test_col", typeName: "INT" }, + { name: "test_col2", typeName: "INT" }, ], }, }, result: { - chunk_index: 0, + chunkIndex: 0, row_count: 1, attachment: REAL_ARROW_ATTACHMENT, }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); // Manifest, statement_id, and attachment are all preserved expect(result.manifest.format).toBe("ARROW_STREAM"); - expect(result.statement_id).toBe("00000001-test-stmt"); + expect(result.statementId).toBe("00000001-test-stmt"); expect(result.result.attachment).toBe(REAL_ARROW_ATTACHMENT); }); test("synthesizes an empty Arrow IPC attachment for empty results so the client always gets a Table", async () => { const connector = createConnector(); - // Empty result: no attachment, no data_array, no external_links — but + // Empty result: no attachment, no dataArray, no external_links — but // the manifest still describes the schema. The connector should fill in // `attachment` with a zero-row Arrow IPC matching the schema. const response = { - statement_id: "stmt-empty", + statementId: "stmt-empty", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { columns: [ - { name: "user_id", type_text: "BIGINT", type_name: "BIGINT" }, - { name: "name", type_text: "STRING", type_name: "STRING" }, + { name: "user_id", typeText: "BIGINT", typeName: "BIGINT" }, + { name: "name", typeText: "STRING", typeName: "STRING" }, { name: "balance", - type_text: "DECIMAL(10,2)", - type_name: "DECIMAL", + typeText: "DECIMAL(10,2)", + typeName: "DECIMAL", }, ], }, total_row_count: 0, }, result: {}, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const transformed = await transform(connector, response); const attachment: string = transformed.result.attachment; @@ -275,18 +278,18 @@ describe("SQLWarehouseConnector._transformDataArray", () => { test("does NOT synthesize an attachment when external_links are present", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-ext", + statementId: "stmt-ext", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", - schema: { columns: [{ name: "x", type_text: "INT" }] }, + schema: { columns: [{ name: "x", typeText: "INT" }] }, }, result: { - external_links: [ - { external_link: "https://example.com/x", expiration: "9999" }, + externalLinks: [ + { externalLink: "https://example.com/x", expiration: "9999" }, ], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const transformed = await transform(connector, response); // External-links path returns the statement_id projection — no attachment. @@ -296,21 +299,21 @@ describe("SQLWarehouseConnector._transformDataArray", () => { test("empty external_links array is a zero-row result → synthesizes an empty table (not the streaming path)", async () => { const connector = createConnector(); - // Some warehouses emit `external_links: []` for a zero-row result rather + // Some warehouses emit `externalLinks: []` for a zero-row result rather // than omitting it. An empty array must NOT go down the streaming path // (streamChunks([]) rejects) — synthesize an empty Arrow table instead. const response = { - statement_id: "stmt-empty-ext", + statementId: "stmt-empty-ext", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { - columns: [{ name: "x", type_text: "INT", type_name: "INT" }], + columns: [{ name: "x", typeText: "INT", typeName: "INT" }], }, total_row_count: 0, }, - result: { external_links: [] }, - } as unknown as sql.StatementResponse; + result: { externalLinks: [] }, + } as unknown as StatementResponse; const transformed = await transform(connector, response); const attachment: string = transformed.result.attachment; @@ -323,11 +326,11 @@ describe("SQLWarehouseConnector._transformDataArray", () => { test("does NOT synthesize an attachment when schema is missing", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-no-schema", + statementId: "stmt-no-schema", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: {}, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const transformed = await transform(connector, response); // Without a schema we cannot build a Table — pass through unchanged. @@ -340,11 +343,11 @@ describe("SQLWarehouseConnector._transformDataArray", () => { // base64 chars decodes to ~27 MiB, comfortably above the limit. const oversized = "A".repeat(36 * 1024 * 1024); const response = { - statement_id: "stmt-oversized", + statementId: "stmt-oversized", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: oversized }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; await expect(transform(connector, response)).rejects.toThrow( /exceeds maximum size/, @@ -352,28 +355,28 @@ describe("SQLWarehouseConnector._transformDataArray", () => { }); }); - describe("ARROW_STREAM with data_array (hypothetical inline variant)", () => { - test("transforms data_array like JSON_ARRAY path", async () => { + describe("ARROW_STREAM with dataArray (hypothetical inline variant)", () => { + test("transforms dataArray like JSON_ARRAY path", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { columns: [ - { name: "id", type_name: "INT" }, - { name: "value", type_name: "STRING" }, + { name: "id", typeName: "INT" }, + { name: "value", typeName: "STRING" }, ], }, }, result: { - data_array: [ + dataArray: [ ["1", "hello"], ["2", "world"], ], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result.result.data).toEqual([ @@ -384,88 +387,88 @@ describe("SQLWarehouseConnector._transformDataArray", () => { }); describe("edge cases", () => { - test("returns response unchanged when no data_array, attachment, or schema", async () => { + test("returns response unchanged when no dataArray, attachment, or schema", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "JSON_ARRAY" }, result: {}, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); expect(result).toBe(response); }); - test("attachment takes priority over data_array when both present", async () => { + test("attachment takes priority over dataArray when both present", async () => { const connector = createConnector(); const response = { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", schema: { columns: [ - { name: "test_col", type_name: "INT" }, - { name: "test_col2", type_name: "INT" }, + { name: "test_col", typeName: "INT" }, + { name: "test_col2", typeName: "INT" }, ], }, }, result: { attachment: REAL_ARROW_ATTACHMENT, - data_array: [["999", "999"]], + dataArray: [["999", "999"]], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; const result = await transform(connector, response); - // Should pass attachment through (client decodes), not transform data_array + // Should pass attachment through (client decodes), not transform dataArray expect(result.result.attachment).toBe(REAL_ARROW_ATTACHMENT); expect(result.result.data).toBeUndefined(); }); }); describe("multi-chunk EXTERNAL_LINKS pagination", () => { - function multiChunkResponse(totalChunks: number): sql.StatementResponse { + function multiChunkResponse(totalChunks: number): StatementResponse { return { - statement_id: "stmt-multi", + statementId: "stmt-multi", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM", - total_chunk_count: totalChunks, - schema: { columns: [{ name: "x", type_name: "INT" }] }, + totalChunkCount: totalChunks, + schema: { columns: [{ name: "x", typeName: "INT" }] }, }, result: { - external_links: [ + externalLinks: [ { - chunk_index: 0, - external_link: "https://example.com/chunk0", - next_chunk_index: 1, + chunkIndex: 0, + externalLink: "https://example.com/chunk0", + nextChunkIndex: 1, }, ], }, - } as unknown as sql.StatementResponse; + } as unknown as StatementResponse; } - test("follows next_chunk_index to resolve every chunk's links", async () => { + test("follows nextChunkIndex to resolve every chunk's links", async () => { const connector = createConnector(); - const getStatementResultChunkN = vi + const getResultData = vi .fn() .mockResolvedValueOnce({ - external_links: [ + externalLinks: [ { - chunk_index: 1, - external_link: "https://example.com/chunk1", - next_chunk_index: 2, + chunkIndex: 1, + externalLink: "https://example.com/chunk1", + nextChunkIndex: 2, }, ], }) .mockResolvedValueOnce({ - external_links: [ - { chunk_index: 2, external_link: "https://example.com/chunk2" }, + externalLinks: [ + { chunkIndex: 2, externalLink: "https://example.com/chunk2" }, ], }); const workspaceClient = { - statementExecution: { getStatementResultChunkN }, + statementExecution: { getResultData }, }; const result = await transform( @@ -474,16 +477,14 @@ describe("SQLWarehouseConnector._transformDataArray", () => { workspaceClient, ); - expect(getStatementResultChunkN).toHaveBeenCalledTimes(2); - expect(getStatementResultChunkN).toHaveBeenNthCalledWith( + expect(getResultData).toHaveBeenCalledTimes(2); + expect(getResultData).toHaveBeenNthCalledWith( 1, - expect.objectContaining({ statement_id: "stmt-multi", chunk_index: 1 }), + expect.objectContaining({ statementId: "stmt-multi", chunkIndex: 1 }), expect.anything(), ); expect( - result.result.external_links.map( - (l: sql.ExternalLink) => l.external_link, - ), + result.result.external_links.map((l: ExternalLink) => l.externalLink), ).toEqual([ "https://example.com/chunk0", "https://example.com/chunk1", @@ -491,20 +492,20 @@ describe("SQLWarehouseConnector._transformDataArray", () => { ]); }); - test("is bounded by total_chunk_count when next_chunk_index never terminates", async () => { + test("is bounded by totalChunkCount when nextChunkIndex never terminates", async () => { const connector = createConnector(); // Misbehaving warehouse: always advertises another chunk. - const getStatementResultChunkN = vi.fn().mockResolvedValue({ - external_links: [ + const getResultData = vi.fn().mockResolvedValue({ + externalLinks: [ { - chunk_index: 1, - external_link: "https://example.com/loop", - next_chunk_index: 99, + chunkIndex: 1, + externalLink: "https://example.com/loop", + nextChunkIndex: 99, }, ], }); const workspaceClient = { - statementExecution: { getStatementResultChunkN }, + statementExecution: { getResultData }, }; const result = await transform( @@ -513,8 +514,8 @@ describe("SQLWarehouseConnector._transformDataArray", () => { workspaceClient, ); - // Terminates (no hang) — capped at total_chunk_count fetches. - expect(getStatementResultChunkN).toHaveBeenCalledTimes(2); + // Terminates (no hang) — capped at totalChunkCount fetches. + expect(getResultData).toHaveBeenCalledTimes(2); expect(result.result.external_links.length).toBeGreaterThan(0); }); }); diff --git a/packages/appkit/src/connectors/sql-warehouse/warehouse-status-emitter.ts b/packages/appkit/src/connectors/sql-warehouse/warehouse-status-emitter.ts index aba8488c3..a9061f61d 100644 --- a/packages/appkit/src/connectors/sql-warehouse/warehouse-status-emitter.ts +++ b/packages/appkit/src/connectors/sql-warehouse/warehouse-status-emitter.ts @@ -1,5 +1,5 @@ import type { Span } from "../../telemetry"; -import type { sql } from "../../workspace-client"; +import type { EndpointState } from "../../workspace-client"; import type { WarehouseStatusUpdate } from "./client"; /** @@ -10,7 +10,7 @@ import type { WarehouseStatusUpdate } from "./client"; */ export class WarehouseStatusEmitter { attempt = 0; - private lastEmittedState: sql.State | null = null; + private lastEmittedState: EndpointState | null = null; constructor( private readonly span: Span, @@ -18,7 +18,7 @@ export class WarehouseStatusEmitter { private readonly onStatus: (update: WarehouseStatusUpdate) => void, ) {} - emit(state: sql.State, summary: string | undefined): void { + emit(state: EndpointState, summary: string | undefined): void { this.attempt += 1; this.span.addEvent("warehouse.status", { "db.warehouse.state": state, diff --git a/packages/appkit/src/connectors/tests/sql-warehouse.test.ts b/packages/appkit/src/connectors/tests/sql-warehouse.test.ts index 285fa8d0b..af1480aa1 100644 --- a/packages/appkit/src/connectors/tests/sql-warehouse.test.ts +++ b/packages/appkit/src/connectors/tests/sql-warehouse.test.ts @@ -61,7 +61,7 @@ describe("SQLWarehouseConnector", () => { await expect( connector.executeStatement(mockWorkspaceClient as any, { statement: sensitiveStatement, - warehouse_id: "test-warehouse", + warehouseId: "test-warehouse", }), ).rejects.toThrow(); @@ -89,7 +89,9 @@ describe("SQLWarehouseConnector", () => { statement_id: "stmt-123", status: { state: "RUNNING" }, }), - getStatement: vi.fn().mockRejectedValue(new Error("polling timeout")), + getStatementResult: vi + .fn() + .mockRejectedValue(new Error("polling timeout")), }, config: { host: "https://test.databricks.com" }, }; @@ -97,7 +99,7 @@ describe("SQLWarehouseConnector", () => { await expect( connector.executeStatement(mockWorkspaceClient as any, { statement: "SELECT secret_data FROM vault", - warehouse_id: "test-warehouse", + warehouseId: "test-warehouse", }), ).rejects.toThrow(); @@ -118,6 +120,141 @@ describe("SQLWarehouseConnector", () => { }); }); + describe("statement error-code propagation", () => { + let connector: SQLWarehouseConnector; + + beforeEach(() => { + vi.clearAllMocks(); + connector = new SQLWarehouseConnector({ timeout: 5000 }); + }); + + // Regression: the modular `@databricks/sdk-core` `ApiError` carries the + // Databricks error code on `.code`, whereas the legacy SDK used + // `.errorCode`. The analytics arrow disposition/format fallback keys on + // this code ("INVALID_PARAMETER_VALUE" / "NOT_IMPLEMENTED") to switch + // INLINE→EXTERNAL_LINKS, so the connector MUST surface either field as + // `ExecutionError.errorCode` — reading only `.errorCode` broke every arrow + // query (the INLINE+ARROW_STREAM probe rejection went unrecognized). + test("surfaces the modular SDK ApiError.code as ExecutionError.errorCode", async () => { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + class FakeApiError extends Error { + readonly code = "INVALID_PARAMETER_VALUE"; + } + const mockWorkspaceClient = { + statementExecution: { + executeStatement: vi + .fn() + .mockRejectedValue( + new FakeApiError( + "Incompatible parameters: The format field must be JSON_ARRAY when the disposition field is INLINE.", + ), + ), + }, + config: { host: "https://test.databricks.com" }, + }; + + await expect( + connector.executeStatement(mockWorkspaceClient as any, { + statement: "SELECT 1", + warehouseId: "test-warehouse", + disposition: "INLINE", + format: "ARROW_STREAM", + }), + ).rejects.toMatchObject({ errorCode: "INVALID_PARAMETER_VALUE" }); + + errorSpy.mockRestore(); + }); + + // A failed statement STATUS (not a thrown ApiError) still carries the code + // on `status.error.errorCode` — the SDK unmarshals `error_code` there, so + // that path was already correct and must stay so. + test("surfaces status.error.errorCode from a FAILED statement status", async () => { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + const mockWorkspaceClient = { + statementExecution: { + executeStatement: vi.fn().mockResolvedValue({ + statementId: "stmt-123", + status: { + state: "FAILED", + error: { + errorCode: "INVALID_PARAMETER_VALUE", + message: "bad parameter", + }, + }, + }), + }, + config: { host: "https://test.databricks.com" }, + }; + + await expect( + connector.executeStatement(mockWorkspaceClient as any, { + statement: "SELECT 1", + warehouseId: "test-warehouse", + }), + ).rejects.toMatchObject({ errorCode: "INVALID_PARAMETER_VALUE" }); + + errorSpy.mockRestore(); + }); + }); + + describe("bigint count normalization", () => { + let connector: SQLWarehouseConnector; + + beforeEach(() => { + vi.clearAllMocks(); + connector = new SQLWarehouseConnector({ timeout: 5000 }); + }); + + // Regression: the modular SDK types rowCount/byteCount/rowOffset (and the + // per-chunk BaseChunkInfo counts) as `bigint`, whereas the legacy SDK used + // `number`. Reyden's INLINE+ARROW_STREAM result is cached by the analytics + // arrow path, and `JSON.stringify` throws ("Do not know how to serialize a + // BigInt") on any surviving bigint — which broke EVERY query on Reyden. The + // connector must coerce these to `number` at the SDK boundary so the result + // stays serializable for the cache / SSE frames. + test("coerces bigint manifest/result/chunk counts so the result is JSON-serializable", async () => { + const mockWorkspaceClient = { + statementExecution: { + executeStatement: vi.fn().mockResolvedValue({ + statementId: "stmt-1", + status: { state: "SUCCEEDED" }, + manifest: { + format: "JSON_ARRAY", + totalRowCount: 2n, + totalByteCount: 100n, + chunks: [ + { chunkIndex: 0, rowOffset: 0n, rowCount: 2n, byteCount: 100n }, + ], + schema: { columns: [{ name: "id", typeName: "INT" }] }, + }, + result: { + dataArray: [["1"], ["2"]], + rowOffset: 0n, + rowCount: 2n, + byteCount: 100n, + }, + }), + }, + config: { host: "https://test.databricks.com" }, + }; + + const out: any = await connector.executeStatement( + mockWorkspaceClient as any, + { statement: "SELECT id FROM t", warehouseId: "reyden" }, + ); + + // The arrow cache serializes exactly this — it must not throw. + expect(() => JSON.stringify(out)).not.toThrow(); + // Counts are coerced to number (legacy parity), including per-chunk ones. + expect(typeof out.manifest.totalRowCount).toBe("number"); + expect(typeof out.manifest.totalByteCount).toBe("number"); + expect(typeof out.manifest.chunks[0].byteCount).toBe("number"); + expect(typeof out.result.rowCount).toBe("number"); + }); + }); + describe("ensureWarehouseRunning", () => { let connector: SQLWarehouseConnector; @@ -137,7 +274,9 @@ describe("SQLWarehouseConnector", () => { test("emits a single RUNNING update and returns when warehouse is already running", async () => { const get = vi.fn().mockResolvedValue({ state: "RUNNING" }); const start = vi.fn(); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const updates: any[] = []; await connector.ensureWarehouseRunning(wsClient as any, "wh-1", { @@ -158,7 +297,9 @@ describe("SQLWarehouseConnector", () => { .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); const start = vi.fn().mockResolvedValue(undefined); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const updates: any[] = []; const promise = connector.ensureWarehouseRunning( @@ -191,7 +332,9 @@ describe("SQLWarehouseConnector", () => { .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); const start = vi.fn(); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const updates: any[] = []; const promise = connector.ensureWarehouseRunning( @@ -212,7 +355,9 @@ describe("SQLWarehouseConnector", () => { test("rejects when warehouse is DELETED", async () => { const get = vi.fn().mockResolvedValue({ state: "DELETED" }); const start = vi.fn(); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const updates: any[] = []; await expect( @@ -228,7 +373,9 @@ describe("SQLWarehouseConnector", () => { test("rejects when warehouse is DELETING", async () => { const get = vi.fn().mockResolvedValue({ state: "DELETING" }); const start = vi.fn(); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const updates: any[] = []; await expect( @@ -243,7 +390,9 @@ describe("SQLWarehouseConnector", () => { test("aborts immediately when signal is already aborted", async () => { const get = vi.fn(); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const controller = new AbortController(); controller.abort(); @@ -258,7 +407,9 @@ describe("SQLWarehouseConnector", () => { test("times out if warehouse never reaches RUNNING", async () => { const get = vi.fn().mockResolvedValue({ state: "STARTING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const promise = connector.ensureWarehouseRunning( wsClient as any, @@ -281,7 +432,7 @@ describe("SQLWarehouseConnector", () => { test("rejects when warehouse_id is empty", async () => { const wsClient = { - warehouses: { get: vi.fn(), start: vi.fn() }, + warehouses: { getWarehouse: vi.fn(), startWarehouse: vi.fn() }, }; await expect( @@ -293,7 +444,9 @@ describe("SQLWarehouseConnector", () => { test("skips the SDK round-trip on a subsequent call within the recently-running TTL", async () => { const get = vi.fn().mockResolvedValue({ state: "RUNNING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const updates1: any[] = []; await connector.ensureWarehouseRunning(wsClient as any, "wh-cache", { @@ -314,7 +467,9 @@ describe("SQLWarehouseConnector", () => { test("rejects with ConfigurationError when STOPPED and autoStart is false", async () => { const get = vi.fn().mockResolvedValue({ state: "STOPPED" }); const start = vi.fn(); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; await expect( connector.ensureWarehouseRunning(wsClient as any, "wh-no-auto", { @@ -332,7 +487,9 @@ describe("SQLWarehouseConnector", () => { .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const updates: any[] = []; const promise = connector.ensureWarehouseRunning( @@ -356,7 +513,9 @@ describe("SQLWarehouseConnector", () => { const sensitive = "getaddrinfo ENOTFOUND adb-1234567890.10.azuredatabricks.net"; const get = vi.fn().mockRejectedValue(new Error(sensitive)); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; await expect( connector.ensureWarehouseRunning(wsClient as any, "wh-leak", { @@ -381,7 +540,9 @@ describe("SQLWarehouseConnector", () => { .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); const start = vi.fn().mockResolvedValue(undefined); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const allUpdates = [0, 1, 2].map(() => [] as { state: string }[]); const waits = allUpdates.map((updates) => @@ -406,7 +567,9 @@ describe("SQLWarehouseConnector", () => { .fn() .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const controller = new AbortController(); const aborted = connector.ensureWarehouseRunning( @@ -438,7 +601,9 @@ describe("SQLWarehouseConnector", () => { .fn() .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const mount1 = new AbortController(); const first = connector.ensureWarehouseRunning( @@ -467,7 +632,9 @@ describe("SQLWarehouseConnector", () => { test("orphan before warehouses.start is aborted on the next microtask", async () => { const get = vi.fn().mockResolvedValue({ state: "STARTING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; const controller = new AbortController(); const only = connector.ensureWarehouseRunning( @@ -485,7 +652,7 @@ describe("SQLWarehouseConnector", () => { await Promise.resolve(); expect(get).toHaveBeenCalledTimes(1); - expect(wsClient.warehouses.start).not.toHaveBeenCalled(); + expect(wsClient.warehouses.startWarehouse).not.toHaveBeenCalled(); }); test("orphan after warehouses.start runs poll to completion", async () => { @@ -495,7 +662,9 @@ describe("SQLWarehouseConnector", () => { .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); const start = vi.fn().mockResolvedValue(undefined); - const wsClient = { warehouses: { get, start } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: start }, + }; const controller = new AbortController(); const only = connector.ensureWarehouseRunning( @@ -532,7 +701,9 @@ describe("SQLWarehouseConnector", () => { .fn() .mockResolvedValueOnce({ state: "STARTING" }) .mockResolvedValueOnce({ state: "RUNNING" }); - const wsClient = { warehouses: { get, start: vi.fn() } }; + const wsClient = { + warehouses: { getWarehouse: get, startWarehouse: vi.fn() }, + }; let callCount = 0; const promise = connector.ensureWarehouseRunning( diff --git a/packages/appkit/src/evals/dataset.ts b/packages/appkit/src/evals/dataset.ts index b6cdc0dac..f49270fcc 100644 --- a/packages/appkit/src/evals/dataset.ts +++ b/packages/appkit/src/evals/dataset.ts @@ -83,7 +83,7 @@ export async function readEvalDataset( : ""; const connector = new SQLWarehouseConnector({}); const response = await connector.executeStatement(client, { - warehouse_id: options.warehouseId, + warehouseId: options.warehouseId, statement: `SELECT inputs, expectations FROM ${options.table}${limit}`, }); diff --git a/packages/appkit/src/evals/tests/dataset.test.ts b/packages/appkit/src/evals/tests/dataset.test.ts index 12214d1ac..76d113b4d 100644 --- a/packages/appkit/src/evals/tests/dataset.test.ts +++ b/packages/appkit/src/evals/tests/dataset.test.ts @@ -45,7 +45,7 @@ describe("readEvalDataset", () => { // SELECT targets the table; no LIMIT when unset. const [, input] = executeStatement.mock.calls[0]; - expect(input.warehouse_id).toBe("wh1"); + expect(input.warehouseId).toBe("wh1"); expect(input.statement).toBe( "SELECT inputs, expectations FROM main.default.eval_ds", ); diff --git a/packages/appkit/src/plugins/analytics/analytics.ts b/packages/appkit/src/plugins/analytics/analytics.ts index dc3543be4..f95ea335d 100644 --- a/packages/appkit/src/plugins/analytics/analytics.ts +++ b/packages/appkit/src/plugins/analytics/analytics.ts @@ -28,7 +28,6 @@ import { AppKitError, ExecutionError } from "../../errors"; import { createLogger } from "../../logging/logger"; import { Plugin, toPlugin } from "../../plugin"; import { defineManifest } from "../../registry"; -import type { WorkspaceClient } from "../../workspace-client"; import { queryDefaults } from "./defaults"; import manifest from "./manifest.json"; import { @@ -1069,7 +1068,7 @@ export class AnalyticsPlugin extends Plugin implements ToolProvider { workspaceClient, { statement, - warehouse_id: warehouseId, + warehouseId, parameters: sqlParameters, ...formatParameters, }, diff --git a/packages/appkit/src/plugins/analytics/query.ts b/packages/appkit/src/plugins/analytics/query.ts index bcd77a817..4b57b051b 100644 --- a/packages/appkit/src/plugins/analytics/query.ts +++ b/packages/appkit/src/plugins/analytics/query.ts @@ -4,7 +4,7 @@ import { isSQLTypeMarker, type SQLTypeMarker, sql as sqlHelpers } from "shared"; import { getWorkspaceId } from "../../context"; import { ValidationError } from "../../errors"; -import type { sql } from "../../workspace-client"; +import type { StatementParameter } from "../../workspace-client"; type SQLParameterValue = SQLTypeMarker | null | undefined; @@ -37,8 +37,8 @@ export class QueryProcessor { convertToSQLParameters( query: string, parameters?: Record, - ): { statement: string; parameters: sql.StatementParameterListItem[] } { - const sqlParameters: sql.StatementParameterListItem[] = []; + ): { statement: string; parameters: StatementParameter[] } { + const sqlParameters: StatementParameter[] = []; if (parameters) { // extract all params from the query @@ -72,7 +72,7 @@ export class QueryProcessor { private _createParameter( key: string, value: SQLParameterValue, - ): sql.StatementParameterListItem | null { + ): StatementParameter | null { if (value === null || value === undefined) { return null; } diff --git a/packages/appkit/src/plugins/analytics/result-delivery.ts b/packages/appkit/src/plugins/analytics/result-delivery.ts index a0435513c..3f40439d2 100644 --- a/packages/appkit/src/plugins/analytics/result-delivery.ts +++ b/packages/appkit/src/plugins/analytics/result-delivery.ts @@ -4,7 +4,7 @@ import type { SQLTypeMarker } from "shared"; import { ExecutionError } from "../../errors"; import { createLogger } from "../../logging/logger"; import type { RefreshChunkLink } from "../../stream/arrow-stream-processor"; -import type { sql } from "../../workspace-client"; +import type { ExternalLink } from "../../workspace-client"; /** * Centralized disposition/format fallback for analytics result delivery. @@ -39,7 +39,7 @@ export interface QueryExecutor { | { attachment?: string; data?: Record[]; - external_links?: sql.ExternalLink[]; + external_links?: ExternalLink[]; columnNames?: string[]; statement_id?: string; status?: unknown; @@ -52,7 +52,7 @@ export interface QueryExecutor { /** Streams already-resolved EXTERNAL_LINKS chunks; the connector provides it. */ export interface ArrowChunkStreamer { streamExternalLinks( - chunks: sql.ExternalLink[], + chunks: ExternalLink[], signal?: AbortSignal, refresh?: RefreshChunkLink, ): AsyncGenerator; diff --git a/packages/appkit/src/plugins/analytics/tests/analytics.integration.test.ts b/packages/appkit/src/plugins/analytics/tests/analytics.integration.test.ts index c69a82e83..0265feeea 100644 --- a/packages/appkit/src/plugins/analytics/tests/analytics.integration.test.ts +++ b/packages/appkit/src/plugins/analytics/tests/analytics.integration.test.ts @@ -26,7 +26,7 @@ describe("Analytics Plugin Integration", () => { let app: TestApp<[ReturnType]>; /** The SQL mock the analytics route drives, via the harness's client. */ let executeStatement: ReturnType; - let getStatement: ReturnType; + let getStatementResult: ReturnType; beforeAll(async () => { // The harness owns the env setup, the singleton resets, the mock client, the @@ -36,7 +36,10 @@ describe("Analytics Plugin Integration", () => { app.client, "statementExecution.executeStatement", ); - getStatement = getMock(app.client, "statementExecution.getStatement"); + getStatementResult = getMock( + app.client, + "statementExecution.getStatementResult", + ); }); afterAll(async () => { @@ -48,7 +51,7 @@ describe("Analytics Plugin Integration", () => { // Reset drops the built-in canned SUCCEEDED default too, matching the // "script it yourself" semantics this suite relied on before. executeStatement.mockReset(); - getStatement.mockReset(); + getStatementResult.mockReset(); getAppQuerySpy.mockReset(); }); @@ -60,8 +63,8 @@ describe("Analytics Plugin Integration", () => { ["Bob", "25"], ]; const mockColumns = [ - { name: "name", type_name: "STRING" }, - { name: "age", type_name: "STRING" }, + { name: "name", typeName: "STRING" }, + { name: "age", typeName: "STRING" }, ]; getAppQuerySpy.mockResolvedValueOnce({ @@ -93,7 +96,7 @@ describe("Analytics Plugin Integration", () => { expect(executeStatement).toHaveBeenCalledWith( expect.objectContaining({ statement: testQuery, - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", }), expect.anything(), ); diff --git a/packages/appkit/src/plugins/analytics/tests/analytics.test.ts b/packages/appkit/src/plugins/analytics/tests/analytics.test.ts index 2151103eb..f68066a6a 100644 --- a/packages/appkit/src/plugins/analytics/tests/analytics.test.ts +++ b/packages/appkit/src/plugins/analytics/tests/analytics.test.ts @@ -173,7 +173,7 @@ describe("Analytics Plugin", () => { expect.anything(), expect.objectContaining({ statement: "SELECT * FROM test", - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", }), expect.any(AbortSignal), ); @@ -241,7 +241,7 @@ describe("Analytics Plugin", () => { expect.anything(), expect.objectContaining({ statement: "SELECT * FROM users WHERE id = :user_id", - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", }), expect.any(AbortSignal), ); @@ -638,7 +638,7 @@ describe("Analytics Plugin", () => { expect.objectContaining({ statement: "SELECT * FROM test", parameters: [], - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", }), expect.any(AbortSignal), ); @@ -673,7 +673,7 @@ describe("Analytics Plugin", () => { expect.anything(), expect.objectContaining({ statement: "SELECT * FROM test", - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", disposition: "INLINE", format: "ARROW_STREAM", }), @@ -1682,7 +1682,7 @@ describe("Analytics Plugin", () => { result: { data: [] }, }), }, - warehouses: { get: warehouseGet, start: vi.fn() }, + warehouses: { getWarehouse: warehouseGet, startWarehouse: vi.fn() }, }, }); const mockReq = createMockRequest({ diff --git a/packages/appkit/src/plugins/analytics/tests/arrow-delivery.integration.test.ts b/packages/appkit/src/plugins/analytics/tests/arrow-delivery.integration.test.ts index 934e83c28..404719440 100644 --- a/packages/appkit/src/plugins/analytics/tests/arrow-delivery.integration.test.ts +++ b/packages/appkit/src/plugins/analytics/tests/arrow-delivery.integration.test.ts @@ -38,9 +38,9 @@ describe.runIf(!!warehouseId)("arrow delivery (live warehouse)", () => { client, { statement, - warehouse_id: warehouseId as string, - wait_timeout: "50s", - on_wait_timeout: "CONTINUE", + warehouseId: warehouseId as string, + waitTimeout: "50s", + onWaitTimeout: "CONTINUE", disposition: fp.disposition as never, format: fp.format as never, }, diff --git a/packages/appkit/src/plugins/analytics/tests/metric.test.ts b/packages/appkit/src/plugins/analytics/tests/metric.test.ts index bf721feee..dee6c4df2 100644 --- a/packages/appkit/src/plugins/analytics/tests/metric.test.ts +++ b/packages/appkit/src/plugins/analytics/tests/metric.test.ts @@ -731,7 +731,7 @@ describe("analytics metric route", () => { expect.objectContaining({ statement: "SELECT MEASURE(`arr`) AS `arr` FROM `cat`.`sch`.`revenue_metrics`", - warehouse_id: "test-warehouse-id", + warehouseId: "test-warehouse-id", }), expect.any(AbortSignal), ); @@ -779,7 +779,7 @@ describe("analytics metric route", () => { result: { data: [] }, }), }, - warehouses: { get: warehouseGet, start: vi.fn() }, + warehouses: { getWarehouse: warehouseGet, startWarehouse: vi.fn() }, }, }); const mockReq = createMockRequest({ diff --git a/packages/appkit/src/stream/arrow-stream-processor.ts b/packages/appkit/src/stream/arrow-stream-processor.ts index 62cab4df4..e063b6abb 100644 --- a/packages/appkit/src/stream/arrow-stream-processor.ts +++ b/packages/appkit/src/stream/arrow-stream-processor.ts @@ -1,11 +1,9 @@ import { ExecutionError, ValidationError } from "../errors"; import { createLogger } from "../logging/logger"; -import type { sql } from "../workspace-client"; +import type { ExternalLink } from "../workspace-client"; const logger = createLogger("stream:arrow"); -type ExternalLink = sql.ExternalLink; - /** * Re-mint a chunk's pre-signed URL. DBSQL external links expire in <= 15 min, * so a large result whose tail chunks are reached after the earlier chunks @@ -83,11 +81,11 @@ export class ArrowStreamProcessor { signal?: AbortSignal, refresh?: RefreshChunkLink, ): AsyncGenerator { - let externalLink = chunk.external_link; + let externalLink = chunk.externalLink; if (!externalLink) { // A missing link cannot be fixed by retrying — fail loudly. throw ExecutionError.statementFailed( - `External link missing for chunk ${chunk.chunk_index}`, + `External link missing for chunk ${chunk.chunkIndex}`, ); } @@ -114,7 +112,7 @@ export class ArrowStreamProcessor { clearTimeout(timer); if (!r.ok) { throw ExecutionError.statementFailed( - `Failed to download chunk ${chunk.chunk_index}: ${r.status} ${r.statusText}`, + `Failed to download chunk ${chunk.chunkIndex}: ${r.status} ${r.statusText}`, ); } // Keep this attempt's controller alive to drive the body read + idle @@ -134,16 +132,16 @@ export class ArrowStreamProcessor { // chunk's link — a stale URL would just 403 again on the same address. // Only meaningful before any bytes are yielded (below), which is why // this lives in the establish-response loop. - if (refresh && chunk.chunk_index != null) { + if (refresh && chunk.chunkIndex != null) { try { - const fresh = await refresh(chunk.chunk_index, signal); - if (fresh?.external_link) externalLink = fresh.external_link; + const fresh = await refresh(chunk.chunkIndex, signal); + if (fresh?.externalLink) externalLink = fresh.externalLink; } catch (refreshError) { // Keep retrying the current URL; surface the original error if // all attempts fail. logger.warn( "Failed to re-resolve link for chunk %s: %O", - chunk.chunk_index, + chunk.chunkIndex, refreshError, ); } @@ -154,7 +152,7 @@ export class ArrowStreamProcessor { if (!response || !controller) { throw ExecutionError.statementFailed( - `Failed to download chunk ${chunk.chunk_index} after ${this.options.retries} attempts: ${ + `Failed to download chunk ${chunk.chunkIndex} after ${this.options.retries} attempts: ${ lastError instanceof Error ? lastError.message : String(lastError) }`, ); @@ -194,13 +192,13 @@ export class ArrowStreamProcessor { if (signal?.aborted) throw ExecutionError.canceled(); logger.error( "Failed streaming chunk %s body: %O", - chunk.chunk_index, + chunk.chunkIndex, error, ); throw error instanceof ExecutionError ? error : ExecutionError.statementFailed( - `Failed streaming chunk ${chunk.chunk_index}: ${ + `Failed streaming chunk ${chunk.chunkIndex}: ${ error instanceof Error ? error.message : String(error) }`, ); diff --git a/packages/appkit/src/stream/tests/arrow-stream-processor.test.ts b/packages/appkit/src/stream/tests/arrow-stream-processor.test.ts index 555f87339..84d1ee031 100644 --- a/packages/appkit/src/stream/tests/arrow-stream-processor.test.ts +++ b/packages/appkit/src/stream/tests/arrow-stream-processor.test.ts @@ -1,6 +1,5 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; -import type { sql } from "../../workspace-client"; import { ArrowStreamProcessor } from "../arrow-stream-processor"; /** A ReadableStream that emits the given pieces in order, then closes. */ @@ -15,10 +14,10 @@ function streamOf(...pieces: Uint8Array[]): ReadableStream { function mockChunks(count: number) { return Array.from({ length: count }, (_, i) => ({ - chunk_index: i, - external_link: `https://example.com/chunk-${i}`, - row_offset: i * 100, - row_count: 100, + chunkIndex: i, + externalLink: `https://example.com/chunk-${i}`, + rowOffset: BigInt(i * 100), + rowCount: 100n, })); } @@ -166,7 +165,7 @@ describe("ArrowStreamProcessor.streamChunks", () => { }); test("throws immediately when a chunk has no external_link", async () => { - const chunks = [{ chunk_index: 0 }] as any; + const chunks = [{ chunkIndex: 0 }] as any; await expect(drain(processor.streamChunks(chunks))).rejects.toThrow( /External link missing/, ); @@ -218,8 +217,8 @@ describe("ArrowStreamProcessor.streamChunks", () => { globalThis.fetch = fetchMock; const refresh = vi.fn(async (chunkIndex: number) => ({ - chunk_index: chunkIndex, - external_link: "https://example.com/fresh-link", + chunkIndex, + externalLink: "https://example.com/fresh-link", })); const p = new ArrowStreamProcessor({ timeout: 5000, retries: 3 }); diff --git a/packages/appkit/src/testing/fixtures.ts b/packages/appkit/src/testing/fixtures.ts index 13f79b5d7..22439c001 100644 --- a/packages/appkit/src/testing/fixtures.ts +++ b/packages/appkit/src/testing/fixtures.ts @@ -532,19 +532,19 @@ export async function runWithRequestContext( */ export function createSuccessfulSQLResponse( data: Any[][], - columns: Array<{ name: string; type_name?: string }>, + columns: Array<{ name: string; typeName?: string }>, ) { return { status: { state: "SUCCEEDED" }, - statement_id: `stmt-${Date.now()}`, + statementId: `stmt-${Date.now()}`, result: { - data_array: data, + dataArray: data, }, manifest: { schema: { columns: columns.map((col) => ({ name: col.name, - type_name: col.type_name ?? "STRING", + typeName: col.typeName ?? "STRING", })), }, }, @@ -560,6 +560,6 @@ export function createFailedSQLResponse(errorMessage: string) { message: errorMessage, }, }, - statement_id: `stmt-${Date.now()}`, + statementId: `stmt-${Date.now()}`, }; } diff --git a/packages/appkit/src/testing/mock-workspace-client.ts b/packages/appkit/src/testing/mock-workspace-client.ts index 7b9298379..59ec97635 100644 --- a/packages/appkit/src/testing/mock-workspace-client.ts +++ b/packages/appkit/src/testing/mock-workspace-client.ts @@ -43,8 +43,9 @@ export type MockWorkspaceClient = WorkspaceClient; /** * Applied beneath caller-supplied `responses`. * - * `statementExecution.executeStatement`, `warehouses.get` and `warehouses.start` - * must stay byte-identical to the old `fixtures.ts` values — suites reach them + * `statementExecution.executeStatement`, `warehouses.getWarehouse` and + * `warehouses.startWarehouse` must stay byte-identical to the old `fixtures.ts` + * values — suites reach them * implicitly through `mockServiceContext`. `currentUser.me` is * required: `ServiceContext.createContext` reads `.id`, so `createApp({ client })` * cannot boot without it. @@ -54,8 +55,8 @@ const DEFAULT_RESPONSES: Record = { status: { state: "SUCCEEDED" }, result: { data: [] }, }, - "warehouses.get": { state: "RUNNING" }, - "warehouses.start": undefined, + "warehouses.getWarehouse": { state: "RUNNING" }, + "warehouses.startWarehouse": undefined, "currentUser.me": { id: "test-service-user", userName: "test-service-user", diff --git a/packages/appkit/src/testing/tests/mock-workspace-client.test.ts b/packages/appkit/src/testing/tests/mock-workspace-client.test.ts index a2cbeda1a..5ec90a0d9 100644 --- a/packages/appkit/src/testing/tests/mock-workspace-client.test.ts +++ b/packages/appkit/src/testing/tests/mock-workspace-client.test.ts @@ -24,8 +24,8 @@ describe("createMockWorkspaceClient", () => { ["genie", "getMessage", undefined], ["jobs", "getRun", undefined], ["servingEndpoints", "get", undefined], - ["warehouses", "get", { state: "RUNNING" }], - ["warehouses", "start", undefined], + ["warehouses", "getWarehouse", { state: "RUNNING" }], + ["warehouses", "startWarehouse", undefined], ["statementExecution", "executeStatement", SUCCEEDED], ["currentUser", "me", TEST_USER], ])("%s.%s resolves its default", async (service, method, expected) => { @@ -248,11 +248,13 @@ describe("createMockWorkspaceClient", () => { await expect( client.statementExecution.executeStatement({} as never), ).resolves.toEqual(SUCCEEDED); - await expect(client.warehouses.get({} as never)).resolves.toEqual({ + await expect( + client.warehouses.getWarehouse({} as never), + ).resolves.toEqual({ state: "RUNNING", }); await expect( - client.warehouses.start({} as never), + client.warehouses.startWarehouse({} as never), ).resolves.toBeUndefined(); }); diff --git a/packages/appkit/src/type-generator/mv-registry/describe.ts b/packages/appkit/src/type-generator/mv-registry/describe.ts index db2302761..a01f26190 100644 --- a/packages/appkit/src/type-generator/mv-registry/describe.ts +++ b/packages/appkit/src/type-generator/mv-registry/describe.ts @@ -25,7 +25,7 @@ export function parseDescribeTableExtendedJson( throw new Error(`DESCRIBE TABLE EXTENDED failed: ${msg}`); } - const rows = response.result?.data_array ?? []; + const rows = response.result?.dataArray ?? []; if (rows.length === 0) { throw new Error( "DESCRIBE TABLE EXTENDED returned no rows. Verify the FQN points to a metric view.", diff --git a/packages/appkit/src/type-generator/query-registry.ts b/packages/appkit/src/type-generator/query-registry.ts index db9cf57b9..b822a9986 100644 --- a/packages/appkit/src/type-generator/query-registry.ts +++ b/packages/appkit/src/type-generator/query-registry.ts @@ -158,7 +158,7 @@ function formatParametersType(sql: string): string { /** * Decode a base64 Arrow IPC attachment from a DESCRIBE QUERY response and * extract column metadata. Returns the same shape as rows parsed from the - * legacy data_array path. + * legacy dataArray path. * * IMPORTANT: a DESCRIBE QUERY response is itself a result *table* with rows * shaped like `(col_name, data_type, comment)` describing the user query's @@ -196,7 +196,7 @@ export function convertToQueryType( sql: string, queryName: string, ): { type: string; hasResults: boolean } { - const dataRows = result.result?.data_array || []; + const dataRows = result.result?.dataArray || []; let columns = dataRows.map((row) => ({ name: row[0] || "", type_name: row[1]?.toUpperCase() || "STRING", @@ -204,10 +204,10 @@ export function convertToQueryType( })); // Fallback: serverless warehouses return ARROW_STREAM format with an inline - // base64 attachment instead of data_array. Decode the Arrow IPC rows (the + // base64 attachment instead of dataArray. Decode the Arrow IPC rows (the // DESCRIBE QUERY result table) to extract column names and types. if (columns.length === 0 && result.result?.attachment) { - logger.debug("data_array empty, decoding Arrow IPC attachment for schema"); + logger.debug("dataArray empty, decoding Arrow IPC attachment for schema"); try { columns = columnsFromArrowAttachment(result.result.attachment); } catch (err) { @@ -849,7 +849,7 @@ export async function generateQueriesFromDescribe( "DESCRIBE result for %s: state=%s, rows=%d, hasAttachment=%s", queryName, result.status.state, - result.result?.data_array?.length ?? 0, + result.result?.dataArray?.length ?? 0, !!result.result?.attachment, ); diff --git a/packages/appkit/src/type-generator/statement-result.ts b/packages/appkit/src/type-generator/statement-result.ts index 7ae091aaf..9f5f791fe 100644 --- a/packages/appkit/src/type-generator/statement-result.ts +++ b/packages/appkit/src/type-generator/statement-result.ts @@ -7,18 +7,18 @@ const logger = createLogger("type-generator:statement-result"); /** * Normalize a Statement Execution response so downstream parsers can always - * read rows from `result.data_array`, regardless of the wire format the + * read rows from `result.dataArray`, regardless of the wire format the * warehouse chose. * * `@databricks/sdk-experimental`'s `executeStatement` defaults to an * `ARROW_STREAM` disposition. With an `INLINE` disposition the single * DESCRIBE row is returned as a base64-encoded Arrow IPC stream in - * `result.attachment` and `result.data_array` is left undefined. The metric - * and query type generators only ever read `result.data_array`, so without + * `result.attachment` and `result.dataArray` is left undefined. The metric + * and query type generators only ever read `result.dataArray`, so without * this normalization an Arrow response reads as "returned no rows" — the * registry ships empty and the runtime fail-closed gate 503s every affected * metric/query. (A warehouse configured to return `JSON_ARRAY` populates - * `data_array` directly and needs no decoding — that path, and every mocked + * `dataArray` directly and needs no decoding — that path, and every mocked * test, flows through here unchanged.) */ export async function normalizeResultRows( @@ -30,18 +30,18 @@ export async function normalizeResultRows( // types. A deliberate throw — unlike the best-effort decode below — that both // callers catch per-entry as a loud per-key/per-query failure. if ( - response.result?.next_chunk_index != null || - response.result?.next_chunk_internal_link != null + response.result?.nextChunkIndex != null || + response.result?.nextChunkInternalLink != null ) { throw new Error( - "DESCRIBE result is multi-chunk (truncated); refusing to emit partial types — see next_chunk_index", + "DESCRIBE result is multi-chunk (truncated); refusing to emit partial types — see nextChunkIndex", ); } // Passthrough: rows already materialized (JSON_ARRAY warehouses + every - // mocked test). `data_array` being an empty array still counts as present — + // mocked test). `dataArray` being an empty array still counts as present — // that is a genuine "no rows" answer we must not overwrite with a decode. - if (response.result?.data_array !== undefined) { + if (response.result?.dataArray !== undefined) { return response; } @@ -78,7 +78,7 @@ export async function normalizeResultRows( ...response, result: { ...response.result, - data_array: dataArray, + dataArray: dataArray, }, }; } catch (err) { @@ -149,7 +149,7 @@ function isFormatRejection( /** * Run a DESCRIBE and return a response whose rows are readable via - * `result.data_array`, adapting to the warehouse's result-format capability. + * `result.dataArray`, adapting to the warehouse's result-format capability. * * No single format is portable: standard DBSQL (PRO/CLASSIC) serves * `INLINE`+`JSON_ARRAY` and rejects `INLINE`+`ARROW_STREAM`; the Reyden engine @@ -175,12 +175,16 @@ export async function describeAdaptive( let lastError: unknown; for (const format of formats) { try { + // Narrow the modular SDK's camelCase StatementResponse straight onto our + // subset. The only gap is `dataArray` cells (the SDK types them as + // `JsonValue[][]`); for a DESCRIBE they are always string/null, so the + // assertion is safe. `attachment` survives via the pinned pnpm patch. const response = (await client.statementExecution.executeStatement({ statement, - warehouse_id: warehouseId, + warehouseId, // Synchronous wait: without it the call can return PENDING/RUNNING with // no rows, which downstream misreads as a no-result degrade. - wait_timeout: "30s", + waitTimeout: "30s", format, disposition: "INLINE", })) as DatabricksStatementExecutionResponse; @@ -189,7 +193,7 @@ export async function describeAdaptive( normalized.status?.state === "FAILED" && isFormatRejection( normalized.status.error?.message, - normalized.status.error?.error_code, + normalized.status.error?.errorCode, ) ) { lastResponse = normalized; diff --git a/packages/appkit/src/type-generator/tests/generate-queries.test.ts b/packages/appkit/src/type-generator/tests/generate-queries.test.ts index 9117cbcb9..d29b12cd7 100644 --- a/packages/appkit/src/type-generator/tests/generate-queries.test.ts +++ b/packages/appkit/src/type-generator/tests/generate-queries.test.ts @@ -30,7 +30,10 @@ vi.mock("../../workspace-client", async (importOriginal) => { ...actual, createWorkspaceClient: () => ({ statementExecution: { executeStatement: mocks.executeStatement }, - warehouses: { get: mocks.getWarehouse, start: mocks.startWarehouse }, + warehouses: { + getWarehouse: mocks.getWarehouse, + startWarehouse: mocks.startWarehouse, + }, }), }; }); @@ -82,15 +85,15 @@ const lastSavedQueries = () => function succeededResult(columns: [string, string, string | null][]) { return { - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "SUCCEEDED" }, - result: { data_array: columns }, + result: { dataArray: columns }, }; } /** * Build a SUCCEEDED DESCRIBE QUERY response whose rows arrive only as a base64 - * Arrow IPC `attachment` (no `data_array`) — the ARROW_STREAM/INLINE wire shape + * Arrow IPC `attachment` (no `dataArray`) — the ARROW_STREAM/INLINE wire shape * the fetcher now requests. The describeOne path pipes this through * normalizeResultRows, which decodes the attachment so convertToQueryType can * read the columns. Each [name, type, comment] triple becomes one DESCRIBE row. @@ -108,10 +111,10 @@ async function succeededArrowAttachmentResult( "base64", ); return { - statement_id: "stmt-arrow", + statementId: "stmt-arrow", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, - // No data_array — rows live in the attachment, like a real INLINE Arrow + // No dataArray — rows live in the attachment, like a real INLINE Arrow // response. This is the condition the silent-degrade bug left unread. result: { attachment }, }; @@ -157,7 +160,7 @@ describe("generateQueriesFromDescribe", () => { test("ARROW attachment path — decodes Arrow rows into a real query schema", async () => { // The warehouse answers ARROW_STREAM/INLINE: columns arrive only as a - // base64 Arrow IPC attachment with data_array undefined. describeOne pipes + // base64 Arrow IPC attachment with dataArray undefined. describeOne pipes // this through normalizeResultRows before convertToQueryType, so the schema // resolves to real columns instead of the degraded `result: unknown`. mocks.readdir.mockResolvedValue(["users.sql"]); @@ -203,8 +206,8 @@ describe("generateQueriesFromDescribe", () => { expect(mocks.executeStatement).toHaveBeenCalledTimes(1); expect(mocks.executeStatement.mock.calls[0][0]).toMatchObject({ - warehouse_id: "wh-123", - wait_timeout: "30s", + warehouseId: "wh-123", + waitTimeout: "30s", format: "JSON_ARRAY", disposition: "INLINE", }); @@ -214,7 +217,7 @@ describe("generateQueriesFromDescribe", () => { mocks.readdir.mockResolvedValue(["bad_table.sql"]); mocks.readFile.mockResolvedValue("SELECT * FROM bad_table"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-2", + statementId: "stmt-2", status: { state: "FAILED", error: { message: "Table or view not found: bad_table" }, @@ -234,7 +237,7 @@ describe("generateQueriesFromDescribe", () => { mocks.readdir.mockResolvedValue(["query.sql"]); mocks.readFile.mockResolvedValue("SELECT 1"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-3", + statementId: "stmt-3", status: { state: "FAILED" }, }); @@ -256,7 +259,7 @@ describe("generateQueriesFromDescribe", () => { mocks.executeStatement .mockResolvedValueOnce(succeededResult([["id", "INT", null]])) .mockResolvedValueOnce({ - statement_id: "stmt-fail", + statementId: "stmt-fail", status: { state: "FAILED", error: { message: "Table not found" }, @@ -288,7 +291,7 @@ describe("generateQueriesFromDescribe", () => { mocks.executeStatement .mockRejectedValueOnce(new Error("Connection refused")) .mockResolvedValueOnce({ - statement_id: "stmt-fail-2", + statementId: "stmt-fail-2", status: { state: "FAILED", error: { message: "Table not found" } }, }); @@ -468,7 +471,7 @@ describe("generateQueriesFromDescribe", () => { .mockResolvedValueOnce("SELECT * FROM whatever"); mocks.executeStatement .mockResolvedValueOnce({ - statement_id: "stmt-syntax", + statementId: "stmt-syntax", status: { state: "FAILED", error: { message: "Table not found" }, @@ -625,7 +628,7 @@ describe("generateQueriesFromDescribe", () => { // state with no result rows. Must degrade like a transient outage, not be // misreported as EMPTY (which would discard a good cached type). mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-1", + statementId: "stmt-1", status: { state: "PENDING" }, }); @@ -660,7 +663,7 @@ describe("generateQueriesFromDescribe", () => { mocks.executeStatement .mockResolvedValueOnce(succeededResult([["id", "INT", null]])) .mockResolvedValueOnce({ - statement_id: "stmt-pending", + statementId: "stmt-pending", status: { state: "RUNNING" }, }); @@ -687,7 +690,7 @@ describe("generateQueriesFromDescribe", () => { mocks.readdir.mockResolvedValue(["broken.sql"]); mocks.readFile.mockResolvedValue("SELECT * FROM missing"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt", + statementId: "stmt", status: { state: "FAILED", error: { message: "Table or view not found: missing" }, diff --git a/packages/appkit/src/type-generator/tests/index.test.ts b/packages/appkit/src/type-generator/tests/index.test.ts index 21b5a7e0c..088b2bcf5 100644 --- a/packages/appkit/src/type-generator/tests/index.test.ts +++ b/packages/appkit/src/type-generator/tests/index.test.ts @@ -298,10 +298,10 @@ describe("generateFromEntryPoint — metric-view emission", () => { const metricFile = path.join(metricsDir, "generated", "metric-views.d.ts"); const describeResponse: DatabricksStatementExecutionResponse = { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, result: { - data_array: [ + dataArray: [ [ JSON.stringify({ columns: [ @@ -460,7 +460,7 @@ describe("generateFromEntryPoint — metric-view emission", () => { // the statement still PENDING — no rows yet. Previously this fell // into the "returned no rows" failure with per-key warns. metricFetcher: async () => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "PENDING" }, }), }), @@ -568,7 +568,7 @@ describe("generateFromEntryPoint — metric-view emission", () => { expect(mocks.executeStatement).toHaveBeenCalledWith( expect.objectContaining({ statement: "DESCRIBE TABLE EXTENDED `demo`.`sales`.`revenue` AS JSON", - warehouse_id: "wh-1", + warehouseId: "wh-1", }), ); const declarations = fs.readFileSync(metricFile, "utf-8"); @@ -708,7 +708,7 @@ describe("generateFromEntryPoint — metric-view emission", () => { warehouseId: "wh-1", mode: "blocking", metricFetcher: async () => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "PENDING" }, }), }), @@ -893,7 +893,7 @@ describe("generateFromEntryPoint — metric-view emission", () => { // The fall-through DESCRIBE hits a still-cold warehouse: non-terminal // response, which classifies as degraded (never an error). mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "PENDING" }, }); const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -1157,10 +1157,10 @@ describe("generateFromEntryPoint — metric cache section", () => { const describeResponseFor = ( measure: string, ): DatabricksStatementExecutionResponse => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, result: { - data_array: [ + dataArray: [ [ JSON.stringify({ columns: [ @@ -1494,26 +1494,26 @@ describe("generateFromEntryPoint — metric cache section", () => { } if (statement.includes("failed_stmt")) { return { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }; } if (statement.includes("no_rows")) { return { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [] }, + result: { dataArray: [] }, }; } if (statement.includes("no_columns")) { return { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [[JSON.stringify({ unrelated: true })]] }, + result: { dataArray: [[JSON.stringify({ unrelated: true })]] }, }; } if (statement.includes("pending")) { - return { statement_id: "stmt-mock", status: { state: "PENDING" } }; + return { statementId: "stmt-mock", status: { state: "PENDING" } }; } return describeResponseFor("total_revenue"); }, @@ -1573,7 +1573,7 @@ describe("generateFromEntryPoint — metric cache section", () => { writeConfig({ revenue: { source: "demo.sales.revenue" } }); mocks.getWarehouseState.mockResolvedValue("RUNNING"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const firstWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -1610,7 +1610,7 @@ describe("generateFromEntryPoint — metric cache section", () => { writeConfig({ revenue: { source: "demo.sales.revenue" } }); mocks.getWarehouseState.mockResolvedValue("RUNNING"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -1627,7 +1627,7 @@ describe("generateFromEntryPoint — metric cache section", () => { vi.clearAllMocks(); mocks.getWarehouseState.mockResolvedValue("RUNNING"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const error = await run({ mode: "blocking" }).then( @@ -1665,7 +1665,7 @@ describe("generateFromEntryPoint — metric cache section", () => { writeConfig({ revenue: { source: "demo.sales.revenue" } }); mocks.getWarehouseState.mockResolvedValue("RUNNING"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -1707,7 +1707,7 @@ describe("generateFromEntryPoint — metric cache section", () => { writeConfig({ revenue: { source: "demo.sales.revenue" } }); mocks.getWarehouseState.mockResolvedValue("RUNNING"); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); @@ -2013,7 +2013,7 @@ describe("generateFromEntryPoint — anti-clobber for blocking mode", () => { warehouseId: "wh-1", mode: "blocking", metricFetcher: async () => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "PENDING" }, }), }), @@ -2060,10 +2060,10 @@ describe("generateFromEntryPoint — anti-clobber for blocking mode", () => { ); const describeResponse: DatabricksStatementExecutionResponse = { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, result: { - data_array: [ + dataArray: [ [ JSON.stringify({ columns: [ diff --git a/packages/appkit/src/type-generator/tests/mv-registry.test.ts b/packages/appkit/src/type-generator/tests/mv-registry.test.ts index 90b07185c..50c69fe1c 100644 --- a/packages/appkit/src/type-generator/tests/mv-registry.test.ts +++ b/packages/appkit/src/type-generator/tests/mv-registry.test.ts @@ -45,10 +45,10 @@ function mockDescribeResponse( payload: unknown, ): DatabricksStatementExecutionResponse { return { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, result: { - data_array: [[JSON.stringify(payload)]], + dataArray: [[JSON.stringify(payload)]], }, }; } @@ -359,7 +359,7 @@ describe("resolveMetricConfig — FQN naming (UC-accurate)", () => { // crashing the pass — exactly the pre-existing degrade behavior. const fetcher = async (): Promise => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); const { schemas, failures } = await syncMetrics(resolution, fetcher); @@ -556,7 +556,7 @@ describe("parseDescribeTableExtendedJson", () => { test("throws on a FAILED status", () => { expect(() => parseDescribeTableExtendedJson({ - statement_id: "x", + statementId: "x", status: { state: "FAILED", error: { message: "no such table" } }, }), ).toThrowError(/no such table/); @@ -565,9 +565,9 @@ describe("parseDescribeTableExtendedJson", () => { test("throws when the response is empty", () => { expect(() => parseDescribeTableExtendedJson({ - statement_id: "x", + statementId: "x", status: { state: "SUCCEEDED" }, - result: { data_array: [] }, + result: { dataArray: [] }, }), ).toThrowError(/no rows/); }); @@ -575,9 +575,9 @@ describe("parseDescribeTableExtendedJson", () => { test("throws when the cell is not a JSON string", () => { expect(() => parseDescribeTableExtendedJson({ - statement_id: "x", + statementId: "x", status: { state: "SUCCEEDED" }, - result: { data_array: [[null]] }, + result: { dataArray: [[null]] }, }), ).toThrowError(/JSON string/); }); @@ -680,8 +680,8 @@ describe("createWorkspaceDescribeFetcher", () => { expect(statements).toHaveLength(1); expect(statements[0]).toMatchObject({ statement: "DESCRIBE TABLE EXTENDED `demo`.`sales`.`revenue` AS JSON", - warehouse_id: "wh-1", - wait_timeout: "30s", + warehouseId: "wh-1", + waitTimeout: "30s", // describeAdaptive tries JSON_ARRAY first (standard DBSQL); it falls back // to ARROW_STREAM only if the warehouse rejects that format. format: "JSON_ARRAY", @@ -691,7 +691,7 @@ describe("createWorkspaceDescribeFetcher", () => { test("decodes an Arrow attachment-only response into parseable columns (fetcher → normalizer → parser)", async () => { // The warehouse answers ARROW_STREAM/INLINE: rows arrive as a base64 Arrow - // IPC attachment with `data_array` undefined. Before the normalizer was + // IPC attachment with `dataArray` undefined. Before the normalizer was // wired in, parseDescribeTableExtendedJson read this as "no rows" and the // metric shipped degraded. Now the fetcher pipes the response through // normalizeResultRows, so the real describe doc is recovered end-to-end. @@ -701,12 +701,12 @@ describe("createWorkspaceDescribeFetcher", () => { executeStatement: async (req: Record) => { statements.push(req); return { - statement_id: "stmt-arrow", + statementId: "stmt-arrow", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, - // Only an attachment — no data_array (the bug's trigger condition). + // Only an attachment — no dataArray (the bug's trigger condition). result: { attachment: ARROW_ATTACHMENT_B64 }, - } as DatabricksStatementExecutionResponse; + }; }, }, } as unknown as Parameters[0]; @@ -715,7 +715,7 @@ describe("createWorkspaceDescribeFetcher", () => { const response = await fetcher("appkit_demo.public.revenue_metrics"); // The fetcher decoded the attachment: rows are now readable. - expect(response.result?.data_array).toBeDefined(); + expect(response.result?.dataArray).toBeDefined(); const parsed = parseDescribeTableExtendedJson(response); const cols = extractMetricColumns(parsed); // The real revenue_metrics describe doc carries measures and dimensions. @@ -1005,7 +1005,7 @@ describe("syncMetrics", () => { test("a multi-chunk (truncated) DESCRIBE surfaces as a loud failure, not a crash (fetcher → normalizer → syncMetrics)", async () => { // End-to-end loudness check for the truncation guard. The warehouse paginates - // the DESCRIBE result (sets next_chunk_index on the first chunk); the fetcher + // the DESCRIBE result (sets nextChunkIndex on the first chunk); the fetcher // pipes the response through normalizeResultRows, which THROWS rather than // emit partial types. That throw must be caught inside describeOne and // recorded as a MetricSyncFailure — never an uncaught crash that aborts the @@ -1015,16 +1015,15 @@ describe("syncMetrics", () => { }); const client = { statementExecution: { - executeStatement: async () => - ({ - statement_id: "stmt-chunked", - status: { state: "SUCCEEDED" }, - manifest: { format: "ARROW_STREAM" }, - result: { - attachment: ARROW_ATTACHMENT_B64, - next_chunk_index: 1, - }, - }) as DatabricksStatementExecutionResponse, + executeStatement: async () => ({ + statementId: "stmt-chunked", + status: { state: "SUCCEEDED" }, + manifest: { format: "ARROW_STREAM" }, + result: { + attachment: ARROW_ATTACHMENT_B64, + nextChunkIndex: 1, + }, + }), }, } as unknown as Parameters[0]; const fetcher = createWorkspaceDescribeFetcher(client, "wh-1"); @@ -1134,24 +1133,24 @@ describe("syncMetrics — failure transience (D′)", () => { [ "a FAILED statement", { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }, ], [ "a SUCCEEDED statement with zero rows", { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [] }, + result: { dataArray: [] }, }, ], [ "an unparseable payload", { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [["{not json"]] }, + result: { dataArray: [["{not json"]] }, }, ], ["zero extracted columns", mockDescribeResponse({ unrelated: true })], @@ -1198,7 +1197,7 @@ describe("syncMetrics — DESCRIBE state classification", () => { test(`a non-terminal ${state} response degrades the schema without recording a failure`, async () => { const fetcher = async (): Promise => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state }, }); @@ -1222,7 +1221,7 @@ describe("syncMetrics — DESCRIBE state classification", () => { test("a FAILED response stays a genuine failure (and its schema is degraded)", async () => { const fetcher = async (): Promise => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "FAILED", error: { message: "no such table" } }, }); @@ -1241,9 +1240,9 @@ describe("syncMetrics — DESCRIBE state classification", () => { // wrong FQN, not warehouse readiness. const fetcher = async (): Promise => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [] }, + result: { dataArray: [] }, }); const { schemas, failures } = await syncMetrics( @@ -1430,7 +1429,7 @@ describe("syncMetrics — bounded-concurrency scheduling", () => { throw new Error(`boom ${key}`); } if (key === nonTerminal) { - return { statement_id: "stmt-mock", status: { state: "PENDING" } }; + return { statementId: "stmt-mock", status: { state: "PENDING" } }; } return mockDescribeResponse({ columns: [ @@ -1602,7 +1601,7 @@ describe("generateMetricTypeDeclarations — snapshot", () => { ): Promise => fqn.endsWith("cold_metric") ? // Stopped/cold warehouse: wait_timeout elapsed → non-terminal, no rows. - { statement_id: "stmt-mock", status: { state: "PENDING" } } + { statementId: "stmt-mock", status: { state: "PENDING" } } : // Genuinely measure-less view: SUCCEEDED with dimension columns only. mockDescribeResponse({ columns: [{ name: "region", type: "STRING", is_measure: false }], @@ -1767,7 +1766,7 @@ describe("metric metadata bundle", () => { // Non-terminal DESCRIBE → degraded schema (empty column arrays). const fetcher = async (): Promise => ({ - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "PENDING" }, }); const { schemas } = await syncMetrics(resolution, fetcher); diff --git a/packages/appkit/src/type-generator/tests/query-registry.test.ts b/packages/appkit/src/type-generator/tests/query-registry.test.ts index 1f8156a2f..00d78e5fb 100644 --- a/packages/appkit/src/type-generator/tests/query-registry.test.ts +++ b/packages/appkit/src/type-generator/tests/query-registry.test.ts @@ -307,10 +307,10 @@ describe("defaultForType", () => { describe("convertToQueryType", () => { // DESCRIBE QUERY returns rows as [col_name, data_type, comment] const mockResponse: DatabricksStatementExecutionResponse = { - statement_id: "test-123", + statementId: "test-123", status: { state: "SUCCEEDED" }, result: { - data_array: [ + dataArray: [ ["id", "STRING", null], ["name", "STRING", null], ["count", "INT", null], @@ -373,10 +373,10 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` test("uses column comment when available", () => { const responseWithComment: DatabricksStatementExecutionResponse = { - statement_id: "test-123", + statementId: "test-123", status: { state: "SUCCEEDED" }, result: { - data_array: [["total", "DECIMAL", "Total amount in USD"]], + dataArray: [["total", "DECIMAL", "Total amount in USD"]], }, }; @@ -391,10 +391,10 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` test("quotes invalid column identifiers", () => { const responseWithInvalidName: DatabricksStatementExecutionResponse = { - statement_id: "test-123", + statementId: "test-123", status: { state: "SUCCEEDED" }, result: { - data_array: [["(1 = 1)", "BOOLEAN", null]], + dataArray: [["(1 = 1)", "BOOLEAN", null]], }, }; @@ -414,9 +414,9 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` test("returns hasResults: false when no columns exist", () => { const emptyResponse: DatabricksStatementExecutionResponse = { - statement_id: "test-123", + statementId: "test-123", status: { state: "SUCCEEDED" }, - result: { data_array: [] }, + result: { dataArray: [] }, }; const { hasResults } = convertToQueryType( emptyResponse, @@ -439,7 +439,7 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` { col_name: "active", data_type: "BOOLEAN", comment: null }, ]); const response: DatabricksStatementExecutionResponse = { - statement_id: "test-arrow", + statementId: "test-arrow", status: { state: "SUCCEEDED" }, result: { attachment }, }; @@ -467,7 +467,7 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` { col_name: "id", data_type: "int", comment: null }, ]); const response: DatabricksStatementExecutionResponse = { - statement_id: "test-arrow", + statementId: "test-arrow", status: { state: "SUCCEEDED" }, result: { attachment }, }; @@ -477,15 +477,15 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` expect(type).toContain("id: number"); }); - test("prefers data_array over attachment when both are present", () => { + test("prefers dataArray over attachment when both are present", () => { const attachment = describeQueryAttachment([ { col_name: "from_arrow", data_type: "STRING", comment: null }, ]); const response: DatabricksStatementExecutionResponse = { - statement_id: "test-both", + statementId: "test-both", status: { state: "SUCCEEDED" }, result: { - data_array: [["from_data_array", "INT", null]], + dataArray: [["from_data_array", "INT", null]], attachment, }, }; @@ -498,7 +498,7 @@ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` test("logs a warning and yields the unknown-result fallback on malformed attachment", () => { mockLoggerWarn.mockClear(); const response: DatabricksStatementExecutionResponse = { - statement_id: "test-bad", + statementId: "test-bad", status: { state: "SUCCEEDED" }, result: { attachment: "not-valid-arrow-ipc" }, }; diff --git a/packages/appkit/src/type-generator/tests/statement-result.test.ts b/packages/appkit/src/type-generator/tests/statement-result.test.ts index 4221cd705..1d1f43a6e 100644 --- a/packages/appkit/src/type-generator/tests/statement-result.test.ts +++ b/packages/appkit/src/type-generator/tests/statement-result.test.ts @@ -43,9 +43,9 @@ const ARROW_REORDERED_FIELDS_B64 = fs.readFileSync( ); describe("normalizeResultRows", () => { - test("decodes an Arrow attachment into data_array (real fixture)", async () => { + test("decodes an Arrow attachment into dataArray (real fixture)", async () => { const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-arrow", + statementId: "stmt-arrow", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: ARROW_ATTACHMENT_B64 }, @@ -54,10 +54,10 @@ describe("normalizeResultRows", () => { const normalized = await normalizeResultRows(response); // One row, one cell — the JSON-string DESCRIBE payload. - expect(normalized.result?.data_array).toHaveLength(1); - expect(normalized.result?.data_array?.[0]).toHaveLength(1); + expect(normalized.result?.dataArray).toHaveLength(1); + expect(normalized.result?.dataArray?.[0]).toHaveLength(1); - const cell = normalized.result?.data_array?.[0]?.[0]; + const cell = normalized.result?.dataArray?.[0]?.[0]; expect(typeof cell).toBe("string"); // The real describe doc parses to an object with a non-empty `columns` array. @@ -66,9 +66,9 @@ describe("normalizeResultRows", () => { expect(parsed.columns.length).toBeGreaterThan(0); }); - test("preserves status, statement_id, and manifest when decoding", async () => { + test("preserves status, statementId, and manifest when decoding", async () => { const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-arrow", + statementId: "stmt-arrow", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: ARROW_ATTACHMENT_B64 }, @@ -76,46 +76,46 @@ describe("normalizeResultRows", () => { const normalized = await normalizeResultRows(response); - expect(normalized.statement_id).toBe("stmt-arrow"); + expect(normalized.statementId).toBe("stmt-arrow"); expect(normalized.status.state).toBe("SUCCEEDED"); expect(normalized.manifest?.format).toBe("ARROW_STREAM"); - // The attachment is left in place; only data_array is added. + // The attachment is left in place; only dataArray is added. expect(normalized.result?.attachment).toBe(ARROW_ATTACHMENT_B64); }); - test("passes through unchanged when data_array is already present", async () => { + test("passes through unchanged when dataArray is already present", async () => { // JSON_ARRAY warehouses (and every mocked test) take this path: no decode. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-json", + statementId: "stmt-json", status: { state: "SUCCEEDED" }, manifest: { format: "JSON_ARRAY" }, - result: { data_array: [['{"columns":[]}']] }, + result: { dataArray: [['{"columns":[]}']] }, }; const normalized = await normalizeResultRows(response); expect(normalized).toBe(response); - expect(normalized.result?.data_array).toEqual([['{"columns":[]}']]); + expect(normalized.result?.dataArray).toEqual([['{"columns":[]}']]); }); - test("treats an empty data_array as present (genuine no-rows, no decode)", async () => { + test("treats an empty dataArray as present (genuine no-rows, no decode)", async () => { // An empty array is a real "no rows" answer — it must not be overwritten by // an attachment decode even if an attachment is somehow also present. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-empty", + statementId: "stmt-empty", status: { state: "SUCCEEDED" }, - result: { data_array: [], attachment: ARROW_ATTACHMENT_B64 }, + result: { dataArray: [], attachment: ARROW_ATTACHMENT_B64 }, }; const normalized = await normalizeResultRows(response); expect(normalized).toBe(response); - expect(normalized.result?.data_array).toEqual([]); + expect(normalized.result?.dataArray).toEqual([]); }); - test("returns response unchanged when neither data_array nor attachment is present", async () => { + test("returns response unchanged when neither dataArray nor attachment is present", async () => { const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-bare", + statementId: "stmt-bare", status: { state: "SUCCEEDED" }, result: {}, }; @@ -123,12 +123,12 @@ describe("normalizeResultRows", () => { const normalized = await normalizeResultRows(response); expect(normalized).toBe(response); - expect(normalized.result?.data_array).toBeUndefined(); + expect(normalized.result?.dataArray).toBeUndefined(); }); test("returns response unchanged when result is entirely absent", async () => { const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-noresult", + statementId: "stmt-noresult", status: { state: "RUNNING" }, }; @@ -141,13 +141,13 @@ describe("normalizeResultRows", () => { test("does not throw when Arrow decoding rejects; degrades to no usable rows", async () => { // Bytes that look like an Arrow IPC header but aren't make `tableFromIPC` // throw. The decoder must swallow that so the generation pass does not - // crash — it leaves data_array absent and the downstream "returned no + // crash — it leaves dataArray absent and the downstream "returned no // rows" degrade fires instead. const notArrow = Buffer.from( "hello world this is plainly not an arrow ipc stream", ).toString("base64"); const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-corrupt", + statementId: "stmt-corrupt", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: notArrow }, @@ -160,19 +160,19 @@ describe("normalizeResultRows", () => { })(), ).resolves.toBeUndefined(); - // No fabricated rows: decode rejected, so data_array stays absent. - expect(normalized.result?.data_array).toBeUndefined(); + // No fabricated rows: decode rejected, so dataArray stays absent. + expect(normalized.result?.dataArray).toBeUndefined(); // The (bad) attachment is preserved; nothing was invented. expect(normalized.result?.attachment).toBe(notArrow); }); - test("decodes garbage that yields an empty Arrow table to an empty data_array", async () => { + test("decodes garbage that yields an empty Arrow table to an empty dataArray", async () => { // Some malformed payloads decode without throwing into a zero-row table - // (e.g. truncated/garbage bytes). That surfaces as an empty data_array — + // (e.g. truncated/garbage bytes). That surfaces as an empty dataArray — // which is itself a valid "no rows" answer and degrades correctly // downstream, never a fabricated row. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-garbage", + statementId: "stmt-garbage", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: "not-valid-base64-arrow-ipc!!!" }, @@ -182,58 +182,57 @@ describe("normalizeResultRows", () => { // Either absent or empty — both mean "no usable rows". Crucially: no // non-empty fabricated row. - expect(normalized.result?.data_array ?? []).toHaveLength(0); + expect(normalized.result?.dataArray ?? []).toHaveLength(0); }); - test("throws on a multi-chunk result flagged by next_chunk_index", async () => { + test("throws on a multi-chunk result flagged by nextChunkIndex", async () => { // A DESCRIBE result that exceeds INLINE's size limit is paginated. The - // first chunk carries `next_chunk_index`; decoding it alone would silently + // first chunk carries `nextChunkIndex`; decoding it alone would silently // cache partial types. The normalizer must throw (loud) rather than degrade // — distinct from the malformed-attachment path which degrades silently. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-chunked-json", + statementId: "stmt-chunked-json", status: { state: "SUCCEEDED" }, manifest: { format: "JSON_ARRAY" }, - // data_array present (first chunk) — but the guard runs ABOVE the + // dataArray present (first chunk) — but the guard runs ABOVE the // passthrough, so truncation still throws instead of returning rows. result: { - data_array: [["col_a", "STRING", null]], - next_chunk_index: 1, + dataArray: [["col_a", "STRING", null]], + nextChunkIndex: 1, }, }; await expect(normalizeResultRows(response)).rejects.toThrow(/multi-chunk/i); await expect(normalizeResultRows(response)).rejects.toThrow( - /next_chunk_index/, + /nextChunkIndex/, ); }); - test("throws on a multi-chunk result flagged by next_chunk_internal_link", async () => { + test("throws on a multi-chunk result flagged by nextChunkInternalLink", async () => { // The attachment transport can paginate too: first chunk arrives as an - // Arrow attachment with `next_chunk_internal_link` set. The guard runs + // Arrow attachment with `nextChunkInternalLink` set. The guard runs // before the decode, so this throws rather than emitting first-chunk types. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-chunked-arrow", + statementId: "stmt-chunked-arrow", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: ARROW_ATTACHMENT_B64, - next_chunk_internal_link: - "/api/2.0/sql/statements/stmt/result/chunks/1", + nextChunkInternalLink: "/api/2.0/sql/statements/stmt/result/chunks/1", }, }; await expect(normalizeResultRows(response)).rejects.toThrow(/multi-chunk/i); }); - test("throws on a multi-chunk result with neither data_array nor attachment", async () => { + test("throws on a multi-chunk result with neither dataArray nor attachment", async () => { // Even when the first chunk somehow carries no inline rows, the chunk // markers alone mean the answer is truncated — refuse, do not fall through // to the "no rows" degrade. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-chunked-bare", + statementId: "stmt-chunked-bare", status: { state: "SUCCEEDED" }, - result: { next_chunk_index: 2 }, + result: { nextChunkIndex: 2 }, }; await expect(normalizeResultRows(response)).rejects.toThrow( @@ -249,7 +248,7 @@ describe("normalizeResultRows", () => { // scrambling the [col_name, data_type, comment] triple. The `[...row]` // iterator preserves field order. const response: DatabricksStatementExecutionResponse = { - statement_id: "stmt-reordered", + statementId: "stmt-reordered", status: { state: "SUCCEEDED" }, manifest: { format: "ARROW_STREAM" }, result: { attachment: ARROW_REORDERED_FIELDS_B64 }, @@ -257,7 +256,7 @@ describe("normalizeResultRows", () => { const normalized = await normalizeResultRows(response); - expect(normalized.result?.data_array).toEqual([ + expect(normalized.result?.dataArray).toEqual([ ["revenue", "DOUBLE", "total revenue"], ]); }); @@ -271,14 +270,16 @@ describe("describeAdaptive", () => { | Promise; // Minimal WorkspaceClient stub: records the formats requested and delegates - // each executeStatement to behavior(format), which may resolve or throw. + // each executeStatement to behavior(format), which may resolve or throw. The + // fixtures are the camelCase domain type — the same shape executeStatement + // returns — so describeAdaptive consumes them directly (no adapter needed). function stubClient(behavior: StubBehavior) { const formats: string[] = []; const client = { statementExecution: { executeStatement: async (req: { format: string }) => { formats.push(req.format); - return behavior(req.format); + return await behavior(req.format); }, }, } as unknown as WorkspaceClient; @@ -288,9 +289,9 @@ describe("describeAdaptive", () => { const rows = ( data: (string | null)[][], ): DatabricksStatementExecutionResponse => ({ - statement_id: "stmt", + statementId: "stmt", status: { state: "SUCCEEDED" }, - result: { data_array: data }, + result: { dataArray: data }, }); test("standard DBSQL: JSON_ARRAY succeeds, memoized, no fallback", async () => { @@ -307,7 +308,7 @@ describe("describeAdaptive", () => { memo, ); - expect(result.result?.data_array).toEqual([["schema"]]); + expect(result.result?.dataArray).toEqual([["schema"]]); expect(memo.format).toBe("JSON_ARRAY"); expect(formats).toEqual(["JSON_ARRAY"]); }); @@ -328,7 +329,7 @@ describe("describeAdaptive", () => { memo, ); - expect(result.result?.data_array).toEqual([["arrow-decoded"]]); + expect(result.result?.dataArray).toEqual([["arrow-decoded"]]); expect(memo.format).toBe("ARROW_STREAM"); expect(formats).toEqual(["JSON_ARRAY", "ARROW_STREAM"]); }); @@ -338,7 +339,7 @@ describe("describeAdaptive", () => { const { client, formats } = stubClient((format) => { if (format === "JSON_ARRAY") { return { - statement_id: "stmt", + statementId: "stmt", status: { state: "FAILED", error: { message: "merge_json_arrays" } }, result: {}, } as DatabricksStatementExecutionResponse; @@ -353,7 +354,7 @@ describe("describeAdaptive", () => { memo, ); - expect(result.result?.data_array).toEqual([["arrow-decoded"]]); + expect(result.result?.dataArray).toEqual([["arrow-decoded"]]); expect(memo.format).toBe("ARROW_STREAM"); expect(formats).toEqual(["JSON_ARRAY", "ARROW_STREAM"]); }); @@ -375,7 +376,7 @@ describe("describeAdaptive", () => { const { client, formats } = stubClient((format) => { if (format === "JSON_ARRAY") { return { - statement_id: "stmt", + statementId: "stmt", status: { state: "FAILED", error: { message: "[TABLE_OR_VIEW_NOT_FOUND]" }, @@ -410,11 +411,11 @@ describe("describeAdaptive", () => { const { client, formats } = stubClient((format) => { if (format === "JSON_ARRAY") { return { - statement_id: "stmt", + statementId: "stmt", status: { state: "FAILED", error: { - error_code: "TABLE_OR_VIEW_NOT_FOUND", + errorCode: "TABLE_OR_VIEW_NOT_FOUND", message: "table x has no disposition column; format unknown", }, }, @@ -433,7 +434,7 @@ describe("describeAdaptive", () => { // The real diagnostic survives unmasked, and no second format was probed. expect(result.status.state).toBe("FAILED"); - expect(result.status.error?.error_code).toBe("TABLE_OR_VIEW_NOT_FOUND"); + expect(result.status.error?.errorCode).toBe("TABLE_OR_VIEW_NOT_FOUND"); expect(memo.format).toBeUndefined(); expect(formats).toEqual(["JSON_ARRAY"]); }); @@ -446,11 +447,11 @@ describe("describeAdaptive", () => { const { client, formats } = stubClient((format) => { if (format === "JSON_ARRAY") { return { - statement_id: "stmt", + statementId: "stmt", status: { state: "FAILED", error: { - error_code: "INVALID_PARAMETER_VALUE", + errorCode: "INVALID_PARAMETER_VALUE", message: "disposition must be one of INLINE, EXTERNAL_LINKS; format must be JSON_ARRAY, ARROW_STREAM", }, @@ -468,7 +469,7 @@ describe("describeAdaptive", () => { memo, ); - expect(result.result?.data_array).toEqual([["arrow-decoded"]]); + expect(result.result?.dataArray).toEqual([["arrow-decoded"]]); expect(memo.format).toBe("ARROW_STREAM"); expect(formats).toEqual(["JSON_ARRAY", "ARROW_STREAM"]); }); diff --git a/packages/appkit/src/type-generator/tests/sync-metric-views-types.test.ts b/packages/appkit/src/type-generator/tests/sync-metric-views-types.test.ts index a0a4e9995..27a2580b9 100644 --- a/packages/appkit/src/type-generator/tests/sync-metric-views-types.test.ts +++ b/packages/appkit/src/type-generator/tests/sync-metric-views-types.test.ts @@ -63,9 +63,9 @@ function mockDescribeResponse( payload: unknown, ): DatabricksStatementExecutionResponse { return { - statement_id: "stmt-mock", + statementId: "stmt-mock", status: { state: "SUCCEEDED" }, - result: { data_array: [[JSON.stringify(payload)]] }, + result: { dataArray: [[JSON.stringify(payload)]] }, }; } @@ -232,7 +232,7 @@ describe("syncMetricViewsTypes", () => { mode: "blocking", suppressDegradedWrite: true, metricFetcher: async () => ({ - statement_id: "stmt-pending", + statementId: "stmt-pending", status: { state: "PENDING" }, }), }); diff --git a/packages/appkit/src/type-generator/tests/unreachable-warehouse-gate.test.ts b/packages/appkit/src/type-generator/tests/unreachable-warehouse-gate.test.ts index 6134f8348..0ffc0ed71 100644 --- a/packages/appkit/src/type-generator/tests/unreachable-warehouse-gate.test.ts +++ b/packages/appkit/src/type-generator/tests/unreachable-warehouse-gate.test.ts @@ -21,7 +21,7 @@ vi.mock("../../workspace-client", async (importOriginal) => { ...actual, createWorkspaceClient: () => ({ statementExecution: { executeStatement: mocks.executeStatement }, - warehouses: { get: mocks.getWarehouse, start: vi.fn() }, + warehouses: { getWarehouse: mocks.getWarehouse, startWarehouse: vi.fn() }, }), }; }); @@ -140,7 +140,7 @@ describe("--wait gate: environmental query failures (real query path)", () => { test("non-terminal DESCRIBE + no committed types → crashes instead of silently exiting 0", async () => { mocks.getWarehouse.mockResolvedValue({ state: "RUNNING" }); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-pending", + statementId: "stmt-pending", status: { state: "PENDING" }, }); @@ -162,7 +162,7 @@ describe("--wait gate: environmental query failures (real query path)", () => { test("non-terminal DESCRIBE + committed types → warns unavailable and keeps them", async () => { mocks.getWarehouse.mockResolvedValue({ state: "RUNNING" }); mocks.executeStatement.mockResolvedValue({ - statement_id: "stmt-pending", + statementId: "stmt-pending", status: { state: "RUNNING" }, }); fs.mkdirSync(path.dirname(outFile), { recursive: true }); diff --git a/packages/appkit/src/type-generator/tests/warehouse-status.test.ts b/packages/appkit/src/type-generator/tests/warehouse-status.test.ts index 882188e34..4a1b0f7b3 100644 --- a/packages/appkit/src/type-generator/tests/warehouse-status.test.ts +++ b/packages/appkit/src/type-generator/tests/warehouse-status.test.ts @@ -8,15 +8,15 @@ import { } from "../warehouse-status"; /** - * Build a minimal WorkspaceClient stub exposing only `warehouses.get`, the one - * method these helpers touch. Cast through `unknown` to the SDK type so callers - * type-check without us constructing a real client. + * Build a minimal WorkspaceClient stub exposing only `warehouses.getWarehouse`, + * the one method these helpers touch. Cast through `unknown` to the SDK type so + * callers type-check without us constructing a real client. */ function makeClient(get: ReturnType): WorkspaceClient { - return { warehouses: { get } } as unknown as WorkspaceClient; + return { warehouses: { getWarehouse: get } } as unknown as WorkspaceClient; } -/** A warehouses.get resolution carrying a given lifecycle state. */ +/** A warehouses.getWarehouse resolution carrying a given lifecycle state. */ const stateResponse = (state: WarehouseState) => ({ state }); describe("getWarehouseState", () => { diff --git a/packages/appkit/src/type-generator/types.ts b/packages/appkit/src/type-generator/types.ts index 954bde706..124a990c5 100644 --- a/packages/appkit/src/type-generator/types.ts +++ b/packages/appkit/src/type-generator/types.ts @@ -2,34 +2,39 @@ * Databricks statement execution response interface for DESCRIBE QUERY / * DESCRIBE TABLE EXTENDED. * + * A hand-written camelCase subset of the modular SDK's `StatementResponse` — + * only the fields the type generators read. `describeAdaptive` narrows the SDK + * response into this type directly (the sole difference is `dataArray`, which + * the SDK types as `JsonValue[][]`; DESCRIBE cells are always string/null). + * * Two result shapes matter here: - * - `result.data_array` — rows already materialized as JSON arrays. Present + * - `result.dataArray` — rows already materialized as JSON arrays. Present * when the warehouse returns `JSON_ARRAY` (and what every mocked test * builds). * - `result.attachment` — a base64-encoded Arrow IPC stream. Present when the * statement runs with `format: "ARROW_STREAM"` + `disposition: "INLINE"`, * which is the SDK's default disposition. The single row lands here and - * `data_array` is left undefined. {@link normalizeResultRows} decodes this - * back into `data_array` so downstream parsers stay shape-agnostic. + * `dataArray` is left undefined. {@link normalizeResultRows} decodes this + * back into `dataArray` so downstream parsers stay shape-agnostic. * - * @property statement_id - the id of the statement + * @property statementId - the id of the statement * @property status - the status of the statement * @property manifest - result metadata; `manifest.format` echoes the wire * format (`ARROW_STREAM`, `JSON_ARRAY`, ...) the warehouse chose. - * @property result - the result; either `data_array` (rows as + * @property result - the result; either `dataArray` (rows as * `[col_name, data_type, comment]` arrays) or `attachment` (base64 Arrow IPC) */ export interface DatabricksStatementExecutionResponse { - statement_id: string; + statementId: string; status: { state: string; - error?: { error_code?: string; message?: string }; + error?: { errorCode?: string; message?: string }; }; manifest?: { format?: string; }; result?: { - data_array?: (string | null)[][]; + dataArray?: (string | null)[][]; /** Base64-encoded Arrow IPC stream (ARROW_STREAM + INLINE disposition). */ attachment?: string; /** @@ -37,9 +42,9 @@ export interface DatabricksStatementExecutionResponse { * limit). Its presence means this response holds only the FIRST chunk; * {@link normalizeResultRows} throws rather than emit truncated types. */ - next_chunk_index?: number; - /** Companion to {@link next_chunk_index}: link to fetch the next chunk. */ - next_chunk_internal_link?: string; + nextChunkIndex?: number; + /** Companion to {@link nextChunkIndex}: link to fetch the next chunk. */ + nextChunkInternalLink?: string; }; } diff --git a/packages/appkit/src/type-generator/warehouse-status.ts b/packages/appkit/src/type-generator/warehouse-status.ts index 27a0afeb5..8aae70e5e 100644 --- a/packages/appkit/src/type-generator/warehouse-status.ts +++ b/packages/appkit/src/type-generator/warehouse-status.ts @@ -71,14 +71,14 @@ export async function getWarehouseState( client: WorkspaceClient, warehouseId: string, ): Promise { - const response = await client.warehouses.get({ id: warehouseId }); + const response = await client.warehouses.getWarehouse({ id: warehouseId }); return response.state as WarehouseState; } /** * Initiate a start of a stopped/stopping SQL warehouse. * - * Only KICKS OFF the start: the SDK's `start()` returns a Waiter, but we + * Only KICKS OFF the start: the SDK's `startWarehouse()` returns a Waiter, but we * deliberately do not `.wait()` on it. Blocking on the full cold-start isn't our * job here — {@link waitUntilRunning} is the poller that watches the warehouse * the rest of the way to RUNNING. We just nudge it out of the stopped state. @@ -90,7 +90,7 @@ export async function startWarehouse( client: WorkspaceClient, warehouseId: string, ): Promise { - await client.warehouses.start({ id: warehouseId }); + await client.warehouses.startWarehouse({ id: warehouseId }); } /** diff --git a/packages/appkit/src/workspace-client/index.ts b/packages/appkit/src/workspace-client/index.ts index 581cb79a8..a7d7e1c34 100644 --- a/packages/appkit/src/workspace-client/index.ts +++ b/packages/appkit/src/workspace-client/index.ts @@ -13,15 +13,8 @@ export { Time, TimeUnits, } from "shared"; -export type { - CancellationToken, - ClientOptions, - files, - GenieMessage, - jobs, - serving, - sql, - Waiter, - WorkspaceClient, - WorkspaceClientOptions, -} from "shared/workspace-client"; +// Forwards every wrapper type — legacy service namespaces (files/jobs/serving), +// the client option/waiter types, and the modular SDK client + model types +// (warehouses, statementExecution). `sql` is gone: its statement + warehouse +// types now come from the modular SDK. +export type * from "shared/workspace-client"; diff --git a/packages/shared/package.json b/packages/shared/package.json index a25379b08..4e127007e 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -47,7 +47,12 @@ "dependencies": { "@ast-grep/napi": "0.37.0", "@clack/prompts": "1.0.1", + "@databricks/sdk-auth": "0.46.0", + "@databricks/sdk-core": "0.46.0", "@databricks/sdk-experimental": "0.17.0", + "@databricks/sdk-options": "0.46.0", + "@databricks/sdk-statementexecution": "0.46.0", + "@databricks/sdk-warehouses": "0.46.0", "@standard-schema/spec": "1.1.0", "commander": "12.1.0", "dotenv": "16.6.1", diff --git a/packages/shared/src/workspace-client/client.ts b/packages/shared/src/workspace-client/client.ts index 18ef76a17..adf30c8db 100644 --- a/packages/shared/src/workspace-client/client.ts +++ b/packages/shared/src/workspace-client/client.ts @@ -12,11 +12,19 @@ import { type LegacyWorkspaceClient, type WorkspaceClientOptions, } from "./legacy"; +import { + buildStatementExecutionClient, + buildWarehousesClient, + type StatementExecutionClient, + type WarehousesClient, +} from "./modular"; import type { WorkspaceClient } from "./types"; export class AppKitWorkspaceClient implements WorkspaceClient { readonly #opts: WorkspaceClientOptions; #legacy?: LegacyWorkspaceClient; + #warehouses?: WarehousesClient; + #statementExecution?: StatementExecutionClient; constructor(opts: WorkspaceClientOptions) { this.#opts = opts; @@ -26,8 +34,12 @@ export class AppKitWorkspaceClient implements WorkspaceClient { return this.#getLegacy().files; } - get warehouses() { - return this.#getLegacy().warehouses; + // Migrated to the modular SDK — built lazily, independent of the legacy client. + get warehouses(): WarehousesClient { + if (!this.#warehouses) { + this.#warehouses = buildWarehousesClient(this.#opts); + } + return this.#warehouses; } get genie() { @@ -38,8 +50,12 @@ export class AppKitWorkspaceClient implements WorkspaceClient { return this.#getLegacy().jobs; } - get statementExecution() { - return this.#getLegacy().statementExecution; + // Migrated to the modular SDK — built lazily, independent of the legacy client. + get statementExecution(): StatementExecutionClient { + if (!this.#statementExecution) { + this.#statementExecution = buildStatementExecutionClient(this.#opts); + } + return this.#statementExecution; } get servingEndpoints() { diff --git a/packages/shared/src/workspace-client/index.ts b/packages/shared/src/workspace-client/index.ts index 91921efeb..2b981ebec 100644 --- a/packages/shared/src/workspace-client/index.ts +++ b/packages/shared/src/workspace-client/index.ts @@ -23,3 +23,5 @@ export { TimeUnits, } from "./legacy"; export type { files, jobs, serving, sql, WorkspaceClient } from "./types"; +// Modular SDK client + model types (warehouses). +export type * from "./modular"; diff --git a/packages/shared/src/workspace-client/modular.ts b/packages/shared/src/workspace-client/modular.ts new file mode 100644 index 000000000..2714b8dbd --- /dev/null +++ b/packages/shared/src/workspace-client/modular.ts @@ -0,0 +1,185 @@ +/** + * The single module allowed to import the modular `@databricks/sdk-*` SDK + * directly — the new-SDK sibling of {@link ./legacy.ts}. Every other AppKit + * module reaches these clients through the {@link WorkspaceClient} facade and + * the type re-exports below, so the modular SDK stays isolated exactly like the + * legacy one (the oxlint `no-restricted-imports` boundary walls `@databricks/sdk-*` + * off everywhere outside `packages/shared/src/workspace-client/`). + * + * Migrated services are built here as per-service clients; the facade delegates + * their accessors to these instead of the legacy monolithic client. Currently + * `warehouses` and `statementExecution` are migrated; every other service still + * routes through `legacy.ts`. + * + * NOTE: statementExecution relies on a pinned pnpm patch + * (`patches/@databricks__sdk-statementexecution@0.46.0.patch`) that restores the + * undocumented Reyden `attachment` response field, which the SDK's generated + * unmarshal transform would otherwise strip. + */ +import { + newM2mCredentials, + newPatCredentials, +} from "@databricks/sdk-auth/credentials"; +import { addToDefault, setProduct } from "@databricks/sdk-core/clientinfo"; +import type { ClientOptions } from "@databricks/sdk-options/client"; +import { StatementExecutionClient } from "@databricks/sdk-statementexecution/v1"; +import { WarehousesClient } from "@databricks/sdk-warehouses/v1"; + +import type { WorkspaceClientOptions } from "./legacy"; + +/** + * Prepend `https://` to a scheme-less host. The legacy SDK normalized the host + * this way; the modular SDK does NOT — it passes the host straight into `fetch`, + * so a bare `DATABRICKS_HOST=my-workspace.cloud.databricks.com` (the common form, + * and what the Databricks Apps runtime sets) yields `TypeError: Invalid URL`. + */ +function normalizeHost(host: string | undefined): string | undefined { + const trimmed = host?.trim(); + if (!trimmed) return undefined; + return /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`; +} + +/** + * Map wrapper options onto the modular SDK's `ClientOptions`, reproducing the + * legacy SDK's auth resolution: explicit token → PAT (the OBO path); profile → + * profile file; otherwise the service principal from the environment. It carries + * the privilege-escalation guard — check `token !== undefined` (NOT truthiness) + * so an explicitly-passed token, even an empty string, pins the PAT path and + * fails loudly at request time rather than silently falling through to the + * service-principal env credentials (which would be an OBO privilege escalation). + */ +function mapToClientOptions(opts: WorkspaceClientOptions): ClientOptions { + const clientOptions: ClientOptions = {}; + // Resolve + scheme-normalize the host the way the legacy SDK did. Explicit + // `opts.host` wins; otherwise fall back to `DATABRICKS_HOST` (env is where the + // Apps runtime and dev set it). When a profile is selected without an explicit + // host, defer to the SDK's profile-file resolution instead of the env. + const host = normalizeHost( + opts.host ?? (opts.profile ? undefined : process.env.DATABRICKS_HOST), + ); + if (host) { + clientOptions.host = host; + } + if (opts.token !== undefined) { + // Explicit token (this is the OBO path: `asUser` passes the user's token). + clientOptions.credentials = newPatCredentials(opts.token); + } else if (opts.profile) { + clientOptions.profileOptions = { profile: opts.profile }; + } else { + // No token, no profile: authenticate as the service principal from the + // environment, the way the legacy SDK did. The modular SDK's default auth + // chain resolves ONLY from a `~/.databrickscfg` profile — it reads no + // `DATABRICKS_*` env vars — so on the Databricks Apps runtime (which injects + // the app's SP credentials via env, with no config file) it would find no + // credentials and every request would fail. Resolve them here instead: + // M2M (client id + secret, what Apps injects) first, then a PAT, else fall + // through to the default chain for local dev with a config file. + const clientId = process.env.DATABRICKS_CLIENT_ID; + const clientSecret = process.env.DATABRICKS_CLIENT_SECRET; + const envToken = process.env.DATABRICKS_TOKEN; + if (host && clientId && clientSecret) { + clientOptions.credentials = newM2mCredentials({ + host, + clientId, + clientSecret, + }); + } else if (envToken) { + clientOptions.credentials = newPatCredentials(envToken); + } + // Otherwise leave credentials unset and let the SDK walk its profile-based + // default chain (local dev with `~/.databrickscfg`). + } + return clientOptions; +} + +// The modular SDK has no per-client User-Agent option; product/client-info is a +// process-global set once via `setProduct`/`addToDefault` before any client is +// built. The AppKit product/version/userAgentExtra arrive on `opts.clientOptions` +// (from `getClientOptions()`); build-time callers omit them and are left unstamped, +// preserving the legacy behavior where build-time clients carry no AppKit UA. The +// flag latches only once we actually stamp, so a first (unstamped) build-time +// client never blocks a later runtime client from stamping. +let clientInfoStamped = false; + +/** + * Coerce an arbitrary string into a valid client-info segment. The modular SDK + * validates keys as simple tokens and throws `ClientInfoError` on anything else, + * so the legacy product name `@databricks/appkit` (with `@` and `/`) is rejected + * — collapse invalid runs to `-` and trim the ends (`@databricks/appkit` → + * `databricks-appkit`). + */ +function toClientInfoKey(value: string): string { + return value.replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, ""); +} + +function ensureClientInfo(opts: WorkspaceClientOptions): void { + if (clientInfoStamped) { + return; + } + const co = opts.clientOptions; + if (!co?.product || !co?.productVersion) { + return; + } + // User-Agent stamping is best-effort: a value the SDK's client-info validator + // rejects must NEVER break client construction (the legacy SDK stamped the UA + // without validating). On failure the outbound request just carries the SDK's + // default User-Agent. + try { + setProduct(toClientInfoKey(co.product), co.productVersion); + if (co.userAgentExtra) { + for (const [key, value] of Object.entries(co.userAgentExtra)) { + addToDefault(toClientInfoKey(key), String(value)); + } + } + clientInfoStamped = true; + } catch { + clientInfoStamped = true; + } +} + +/** Build a modular Warehouses client from wrapper options. */ +export function buildWarehousesClient( + opts: WorkspaceClientOptions, +): WarehousesClient { + ensureClientInfo(opts); + return new WarehousesClient(mapToClientOptions(opts)); +} + +/** Build a modular Statement Execution client from wrapper options. */ +export function buildStatementExecutionClient( + opts: WorkspaceClientOptions, +): StatementExecutionClient { + ensureClientInfo(opts); + return new StatementExecutionClient(mapToClientOptions(opts)); +} + +// ── Client type re-exports (for the facade accessor types) ─────────────── +export type { StatementExecutionClient } from "@databricks/sdk-statementexecution/v1"; +export type { WarehousesClient } from "@databricks/sdk-warehouses/v1"; + +// ── Model type re-exports ──────────────────────────────────────────────── +// AppKit modules import request/response/enum types from the wrapper rather +// than the SDK, so the import boundary holds. Type-only: the connector compares +// state against string literals, which satisfy the SDK's `Enum | (string & {})` +// field unions — no runtime enum values needed. +export type { + ColumnInfo, + Disposition, + ExecuteStatementRequest, + ExternalLink, + Format, + ResultData, + ResultManifest, + Schema, + ServiceError, + StatementParameter, + StatementResponse, + StatementStatus, + StatementStatus_State, +} from "@databricks/sdk-statementexecution/v1"; +export type { + EndpointHealth, + EndpointInfo, + EndpointState, + GetWarehouseResponse, +} from "@databricks/sdk-warehouses/v1"; diff --git a/packages/shared/src/workspace-client/tests/modular.test.ts b/packages/shared/src/workspace-client/tests/modular.test.ts new file mode 100644 index 000000000..07af9ef07 --- /dev/null +++ b/packages/shared/src/workspace-client/tests/modular.test.ts @@ -0,0 +1,187 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; + +// The wrapper's own tests are the one place allowed to mock the SDK directly. +// Capture the `ClientOptions` the modular `WarehousesClient` constructor receives +// so we can assert how wrapper options map onto the modular SDK's config. +const { ctorOpts, patTokens, m2mOpts, productCalls } = vi.hoisted(() => ({ + ctorOpts: [] as Array>, + patTokens: [] as string[], + m2mOpts: [] as Array>, + productCalls: [] as Array<[string, string]>, +})); + +vi.mock("@databricks/sdk-warehouses/v1", () => ({ + WarehousesClient: vi.fn().mockImplementation((opts) => { + ctorOpts.push(opts); + return { opts }; + }), +})); +vi.mock("@databricks/sdk-statementexecution/v1", () => ({ + StatementExecutionClient: vi.fn().mockImplementation((opts) => ({ opts })), +})); +vi.mock("@databricks/sdk-auth/credentials", () => ({ + newPatCredentials: vi.fn((token: string) => { + patTokens.push(token); + return { kind: "pat", token }; + }), + newM2mCredentials: vi.fn((opts: Record) => { + m2mOpts.push(opts); + return { kind: "m2m", ...opts }; + }), +})); +vi.mock("@databricks/sdk-core/clientinfo", () => ({ + setProduct: vi.fn((name: string, version: string) => { + // Mirror the real SDK: reject client-info keys that aren't simple tokens. + if (/[^A-Za-z0-9._-]/.test(name)) { + throw new Error(`Invalid key: ${name}.`); + } + productCalls.push([name, version]); + }), + addToDefault: vi.fn(), +})); + +import { buildWarehousesClient } from "../modular"; + +describe("modular mapToClientOptions (via buildWarehousesClient)", () => { + // Auth resolution reads these env vars; snapshot + clear them so the dev + // machine's own DATABRICKS_* values never leak into a case. + const AUTH_ENV = [ + "DATABRICKS_HOST", + "DATABRICKS_CLIENT_ID", + "DATABRICKS_CLIENT_SECRET", + "DATABRICKS_TOKEN", + ] as const; + const originalEnv: Record = {}; + + beforeEach(() => { + ctorOpts.length = 0; + patTokens.length = 0; + m2mOpts.length = 0; + productCalls.length = 0; + for (const key of AUTH_ENV) { + originalEnv[key] = process.env[key]; + delete process.env[key]; + } + }); + + afterEach(() => { + for (const key of AUTH_ENV) { + if (originalEnv[key] === undefined) delete process.env[key]; + else process.env[key] = originalEnv[key]; + } + }); + + test("prepends https:// to a scheme-less explicit host", () => { + buildWarehousesClient({ host: "ws.cloud.databricks.com" }); + expect(ctorOpts[0].host).toBe("https://ws.cloud.databricks.com"); + }); + + test("leaves an explicit host that already has a scheme unchanged", () => { + buildWarehousesClient({ host: "https://ws.cloud.databricks.com" }); + expect(ctorOpts[0].host).toBe("https://ws.cloud.databricks.com"); + }); + + test("falls back to DATABRICKS_HOST (scheme-normalized) when no host is passed", () => { + process.env.DATABRICKS_HOST = "envhost.cloud.databricks.com"; + buildWarehousesClient({}); + expect(ctorOpts[0].host).toBe("https://envhost.cloud.databricks.com"); + }); + + test("a token takes the PAT path and pins the resolved host", () => { + buildWarehousesClient({ token: "abc", host: "https://x" }); + expect(patTokens).toEqual(["abc"]); + expect(ctorOpts[0].host).toBe("https://x"); + expect(ctorOpts[0].credentials).toEqual({ kind: "pat", token: "abc" }); + }); + + test("an empty-string token still uses PAT (no silent fall-through to default auth)", () => { + buildWarehousesClient({ token: "", host: "https://x" }); + expect(patTokens).toEqual([""]); + expect(ctorOpts[0].credentials).toEqual({ kind: "pat", token: "" }); + }); + + test("a profile sets profileOptions and defers host to the SDK (ignores env)", () => { + process.env.DATABRICKS_HOST = "envhost.cloud.databricks.com"; + buildWarehousesClient({ profile: "myprofile" }); + expect(ctorOpts[0].profileOptions).toEqual({ profile: "myprofile" }); + expect(ctorOpts[0].host).toBeUndefined(); + expect(patTokens).toEqual([]); + }); + + test("no host, no token, no profile, no env → empty options (SDK default chain)", () => { + buildWarehousesClient({}); + expect(ctorOpts[0].host).toBeUndefined(); + expect(ctorOpts[0].credentials).toBeUndefined(); + expect(ctorOpts[0].profileOptions).toBeUndefined(); + }); + + test("service-principal by default: DATABRICKS_CLIENT_ID/SECRET + host env → M2M creds", () => { + // The Databricks Apps runtime injects the app's SP credentials this way + // (env only, no config file). The modular SDK's default chain reads no env, + // so we must map them to M2M credentials ourselves. + process.env.DATABRICKS_HOST = "envhost.cloud.databricks.com"; + process.env.DATABRICKS_CLIENT_ID = "sp-client-id"; + process.env.DATABRICKS_CLIENT_SECRET = "sp-secret"; + buildWarehousesClient({}); + expect(m2mOpts).toEqual([ + { + host: "https://envhost.cloud.databricks.com", + clientId: "sp-client-id", + clientSecret: "sp-secret", + }, + ]); + expect(ctorOpts[0].credentials).toEqual({ + kind: "m2m", + host: "https://envhost.cloud.databricks.com", + clientId: "sp-client-id", + clientSecret: "sp-secret", + }); + expect(patTokens).toEqual([]); + }); + + test("falls back to DATABRICKS_TOKEN (PAT) when no client id/secret is set", () => { + process.env.DATABRICKS_HOST = "envhost.cloud.databricks.com"; + process.env.DATABRICKS_TOKEN = "env-pat"; + buildWarehousesClient({}); + expect(patTokens).toEqual(["env-pat"]); + expect(ctorOpts[0].credentials).toEqual({ kind: "pat", token: "env-pat" }); + expect(m2mOpts).toEqual([]); + }); + + test("an explicit (OBO) token wins over env SP credentials — no escalation", () => { + // asUser passes the user's token; it must NOT be shadowed by the SP env + // creds the deployed runtime also sets. + process.env.DATABRICKS_CLIENT_ID = "sp-client-id"; + process.env.DATABRICKS_CLIENT_SECRET = "sp-secret"; + buildWarehousesClient({ token: "user-token", host: "https://x" }); + expect(patTokens).toEqual(["user-token"]); + expect(ctorOpts[0].credentials).toEqual({ + kind: "pat", + token: "user-token", + }); + expect(m2mOpts).toEqual([]); + }); + + test("M2M needs a host: client id/secret with no resolvable host falls through to the default chain", () => { + process.env.DATABRICKS_CLIENT_ID = "sp-client-id"; + process.env.DATABRICKS_CLIENT_SECRET = "sp-secret"; + buildWarehousesClient({}); + expect(m2mOpts).toEqual([]); + expect(ctorOpts[0].credentials).toBeUndefined(); + }); + + test("client-info: sanitizes an invalid product name (e.g. @databricks/appkit) rather than crashing the client build", () => { + // Regression: the modular SDK's `setProduct` rejects `@databricks/appkit` + // (INVALID_KEY), which the legacy SDK accepted. UA stamping must be + // best-effort — a bad product string must never break client construction. + const client = buildWarehousesClient({ + clientOptions: { + product: "@databricks/appkit", + productVersion: "0.64.0", + userAgentExtra: { mode: "dev" }, + }, + } as never); + expect(client).toBeDefined(); + expect(productCalls[0]).toEqual(["databricks-appkit", "0.64.0"]); + }); +}); diff --git a/packages/shared/src/workspace-client/types.ts b/packages/shared/src/workspace-client/types.ts index 398a4afe0..6d9865ace 100644 --- a/packages/shared/src/workspace-client/types.ts +++ b/packages/shared/src/workspace-client/types.ts @@ -14,10 +14,17 @@ * as each service migrates. */ import type { LegacyWorkspaceClient } from "./legacy"; +import type { StatementExecutionClient, WarehousesClient } from "./modular"; -// SDK type namespaces, re-exported so AppKit modules import them from the -// wrapper rather than the SDK directly. +// Legacy SDK type namespaces for un-migrated services, re-exported so AppKit +// modules import them from the wrapper rather than the SDK directly. `sql` +// stays only for the dev-mode warehouse listing in service-context, which reads +// the raw (snake_case) `/api/2.0/sql/warehouses` body via the still-legacy +// `apiClient` and types it as `sql.EndpointInfo[]`. Statement + warehouse +// service types now come from `./modular`. export type { files, jobs, serving, sql } from "@databricks/sdk-experimental"; +// Modular SDK client + model types (warehouses, statementExecution). +export type * from "./modular"; /** * AppKit's workspace client facade. Mirrors the multi-client shape of the @@ -31,8 +38,8 @@ export interface WorkspaceClient { /** UC Volumes / Files API. */ readonly files: LegacyWorkspaceClient["files"]; - /** SQL Warehouses. */ - readonly warehouses: LegacyWorkspaceClient["warehouses"]; + /** SQL Warehouses (modular SDK). */ + readonly warehouses: WarehousesClient; /** Genie / dashboards. */ readonly genie: LegacyWorkspaceClient["genie"]; @@ -40,8 +47,8 @@ export interface WorkspaceClient { /** Jobs. */ readonly jobs: LegacyWorkspaceClient["jobs"]; - /** Statement Execution. */ - readonly statementExecution: LegacyWorkspaceClient["statementExecution"]; + /** Statement Execution (modular SDK). */ + readonly statementExecution: StatementExecutionClient; /** Serving Endpoints. */ readonly servingEndpoints: LegacyWorkspaceClient["servingEndpoints"]; diff --git a/patches/@databricks__sdk-statementexecution@0.46.0.patch b/patches/@databricks__sdk-statementexecution@0.46.0.patch new file mode 100644 index 000000000..c206b65c4 --- /dev/null +++ b/patches/@databricks__sdk-statementexecution@0.46.0.patch @@ -0,0 +1,34 @@ +diff --git a/dist/v1/model.d.ts b/dist/v1/model.d.ts +index e8d95659ea348b384a3d32b6a3d4f754287b38b6..705b9bed203981a2f3cde5417ed8019ff3a7065c 100644 +--- a/dist/v1/model.d.ts ++++ b/dist/v1/model.d.ts +@@ -385,6 +385,8 @@ interface QueryTag { + * link is returned.) + */ + interface ResultData { ++ /** PATCH(appkit): Reyden's non-standard INLINE ARROW_STREAM payload (base64 Arrow IPC). */ ++ attachment?: string | undefined; + externalLinks?: ExternalLink[] | undefined; + /** + * The `JSON_ARRAY` format is an array of arrays of values, where each non-null value is +diff --git a/dist/v1/model.js b/dist/v1/model.js +index fc35e28bbad5e7e873c14f6492696f9086ec9280..3bd78f3ad2dea39cdc883fa0daddb940e99e83b9 100644 +--- a/dist/v1/model.js ++++ b/dist/v1/model.js +@@ -177,10 +177,15 @@ const unmarshalResultDataSchema = z.object({ + z.string() + ]).transform((v) => BigInt(v)).optional(), + next_chunk_index: z.number().optional(), +- next_chunk_internal_link: z.string().optional() ++ next_chunk_internal_link: z.string().optional(), ++ // PATCH(appkit): preserve Reyden's non-standard INLINE ARROW_STREAM `attachment` ++ // (base64 Arrow IPC). The generated schema + rebuild-transform would otherwise ++ // strip it, breaking the inline-arrow delivery path. See patches/ for rationale. ++ attachment: z.string().optional() + }).transform((d) => ({ + externalLinks: d.external_links, + dataArray: d.data_array, ++ attachment: d.attachment, + chunkIndex: d.chunk_index, + rowOffset: d.row_offset, + rowCount: d.row_count, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8c6660f4d..f14b66053 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,11 @@ overrides: qs@<6.15.2: 6.15.2 size-sensor: 1.0.3 +patchedDependencies: + '@databricks/sdk-statementexecution@0.46.0': + hash: a0fde44d73faf28cc107a930fea00967d00dd4e74796002c77686bb5bf56569d + path: patches/@databricks__sdk-statementexecution@0.46.0.patch + importers: .: @@ -258,9 +263,24 @@ importers: '@databricks/lakebase': specifier: workspace:* version: link:../lakebase + '@databricks/sdk-auth': + specifier: 0.46.0 + version: 0.46.0 + '@databricks/sdk-core': + specifier: 0.46.0 + version: 0.46.0 '@databricks/sdk-experimental': specifier: 0.17.0 version: 0.17.0 + '@databricks/sdk-options': + specifier: 0.46.0 + version: 0.46.0 + '@databricks/sdk-statementexecution': + specifier: 0.46.0 + version: 0.46.0(patch_hash=a0fde44d73faf28cc107a930fea00967d00dd4e74796002c77686bb5bf56569d) + '@databricks/sdk-warehouses': + specifier: 0.46.0 + version: 0.46.0 '@mlflow/core': specifier: 0.4.0 version: 0.4.0(bufferutil@4.0.9) @@ -573,9 +593,24 @@ importers: '@clack/prompts': specifier: 1.0.1 version: 1.0.1 + '@databricks/sdk-auth': + specifier: 0.46.0 + version: 0.46.0 + '@databricks/sdk-core': + specifier: 0.46.0 + version: 0.46.0 '@databricks/sdk-experimental': specifier: 0.17.0 version: 0.17.0 + '@databricks/sdk-options': + specifier: 0.46.0 + version: 0.46.0 + '@databricks/sdk-statementexecution': + specifier: 0.46.0 + version: 0.46.0(patch_hash=a0fde44d73faf28cc107a930fea00967d00dd4e74796002c77686bb5bf56569d) + '@databricks/sdk-warehouses': + specifier: 0.46.0 + version: 0.46.0 '@standard-schema/spec': specifier: 1.1.0 version: 1.1.0 @@ -1935,6 +1970,14 @@ packages: engines: {node: ^20 || ^22 || ^24 || ^25, pnpm: '>=10'} hasBin: true + '@databricks/sdk-auth@0.46.0': + resolution: {integrity: sha512-cMrwxsFtpiEKFxta5dKHchKdrgmHkQ6upJ2C4OacmlHrOHJ+ChzQBVTpAiVtjX62xj+YjNgp/29IpSdKKYUVDA==} + engines: {node: '>=22.0.0'} + + '@databricks/sdk-core@0.46.0': + resolution: {integrity: sha512-Q2LAGWYIi+jyeKR9OIqvkgyde2GdzqfSG8lewxA9Xu/C9RJBBFbSfg5Nh8ZC66TKElGIosVOecoEJdbxnNMuxw==} + engines: {node: '>=22.0.0'} + '@databricks/sdk-experimental@0.15.0': resolution: {integrity: sha512-HkoMiF7dNDt6WRW0xhi7oPlBJQfxJ9suJhEZRFt08VwLMaWcw2PiF8monfHlkD4lkufEYV6CTxi5njQkciqiHA==} engines: {node: '>=22.0', npm: '>=10.0.0'} @@ -1943,6 +1986,18 @@ packages: resolution: {integrity: sha512-dOJIt4F2nBk6HKObnv7Xbmy/qLYTy2835qhXSuW0Qw1QAXui9plmCet1KqG3yeQcMTyncWGbnhjGdQi8GEGQSA==} engines: {node: '>=22.0', npm: '>=10.0.0'} + '@databricks/sdk-options@0.46.0': + resolution: {integrity: sha512-UtADlR+41rYEoOCycZvJh1g96uDN6GVWgqQk+72cHBzcxi+koxKSJXHcYRI997Sc4Fcc1d2oyAC2I2ddVhurjA==} + engines: {node: '>=22.0.0'} + + '@databricks/sdk-statementexecution@0.46.0': + resolution: {integrity: sha512-VJA3e7UHmxRxN42/mV5VtKeINME0vCz3Na3hrwmta3tZqJWZbBU7XTfUdD1yOQ5Z1JU5UIM65OlWq8gc4IzHFg==} + engines: {node: '>=22.0.0'} + + '@databricks/sdk-warehouses@0.46.0': + resolution: {integrity: sha512-9r/gbdTb6ASiWCiibCwAOF8QizqNacidIw78uwzJYKK9dbdqmWIfNK0pF/jt3BG1sUiXz2b1I6URPdX7Qi0oLg==} + engines: {node: '>=22.0.0'} + '@date-fns/tz@1.4.1': resolution: {integrity: sha512-P5LUNhtbj6YfI3iJjw5EL9eUAG6OitD0W3fWQcpQjDRc/QIsL0tRNuO1PcDvPccWL1fSTXXdE1ds+l95DV/OFA==} @@ -2609,6 +2664,10 @@ packages: '@js-sdsl/ordered-map@4.4.2': resolution: {integrity: sha512-iUKgm52T8HOE/makSxjqoWhe95ZJA1/G1sYsGev2JDKUSS14KAgg1LHb+Ba+IPow0xflbnSkOsZcO08C7w1gYw==} + '@js-temporal/polyfill@0.5.1': + resolution: {integrity: sha512-hloP58zRVCRSpgDxmqCWJNlizAlUgJFqG2ypq79DCvyv9tHjRYMDOcPFjzfl/A1/YxDvRCZz8wvZvmapQnKwFQ==} + engines: {node: '>=12'} + '@jsep-plugin/assignment@1.3.0': resolution: {integrity: sha512-VVgV+CXrhbMI3aSusQyclHkenWSAm95WaiKrMxRFam3JSUiIaQjoMIw2sEs/OX4XifnqeQUN4DYbJjlA8EfktQ==} engines: {node: '>= 10.16.0'} @@ -8668,6 +8727,9 @@ packages: resolution: {integrity: sha512-CY6crGq313MX8GkwvB7tzgp99vjQxY1++5y10/BKN/GUfHqWaOGQMNZkBvqSzsZKWk/ijwHlWzzkLulsGHhjWQ==} hasBin: true + jsbi@4.3.2: + resolution: {integrity: sha512-9fqMSQbhJykSeii05nxKl4m6Eqn2P6rOlYiS+C5Dr/HPIU/7yZxu5qzbs40tgaFORiw2Amd0mirjxatXYMkIew==} + jsdom@27.0.0: resolution: {integrity: sha512-lIHeR1qlIRrIN5VMccd8tI2Sgw6ieYXSVktcSHaNe3Z5nE/tcPQYQWOq00wxMvYOsz+73eAkNenVvmPC6bba9A==} engines: {node: '>=20'} @@ -14215,6 +14277,16 @@ snapshots: transitivePeerDependencies: - supports-color + '@databricks/sdk-auth@0.46.0': + dependencies: + '@databricks/sdk-core': 0.46.0 + zod: 4.3.6 + + '@databricks/sdk-core@0.46.0': + dependencies: + json-bigint: 1.0.0 + zod: 4.3.6 + '@databricks/sdk-experimental@0.15.0': dependencies: google-auth-library: 10.5.0 @@ -14233,6 +14305,29 @@ snapshots: transitivePeerDependencies: - supports-color + '@databricks/sdk-options@0.46.0': + dependencies: + '@databricks/sdk-auth': 0.46.0 + '@databricks/sdk-core': 0.46.0 + + '@databricks/sdk-statementexecution@0.46.0(patch_hash=a0fde44d73faf28cc107a930fea00967d00dd4e74796002c77686bb5bf56569d)': + dependencies: + '@databricks/sdk-auth': 0.46.0 + '@databricks/sdk-core': 0.46.0 + '@databricks/sdk-options': 0.46.0 + '@js-temporal/polyfill': 0.5.1 + json-bigint: 1.0.0 + zod: 4.3.6 + + '@databricks/sdk-warehouses@0.46.0': + dependencies: + '@databricks/sdk-auth': 0.46.0 + '@databricks/sdk-core': 0.46.0 + '@databricks/sdk-options': 0.46.0 + '@js-temporal/polyfill': 0.5.1 + json-bigint: 1.0.0 + zod: 4.3.6 + '@date-fns/tz@1.4.1': {} '@discoveryjs/json-ext@0.5.7': {} @@ -15431,6 +15526,10 @@ snapshots: '@js-sdsl/ordered-map@4.4.2': {} + '@js-temporal/polyfill@0.5.1': + dependencies: + jsbi: 4.3.2 + '@jsep-plugin/assignment@1.3.0(jsep@1.4.0)': dependencies: jsep: 1.4.0 @@ -22044,6 +22143,8 @@ snapshots: dependencies: argparse: 2.0.1 + jsbi@4.3.2: {} + jsdom@27.0.0(bufferutil@4.0.9)(postcss@8.5.6): dependencies: '@asamuzakjp/dom-selector': 6.6.2