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

CustomFunction #798

Open
T828367-SPARK opened this issue Mar 24, 2024 · 1 comment
Open

CustomFunction #798

T828367-SPARK opened this issue Mar 24, 2024 · 1 comment

Comments

@T828367-SPARK
Copy link

Hi I am using Pypika with Snowflake and trying to create a custom function for Snowflakes OBJECT_CONSTRUCT function.
Snowflake docs - https://docs.snowflake.com/en/sql-reference/functions/object_construct.

This function takes in n parameters. With every odd parameter being a string, and every even parameter being a database column.

How do I create a customFunction that types a dynamic number of parameters. While also making every odd parameter a string.

SQL query should come out looking like:
select site ,
object_construct(
'Name', tbl.name,
'Lat', tbl.site_lat,
'Long', tbl.site_long,
) as myObject
from myTable tbl

Is this possible with Pypika's current functionality?

Thanks

@nlassaux
Copy link

nlassaux commented Apr 2, 2024

Hey! Have you tried the following?

def object_construct(function_name: str, params: list[tuple[str, str]]) -> CustomFunction:
    parameters = []
    for p in params:
        params.append(param[0]) #append the param's name
        params.append(param[1]) #append the param's value
    return CustomFunction(function_name, parameters)

That'd use this system: https://github.com/kayak/pypika?tab=readme-ov-file#custom-functions

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