microo8 / plgo

easily create postgresql extensions in golang; moved to gitlab.com/microo8/plgo
292 stars 23 forks source link

Support request for multiple return types aka when len(results.List) > 1 #17

Open sachaarbonel opened 6 years ago

sachaarbonel commented 6 years ago

https://github.com/microo8/plgo/blob/a744149107575082de08228b103e8bc95129ca43/plgo/functions.go#L129

Hi @microo8 sorry to bothering you again could you add support for multiple return types. I thought I could make a PR but my go skills still need some work lol. BUT I've made my homework here is the AST on my package :

33  .  .  .  Type: *ast.FuncType {
    34  .  .  .  .  Func: returned-values.go:5:1
    35  .  .  .  .  Params: *ast.FieldList {
    36  .  .  .  .  .  Opening: returned-values.go:5:7
    37  .  .  .  .  .  Closing: returned-values.go:5:8
    38  .  .  .  .  }
    39  .  .  .  .  Results: *ast.FieldList {
    40  .  .  .  .  .  Opening: returned-values.go:5:10
    41  .  .  .  .  .  List: []*ast.Field (len = 3) {
    42  .  .  .  .  .  .  0: *ast.Field {
    43  .  .  .  .  .  .  .  Type: *ast.ArrayType {
    44  .  .  .  .  .  .  .  .  Lbrack: returned-values.go:5:11
    45  .  .  .  .  .  .  .  .  Elt: *ast.Ident {
    46  .  .  .  .  .  .  .  .  .  NamePos: returned-values.go:5:13
    47  .  .  .  .  .  .  .  .  .  Name: "float64"
    48  .  .  .  .  .  .  .  .  }
    49  .  .  .  .  .  .  .  }
    50  .  .  .  .  .  .  }
    51  .  .  .  .  .  .  1: *ast.Field {
    52  .  .  .  .  .  .  .  Type: *ast.ArrayType {
    53  .  .  .  .  .  .  .  .  Lbrack: returned-values.go:5:22
    54  .  .  .  .  .  .  .  .  Elt: *ast.Ident {
    55  .  .  .  .  .  .  .  .  .  NamePos: returned-values.go:5:24
    56  .  .  .  .  .  .  .  .  .  Name: "float64"
    57  .  .  .  .  .  .  .  .  }
    58  .  .  .  .  .  .  .  }
    59  .  .  .  .  .  .  }
    60  .  .  .  .  .  .  2: *ast.Field {
    61  .  .  .  .  .  .  .  Type: *ast.ArrayType {
    62  .  .  .  .  .  .  .  .  Lbrack: returned-values.go:5:33
    63  .  .  .  .  .  .  .  .  Elt: *ast.Ident {
    64  .  .  .  .  .  .  .  .  .  NamePos: returned-values.go:5:35
    65  .  .  .  .  .  .  .  .  .  Name: "float64"
    66  .  .  .  .  .  .  .  .  }
    67  .  .  .  .  .  .  .  }
    68  .  .  .  .  .  .  }
    69  .  .  .  .  .  }
microo8 commented 6 years ago

The results.List must be just one. You are writing functions that are called from sql.

What do you expect to get when calling select E()?

sachaarbonel commented 6 years ago

Well a possible solution could be :

SELECT (E(inputed_floats)).outCA, (E(inputed_floats)).outB, (E(inputed_floats)).outC FROM some_table;

With my E func being slightly changed for the example :

func E(inA float64, inB float64, inC float64,) (outA []float64, outB []float64, outC []float64) {
    return []float64{inA}, []float64{inB}, []float64{inC}
}

I expect the result to be like that :

    outA     |      outB       |    outC  
-----------+-------------+-----------
{inAvalue} |   {inBvalue} |  {inCvalue}
(1 row)

I guess we'll have to parse the returned values variable names for the composite types. Let me know what do you think of my proposal.