Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable casting values from a subquery #9665

Merged
merged 1 commit into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 51 additions & 21 deletions query/cast.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,88 @@
package query

func castToFloat(v interface{}) float64 {
import "github.com/influxdata/influxql"

// castToType will coerce the underlying interface type to another
// interface depending on the type.
func castToType(v interface{}, typ influxql.DataType) interface{} {
switch typ {
case influxql.Float:
if val, ok := castToFloat(v); ok {
v = val
}
case influxql.Integer:
if val, ok := castToInteger(v); ok {
v = val
}
case influxql.Unsigned:
if val, ok := castToUnsigned(v); ok {
v = val
}
case influxql.String:
if val, ok := castToString(v); ok {
v = val
}
case influxql.Boolean:
if val, ok := castToBoolean(v); ok {
v = val
}
}
return v
}

func castToFloat(v interface{}) (float64, bool) {
switch v := v.(type) {
case float64:
return v
return v, true
case int64:
return float64(v)
return float64(v), true
case uint64:
return float64(v)
return float64(v), true
default:
return float64(0)
return float64(0), false
}
}

func castToInteger(v interface{}) int64 {
func castToInteger(v interface{}) (int64, bool) {
switch v := v.(type) {
case float64:
return int64(v)
return int64(v), true
case int64:
return v
return v, true
case uint64:
return int64(v)
return int64(v), true
default:
return int64(0)
return int64(0), false
}
}

func castToUnsigned(v interface{}) uint64 {
func castToUnsigned(v interface{}) (uint64, bool) {
switch v := v.(type) {
case float64:
return uint64(v)
return uint64(v), true
case uint64:
return v
return v, true
case int64:
return uint64(v)
return uint64(v), true
default:
return uint64(0)
return uint64(0), false
}
}

func castToString(v interface{}) string {
func castToString(v interface{}) (string, bool) {
switch v := v.(type) {
case string:
return v
return v, true
default:
return ""
return "", false
}
}

func castToBoolean(v interface{}) bool {
func castToBoolean(v interface{}) (bool, bool) {
switch v := v.(type) {
case bool:
return v
return v, true
default:
return false
return false, false
}
}
37 changes: 36 additions & 1 deletion query/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ func newFilterCursor(cur Cursor, filter influxql.Expr) *filterCursor {
for _, name := range influxql.ExprNames(filter) {
for i, col := range cur.Columns() {
if name.Val == col.Val {
fields[name.Val] = FieldMap(i)
fields[name.Val] = FieldMap{
Index: i,
Type: name.Type,
}
break
}
}
Expand Down Expand Up @@ -385,6 +388,38 @@ func (cur *filterCursor) Scan(row *Row) bool {
return false
}

type nullCursor struct {
columns []influxql.VarRef
}

func newNullCursor(fields []*influxql.Field) *nullCursor {
columns := make([]influxql.VarRef, len(fields))
for i, f := range fields {
columns[i].Val = f.Name()
}
return &nullCursor{columns: columns}
}

func (cur *nullCursor) Scan(row *Row) bool {
return false
}

func (cur *nullCursor) Stats() IteratorStats {
return IteratorStats{}
}

func (cur *nullCursor) Err() error {
return nil
}

func (cur *nullCursor) Columns() []influxql.VarRef {
return cur.columns
}

func (cur *nullCursor) Close() error {
return nil
}

// DrainCursor will read and discard all values from a Cursor and return the error
// if one happens.
func DrainCursor(cur Cursor) error {
Expand Down
Loading