-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_unnest.go
43 lines (36 loc) · 1.09 KB
/
expr_unnest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package rel
import (
"context"
"fmt"
"github.com/arr-ai/wbnf/parser"
"github.com/go-errors/errors"
)
// UnnestExpr returns the relation with names grouped into a nested relation.
type UnnestExpr struct {
ExprScanner
lhs Expr
attr string
}
// NewUnnestExpr returns a new UnnestExpr.
func NewUnnestExpr(scanner parser.Scanner, lhs Expr, attr string) Expr {
return &UnnestExpr{ExprScanner{scanner}, lhs, attr}
}
// AttrToUnnest returns the attr name to unnest.
func (e *UnnestExpr) AttrToUnnest() string {
return e.attr
}
// String returns a string representation of the expression.
func (e *UnnestExpr) String() string {
return fmt.Sprintf("(%s unnest %s)", e.lhs, e.attr)
}
// Eval returns e.lhs with e.attrs grouped under e.attr.
func (e *UnnestExpr) Eval(ctx context.Context, local Scope) (Value, error) {
value, err := e.lhs.Eval(ctx, local)
if err != nil {
return nil, WrapContextErr(err, e, local)
}
if set, ok := value.(Set); ok {
return Unnest(set, e.attr)
}
return nil, WrapContextErr(errors.Errorf("unnest lhs must be relation, not %s", ValueTypeAsString(value)), e, local)
}