Skip to content

Add support for CAST for a column #1066

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 Oct 11, 2021 · 8 comments
Closed

Add support for CAST for a column #1066

nt-gt opened this issue Oct 11, 2021 · 8 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@nt-gt
Copy link

nt-gt commented Oct 11, 2021

I have a case where I want to cast a column to a VARCHAR for the comparison to support a UI feature of doing substring searches. Simplified to something like SELECT * FROM customer WHERE CAST(customerId AS VARCHAR) LIKE '%123%'.

As far as I can tell, the most portable way of doing this is to use CAST(x AS type) and so I thought I would abuse the following:

                Column x = ...;
                Column aliased = x.as(SqlIdentifier.unquoted("VARCHAR"));
                return SimpleFunction.create("CAST", Collections.singletonList(aliased));

Unfortunately, under (at least postgres) this generates the following invalid SQL:

CAST(x.VARCHAR)

Admittedly, this is abuse as column.as says it is for a different purpose. But I hope you can clarify how I would do a SQL type cast.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 11, 2021
@christophstrobl christophstrobl removed the status: waiting-for-triage An issue we've not yet triaged label Oct 11, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 11, 2021
@schauder
Copy link
Contributor

SimpleFunction doesn't do the trick here because it assumes something with comma separated arguments.

SQL.just should do the trick though.

@nt-gt
Copy link
Author

nt-gt commented Oct 18, 2021

SimpleFunction doesn't do the trick here because it assumes something with comma separated arguments.

In the given case, it only receives a single argument, which is why I thought I might pull it off in my work around. :)

SQL.just should do the trick though.

I cannot find the SQL.just (looking at org.springframework.data.relational.core.sql.SQL.just).

If you are thinking of the method where I can throw a raw string into it that will be passed "as-is", then my concern would be that I would lose the dialect mapping support (how names are formatted etc.). I have been trying hard to avoid ending up generating my own queries to ensure portability. :)

(Context: The code base I am working on used to generate the SQL itself and we had a lot of issues with corner cases because of this.)

@schauder
Copy link
Contributor

The method I tried to reference is Expressions.just sorry for that.

I understand your concerns. It was intended as a workaround not a as a proper solution.

@nt-gt
Copy link
Author

nt-gt commented Oct 18, 2021

Ok. Thanks for the suggestion. :)

Also many thanks for fixing the join related issues I reported. It was very much appreciated. :)

@mp911de
Copy link
Member

mp911de commented Oct 18, 2021

How about adding us some convenience support here?

@nt-gt
Copy link
Author

nt-gt commented Oct 18, 2021

Hi @mp911de

Assuming the comment was for me, could you elaborate a bit on what that convenience support would look like? :)

@mp911de
Copy link
Member

mp911de commented Oct 18, 2021

We have utility classes such as Functions defining a few common functions such as count or upper. We could add CAST there as well.

@nt-gt
Copy link
Author

nt-gt commented Oct 18, 2021

Ah. Yes, a Functions.cast(column, "VARCHAR") would work quite well for me. :)

@schauder schauder assigned schauder and unassigned mp911de Oct 19, 2021
schauder added a commit that referenced this issue Oct 19, 2021
Example: Functions.cast(table_user.column("name"),"VARCHAR2")

Also adds a toString to AbstractSegment to avoid stack overflows.

Closes #1066
@schauder schauder added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 19, 2021
mp911de added a commit that referenced this issue Oct 25, 2021
Remove unecessary toString calls. Make Cast.create public to align with other factory methods. Fix AbstractSegment.toString

See #1066
Original pull request: #1071.
@mp911de mp911de changed the title How do you CAST a column via spring-data's API? Add support for CAST for a column Oct 25, 2021
@mp911de mp911de added this to the 2.3 GA (2021.1.0) milestone Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants