// Begin will set up the mapper to run the map function for a given aggregate call starting at the passed in time func (l *LocalMapper) Begin(c *influxql.Call, startingTime int64) error { // set up the buffers. These ensure that we return data in time order mapFunc, err := influxql.InitializeMapFunc(c) if err != nil { return err } l.mapFunc = mapFunc l.keyBuffer = make([]int64, len(l.cursors)) l.valueBuffer = make([][]byte, len(l.cursors)) l.tmin = startingTime // determine if this is a raw data query with a single field, multiple fields, or an aggregate var fieldName string if c == nil { // its a raw data query l.isRaw = true if len(l.selectFields) == 1 { fieldName = l.selectFields[0].Name } } else { lit, ok := c.Args[0].(*influxql.VarRef) if !ok { return fmt.Errorf("aggregate call didn't contain a field %s", c.String()) } fieldName = lit.Val } // set up the field info if a specific field was set for this mapper if fieldName != "" { f := l.decoder.FieldByName(fieldName) if f == nil { return fmt.Errorf("%s isn't a field on measurement %s", fieldName, l.job.MeasurementName) } l.fieldID = f.ID l.fieldName = f.Name } // seek the bolt cursors and fill the buffers for i, c := range l.cursors { // this series may have never been written in this shard group (time range) so the cursor would be nil if c == nil { l.keyBuffer[i] = 0 l.valueBuffer[i] = nil continue } k, v := c.Seek(u64tob(uint64(l.job.TMin))) if k == nil { l.keyBuffer[i] = 0 l.valueBuffer[i] = nil continue } l.cursorsEmpty = false t := int64(btou64(k)) l.keyBuffer[i] = t l.valueBuffer[i] = v } return nil }
// Begin will set up the mapper to run the map function for a given aggregate call starting at the passed in time func (l *LocalMapper) Begin(c *influxql.Call, startingTime int64, chunkSize int) error { // set up the buffers. These ensure that we return data in time order mapFunc, err := influxql.InitializeMapFunc(c) if err != nil { return err } l.mapFunc = mapFunc l.keyBuffer = make([]int64, len(l.cursors)) l.valueBuffer = make([][]byte, len(l.cursors)) l.chunkSize = chunkSize l.tmin = startingTime var isCountDistinct bool // determine if this is a raw data query with a single field, multiple fields, or an aggregate var fieldName string if c == nil { // its a raw data query l.isRaw = true if len(l.selectFields) == 1 { fieldName = l.selectFields[0] } // if they haven't set a limit, just set it to the max int size if l.limit == 0 { l.limit = math.MaxUint64 } } else { // Check for calls like `derivative(mean(value), 1d)` var nested *influxql.Call = c if fn, ok := c.Args[0].(*influxql.Call); ok { nested = fn } switch lit := nested.Args[0].(type) { case *influxql.VarRef: fieldName = lit.Val case *influxql.Distinct: if c.Name != "count" { return fmt.Errorf("aggregate call didn't contain a field %s", c.String()) } isCountDistinct = true fieldName = lit.Val default: return fmt.Errorf("aggregate call didn't contain a field %s", c.String()) } isCountDistinct = isCountDistinct || (c.Name == "count" && nested.Name == "distinct") } // set up the field info if a specific field was set for this mapper if fieldName != "" { fid, err := l.decoder.FieldIDByName(fieldName) if err != nil { switch { case c != nil && c.Name == "distinct": return fmt.Errorf(`%s isn't a field on measurement %s; to query the unique values for a tag use SHOW TAG VALUES FROM %[2]s WITH KEY = "%[1]s`, fieldName, l.job.MeasurementName) case isCountDistinct: return fmt.Errorf("%s isn't a field on measurement %s; count(distinct) on tags isn't yet supported", fieldName, l.job.MeasurementName) } } l.fieldID = fid l.fieldName = fieldName } // seek the bolt cursors and fill the buffers for i, c := range l.cursors { // this series may have never been written in this shard group (time range) so the cursor would be nil if c == nil { l.keyBuffer[i] = 0 l.valueBuffer[i] = nil continue } k, v := c.Seek(u64tob(uint64(l.job.TMin))) if k == nil { l.keyBuffer[i] = 0 l.valueBuffer[i] = nil continue } l.cursorsEmpty = false t := int64(btou64(k)) l.keyBuffer[i] = t l.valueBuffer[i] = v } return nil }