Skip to content
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 inside left lateral join returns column named "coalesce" #1298

Open
weeksie opened this issue Dec 21, 2024 · 3 comments
Open

Comments

@weeksie
Copy link

weeksie commented Dec 21, 2024

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.

        eb
          .selectFrom("connectedDivisions")
          .leftJoinLateral(
            ({ selectFrom }) =>
              // throwing a bare jsonArrayFrom over this query 
              // doesn't let you add an alias for the column so 
              // you end up with `coalesce` as the column name 
              // b/c under the hood the query is
              //    SELECT coalesce(json_agg(...
              jsonArrayFrom(
                selectFrom("connectionDivisionFulfillmentRules")
                  .innerJoin(
                    "fulfillmentRules",
                    "fulfillmentRules.id",
                    "connectionDivisionFulfillmentRules.fulfillmentRuleId"
                  )
                  .whereRef(
                    "connectionDivisionFulfillmentRules.connectionDivisionId",
                    "=",
                    "connectedDivisions.id"
                  )
                  .selectAll("fulfillmentRules")
                  .select(({ ref }) => [
                    jsonObjectFrom(joinAddress(ref("fulfillmentRules.addressId"))).as("address"),
                    jsonObjectFrom(joinSchedule(ref("fulfillmentRules.scheduleId")))
                      .$notNull()
                      .as("schedule"),
                  ])
              ).as("rules"),
            (join) => join.onTrue()
          )
          .selectAll(["connectedDivisions"])
          // see left lateral join for why `rules.coalesce`
          .select(() => [sql`rules.coalesce`.as("rules")])
          .whereRef("connectedDivisions.connectionId", "=", "filteredConnections.id")

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 by jsonArrayFrom isn't getting an alias so it's using the function name.

@igalklebanov
Copy link
Member

Hey 👋

Can you provide a kyse.link that reproduces this?

@koskimas
Copy link
Member

Is there a reason you're using lateral join instead of selecting the rules?

@weeksie
Copy link
Author

weeksie commented Dec 23, 2024

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants