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

Feat: select without from and with recursive #1405

Open
wants to merge 19 commits into
base: beta
Choose a base branch
from

Conversation

Angelelz
Copy link
Collaborator

@Angelelz Angelelz commented Oct 22, 2023

This will close #372 and will close #209. Also related to #1215?

This PR depends on #1218.

Upon merging, It will be possible to write a select statement without the .from() method. That will make possible to create recursive statements like the following (adapted from mysql docs example):

const fibonacci = db.$withRecursive('fibonacci').as((qb) =>
  qb
    .select({
      n: sql<number>`1`.as('n'),
      fibN: sql<number>`0`.as('fibN'),
      nextFibN: sql<number>`1`.as('nextFibN'),
      goldenRatio: sql<string>`cast(0 as decimal(5,4))`.as('gRatio'),
    })
    .unionAll((_, fibonacci) =>
      qb
        .select({
          n: sql<number>`${fibonacci.n} + 1`,
          fibN: fibonacci.nextFibN,
          nextFibN: sql<number>`${fibonacci.fibN} + ${fibonacci.nextFibN}`,
          goldenRatio: sql<
            string
          >`case when ${fibonacci.fibN} = 0 then 0 else ${fibonacci.nextFibN} / ${fibonacci.fibN} end`,
        })
        .from(fibonacci.as("recFib"))
        .where(and(lt(fibonacci.n, 10)))
    )
);

const result = await db.withRecursive(fibonacci).select().from(fibonacci);

And the result will be:

[
  { n: 1, fibN: 0, nextFibN: 1, goldenRatio: '0.0000' },
  { n: 2, fibN: 1, nextFibN: 1, goldenRatio: '0.0000' },
  { n: 3, fibN: 1, nextFibN: 2, goldenRatio: '1.0000' },
  { n: 4, fibN: 2, nextFibN: 3, goldenRatio: '2.0000' },
  { n: 5, fibN: 3, nextFibN: 5, goldenRatio: '1.5000' },
  { n: 6, fibN: 5, nextFibN: 8, goldenRatio: '1.6667' },
  { n: 7, fibN: 8, nextFibN: 13, goldenRatio: '1.6000' },
  { n: 8, fibN: 13, nextFibN: 21, goldenRatio: '1.6250' },
  { n: 9, fibN: 21, nextFibN: 34, goldenRatio: '1.6154' },
  { n: 10, fibN: 34, nextFibN: 55, goldenRatio: '1.6190' },
]

Please take a look at this tweet for another example.

@Angelelz Angelelz marked this pull request as ready for review November 2, 2023 22:20
@Angelelz Angelelz force-pushed the feat-select-without-from-and-with-recursive branch from 7df8fa1 to 9aa7c2d Compare November 8, 2023 11:48
@Angelelz Angelelz force-pushed the feat-select-without-from-and-with-recursive branch from 9aa7c2d to ce60bb2 Compare November 10, 2023 02:10
@mylescarrick
Copy link

This is so brilliant – I'm using this in my app, working with recursive data structures in postgres. Is there anything I can do to help move it toward review?

@lxia1220
Copy link

lxia1220 commented Dec 1, 2023

I used kysely to write hierarchy structure sql, this pr works well for me in most cases.
Is there any blocker for it?

@adamgreg
Copy link

This would be really helpful for me. Any chance it will make it into the next release?

@scape76
Copy link

scape76 commented Feb 19, 2024

any updates? +1 to the above @dankochetov

@mpost
Copy link

mpost commented Mar 2, 2024

I would be very happy to see this PR being merged. Working with postgres generate_series would be much more viable.

@johaven
Copy link

johaven commented Apr 6, 2024

+1

@JohnAllenTech
Copy link

Again +1 to this. Very much a blocker for me at the minute not to have this functionality.

@goto3
Copy link

goto3 commented Apr 8, 2024

+1

1 similar comment
@mikkelwf
Copy link

mikkelwf commented Apr 9, 2024

+1

@jonataslaw
Copy link

Firstly, thank you for your work.
Second: I really need this.
Third, the PR that you indicated as pending has already been merged into master, is there any reason for it not to be merged now?

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

Successfully merging this pull request may close these issues.

None yet