From b0f1ee3fce702ee4853516f8f667cee80ce4364e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Wed, 1 Apr 2020 15:19:05 +0200 Subject: [PATCH] . (#225) --- executor.go | 1 + executor_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ pql/ast.go | 5 +++++ 3 files changed, 56 insertions(+) diff --git a/executor.go b/executor.go index 51c308aae..ef200b5fd 100644 --- a/executor.go +++ b/executor.go @@ -1362,6 +1362,7 @@ func (e *executor) executeTopN(ctx context.Context, index string, c *pql.Call, s }, nil } // Only the original caller should refetch the full counts. + // TODO(@kuba--): ...but do we really need `Clone` here? other := c.Clone() ids := Pairs(pairs.Pairs).Keys() diff --git a/executor_test.go b/executor_test.go index 9af5a478f..3a09c678b 100644 --- a/executor_test.go +++ b/executor_test.go @@ -5111,3 +5111,53 @@ func TestExecutor_BareDistinct(t *testing.T) { } } } + +func TestExecutor_Execute_TopNDistinct(t *testing.T) { + data, err := ioutil.ReadFile("testdata/schema.json") + if err != nil { + t.Fatal(err) + } + + c := test.MustRunCluster(t, 1) + + defer c.Close() + api := c[0].API + + schema := &pilosa.Schema{} + if err := json.NewDecoder(bytes.NewReader(data)).Decode(schema); err != nil { + t.Fatal(err) + } + if err := api.ApplySchema(context.TODO(), schema, false); err != nil { + t.Fatal(err) + } + + writeQuery := `Set(100, type=AntidotePoint)Set(100, equip_id=100)Set(100, site_id=100)Set(100, id=100)` + for _, i := range schema.Indexes { + if _, err := api.Query(context.TODO(), &pilosa.QueryRequest{Index: i.Name, Query: writeQuery}); err != nil { + t.Fatal(err) + } + } + + pql := `TopN(type, Distinct(Row(type=AntidotePoint), index=power_ts, field=equip_id))` + + // Check if test query gives correct results (one column 100) + t.Run("TopN", func(t *testing.T) { + resp, err := api.Query(context.TODO(), &pilosa.QueryRequest{ + Index: "equipment", + Query: pql, + }) + if err != nil { + t.Fatal(err) + } + pf, ok := resp.Results[0].(*pilosa.PairsField) + if !ok { + t.Fatalf("invalid response type, expected: *pilosa.PairsField, got: %T", resp.Results[0]) + } + if len(pf.Pairs) != 1 { + t.Fatalf("invalid Pairs length, expected: 1, got: %v", len(pf.Pairs)) + } + if pf.Pairs[0].Count != 1 { + t.Fatalf("invalid Pairs count, expected: 1, got: %v", pf.Pairs[0].Count) + } + }) +} diff --git a/pql/ast.go b/pql/ast.go index 1b39d1229..2e8d31880 100644 --- a/pql/ast.go +++ b/pql/ast.go @@ -690,6 +690,11 @@ func (c *Call) Clone() *Call { other.Children[i] = c.Children[i].Clone() } } + // @seebs "...it should be safe, + // because nothing should be writing to Precomputed + // once it's gotten created in the first place." + other.Precomputed = c.Precomputed + return other }