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

Include user defined SQL Functions #10638

Open
olmohake opened this issue Dec 27, 2024 · 7 comments
Open

Include user defined SQL Functions #10638

olmohake opened this issue Dec 27, 2024 · 7 comments
Labels
c/v3-ndc-postgres Data connector for PostgreSQL k/enhancement New feature or improve an existing feature

Comments

@olmohake
Copy link

v3-engine / postgres-ndc

Is your proposal related to a problem?

I have a sql function that does complex aggregations for a given territory and date and would like to make it available in the supergraph, but the postgres connector does not support user defined functions .

Describe the solution you'd like

Implement introspection and execution for user defined functions.

Describe alternatives you've considered

Reimplement the aggregation logic in a ts or python connector.

@olmohake olmohake added the k/enhancement New feature or improve an existing feature label Dec 27, 2024
@robertjdominguez robertjdominguez added the c/v3-ndc-postgres Data connector for PostgreSQL label Dec 27, 2024
@robertjdominguez
Copy link
Contributor

cc @BenoitRanque

@tirumaraiselvan
Copy link
Contributor

An easier alternative is to use Native Queries: Here is documentation on adding native queries: https://hasura.io/docs/3.0/reference/connectors/postgresql/native-operations/native-queries

Here are few examples of native query templates that you can use for your UDFs (reference for syntax: https://www.postgresql.org/docs/current/sql-syntax-calling-funcs.html) :

SELECT * FROM search_articles(search_term => {{search_term}})

OR

SELECT * FROM search_articles(search_term := {{search_term}})

OR

SELECT * FROM search_articles({{search_term}})

Once you have added a native query to your connector, you can reintrospect and add it as a model:

1. ddn connector introspect postgres_prod
2. ddn model add search_articles

@robertjdominguez
Copy link
Contributor

robertjdominguez commented Jan 1, 2025

Given that we've outlined the option to use native queries — and that we're tracking this internally with support in the future — I'm going to close this issue, @olmohake. While it's not a priority right now, that could change should demand increase.

For anyone landing on this issue seeking UDFs directly and automatically added to your metadata when introspecting a source, please add a reaction to this comment 🙏

@robertjdominguez
Copy link
Contributor

@olmohake — for public tracking, I'm going to reopen this issue and add it to our roadmap 👍

@robertjdominguez robertjdominguez moved this to Backlog in V3 Jan 2, 2025
@olmohake
Copy link
Author

olmohake commented Jan 2, 2025

I have a function with the following signature:

create function age_groups(
    area CHAR(8), 
    date text, 
    cuts INTEGER[]
) RETURNS TABLE (
    age_group TEXT,
    amount INTEGER
) AS $$
...

and defined the following native query

select * from age_groups({{area}}, {{date}}, {{cuts}})

however introspection is dropping the array and returns

{
...
"arguments": {
   "cuts":{
      "name": "cuts",
      "type":{
         "scalarType": "int4"
      }
   }
}

and accordingly the model looks like this:

kind: Model
version: v1
definition:
  name: Agegroups
  objectType: Agegroups
  arguments:
    - name: cuts
      type: Int4!

should i open a separate issue regarding array inputs?

@BenoitRanque
Copy link
Contributor

@olmohake it seems we only support scalar arguments at the moment. Working on changing that, to support array arguments at least.

@BenoitRanque
Copy link
Contributor

@olmohake just wanted you to know, as a workaround: this issue is only a problem with the function introspection.
This means that, as a workaround, you can manually write the configuration metadata.

Do let us know if you require help with that while we work on permanently solving this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/v3-ndc-postgres Data connector for PostgreSQL k/enhancement New feature or improve an existing feature
Projects
Status: Backlog
Development

No branches or pull requests

4 participants