Skip to content

OrderByClauseVisitor does not use fully qualified column names #968

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

Closed
nt-gt opened this issue Apr 20, 2021 · 1 comment
Closed

OrderByClauseVisitor does not use fully qualified column names #968

nt-gt opened this issue Apr 20, 2021 · 1 comment
Assignees
Labels
in: selectbuilder SelectBuilder stands for the complete API for creating SQL statements programmatically. type: enhancement A general enhancement

Comments

@nt-gt
Copy link

nt-gt commented Apr 20, 2021

Hi,

For queries with joins, we occasionally end up with situations where a column name becomes ambiguous and we need to qualify it with the table / alias - as an example, our hand-rolled SQL generated created the following SQL from our test samples:

SELECT order_table.orderline AS "orderline", customer_table.customer_name AS "customerName", customer_address.street_name AS "customerAddress", warehouse_address.street_name AS "warehouseAddress"
 FROM order_table
 JOIN customer_table ON order_table.customer_id = customer_table.address_id
 JOIN address_table customer_address ON customer_table.address_id = customer_address.address_id
 JOIN address_table warehouse_address ON order_table.address_id = warehouse_address.address_id
 WHERE warehouse_address.street_name = :warehouse AND customer_table.customer_name = :customerName AND customer_address.street_name = :customerAddress
 ORDER BY customer_address.street_name ASC, warehouse_address.street_name ASC

When migrating our SQL generator to generate Select and use the SelectRenderer, we discovered that the generated ORDER BY clause became ORDER BY street_name ASC, street_name ASC. This is ambiguous as street_name is defined from multiple sources (once from customer_address and once from warehouse_address).

As a work around, I have migrated it to use the aliased columns, which mitigates the problem as long as the ORDER BY only references columns that are SELECT'ed. In at least PostgreSQL, you can include other columns in ORDER BY, so we would like this to be supported via Spring as well.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 20, 2021
@nt-gt
Copy link
Author

nt-gt commented Apr 20, 2021

In case it matters, I am using PostgresDialect from r2dbc which extends org.springframework.data.relational.core.dialect.PostgresDialect.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels May 3, 2021
@schauder schauder self-assigned this Jul 21, 2021
@schauder schauder added the in: selectbuilder SelectBuilder stands for the complete API for creating SQL statements programmatically. label Jul 21, 2021
schauder added a commit that referenced this issue Nov 4, 2021
@mp911de mp911de closed this as completed in 7d1ed85 Nov 8, 2021
mp911de added a commit that referenced this issue Nov 8, 2021
Use column aliases in order-by/expressions when column is aliased. Reduce method visibility in tests.

Closes #968
Original pull request: #1080.
@mp911de mp911de added this to the 2.3 GA (2021.1.0) milestone Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: selectbuilder SelectBuilder stands for the complete API for creating SQL statements programmatically. type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants