-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsonArrayFrom
support outside of select
.
#1298
Comments
Hey 👋 Can you provide a kyse.link that reproduces this? |
Is there a reason you're using lateral join instead of selecting the |
@igalklebanov ah soz, I should have led with that. https://kyse.link/Zrxmu @koskimas this is part of a larger query and even though you wouldn't necessarily think it from the query plan, the left lateral performs significantly better than a subselect in practice (in this case) |
Agree, this would be an amazing addition if it worked with the correct types. The correlated subquery from the docs performs much worse also for me. IMO FYI |
Here's a workaround: import { Expression, RawBuilder, Simplify, sql } from "kysely";
const rows = await db
.selectFrom("person")
.leftJoinLateral(
(eb) =>
jsonArrayFromForJoins(
eb
.selectFrom("pet")
.selectAll("pet")
.whereRef("owner_id", "=", "person.id"),
).as("pets"),
(join) => join.onTrue(),
)
.where("first_name", "=", sql.lit("Jennifer"))
.select(["first_name", "pets.items"])
.execute();
export function jsonArrayFromForJoins<O>(
expr: Expression<O>,
): RawBuilder<{ items: Simplify<O>[] }> {
return sql`(select coalesce(json_agg(agg), '[]') as items from ${expr} as agg)`;
} |
jsonArrayFrom
support outside of select
.
Renamed this. It's not a bug, you're holding it wrong - There is an enhancement opportunity here:
|
Amazing, thanks for the workaround. I would adapt it slightly to this, so if there's multiple lateral joins, I have proper names for import {
Expression,
RawBuilder,
AliasedRawBuilder,
Simplify,
sql,
} from "kysely";
const rows = await db
.selectFrom("person")
.leftJoinLateral(
(eb) =>
jsonArrayFromForJoins(
eb
.selectFrom("pet")
.selectAll("pet")
.whereRef("owner_id", "=", "person.id"),
"pets",
),
(join) => join.onTrue(),
)
.where("first_name", "=", sql.lit("Jennifer"))
.selectAll("pets")
.select(["first_name"])
.execute();
export function jsonArrayFromForJoins<O, T extends string>(
expr: Expression<O>,
as: T,
): AliasedRawBuilder<Simplify<Record<T, O[]>>, T> {
const sqlExpression: RawBuilder<Simplify<Record<T, O[]>>> =
sql`(select coalesce(json_agg(agg), '[]') as ${sql.raw(as)} from ${expr} as agg)`;
return sqlExpression.as(as);
} |
First off, great library. I can't believe how few corner cases I run into. Really just awesome stuff.
Here's the relevant section of a query where I've run into this.
The reason I assume it's bug behavior is because the select clause can't pick up the
rules.coalesce
column so I have to dip into sql to get to it.at the mechanical level it's clear that the
coalesce
function that's created byjsonArrayFrom
isn't getting an alias so it's using the function name.The text was updated successfully, but these errors were encountered: