Skip to content

Commit

Permalink
Fix optional arguments in scalar computed column functions (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Nov 4, 2024
2 parents ba3e6d7 + e411e71 commit f4655e2
Show file tree
Hide file tree
Showing 63 changed files with 25,125 additions and 8,097 deletions.
6 changes: 6 additions & 0 deletions .changeset/curly-tigers-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-build-pg": patch
"postgraphile": patch
---

Fixes bug handling optional arguments to computed column functions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
PgInsertSingleStep,
PgResource,
PgResourceParameter,
PgSelectArgumentDigest,
PgSelectArgumentSpec,
PgSelectStep,
PgTypedExecutableStep,
Expand Down Expand Up @@ -1100,25 +1101,42 @@ function modFields(
typeof resource.from === "function"
) {
// This is a scalar computed attribute, let's inline the expression
const placeholders = selectArgs.map((arg, i) => {
if (i === 0) {
return $row.getClassStep().alias;
} else if ("pgCodec" in arg && arg.pgCodec) {
return $row.placeholder(arg.step, arg.pgCodec);
} else {
return $row.placeholder(
arg.step as PgTypedExecutableStep<any>,
);
}
});
const newSelectArgs = selectArgs.map(
(
arg,
i,
): PgSelectArgumentDigest & {
// We _MUST_ set `name` if we're told one
name: string | undefined;
} => {
const { name } = arg;
if (i === 0) {
return {
name,
placeholder: $row.getClassStep().alias,
};
} else if ("pgCodec" in arg && arg.pgCodec) {
return {
name,
placeholder: $row.placeholder(
arg.step,
arg.pgCodec,
),
};
} else {
return {
name,
placeholder: $row.placeholder(
arg.step as PgTypedExecutableStep<any>,
),
};
}
},
);
return pgClassExpression(
$row,
resource.codec,
)`${resource.from(
...placeholders.map((placeholder) => ({
placeholder,
})),
)}`;
)`${resource.from(...newSelectArgs)}`;
}
// PERF: or here, if scalar add select to `$row`?
return resource.execute(selectArgs);
Expand Down
8 changes: 8 additions & 0 deletions postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ create function c.person_friends(person c.person) returns setof c.person as $$ s
comment on function c.person_friends(c.person) is E'@sortable';
create function c.person_first_post(person c.person) returns a.post as $$ select * from a.post where a.post.author_id = person.id order by id asc limit 1 $$ language sql stable;
comment on function c.person_first_post(c.person) is 'The first post by the person.';

-- Same as optional_missing_middle_* functions above, but computed column
create function c.person_optional_missing_middle_1(p c.person, int, b int default 2, c int default 3) returns int as $$ select $2 + $3 + $4 $$ language sql immutable strict;
create function c.person_optional_missing_middle_2(p c.person, a int, b int default 2, c int default 3) returns int as $$ select $2 + $3 + $4 $$ language sql immutable strict;
create function c.person_optional_missing_middle_3(p c.person, a int, int default 2, c int default 3) returns int as $$ select $2 + $3 + $4 $$ language sql immutable strict;
create function c.person_optional_missing_middle_4(p c.person, int, b int default 2, int default 3) returns int as $$ select $2 + $3 + $4 $$ language sql immutable strict;
create function c.person_optional_missing_middle_5(p c.person, a int, int default 2, int default 3) returns int as $$ select $2 + $3 + $4 $$ language sql immutable strict;

create function c.compound_type_computed_field(compound_type c.compound_type) returns integer as $$ select compound_type.a + compound_type.foo_bar $$ language sql stable;
create function a.post_headline_trimmed(post a.post, length int default 10, omission text default '') returns text as $$ select substr(post.headline, 0, length) || omission $$ language sql stable;
create function a.post_headline_trimmed_strict(post a.post, length int default 10, omission text default '') returns text as $$ select substr(post.headline, 0, length) || omission $$ language sql stable strict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1633,4 +1633,12 @@
},
],
},
personById: {
optionalMissingMiddle1: 10,
optionalMissingMiddle1_2: 16,
optionalMissingMiddle2: 10,
optionalMissingMiddle3: 10,
optionalMissingMiddle4: null,
optionalMissingMiddle5: null,
},
}
Loading

0 comments on commit f4655e2

Please sign in to comment.