obs-browser icon indicating copy to clipboard operation
obs-browser copied to clipboard

SETOF support

Open GrandFelix opened this issue 6 years ago • 3 comments

Is possible to get SETOF support

GrandFelix avatar Jun 28 '18 07:06 GrandFelix

I don't have the necessity for SETOF for the moment in my project but I was searching informations about the subject I found this and this

sachaarbonel avatar Aug 22 '18 18:08 sachaarbonel

Is here something new maybe? 😇

GrandFelix avatar Sep 13 '18 14:09 GrandFelix

So the SETOF functionality isn't just eg. a function that returns an array You cannot write something like:

func FibonacciSETOF(n int) []int {
    f := make([]int, n+1, n+2)
    if n < 2 {
        f = f[0:2]
    }
    f[0] = 0
    f[1] = 1
    for i := 2; i <= n; i++ {
        f[i] = f[i-1] + f[i-2]
    }
    return f
}

and expect it to be a SETOF returning function. These functions are written so, that it is called multiple times and every time it receives the user "context" and information of where it is in the "SET" . https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-RETURN-SET

It is possible to split the function to these calls. something like:

//returning the data to be stored in context and the maximum number of calls
func FibonacciSETOFInit(n int) (interface{}, int) {
    f := make([]int, n+1, n+2)
    if n < 2 {
        f = f[0:2]
    }
    f[0] = 0
    f[1] = 1
    return f, n
}

//this function will be called n-times
//getting the context, with the data and iteration number
func FibonacciSETOFNext(ctx plgo.Context) int {
    f := ctx.Value().([]int)
    f[ctx.Iteration()] = f[ctx.Iteration()-1] + f[ctx.Iteration()-2]
    return f[ctx.Iteration()]
}

so if you call select fibonacci(10) plgo will generate the function where the first time it will call FibonacciSETOFInit and then 10 times FibonacciSETOFNext.

There is a user_fctx pointer, to user defined data, that are allocated in the "init" phase and then used in the function calls. The problem is, that memory controlled by go, cannot be passed to C. So this context cannot be used by plgo.

And if it is somehow possible, writing something like this is complicated. So maybe it is better to just write a function returning an array and then call it with unnest: select unnest(Fibonacci(10)) Any ideas what to do?

microo8 avatar Dec 07 '19 13:12 microo8