@rex0220/kintone-sql-tools 3.35.0 → 3.36.0

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.
package/dist-cli/ksql.js CHANGED
@@ -18252,18 +18252,20 @@ function rememberSelectWhereCapability(stmt, capability) {
18252
18252
  }
18253
18253
  function isCountStarTotalCountEligible(stmt, whereCapability) {
18254
18254
  if (whereCapability.capability !== "EXACT_PUSHDOWN") return false;
18255
- if (stmt.columns.length !== 1) return false;
18256
- const column = stmt.columns[0];
18257
- if (column?.type !== "AGGREGATE" || column.func !== "COUNT" || column.distinct || column.arg.type !== "WILDCARD") {
18258
- return false;
18259
- }
18255
+ const countColumns = stmt.columns.filter(
18256
+ (column) => column.type === "AGGREGATE" && column.func === "COUNT" && !column.distinct && column.arg.type === "WILDCARD"
18257
+ );
18258
+ if (countColumns.length !== 1) return false;
18259
+ if (stmt.columns.some(
18260
+ (column) => column !== countColumns[0] && column.type !== "LITERAL_COL"
18261
+ )) return false;
18260
18262
  return !stmt.distinct && normalizeGroupingSpec(stmt).type === "NONE" && stmt.having === null && stmt.joins.length === 0 && stmt.from.cteName === null && !stmt.from.subtableCode && stmt.limit === null && stmt.offset === null;
18261
18263
  }
18262
18264
  function isValidTotalCount(value) {
18263
18265
  return typeof value === "string" && /^(?:0|[1-9]\d*)$/.test(value);
18264
18266
  }
18265
18267
  async function tryCountStarWithTotalCount(stmt, client) {
18266
- const countColumn = stmt.columns[0];
18268
+ const countColumn = stmt.columns.find((column) => column.type === "AGGREGATE");
18267
18269
  if (countColumn?.type !== "AGGREGATE") return null;
18268
18270
  const baseQuery = stmt.where === null ? "" : whereToKintone(stmt.where);
18269
18271
  const response = await client.getRecords({
@@ -18274,11 +18276,15 @@ async function tryCountStarWithTotalCount(stmt, client) {
18274
18276
  });
18275
18277
  if (response.searchAborted) throw new SearchAbortedError();
18276
18278
  if (!isValidTotalCount(response.totalCount)) return null;
18277
- const column = countColumn.alias ?? "COUNT(*)";
18279
+ const countKey = countColumn.alias ?? "COUNT(*)";
18280
+ const projected = project(
18281
+ [{ [countKey]: response.totalCount }],
18282
+ stmt.columns
18283
+ );
18278
18284
  return {
18279
18285
  type: "SELECT",
18280
- rows: [{ [column]: response.totalCount }],
18281
- columns: [column],
18286
+ rows: projected.rows,
18287
+ columns: projected.columns,
18282
18288
  rowCount: 1,
18283
18289
  warnings: []
18284
18290
  };
@@ -18454,7 +18460,7 @@ async function executeUnion(stmt, client, options, cacheContext, captureColumnMe
18454
18460
  captureColumnMeta,
18455
18461
  forLibraryCapture
18456
18462
  ) : executeSelect(
18457
- stmt.left,
18463
+ markCountTotalCountRoot(stmt.left),
18458
18464
  client,
18459
18465
  effectiveOptions,
18460
18466
  cacheContext,
@@ -18463,7 +18469,7 @@ async function executeUnion(stmt, client, options, cacheContext, captureColumnMe
18463
18469
  forLibraryCapture
18464
18470
  ),
18465
18471
  executeSelect(
18466
- stmt.right,
18472
+ markCountTotalCountRoot(stmt.right),
18467
18473
  client,
18468
18474
  effectiveOptions,
18469
18475
  cacheContext,
@@ -23140,7 +23146,7 @@ function buildUnionPlan(stmt, capabilities, orderPlans, plainGroupByPlans) {
23140
23146
  const lines = [];
23141
23147
  selects.forEach((sel, i) => {
23142
23148
  if (i > 0) lines.push("");
23143
- lines.push(...buildSelectPlan(sel, `[union:${i + 1}]`, capabilities, orderPlans, plainGroupByPlans, false));
23149
+ lines.push(...buildSelectPlan(sel, `[union:${i + 1}]`, capabilities, orderPlans, plainGroupByPlans, true));
23144
23150
  });
23145
23151
  return lines;
23146
23152
  }