Skip to content

Commit

Permalink
Merge pull request #268 from dakimura/feature/select-columns
Browse files Browse the repository at this point in the history
select columns feature
  • Loading branch information
umitanuki authored Jan 27, 2020
2 parents 2d8c457 + 9902a56 commit 7537537
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion frontend/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type QueryRequest struct {
LimitRecordCount *int `msgpack:"limit_record_count,omitempty"`
// Set to true if LimitRecordCount should be from the lower
LimitFromStart *bool `msgpack:"limit_from_start,omitempty"`
// Array of column names to be returned
Columns []string `msgpack:"columns,omitempty"`

// Support for functions is experimental and subject to change
Functions []string `msgpack:"functions,omitempty"`
Expand Down Expand Up @@ -162,13 +164,18 @@ func (s *DataService) Query(r *http.Request, reqs *MultiQueryRequest, response *
if req.LimitFromStart != nil {
limitFromStart = *req.LimitFromStart
}
columns := make([]string, 0)
if req.Columns != nil {
columns = req.Columns
}

start := io.ToSystemTimezone(time.Unix(epochStart, 0))
stop := io.ToSystemTimezone(time.Unix(epochEnd, 0))
csm, err := executeQuery(
dest,
start, stop,
limitRecordCount, limitFromStart,
columns,
)
if err != nil {
return err
Expand Down Expand Up @@ -241,7 +248,7 @@ Utility functions
*/

func executeQuery(tbk *io.TimeBucketKey, start, end time.Time, LimitRecordCount int,
LimitFromStart bool) (io.ColumnSeriesMap, error) {
LimitFromStart bool, columns []string) (io.ColumnSeriesMap, error) {

query := planner.NewQuery(executor.ThisInstance.CatalogDir)

Expand Down Expand Up @@ -291,6 +298,9 @@ func executeQuery(tbk *io.TimeBucketKey, start, end time.Time, LimitRecordCount
log.Error("Error returned from query scanner: %s\n", err)
return nil, err
}

csm.FilterColumns(columns)

return csm, err
}

Expand Down
21 changes: 21 additions & 0 deletions utils/io/columnseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io

import (
"fmt"
"github.com/alpacahq/marketstore/utils/log"
"reflect"
"sort"
"strconv"
Expand Down Expand Up @@ -423,6 +424,26 @@ func (csm ColumnSeriesMap) ToRowSeriesMap(dataShapesMap map[TimeBucketKey][]Data
return rsMap
}

// FilterColumns removes columns other than the specified columns from all ColumnSeries in a ColumnSeriesMap.
func (csm *ColumnSeriesMap) FilterColumns(columns []string) {
if len(columns) == 0 {
return
}

// index columns (=Epoch and Nanoseconds) are always necessary and Epoch should be the first column
keepColumns := []string{"Epoch"}
keepColumns = append(keepColumns, columns...)
keepColumns = append(keepColumns, "Nanoseconds")

for _, cs := range *csm {
// filter out unnecessary columns
err := cs.Project(keepColumns)
if err != nil {
log.Error("failed to filter out columns", keepColumns)
}
}
}

func GetNamesFromDSV(dataShapes []DataShape) (out []string) {
for _, shape := range dataShapes {
out = append(out, shape.Name)
Expand Down

0 comments on commit 7537537

Please sign in to comment.