mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-10-10 04:47:53 +00:00
Merge pull request #1534 from kuba--/call-agg/335
CORE-335 Consistency with aggregate functions
This commit is contained in:
commit
16245b1742
6 changed files with 1678 additions and 1233 deletions
30
executor.go
30
executor.go
|
|
@ -1125,9 +1125,9 @@ func (e *executor) executeSum(ctx context.Context, qcx *Qcx, index string, c *pq
|
|||
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeSum")
|
||||
defer span.Finish()
|
||||
|
||||
fieldName, ok := c.Args["field"].(string)
|
||||
if !ok || fieldName == "" {
|
||||
return ValCount{}, errors.New("Sum(): field required")
|
||||
fieldName, err := c.FirstStringArg("field", "_field")
|
||||
if err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Sum(): field required")
|
||||
}
|
||||
|
||||
if len(c.Children) > 1 {
|
||||
|
|
@ -1229,8 +1229,9 @@ func (e *executor) executeDistinct(ctx context.Context, qcx *Qcx, index string,
|
|||
func (e *executor) executeMin(ctx context.Context, qcx *Qcx, index string, c *pql.Call, shards []uint64, opt *execOptions) (_ ValCount, err error) {
|
||||
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeMin")
|
||||
defer span.Finish()
|
||||
if field := c.Args["field"]; field == "" {
|
||||
return ValCount{}, errors.New("Min(): field required")
|
||||
|
||||
if _, err := c.FirstStringArg("field", "_field"); err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Min(): field required")
|
||||
}
|
||||
|
||||
if len(c.Children) > 1 {
|
||||
|
|
@ -1265,8 +1266,8 @@ func (e *executor) executeMax(ctx context.Context, qcx *Qcx, index string, c *pq
|
|||
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeMax")
|
||||
defer span.Finish()
|
||||
|
||||
if field := c.Args["field"]; field == "" {
|
||||
return ValCount{}, errors.New("Max(): field required")
|
||||
if _, err := c.FirstStringArg("field", "_field"); err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Max(): field required")
|
||||
}
|
||||
|
||||
if len(c.Children) > 1 {
|
||||
|
|
@ -1889,7 +1890,10 @@ func (e *executor) executeSumCountShard(ctx context.Context, qcx *Qcx, index str
|
|||
filter = row
|
||||
}
|
||||
|
||||
fieldName, _ := c.Args["field"].(string)
|
||||
fieldName, err := c.FirstStringArg("field", "_field")
|
||||
if err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Sum(): field required")
|
||||
}
|
||||
|
||||
field := e.Holder.Field(index, fieldName)
|
||||
if field == nil {
|
||||
|
|
@ -1940,7 +1944,10 @@ func (e *executor) executeMinShard(ctx context.Context, qcx *Qcx, index string,
|
|||
filter = row
|
||||
}
|
||||
|
||||
fieldName, _ := c.Args["field"].(string)
|
||||
fieldName, err := c.FirstStringArg("field", "_field")
|
||||
if err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Min(): field required")
|
||||
}
|
||||
|
||||
field := e.Holder.Field(index, fieldName)
|
||||
if field == nil {
|
||||
|
|
@ -1969,7 +1976,10 @@ func (e *executor) executeMaxShard(ctx context.Context, qcx *Qcx, index string,
|
|||
filter = row
|
||||
}
|
||||
|
||||
fieldName, _ := c.Args["field"].(string)
|
||||
fieldName, err := c.FirstStringArg("field", "_field")
|
||||
if err != nil {
|
||||
return ValCount{}, errors.Wrap(err, "Max(): field required")
|
||||
}
|
||||
|
||||
field := e.Holder.Field(index, fieldName)
|
||||
if field == nil {
|
||||
|
|
|
|||
|
|
@ -1581,6 +1581,42 @@ func TestExecutor_Execute_MinMax(t *testing.T) {
|
|||
t.Fatalf("unexpected max result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Min", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Min(field="%s")`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: test.set, Count: 1}) {
|
||||
t.Fatalf("unexpected min result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Max", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Max(field="%s")`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: test.set, Count: 1}) {
|
||||
t.Fatalf("unexpected max result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Min", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Min(%s)`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: test.set, Count: 1}) {
|
||||
t.Fatalf("unexpected min result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Max", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Max(%s)`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: test.set, Count: 1}) {
|
||||
t.Fatalf("unexpected max result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
@ -1695,6 +1731,24 @@ func TestExecutor_Execute_MinMax(t *testing.T) {
|
|||
t.Fatalf("unexpected max result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Min", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Min(%s)`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{DecimalVal: &test.exp, Count: 1}) {
|
||||
t.Fatalf("unexpected min result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Max", func(t *testing.T) {
|
||||
pql = fmt.Sprintf(`Max(%s)`, fld)
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{DecimalVal: &test.exp, Count: 1}) {
|
||||
t.Fatalf("unexpected max result, test %d: %s", i, spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
@ -2024,6 +2078,22 @@ func TestExecutor_Execute_Sum(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("NoFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(field="foo")`}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: 200, Count: 5}) {
|
||||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(foo)`}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: 200, Count: 5}) {
|
||||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WithFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(Row(x=0), field=foo)`}); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -2031,6 +2101,14 @@ func TestExecutor_Execute_Sum(t *testing.T) {
|
|||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WithFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(foo, Row(x=0))`}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: 80, Count: 2}) {
|
||||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Decimal", func(t *testing.T) {
|
||||
|
|
@ -2049,6 +2127,23 @@ func TestExecutor_Execute_Sum(t *testing.T) {
|
|||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(dec)`}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{DecimalVal: &pql.Decimal{Value: 700007, Scale: 3}, Count: 3}) {
|
||||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WithFilter", func(t *testing.T) {
|
||||
if result, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: `Sum(dec, Row(x=0))`}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{DecimalVal: &pql.Decimal{Value: 500005, Scale: 3}, Count: 2}) {
|
||||
t.Fatalf("unexpected result: %s", spew.Sdump(result))
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
|||
18
pql/ast.go
18
pql/ast.go
|
|
@ -335,7 +335,8 @@ var stringOrInt64 stringOrInt64Type
|
|||
var allowField = callInfo{
|
||||
allowUnknown: false,
|
||||
prototypes: map[string]interface{}{
|
||||
"field": "",
|
||||
"_field": "",
|
||||
"field": "",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -722,6 +723,21 @@ func (c *Call) StringArg(key string) (string, bool, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Call) FirstStringArg(keys ...string) (string, error) {
|
||||
for _, k := range keys {
|
||||
val, ok, err := c.StringArg(k)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("keys: %v not found", keys)
|
||||
}
|
||||
|
||||
// CallArg is for reading the value at key from call.Args as a Call. If the
|
||||
// key is not in Call.Args, the value of the returned value will be nil, and
|
||||
// the error will be nil. An error is returned if the value is not a Call.
|
||||
|
|
|
|||
|
|
@ -14,8 +14,11 @@ Call <- "Set" {p.startCall("Set")} open col comma args (comma timestamp)? close
|
|||
/ "Store" {p.startCall("Store")} open Call comma arg close {p.endCall()}
|
||||
/ "TopN" {p.startCall("TopN")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "TopK" {p.startCall("TopK")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Percentile" {p.startCall("Percentile")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Percentile" {p.startCall("Percentile")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Rows" {p.startCall("Rows")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Min" {p.startCall("Min")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Max" {p.startCall("Max")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Sum" {p.startCall("Sum")} open posfield (comma allargs)? close {p.endCall()}
|
||||
/ "Range" {p.startCall("Range")} open field eq value comma 'from='? {p.addField("from")} timestampfmt {p.addVal(text)} comma 'to='? sp {p.addField("to")} timestampfmt {p.addVal(text)} close {p.endCall()}
|
||||
/ < IDENT > { p.startCall(text) } open allargs comma? close { p.endCall() }
|
||||
allargs <- Call (comma Call)* (comma args)? / args / sp
|
||||
|
|
|
|||
2704
pql/pql.peg.go
2704
pql/pql.peg.go
File diff suppressed because it is too large
Load diff
|
|
@ -710,13 +710,58 @@ func TestPQLDeepEquality(t *testing.T) {
|
|||
},
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Sum",
|
||||
call: "Sum(f)",
|
||||
exp: &Call{
|
||||
Name: "Sum",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Sum",
|
||||
call: "Sum(field=f)",
|
||||
exp: &Call{
|
||||
Name: "Sum",
|
||||
Args: map[string]interface{}{
|
||||
"field": "f",
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Max",
|
||||
call: "Max(f)",
|
||||
exp: &Call{
|
||||
Name: "Max",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Max",
|
||||
call: "Max(field=f)",
|
||||
exp: &Call{
|
||||
Name: "Max",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Min",
|
||||
call: "Min(f)",
|
||||
exp: &Call{
|
||||
Name: "Min",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "Min",
|
||||
call: "Min(field=f)",
|
||||
exp: &Call{
|
||||
Name: "Min",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
}},
|
||||
{
|
||||
|
|
@ -740,6 +785,18 @@ func TestPQLDeepEquality(t *testing.T) {
|
|||
{Name: "Row"},
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "SumChild",
|
||||
call: "Sum(f, Row())",
|
||||
exp: &Call{
|
||||
Name: "Sum",
|
||||
Args: map[string]interface{}{
|
||||
"_field": "f",
|
||||
},
|
||||
Children: []*Call{
|
||||
{Name: "Row"},
|
||||
},
|
||||
}},
|
||||
{
|
||||
name: "MinChild",
|
||||
call: "Min(Row(), field=f)",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue