cube icon indicating copy to clipboard operation
cube copied to clipboard

Need to define a cube based on a Postgre sql User defined function with string parameter

Open midhunjohn11 opened this issue 1 year ago • 2 comments

Problem

I have a requirement to create a cube that is based on an existing PostgreSQL user-defined function with a string parameter as input. And the parameters will be supplied from the Rest API call to Cube.js. Is it possible to build such a cube? Or what's the best way to implement it?

Below is a sample code I tried but it gives me an error "Referencev_value is not defined."

cube(New1, { sql: => SELECT * from test.ufn_function(${v_value}) ,
dimensions: { is_total: { sql: id, type: number, primary_key: true, shown:true }, "name": { sql: "name", type: number } },
});

Related Cube.js-generated SQL

This is not getting generated due to an error.

Can someone help me here? Or guide me with proper way to create a cube using user-defined functions with parameters

midhunjohn11 avatar Feb 02 '24 17:02 midhunjohn11

@paveltiunov Can you help me on this?

midhunjohn11 avatar Feb 05 '24 20:02 midhunjohn11

I found a way to do this, but not sure if this is the right approach. I have changed the cube definition like below and send the params as payload in API call

cube('New1', { sql: () => SELECT * FROM test.ufn_function(${SECURITY_CONTEXT.type.unsafeValue()}), dimensions: { is_total: { sql: 'id', type: 'number', primary_key: true, shown: true }, "name": { sql: '"name"', type: 'number' } }, });

midhunjohn11 avatar Feb 08 '24 15:02 midhunjohn11

Hi @midhunjohn11 👋

Could you please zoom out and provide more context here? What is that user-defined function doing? Why would you want to pass parameters to it?

I found a way to do this

Please see the docs: https://cube.dev/docs/reference/data-model/context-variables#security_context. As mentioned there, using SECURITY_CONTEXT in cube definitions is deprecated. It would basically stop working in future versions of Cube.

igorlukanin avatar Feb 20 '24 12:02 igorlukanin

hi @igorlukanin Thanks for the response. The PoC we are trying to achieve is to deploy Cube.Dev as a semantic layer between high charts and postgre DB. And the above user defined function is at the Postgresql DB which does some complex logic. So I'm trying to create a cube in the Cube.Dev on top of this user defined function, with the value being passed dynamically. I am not sure whether this is a right approach or not. I might be wrong here. Can you suggest anything on this? And keep in mind that we need to pass the user defined function logic results to the High charts to build reports.

midhunjohn11 avatar Mar 05 '24 21:03 midhunjohn11

Could you please elaborate on that value that is passed as an argument to the function? What is the range of values, how many unique values are out there, etc.

If you really want to use the approach that you outlined above, please switch to COMPILE_CONTEXT and configure multitenancy accordingly: https://cube.dev/docs/reference/data-model/context-variables#compile_context

igorlukanin avatar Mar 20 '24 15:03 igorlukanin