rraymondgh commented on PR #42:
URL: https://github.com/apache/spark-connect-go/pull/42#issuecomment-2286048363
This doesn't work and I can't find a way to make it work. (arrow is too
obscure for me to work out)
What I want is an array of structs from a dataframe. Using a work around to
encode lists in records as json, the following allows me to achieve what I
want. Closing.
```
func collectStruct[T interface{}](ctx context.Context, df sql.DataFrame)
([]T, error) {
rows, err := df.Collect(ctx)
if err != nil {
return []T{}, err
}
schema, err := df.Schema(ctx)
if err != nil {
return []T{}, err
}
res := make([]T, len(rows))
for i, row := range rows {
vals, _ := row.Values()
u := map[string]interface{}{}
a := []interface{}{}
for ci, v := range vals {
// embedded lists don't work with spark-connect.
encode as json
err = json.Unmarshal([]byte(fmt.Sprint(v)), &a)
if err == nil {
v = a
}
u[schema.Fields[ci].Name] = v
}
dbByte, _ := json.Marshal(u)
_ = json.Unmarshal(dbByte, &res[i])
}
return res, nil
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]