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

Cannot use Func within CTE - Can't export unaliased expression subQuery #438

Open
dragondgold opened this issue Dec 30, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@dragondgold
Copy link

dragondgold commented Dec 30, 2024

Describe the bug
When using a custom Func within a CTE like this:

func ARRAY_AGG_ORDER_BY(a Expression, b Expression) Expression {
	return Func("array_agg", CustomExpression(a, Token("ORDER BY"), b))
}

reservationsCTE := CTE("reservations")

stmtTest := WITH(
	reservationsCTE.AS(
		Schedules.SELECT(
			ARRAY_AGG_ORDER_BY(Users.Name, Users.Name),
		).FROM(
			Schedules.
				INNER_JOIN(Users, Users.ID.EQ(ScheduleSlots.UserID)),
		).GROUP_BY(Schedules.CreatedByUserID),
	),
)(
	SELECT(reservationsCTE.AllColumns()).
		FROM(reservationsCTE),
)

I get the following panic:

panic: jet: can't export unaliased expression subQuery: reservations, expression: array_agg(users.name ORDER BY users.name) [recovered]
	panic: jet: can't export unaliased expression subQuery: reservations, expression: array_agg(users.name ORDER BY users.name)

goroutine 5 [running]:
testing.tRunner.func1.2({0x1010076c0, 0x14000262260})
	/usr/local/go/src/testing/testing.go:1632 +0x3c0
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1635 +0x57c
panic({0x1010076c0?, 0x14000262260?})
	/usr/local/go/src/runtime/panic.go:785 +0xf0
github.com/go-jet/jet/v2/internal/jet.(*ExpressionInterfaceImpl).fromImpl(0x14000266c80, {0x101092a10, 0x14000266d00})
	/Users/andrestorti/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/expression.go:38 +0x174
github.com/go-jet/jet/v2/internal/jet.ProjectionList.fromImpl({0x140002621c0, 0x1, 0x1}, {0x101092a10, 0x14000266d00})
	/Users/andrestorti/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/projection.go:21 +0xc8
github.com/go-jet/jet/v2/internal/jet.selectTableImpl.AllColumns({{0x127e93880, 0x14000260580}, {0x100f8f90f, 0xc}, {0x0, 0x0, 0x0}})
	/Users/andrestorti/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/select_table.go:44 +0xe4
github.com/test/core/pkg/repositories/reports.(*ReportsRepository).GetReservationsReport(0x101254840, {0x101092968, 0x101254840}, {0x100f9a8d0, 0x29}, {0x401fc58, 0xededd1f4b, 0x0}, {0x4020040, 0xedf04ac4b, ...})
	/Users/andrestorti/test/core/pkg/repositories/reports/reservations.go:47 +0xac8
github.com/test/core/pkg/repositories/reports_test.TestGetReservations(0x1400023c4e0)
	/Users/andrestorti/test/core/pkg/repositories/reports/reservations_test.go:13 +0xd4
testing.tRunner(0x1400023c4e0, 0x10108ef80)
	/usr/local/go/src/testing/testing.go:1690 +0x1a8
created by testing.(*T).Run in goroutine 1
	/usr/local/go/src/testing/testing.go:1743 +0x668
Process 83174 has exited with status 2

This does not happen when using the custom Func outside a CTE

Environment:

  • OS: macosx
  • Database: postgres
  • Database driver: pgx
  • Jet version 2.12.0
@dragondgold dragondgold added the bug Something isn't working label Dec 30, 2024
@houten11
Copy link

As the error indicates, you need to alias your expression:

		Schedules.SELECT(
			ARRAY_AGG_ORDER_BY(Users.Name, Users.Name).AS("foo"),
		)

@dragondgold
Copy link
Author

As the error indicates, you need to alias your expression:

		Schedules.SELECT(
			ARRAY_AGG_ORDER_BY(Users.Name, Users.Name).AS("foo"),
		)

Ohhhh got it, but why does it work without an explicit alias when not used within a CTE?

@go-jet
Copy link
Owner

go-jet commented Jan 7, 2025

In this line:

	SELECT(reservationsCTE.AllColumns()).

you're referring to a projection from a CTE. Since the projection is not named, it cannot be referenced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants