Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions docs/docs/api/appkit/Class.ExecutionError.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ throw new ExecutionError("Statement was canceled");
new ExecutionError(message: string, options?: {
cause?: Error;
context?: Record<string, unknown>;
errorCode?: string;
}): ExecutionError;
```

Expand All @@ -30,15 +31,16 @@ new ExecutionError(message: string, options?: {
| Parameter | Type |
| ------ | ------ |
| `message` | `string` |
| `options?` | \{ `cause?`: `Error`; `context?`: `Record`\<`string`, `unknown`\>; \} |
| `options?` | \{ `cause?`: `Error`; `context?`: `Record`\<`string`, `unknown`\>; `errorCode?`: `string`; \} |
| `options.cause?` | `Error` |
| `options.context?` | `Record`\<`string`, `unknown`\> |
| `options.errorCode?` | `string` |

#### Returns

`ExecutionError`

#### Inherited from
#### Overrides

[`AppKitError`](Class.AppKitError.md).[`constructor`](Class.AppKitError.md#constructor)

Expand Down Expand Up @@ -86,6 +88,19 @@ Additional context for the error

***

### errorCode?

```ts
readonly optional errorCode: string;
```

Structured error code from the upstream source (typically the warehouse's
`error_code` for statement-level failures, or the SDK's `ApiError.errorCode`
for HTTP failures). Preserved through wrapping so callers can branch on a
stable identifier without substring-matching the message.

***

### isRetryable

```ts
Expand Down Expand Up @@ -202,16 +217,17 @@ Create an execution error for closed/expired results
### statementFailed()

```ts
static statementFailed(errorMessage?: string): ExecutionError;
static statementFailed(errorMessage?: string, errorCode?: string): ExecutionError;
```

Create an execution error for statement failure
Create an execution error for statement failure.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `errorMessage?` | `string` |
| Parameter | Type | Description |
| ------ | ------ | ------ |
| `errorMessage?` | `string` | Human-readable error from the warehouse / SDK. |
| `errorCode?` | `string` | Structured code (e.g. "INVALID_PARAMETER_VALUE") to preserve through wrapping. Optional. |

#### Returns

Expand Down
6 changes: 5 additions & 1 deletion packages/appkit-ui/src/js/sse/connect-sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export async function connectSSE<Payload = unknown>(
lastEventId: initialLastEventId = null,
retryDelay = 2000,
maxRetries = 3,
maxBufferSize = 1024 * 1024, // 1MB
// 1 MiB — matches the server's `streamDefaults.maxEventSize`. SSE
// carries only short JSON control messages; bulk Arrow payloads flow
// over plain HTTP via `/api/analytics/arrow-result/:jobId`, so this
// buffer never needs to hold multi-MiB attachments.
maxBufferSize = 1 * 1024 * 1024,
timeout = 300000, // 5 minutes
onError,
} = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("isQueryProps", () => {
const props = {
queryKey: "test_query",
parameters: { limit: 100 },
format: "json" as const,
format: "json_array" as const,
};

expect(isQueryProps(props as any)).toBe(true);
Expand Down
22 changes: 18 additions & 4 deletions packages/appkit-ui/src/react/charts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ import type { Table } from "apache-arrow";
// Data Format Types
// ============================================================================

/** Supported data formats for analytics queries */
export type DataFormat = "json" | "arrow" | "auto";
/**
* Supported data formats for analytics queries.
*
* "json" and "arrow" are legacy aliases kept for backwards compatibility
* with appkit-ui < 0.33.0 — safe to remove once no consumer is on a
* pre-0.33.0 version. resolveFormat() normalizes them to their canonical
* equivalents before any downstream code reads the value.
*/
export type DataFormat =
| "json_array"
| "arrow_stream"
| "auto"
/** @deprecated Use "json_array". Safe to remove once no consumer is on appkit-ui < 0.33.0. */
| "json"
/** @deprecated Use "arrow_stream". Safe to remove once no consumer is on appkit-ui < 0.33.0. */
| "arrow";

/** Chart orientation */
export type Orientation = "vertical" | "horizontal";
Expand Down Expand Up @@ -77,8 +91,8 @@ export interface QueryProps extends ChartBaseProps {
parameters?: Record<string, unknown>;
/**
* Data format to use
* - "json": Use JSON format (smaller payloads, simpler)
* - "arrow": Use Arrow format (faster for large datasets)
* - "json_array": Use JSON format (smaller payloads, simpler)
* - "arrow_stream": Use Arrow format (faster for large datasets)
* - "auto": Automatically select based on expected data size
* @default "auto"
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { renderHook, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";

// Capture the onMessage handler so tests can drive SSE messages directly.
let lastConnectArgs: any = null;
const mockProcessArrowBuffer = vi.fn();
const mockFetchArrow = vi.fn();

vi.mock("@/js", () => ({
connectSSE: vi.fn((args: any) => {
lastConnectArgs = args;
return () => {};
}),
ArrowClient: {
fetchArrow: (...args: unknown[]) => mockFetchArrow(...args),
processArrowBuffer: (...args: unknown[]) => mockProcessArrowBuffer(...args),
},
}));

// useQueryHMR is a no-op shim for tests; mock to avoid HMR side effects.
vi.mock("../use-query-hmr", () => ({
useQueryHMR: vi.fn(),
}));

import { useAnalyticsQuery } from "../use-analytics-query";

describe("useAnalyticsQuery", () => {
beforeEach(() => {
vi.clearAllMocks();
lastConnectArgs = null;
});

test("fetches an arrow message (warehouse statement id) via /arrow-result", async () => {
const fakeTable = { numRows: 1, schema: { fields: [] } };
const fakeBytes = new Uint8Array([1, 2, 3]);
mockFetchArrow.mockResolvedValueOnce(fakeBytes);
mockProcessArrowBuffer.mockResolvedValueOnce(fakeTable);

const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "ARROW_STREAM" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({ type: "arrow", statement_id: "stmt-warehouse-1" }),
});

await waitFor(() => {
expect(result.current.data).toBe(fakeTable);
});

expect(mockFetchArrow).toHaveBeenCalledTimes(1);
expect(mockFetchArrow).toHaveBeenCalledWith(
"/api/analytics/arrow-result/stmt-warehouse-1",
);
expect(mockProcessArrowBuffer).toHaveBeenCalledWith(fakeBytes);
});

test("fetches an arrow message with synthetic inline- id through the same /arrow-result path", async () => {
// The client must treat inline and external-links responses uniformly —
// it never decodes base64 locally. The /arrow-result route on the
// server is the only place that knows which path the bytes came from.
const fakeTable = { numRows: 1, schema: { fields: [] } };
const fakeBytes = new Uint8Array([1, 2, 3, 4, 5]);
mockFetchArrow.mockResolvedValueOnce(fakeBytes);
mockProcessArrowBuffer.mockResolvedValueOnce(fakeTable);

const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "ARROW_STREAM" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({
type: "arrow",
statement_id: "inline-abc-xyz",
}),
});

await waitFor(() => {
expect(result.current.data).toBe(fakeTable);
});

expect(mockFetchArrow).toHaveBeenCalledTimes(1);
expect(mockFetchArrow).toHaveBeenCalledWith(
"/api/analytics/arrow-result/inline-abc-xyz",
);
});

test("surfaces an error when the arrow fetch fails", async () => {
mockFetchArrow.mockRejectedValueOnce(new Error("network"));

const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "ARROW_STREAM" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({ type: "arrow", statement_id: "stmt-1" }),
});

await waitFor(() => {
expect(result.current.error).toBe(
"Unable to load data, please try again",
);
});
expect(result.current.loading).toBe(false);
});

test("rejects the retired arrow_inline message type as schema-invalid", async () => {
// arrow_inline was the prior wire shape. The discriminated union no
// longer accepts it, so it falls through to the generic error/code
// branch — but critically, it must NEVER trigger ArrowClient calls.
const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "ARROW_STREAM" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({ type: "arrow_inline", attachment: "AQID" }),
});

// Whatever the hook surfaces (error or noop), it must not have tried to
// decode the payload locally.
await waitFor(() => {
// Either an error is set or loading completed without data — both are
// acceptable, but processArrowBuffer must never run on a base64 input.
expect(
result.current.loading ||
result.current.error ||
result.current.data === null,
).toBeTruthy();
});
expect(mockProcessArrowBuffer).not.toHaveBeenCalled();
expect(mockFetchArrow).not.toHaveBeenCalled();
});

test("normalizes an empty result message (no data field) to []", async () => {
// The wire schema makes `data` optional — empty result sets may omit
// it. The hook must surface that as an explicit empty array rather
// than `undefined`, so callers can rely on `data` being either null
// (no message yet) or a value of the inferred result type.
const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "JSON_ARRAY" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({ type: "result" }),
});

await waitFor(() => {
expect(result.current.data).toEqual([]);
});
expect(result.current.loading).toBe(false);
expect(result.current.error).toBeNull();
});

test("still handles type:result rows for JSON_ARRAY", async () => {
const { result } = renderHook(() =>
useAnalyticsQuery("q", null, { format: "JSON_ARRAY" }),
);

await lastConnectArgs.onMessage({
data: JSON.stringify({
type: "result",
data: [{ id: 1 }, { id: 2 }],
}),
});

await waitFor(() => {
expect(result.current.data).toEqual([{ id: 1 }, { id: 2 }]);
});
expect(mockProcessArrowBuffer).not.toHaveBeenCalled();
expect(mockFetchArrow).not.toHaveBeenCalled();
});
});
Loading
Loading