Skip to content

Commit

Permalink
Add missing hasMore to pgUnionAll
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 23, 2023
1 parent 26b3f68 commit ab106b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
35 changes: 22 additions & 13 deletions grafast/dataplan-pg/src/steps/pgPageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { PgCursorStep } from "./pgCursor.js";
import type { PgSelectParsedCursorStep, PgSelectStep } from "./pgSelect.js";
import { PgSelectSingleStep } from "./pgSelectSingle.js";
import type { PgUnionAllStep } from "./pgUnionAll.js";
import type { PgUnionAllSingleStep, PgUnionAllStep } from "./pgUnionAll.js";

/*
* **IMPORTANT**: see pgPageInfo.md for reasoning behind decisions made in this file
Expand Down Expand Up @@ -95,12 +95,14 @@ export class PgPageInfoStep<
const lastExists = last && !last.evalIs(null) && !last.evalIs(undefined);
if (firstExists && !lastExists) {
const nodePlan = (
$connection as ConnectionStep<
any,
PgSelectParsedCursorStep,
PgSelectStep<any>,
any
>
$connection as
| ConnectionStep<
any,
PgSelectParsedCursorStep,
PgSelectStep<any>,
any
>
| ConnectionStep<any, PgSelectParsedCursorStep, PgUnionAllStep, any>
).cloneSubplanWithPagination();
return nodePlan.hasMore();
} else {
Expand Down Expand Up @@ -132,12 +134,19 @@ export class PgPageInfoStep<
const lastExists = last && !last.evalIs(null) && !last.evalIs(undefined);
if (lastExists && !firstExists) {
const nodePlan = (
$connection as ConnectionStep<
PgSelectSingleStep<any>,
PgSelectParsedCursorStep,
PgSelectStep<any>,
PgSelectSingleStep<any>
>
$connection as
| ConnectionStep<
PgSelectSingleStep<any>,
PgSelectParsedCursorStep,
PgSelectStep<any>,
PgSelectSingleStep<any>
>
| ConnectionStep<
PgUnionAllSingleStep,
PgSelectParsedCursorStep,
PgUnionAllStep,
PgUnionAllSingleStep
>
).cloneSubplanWithPagination();
return nodePlan.hasMore();
} else if (
Expand Down
17 changes: 17 additions & 0 deletions grafast/dataplan-pg/src/steps/pgUnionAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const EMPTY_ARRAY: ReadonlyArray<any> = Object.freeze([]);
const hash = (text: string): string =>
createHash("sha256").update(text).digest("hex").slice(0, 63);

// Constant functions so lambdas can be optimized
function listHasMore(list: any | null | undefined) {
return list?.hasMore || false;
}
listHasMore.isSyncAndSafe = true; // Optimization

function parseCursor(cursor: string | null) {
if (cursor == null) {
// This throw should never happen, so we can still be isSyncAndSafe.
Expand Down Expand Up @@ -965,6 +971,17 @@ on (${sql.indent(
}
}

/**
* Someone (probably pageInfo) wants to know if there's more records. To
* determine this we fetch one extra record and then throw it away.
*/
public hasMore(): ExecutableStep<boolean> {
this.fetchOneExtra = true;
// HACK: This is a truly hideous hack. We should solve this by having this
// plan resolve to an object with rows and metadata.
return lambda(this, listHasMore);
}

public placeholder($step: PgTypedExecutableStep<any>): SQL;
public placeholder($step: ExecutableStep, codec: PgCodec): SQL;
public placeholder(
Expand Down

0 comments on commit ab106b8

Please sign in to comment.