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

T-SQL LEFT and RIGHT functions accept integers, should cast arguments as varchar when translating to Trino #2899

Closed
joshnatis opened this issue Jan 30, 2024 · 2 comments

Comments

@joshnatis
Copy link

Hi Toby and friends. This is a very similar issue to #2885, except regarding the LEFT and RIGHT functions instead of the LEN function.

T-SQL

image

As seen above, T-SQL accepts non-varchar data as arguments for its LEFT and RIGHT functions. The documentation explicitly documents this this time:

https://learn.microsoft.com/en-us/sql/t-sql/functions/left-transact-sql?view=sql-server-ver16

LEFT ( character_expression , integer_expression )

character_expression
Is an expression of character or binary data. character_expression can be a constant, variable, or column. character_expression can be of any data type, except text or ntext, that can be implicitly converted to varchar or nvarchar. Otherwise, use the CAST function to explicitly convert character_expression.

Trino

query = sqlglot.transpile("SELECT RIGHT(123456, 2)", read="tsql", write="trino")[0]
print(query)

SELECT SUBSTRING(123456, LENGTH(123456) - (2 - 1))

Running the translated result in AWS Athena, we get:

FUNCTION_NOT_FOUND: line 1:26: Unexpected parameters (integer) for function length. Expected: length(varchar(x)), length(char(x)), length(varbinary)

We need to cast the argument for LENGTH in order to support the specification

Thanks, still hope this isn't too annoying. I'm dealing with a large codebase of MSSQL queries, so that's why I keep coming back with issues :). But it's actually quite magical to see how almost everything works without requiring modifications

@tobymao
Copy link
Owner

tobymao commented Jan 31, 2024

@joshnatis would appreciate it if you took the time to learn the code base to do these easy transpilations in the future. that way you can become a contributor! happy to help :)

@joshnatis
Copy link
Author

@tobymao noted! I’ll study the changes you made when closing out these issues, thanks for the encouragement

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

No branches or pull requests

2 participants