@altertable/cli 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +214 -26
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -7033,7 +7033,7 @@ var require_public_api = __commonJS((exports) => {
7033
7033
  });
7034
7034
 
7035
7035
  // src/version.ts
7036
- var VERSION = "1.4.0";
7036
+ var VERSION = "1.5.1";
7037
7037
  var USER_AGENT = `altertable-cli/${VERSION}`;
7038
7038
 
7039
7039
  // src/lib/transport-defaults.ts
@@ -8232,6 +8232,9 @@ function createDefaultOutputSink(context) {
8232
8232
  writeRaw(body) {
8233
8233
  console.log(body);
8234
8234
  },
8235
+ async writeBytes(body) {
8236
+ await Bun.write(Bun.stdout, body);
8237
+ },
8235
8238
  writeHuman(text) {
8236
8239
  console.log(text);
8237
8240
  },
@@ -8351,6 +8354,11 @@ function mergeGlobalFlagsFromArgs(context, args) {
8351
8354
  }
8352
8355
 
8353
8356
  // src/lib/command.ts
8357
+ var ALTERTABLE_COMMAND_GROUPS = [
8358
+ { id: "platform", title: "Platform" },
8359
+ { id: "ingest", title: "Ingest" },
8360
+ { id: "query", title: "Query" }
8361
+ ];
8354
8362
  function defineArguments(commandArguments) {
8355
8363
  return commandArguments;
8356
8364
  }
@@ -12294,6 +12302,16 @@ function buildQueryPayload(input) {
12294
12302
  payload.query_id = input.queryId;
12295
12303
  if (input.sessionId)
12296
12304
  payload.session_id = input.sessionId;
12305
+ if (input.computeSize)
12306
+ payload.compute_size = input.computeSize;
12307
+ if (input.format)
12308
+ payload.format = input.format;
12309
+ if (input.dialect)
12310
+ payload.dialect = input.dialect;
12311
+ if (input.catalog)
12312
+ payload.catalog = input.catalog;
12313
+ if (input.schema)
12314
+ payload.schema = input.schema;
12297
12315
  return payload;
12298
12316
  }
12299
12317
  function buildLakehouseQueryRequest(input, streaming) {
@@ -12325,6 +12343,9 @@ async function executeLakehouseQuery(input, execution, streaming) {
12325
12343
  }
12326
12344
  return parseLakehouseQueryResponse(await sendHttp(request, execution));
12327
12345
  }
12346
+ async function executeLakehouseQueryBytes(input, execution) {
12347
+ return sendHttpStream(buildLakehouseQueryRequest(input, true), execution);
12348
+ }
12328
12349
  function buildLakehouseQueryShowRequest(queryId) {
12329
12350
  return {
12330
12351
  plane: "lakehouse",
@@ -15478,11 +15499,74 @@ async function writePagedOutput(text3, options, sink) {
15478
15499
  }
15479
15500
  }
15480
15501
 
15502
+ // src/lib/query-destination.ts
15503
+ async function writeStreamChunks(stream, writeChunk) {
15504
+ const reader = stream.getReader();
15505
+ try {
15506
+ while (true) {
15507
+ const { done, value } = await reader.read();
15508
+ if (done) {
15509
+ return;
15510
+ }
15511
+ await writeChunk(value);
15512
+ }
15513
+ } finally {
15514
+ reader.releaseLock();
15515
+ }
15516
+ }
15517
+ async function writeQueryDestination(content, options = {}) {
15518
+ const { outputPath, sink } = options;
15519
+ if (typeof content === "string") {
15520
+ const bytes = new TextEncoder().encode(content.endsWith(`
15521
+ `) ? content : `${content}
15522
+ `);
15523
+ if (outputPath !== undefined) {
15524
+ await Bun.write(outputPath, bytes);
15525
+ return;
15526
+ }
15527
+ if (sink) {
15528
+ await sink.writeBytes(bytes);
15529
+ return;
15530
+ }
15531
+ await Bun.write(Bun.stdout, bytes);
15532
+ return;
15533
+ }
15534
+ if (outputPath !== undefined) {
15535
+ const writer = Bun.file(outputPath).writer();
15536
+ try {
15537
+ await writeStreamChunks(content, async (chunk) => {
15538
+ await writer.write(chunk);
15539
+ });
15540
+ } finally {
15541
+ await writer.end();
15542
+ }
15543
+ return;
15544
+ }
15545
+ if (sink) {
15546
+ await writeStreamChunks(content, (chunk) => sink.writeBytes(chunk));
15547
+ return;
15548
+ }
15549
+ await writeStreamChunks(content, async (chunk) => {
15550
+ await Bun.write(Bun.stdout, chunk);
15551
+ });
15552
+ }
15553
+
15481
15554
  // src/lib/query-output.ts
15482
- var QUERY_RESULT_FORMATS = new Set(["human", "json", "csv", "markdown"]);
15555
+ var API_NATIVE_QUERY_FORMATS = new Set(["csv", "jsonl", "parquet"]);
15556
+ var QUERY_RESULT_FORMATS = new Set([
15557
+ "human",
15558
+ "json",
15559
+ "csv",
15560
+ "jsonl",
15561
+ "parquet",
15562
+ "markdown"
15563
+ ]);
15564
+ function isApiNativeQueryFormat(format) {
15565
+ return API_NATIVE_QUERY_FORMATS.has(format);
15566
+ }
15483
15567
  function parseQueryResultFormat(format) {
15484
15568
  if (!QUERY_RESULT_FORMATS.has(format)) {
15485
- throw new CliError(`Unsupported format: ${format}. Use csv or markdown.`);
15569
+ throw new CliError(`Unsupported format: ${format}. Use csv, jsonl, parquet, or markdown.`);
15486
15570
  }
15487
15571
  return format;
15488
15572
  }
@@ -15523,16 +15607,33 @@ function renderQueryOutputText(result, format, displayOptions) {
15523
15607
  const columnNames = getQueryColumnNames(result);
15524
15608
  return renderQueryMarkdown(result, columnNames, displayOptions ?? defaultDisplayOptions());
15525
15609
  }
15610
+ if (isApiNativeQueryFormat(format)) {
15611
+ throw new CliError(`Format ${format} must be streamed from the lakehouse API.`);
15612
+ }
15526
15613
  return renderQueryHumanOutput(result, displayOptions ?? defaultDisplayOptions());
15527
15614
  }
15528
- async function writeQueryOutput(result, format, sink, displayOptions, pagerOptions) {
15529
- const outputText = renderQueryOutputText(result, sink.json ? "json" : format, displayOptions);
15530
- const usePager = format === "human" && !sink.json;
15615
+ async function writeQueryOutput(result, format, sink, displayOptions, pagerOptions, outputPath) {
15616
+ const effectiveFormat = sink.json ? "json" : format;
15617
+ if (isApiNativeQueryFormat(effectiveFormat)) {
15618
+ throw new CliError(`Format ${effectiveFormat} must be streamed from the lakehouse API.`);
15619
+ }
15620
+ const outputText = renderQueryOutputText(result, effectiveFormat, displayOptions);
15621
+ const usePager = format === "human" && !sink.json && outputPath === undefined;
15531
15622
  if (usePager) {
15532
15623
  await writePagedOutput(outputText, pagerOptions ?? resolvePagerOptions(), sink);
15533
15624
  return;
15534
15625
  }
15535
- if (format === "json" || sink.json) {
15626
+ if (outputPath !== undefined) {
15627
+ if (effectiveFormat === "json") {
15628
+ await writeQueryDestination(JSON.stringify(JSON.parse(outputText), null, 2), {
15629
+ outputPath
15630
+ });
15631
+ return;
15632
+ }
15633
+ await writeQueryDestination(outputText, { outputPath });
15634
+ return;
15635
+ }
15636
+ if (effectiveFormat === "json" || sink.json) {
15536
15637
  sink.writeJson(JSON.parse(outputText));
15537
15638
  return;
15538
15639
  }
@@ -16354,14 +16455,28 @@ var appendCommand = defineCommand({
16354
16455
 
16355
16456
  // src/lib/query-output-args.ts
16356
16457
  var MIN_MAX_COLUMN_WIDTH = 8;
16357
- var QUERY_RESULT_FORMAT_OPTIONS = ["csv", "markdown"];
16458
+ var PRESENTATION_RESULT_FORMAT_OPTIONS = ["csv", "markdown"];
16459
+ var QUERY_RESULT_FORMAT_OPTIONS = ["csv", "jsonl", "parquet", "markdown"];
16358
16460
  var PAGER_MODE_OPTIONS = ["auto", "always", "never"];
16359
16461
  var PAGER_MODES = new Set(PAGER_MODE_OPTIONS);
16360
16462
  var AGENT_INCOMPATIBLE_QUERY_FLAGS = ["--layout", "--pager", "--max-width"];
16463
+ var API_NATIVE_INCOMPATIBLE_PRESENTATION_FLAGS = [
16464
+ "--layout",
16465
+ "--columns",
16466
+ "--max-width",
16467
+ "--pager"
16468
+ ];
16361
16469
  var queryResultFormatArgs = defineArguments({
16362
16470
  format: {
16363
16471
  type: "enum",
16364
16472
  description: "Serialized output format; use global --json for JSON",
16473
+ options: [...PRESENTATION_RESULT_FORMAT_OPTIONS]
16474
+ }
16475
+ });
16476
+ var queryApiResultFormatArgs = defineArguments({
16477
+ format: {
16478
+ type: "enum",
16479
+ description: "Result format: csv, jsonl, and parquet stream from the API; markdown is rendered by the CLI. Use global --json for structured JSON",
16365
16480
  options: [...QUERY_RESULT_FORMAT_OPTIONS]
16366
16481
  }
16367
16482
  });
@@ -16386,6 +16501,29 @@ var queryPagerArgs = defineArguments({
16386
16501
  options: [...PAGER_MODE_OPTIONS]
16387
16502
  }
16388
16503
  });
16504
+ var queryRequestArgs = defineArguments({
16505
+ "compute-size": {
16506
+ type: "string",
16507
+ description: "Compute size for the query",
16508
+ default: "AUTO"
16509
+ },
16510
+ dialect: {
16511
+ type: "string",
16512
+ description: "Source SQL dialect to transpile from (server default: DuckDB)"
16513
+ },
16514
+ catalog: {
16515
+ type: "string",
16516
+ description: "Catalog name (optional; can also come from the session)"
16517
+ },
16518
+ schema: {
16519
+ type: "string",
16520
+ description: "Schema name (optional; can also come from the session)"
16521
+ },
16522
+ output: {
16523
+ type: "string",
16524
+ description: "Write result bytes/text to this path instead of stdout"
16525
+ }
16526
+ });
16389
16527
  function hasArgvFlag(rawArgs, flag) {
16390
16528
  return rawArgs.some((arg) => arg === flag || arg.startsWith(`${flag}=`));
16391
16529
  }
@@ -16398,6 +16536,18 @@ function validateAgentQueryFlags(options) {
16398
16536
  }
16399
16537
  }
16400
16538
  }
16539
+ function validateApiNativeFormatFlags(format, options) {
16540
+ if (!isApiNativeQueryFormat(format))
16541
+ return;
16542
+ if (options.json || options.agent) {
16543
+ throw new CliError(`--format ${format} cannot be combined with --json or --agent. Those flags expect structured CLI JSON output.`);
16544
+ }
16545
+ for (const flag of API_NATIVE_INCOMPATIBLE_PRESENTATION_FLAGS) {
16546
+ if (hasArgvFlag(options.rawArgs, flag)) {
16547
+ throw new CliError(`${flag} cannot be used with --format ${format}.`);
16548
+ }
16549
+ }
16550
+ }
16401
16551
  function parseQueryLayout(args) {
16402
16552
  const defaults = defaultDisplayOptions();
16403
16553
  if (args.layout === undefined)
@@ -16432,13 +16582,38 @@ function parsePagerOptions(args, agent) {
16432
16582
  }
16433
16583
  return resolvePagerOptions(pager);
16434
16584
  }
16585
+ function optionalTrimmedString(args, name) {
16586
+ const value = args[name];
16587
+ if (typeof value !== "string")
16588
+ return;
16589
+ const trimmed = value.trim();
16590
+ return trimmed === "" ? undefined : trimmed;
16591
+ }
16592
+ function resolveQueryComputeSize(options) {
16593
+ const computeSize = options.computeSizeArg ?? "AUTO";
16594
+ if (options.sessionId && !options.computeSizeExplicit)
16595
+ return;
16596
+ return computeSize;
16597
+ }
16435
16598
  function parseQueryOutputOptions(args, options) {
16436
16599
  validateAgentQueryFlags(options);
16437
16600
  const format = parseQueryResultFormat(args.format === undefined ? "human" : asCliArgString(args.format));
16601
+ validateApiNativeFormatFlags(format, options);
16602
+ const sessionId = optionalTrimmedString(args, "session-id");
16603
+ const computeSize = resolveQueryComputeSize({
16604
+ sessionId,
16605
+ computeSizeArg: args["compute-size"] === undefined ? undefined : asCliArgString(args["compute-size"]),
16606
+ computeSizeExplicit: hasArgvFlag(options.rawArgs, "--compute-size")
16607
+ });
16438
16608
  return {
16439
16609
  format,
16440
16610
  displayOptions: parseDisplayOptions(args),
16441
- pagerOptions: parsePagerOptions(args, options.agent)
16611
+ pagerOptions: parsePagerOptions(args, options.agent),
16612
+ outputPath: optionalTrimmedString(args, "output"),
16613
+ computeSize,
16614
+ dialect: optionalTrimmedString(args, "dialect"),
16615
+ catalog: optionalTrimmedString(args, "catalog"),
16616
+ schema: optionalTrimmedString(args, "schema")
16442
16617
  };
16443
16618
  }
16444
16619
 
@@ -16450,8 +16625,9 @@ var queryRunArgs = defineArguments({
16450
16625
  required: false,
16451
16626
  directRequired: true
16452
16627
  },
16453
- ...queryResultFormatArgs,
16628
+ ...queryApiResultFormatArgs,
16454
16629
  ...queryDisplayArgs,
16630
+ ...queryRequestArgs,
16455
16631
  "query-id": { type: "string", description: "Optional stable query id" },
16456
16632
  "session-id": { type: "string", description: "Optional session id" },
16457
16633
  ...queryPagerArgs
@@ -16500,11 +16676,11 @@ var queryCommand = defineCommand({
16500
16676
  examples: [
16501
16677
  'altertable query "SELECT id, email, plan FROM analytics.main.users LIMIT 10"',
16502
16678
  'altertable query "SELECT event, timestamp FROM analytics.main.events ORDER BY timestamp DESC LIMIT 10" --json',
16679
+ 'altertable query "SELECT 1" --format csv --output results.csv',
16503
16680
  "altertable query show <query-id>"
16504
16681
  ]
16505
16682
  },
16506
16683
  args: queryRunArgs,
16507
- soleDirectOperands: ["show"],
16508
16684
  subcommands: {
16509
16685
  show: queryShowCommand,
16510
16686
  cancel: queryCancelCommand
@@ -16514,17 +16690,36 @@ var queryCommand = defineCommand({
16514
16690
  if (statement === undefined) {
16515
16691
  throw new CliError('Provide a SQL statement, e.g. altertable query "SELECT 1".');
16516
16692
  }
16517
- const { format, displayOptions, pagerOptions } = parseQueryOutputOptions(args, {
16693
+ const {
16694
+ format,
16695
+ displayOptions,
16696
+ pagerOptions,
16697
+ outputPath,
16698
+ computeSize,
16699
+ dialect,
16700
+ catalog,
16701
+ schema
16702
+ } = parseQueryOutputOptions(args, {
16518
16703
  agent: execution.cli.agent,
16704
+ json: sink.json,
16519
16705
  rawArgs
16520
16706
  });
16521
16707
  const input2 = {
16522
16708
  statement,
16523
16709
  queryId: optionalStringArg(args, "query-id"),
16524
- sessionId: optionalStringArg(args, "session-id")
16710
+ sessionId: optionalStringArg(args, "session-id"),
16711
+ computeSize,
16712
+ dialect,
16713
+ catalog,
16714
+ schema
16525
16715
  };
16716
+ if (isApiNativeQueryFormat(format)) {
16717
+ const stream = await executeLakehouseQueryBytes({ ...input2, format }, execution);
16718
+ await writeQueryDestination(stream, { outputPath, sink });
16719
+ return;
16720
+ }
16526
16721
  const result = await executeLakehouseQuery(input2, execution, !sink.json);
16527
- await writeQueryOutput(result, format, sink, displayOptions, pagerOptions);
16722
+ await writeQueryOutput(result, format, sink, displayOptions, pagerOptions, outputPath);
16528
16723
  }
16529
16724
  });
16530
16725
 
@@ -16641,6 +16836,7 @@ var schemaCommand = defineCommand({
16641
16836
  const catalog = stringArg(args, "catalog");
16642
16837
  const { format, displayOptions, pagerOptions } = parseQueryOutputOptions(args, {
16643
16838
  agent: execution.cli.agent,
16839
+ json: sink.json,
16644
16840
  rawArgs
16645
16841
  });
16646
16842
  const queryInput = {
@@ -21924,11 +22120,6 @@ var HELP_COLUMN_GAP = " ";
21924
22120
  var HELP_MIN_DESCRIPTION_WIDTH = 16;
21925
22121
  var HELP_FALLBACK_TERMINAL_WIDTH = 68;
21926
22122
  var COMMAND_PATTERN = /altertable(?:\s+[^\s'",]+)*/g;
21927
- var COMMAND_GROUP_TITLES = {
21928
- platform: "Platform",
21929
- ingest: "Ingest",
21930
- query: "Query"
21931
- };
21932
22123
  function renderHighlightedCommands(text3) {
21933
22124
  const spans = [];
21934
22125
  let offset = 0;
@@ -22155,11 +22346,7 @@ function renderCommandUsage(descriptor, parentDescriptor) {
22155
22346
  }
22156
22347
  function renderRootUsage(descriptor) {
22157
22348
  const metadata = descriptor.metadata;
22158
- const groupedEntries = {
22159
- platform: [],
22160
- ingest: [],
22161
- query: []
22162
- };
22349
+ const groupedEntries = Object.fromEntries(ALTERTABLE_COMMAND_GROUPS.map(({ id }) => [id, []]));
22163
22350
  const lines = [
22164
22351
  ...wrapHelpText(metadata.description, getHelpTerminalWidth()),
22165
22352
  "",
@@ -22178,9 +22365,10 @@ function renderRootUsage(descriptor) {
22178
22365
  });
22179
22366
  }
22180
22367
  let renderedGroup = false;
22181
- for (const [group, entries] of Object.entries(groupedEntries)) {
22368
+ for (const { id, title } of ALTERTABLE_COMMAND_GROUPS) {
22369
+ const entries = groupedEntries[id];
22182
22370
  if (entries.length > 0) {
22183
- lines.push(...renderedGroup ? [""] : [], ` ${renderDisplayText([span(COMMAND_GROUP_TITLES[group], "muted")])}`, ...formatHelpEntries(entries));
22371
+ lines.push(...renderedGroup ? [""] : [], ` ${renderDisplayText([span(title, "muted")])}`, ...formatHelpEntries(entries));
22184
22372
  renderedGroup = true;
22185
22373
  }
22186
22374
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altertable/cli",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "description": "Altertable CLI — query and manage your data platform",
5
5
  "homepage": "https://github.com/altertable-ai/altertable-cli#readme",
6
6
  "bugs": {