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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WITH RECURSIVE #209

Open
dankochetov opened this issue Mar 1, 2023 · 8 comments 路 May be fixed by #1336
Open

Support WITH RECURSIVE #209

dankochetov opened this issue Mar 1, 2023 · 8 comments 路 May be fixed by #1336
Assignees
Labels
enhancement New feature or request

Comments

@dankochetov
Copy link
Contributor

No description provided.

@dankochetov dankochetov added the enhancement New feature or request label Mar 20, 2023
@neil-and-void
Copy link

I can look into this!

@ngregrichardson
Copy link
Contributor

@neilZon Hey, have you had any luck with this so far? Interested in seeing this come to fruition.

@neil-and-void
Copy link

I started getting some progress but then I got really busy in the past couple of weeks. I should be able to start working on it again this weekend

@fireayehu
Copy link

@neilZon and @dankochetov any updates ?

@ngregrichardson
Copy link
Contributor

I believe @Angelelz is working with the team on a UNION ALL implementation that would be a step towards supporting this.

@fireayehu
Copy link

I believe @Angelelz is working with the team on a UNION ALL implementation that would be a step towards supporting this.

@ngregrichardson Thank You, I was creating a table for Hierarchical data (a tree structure) with adjacency list but now I started implementing it using closure table so I think I can get back to this in the future.

@neil-and-void
Copy link

Just an update, gonna start working on this now. This wil be blocked by #1218 I think but I'll try to working alongside it as this draft pr progresses

@neil-and-void neil-and-void linked a pull request Oct 4, 2023 that will close this issue
@neil-and-void
Copy link

neil-and-void commented Oct 7, 2023

With the changes from @Angelelz I was able to create a recursive query by the following. The issue is within the recursive step, the only way to refer to the outer name of the CTE is by the sql operator. I was thinking about revising it to have some function that creates an alias for recursive queries to allow the recursive step to have access to the columns of the table. However this alias will not have access to columns added within the recursive statement such as the level column. Thoughts @dankochetov?

Current Draft

const employeeHierarchy = db.$withRecursive('employeeHierarchy').as(
        // base
	db.select({
		id: employees.id,
		name: employees.name,
		position: employees.position,
		manager_id: employees.manager_id,
		level: sql<string>`0`.as("level")
	})
		.from(employees)
		.where(isNull(employees.manager_id))
		.union(
                        // recursive
			db.select({
				id: employees.id,
				name: employees.name,
				position: employees.position,
				manager_id: employees.manager_id,
				level: sql<string>`employeeHierarchy.level + 1`.as('level')
			})
				.from(employees)
				.innerJoin(sql<string>`employeeHierarchy`, eq(sql<string>`employeeHierarchy.id`, employees.manager_id))
		)
)
const query = db.with(employeeHierarchy).select().from(employeeHierarchy)

Revision

const employeeHierarchy = recursiveAlias('employeeHierarchy', employees)
const recursiveCte = db.$withRecursive(employeeHierarchy).as(
	db.select({
		id: employees.id,
		name: employees.name,
		position: employees.position,
		manager_id: employees.manager_id,
		level: sql<string>`0`.as("level")
	})
		.from(employees)
		.where(isNull(employees.manager_id))
		.union(
			db.select({
				id: employees.id,
				name: employees.name,
				position: employees.position,
				manager_id: employees.manager_id,
				level: sql<string>`${employeeHierarchy}.level + 1`.as('level')
			})
				.from(employees)
				.innerJoin(employeeHierarchy, eq(employeeHierarchy.id, employees.manager_id))
		)
)
const query = db.with(recursiveCte).select().from(recursiveCte)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

6 participants