Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,732 changes: 3,386 additions & 3,346 deletions compiler/parser/parser.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions compiler/parser/parser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ CountOp
return &ast.CountOp{
Kind: "CountOp",
Expr: rec.(*ast.RecordExpr),
Loc: loc(c),
}, nil
}
/ COUNT &EndOfOp {
return &ast.CountOp{
Kind: "CountOp",
Loc: loc(c),
}, nil
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/semantic/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ func (c *checker) op(typ super.Type, op sem.Op) super.Type {
case *sem.BadOp:
return c.unknown
case *sem.CountOp:
elems := []sem.RecordElem{
&sem.FieldElem{Name: op.Alias, Value: &sem.LiteralExpr{Value: "0(uint64)"}},
}
var elems []sem.RecordElem
if op.Expr != nil {
elems = append(elems, op.Expr.(*sem.RecordExpr).Elems...)
}
elems = append(elems, &sem.FieldElem{
Name: op.Alias,
Value: &sem.LiteralExpr{Value: "0(uint64)"},
})
return c.recordElems(typ, elems)
case *sem.CutOp:
return c.pathsToType(c.assignments(typ, op.Args))
Expand Down
47 changes: 31 additions & 16 deletions compiler/semantic/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,23 +652,38 @@ func (t *translator) semOp(o ast.Op, seq sem.Seq) sem.Seq {
Cases: cases,
})
case *ast.CountOp:
if len(o.Expr.Elems) == 0 {
t.error(o.Expr, errors.New("count record expression must not be empty"))
return append(seq, badOp())
}
first, ok := o.Expr.Elems[0].(*ast.ExprElem)
if !ok {
t.error(o.Expr, errors.New("first element in record expression for count must be an identifier"))
return append(seq, badOp())
}
alias := first.Expr.(*ast.IDExpr).Name
var alias string
var expr sem.Expr
if len(o.Expr.Elems) > 1 {
expr = t.expr(&ast.RecordExpr{
Kind: "RecordExpr",
Elems: o.Expr.Elems[1:],
Loc: o.Expr.Loc,
})
if o.Expr == nil {
alias = "count"
expr = &sem.RecordExpr{
Elems: []sem.RecordElem{
&sem.FieldElem{Name: "that", Value: sem.NewThis(nil, nil)},
},
}
} else {
n := len(o.Expr.Elems)
if n == 0 {
t.error(o.Expr, errors.New("count record expression must not be empty"))
return append(seq, badOp())
}
last := o.Expr.Elems[n-1]
if exprElem, ok := last.(*ast.ExprElem); ok {
if id, ok := exprElem.Expr.(*ast.IDExpr); ok {
alias = id.Name
}
}
if alias == "" {
t.error(last, errors.New("last element in record expression for count must be an identifier"))
return append(seq, badOp())
}
if len(o.Expr.Elems) > 1 {
expr = t.expr(&ast.RecordExpr{
Kind: "RecordExpr",
Elems: o.Expr.Elems[:n-1],
Loc: o.Expr.Loc,
})
}
}
return append(seq, &sem.CountOp{
Node: o,
Expand Down
6 changes: 3 additions & 3 deletions compiler/sfmt/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ func (c *canonDAG) op(p dag.Op) {
c.write("combine")
case *dag.CountOp:
c.next()
c.write("count {%s", p.Alias)
c.write("count {")
if p.Expr != nil {
c.write(",")
c.recordElems(p.Expr.(*dag.RecordExpr).Elems)
c.write(",")
}
c.write("}")
c.write("%s}", p.Alias)
case *dag.CutOp:
c.next()
c.write("cut ")
Expand Down
4 changes: 2 additions & 2 deletions compiler/ztests/par-count.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Test ensures that flowgraphs utilizing the count operator
# are not parallelized.
script: |
SUPER_VAM=1 super compile -C -P 2 'from /dev/null | count {row_number,...this} | aggregate sum(x)'
SUPER_VAM=1 super compile -C -P 2 'from /dev/null | count {...this,row_number} | aggregate sum(x)'

outputs:
- name: stdout
data: |
file /dev/null unordered
| count {row_number,...this}
| count {...this,row_number}
| aggregate
sum:=sum(x)
| values sum
Expand Down
2 changes: 1 addition & 1 deletion runtime/sam/op/count/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Op struct {
func New(sctx *super.Context, parent sbuf.Puller, alias string, in expr.Evaluator) (*Op, error) {
o := &Op{parent: parent, alias: alias}
var elems []expr.RecordElem
elems = append(elems, expr.RecordElem{Name: alias, Field: evalfunc(o.evalCount)})
if in != nil {
elems = append(elems, expr.RecordElem{Spread: in})
}
elems = append(elems, expr.RecordElem{Name: alias, Field: evalfunc(o.evalCount)})
var err error
o.expr, err = expr.NewRecordExpr(sctx, elems)
return o, err
Expand Down
2 changes: 1 addition & 1 deletion runtime/vam/op/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type Count struct {
func NewCount(sctx *super.Context, parent vector.Puller, alias string, in expr.Evaluator) *Count {
o := &Count{parent: parent, alias: alias}
var elems []expr.RecordElem
elems = append(elems, expr.RecordElem{Name: alias, Expr: evalfunc(o.evalCount)})
if in != nil {
elems = append(elems, expr.RecordElem{Expr: in})
}
elems = append(elems, expr.RecordElem{Name: alias, Expr: evalfunc(o.evalCount)})
o.expr = expr.NewRecordExpr(sctx, elems)
return o
}
Expand Down
49 changes: 30 additions & 19 deletions runtime/ztests/op/count.yaml
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
script: |
super -s -c 'values {x:1},{x:2} | count'
echo // ===
super -s -c 'values null,null | count {count}'
echo // ===
super -s -c 'values {x:1},{x:2} | count {row_number,...this}'
super -s -c 'values {x:1},{x:2} | count {...this,row_number}'
echo // ===
super -s -c 'values {x:1,y:1},{x:2,y:2} | count {row_number,x}'
super -s -c 'values {x:1,y:1},{x:2,y:2} | count {x,row_number}'
echo // ===
super -s -c 'values [1,2],[3,4] | unnest this into ( count {c,val:this} )'
! super -s -c 'values null | count {count} | count := count+"hello"'
super -s -c 'values [1,2],[3,4] | unnest this into ( count {val:this,c} )'
! super -s -c 'values null | count | count := count+"hello"'
>&2 echo // ===
! super -s -c 'values {x:"foo"} | count {row,...this} | x := x+1'
! super -s -c 'values {x:"foo"} | count {...this,row} | x := x+1'
>&2 echo // ===
! super -s -c 'values {x:"foo"} | count {...this}'
>&2 echo // ===
! super -s -c 'count {foo:"foo",1}'

vector: true

outputs:
- name: stdout
data: |
{that:{x:1},count:1::uint64}
{that:{x:2},count:2::uint64}
// ===
{count:1::uint64}
{count:2::uint64}
// ===
{row_number:1::uint64,x:1}
{row_number:2::uint64,x:2}
{x:1,row_number:1::uint64}
{x:2,row_number:2::uint64}
// ===
{row_number:1::uint64,x:1}
{row_number:2::uint64,x:2}
{x:1,row_number:1::uint64}
{x:2,row_number:2::uint64}
// ===
{c:1::uint64,val:1}
{c:2::uint64,val:2}
{c:1::uint64,val:3}
{c:2::uint64,val:4}
{val:1,c:1::uint64}
{val:2,c:2::uint64}
{val:3,c:1::uint64}
{val:4,c:2::uint64}
- name: stderr
data: |
type mismatch at line 1, column 40:
values null | count {count} | count := count+"hello"
~~~~~~~~~~~~~
type mismatch at line 1, column 32:
values null | count | count := count+"hello"
~~~~~~~~~~~~~
// ===
type mismatch at line 1, column 47:
values {x:"foo"} | count {row,...this} | x := x+1
values {x:"foo"} | count {...this,row} | x := x+1
~~~
// ===
first element in record expression for count must be an identifier at line 1, column 26:
last element in record expression for count must be an identifier at line 1, column 27:
values {x:"foo"} | count {...this}
~~~~~~~~~
~~~~~~~
// ===
last element in record expression for count must be an identifier at line 1, column 18:
count {foo:"foo",1}
~