diff --git a/executor.go b/executor.go index ade3b9001..c7b9d884f 100644 --- a/executor.go +++ b/executor.go @@ -1587,6 +1587,34 @@ type DistinctTimestamp struct { Name string } +var _ proto.ToRowser = DistinctTimestamp{} + +// ToRows implements the ToRowser interface. +func (d DistinctTimestamp) ToRows(callback func(*proto.RowResponse) error) error { + for _, ts := range d.Values { + row := &proto.RowResponse{ + Headers: []*proto.ColumnInfo{ + { + Name: d.Name, + Datatype: "timestamp", + }, + }, + Columns: []*proto.ColumnResponse{ + { + ColumnVal: &proto.ColumnResponse_TimestampVal{ + TimestampVal: ts, + }, + }, + }, + } + if err := callback(row); err != nil { + return errors.Wrap(err, "calling callback") + } + } + + return nil +} + // Union returns the union of the values of `d` and `other` func (d *DistinctTimestamp) Union(other DistinctTimestamp) DistinctTimestamp { both := map[string]string{} diff --git a/executor_test.go b/executor_test.go index 069f27b7b..bdcef2ee8 100644 --- a/executor_test.go +++ b/executor_test.go @@ -8546,3 +8546,66 @@ func MinMaxTimestampNodeTester(t *testing.T, numNodes int) { t.Fatalf("incorrect max timestamp val. expected: %v, got %v\n", expected, min) } } + +// DistinctTimestamp ToRows should properly encode the timestamp +func TestDistinctTimestampToRows(t *testing.T) { + d := pilosa.DistinctTimestamp{ + Values: []string{ + "2022-03-24T12:08:37Z", + "2022-03-24T12:08:47Z", + "2022-03-24T12:08:57Z", + }, + Name: "timestamp", + } + + expectedHeaders := []*proto.ColumnInfo{ + { + Name: d.Name, + Datatype: "timestamp", + }, + } + expected := []*proto.RowResponse{ + { + Headers: expectedHeaders, + Columns: []*proto.ColumnResponse{ + { + ColumnVal: &proto.ColumnResponse_TimestampVal{ + TimestampVal: "2022-03-24T12:08:37Z", + }, + }, + }, + }, + { + Headers: expectedHeaders, + Columns: []*proto.ColumnResponse{ + { + ColumnVal: &proto.ColumnResponse_TimestampVal{ + TimestampVal: "2022-03-24T12:08:47Z", + }, + }, + }, + }, + { + Headers: expectedHeaders, + Columns: []*proto.ColumnResponse{ + { + ColumnVal: &proto.ColumnResponse_TimestampVal{ + TimestampVal: "2022-03-24T12:08:57Z", + }, + }, + }, + }, + } + + rows := []*proto.RowResponse{} + d.ToRows(func(r *proto.RowResponse) error { + rows = append(rows, r) + return nil + }) + + for i, row := range rows { + if !reflect.DeepEqual(row, expected[i]) { + t.Errorf("expected %v, got %v", expected[i], row) + } + } +}