Skip to content

Commit

Permalink
Fix #575. Selecing single point queries don't interpret timestamp cor…
Browse files Browse the repository at this point in the history
…rectly
  • Loading branch information
jvshahid authored and pauldix committed May 27, 2014
1 parent 441966b commit 99d8165
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
57 changes: 31 additions & 26 deletions src/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,59 @@ package common
import (
"encoding/json"
"fmt"
"math/big"
"os"
"protocol"
"strconv"
"strings"

"time"
"unicode"
)

// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd' and 'w' suffixes.
func ParseTimeDuration(value string) (int64, error) {
// shortcut for nanoseconds
if idx := strings.IndexFunc(value, func(r rune) bool { return !unicode.IsNumber(r) }); idx == -1 {
return strconv.ParseInt(value, 10, 64)
}

parsedFloat, err := strconv.ParseFloat(value[:len(value)-1], 64)
if err != nil {
return 0, err
}
var constant time.Duration
hasPrefix := true

switch value[len(value)-1] {
case 'u':
return int64(parsedFloat * float64(time.Microsecond)), nil
constant = time.Microsecond
case 's':
return int64(parsedFloat * float64(time.Second)), nil
constant = time.Second
case 'm':
return int64(parsedFloat * float64(time.Minute)), nil
constant = time.Minute
case 'h':
return int64(parsedFloat * float64(time.Hour)), nil
constant = time.Hour
case 'd':
return int64(parsedFloat * 24 * float64(time.Hour)), nil
constant = 24 * time.Hour
case 'w':
return int64(parsedFloat * 7 * 24 * float64(time.Hour)), nil
constant = 7 * 24 * time.Hour
case 'y':
return int64(parsedFloat * 365 * 24 * float64(time.Hour)), nil
constant = 365 * 24 * time.Hour
default:
hasPrefix = false
}

lastChar := value[len(value)-1]
if !unicode.IsDigit(rune(lastChar)) && lastChar != '.' {
return 0, fmt.Errorf("Invalid character '%c'", lastChar)
t := big.Rat{}
timeString := value
if hasPrefix {
timeString = value[:len(value)-1]
}

if value[len(value)-2] != '.' {
extraDigit := float64(lastChar - '0')
parsedFloat = parsedFloat*10 + extraDigit
_, err := fmt.Sscan(timeString, &t)
if err != nil {
return 0, err
}

if hasPrefix {
c := big.Rat{}
c.SetFrac64(int64(constant), 1)
t.Mul(&t, &c)
}
if t.IsInt() {
return t.Num().Int64(), nil
}
return int64(parsedFloat), nil
f, _ := t.Float64()
return int64(f), nil
}

func GetFileSize(path string) (int64, error) {
Expand Down
4 changes: 2 additions & 2 deletions src/integration/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ func (self *DataTestSuite) SinglePointSelect(c *C) (Fun, Fun) {
c.Assert(data[0].Points, HasLen, 2)

for _, point := range data[0].Points {
query := fmt.Sprintf("select * from test_single_points where time = %.0f and sequence_number = %0.f;", point[0].(float64), point[1])
query := fmt.Sprintf("select * from test_single_points where time = %.0fu and sequence_number = %0.f;", point[0].(float64), point[1])
data := client.RunQuery(query, c, "u")
c.Assert(data, HasLen, 1)
c.Assert(data[0].Points, HasLen, 1)
Expand All @@ -1319,7 +1319,7 @@ func (self *DataTestSuite) SinglePointSelectWithNullValues(c *C) (Fun, Fun) {
c.Assert(data[0].Points, HasLen, 1)

for _, point := range data[0].Points {
query := fmt.Sprintf("select * from test_single_points_with_nulls where time = %.0f and sequence_number = %0.f;", point[0].(float64), point[1])
query := fmt.Sprintf("select * from test_single_points_with_nulls where time = %.0fu and sequence_number = %0.f;", point[0].(float64), point[1])
data := client.RunQuery(query, c, "u")
c.Assert(data, HasLen, 1)
c.Assert(data[0].Points, HasLen, 1)
Expand Down
3 changes: 1 addition & 2 deletions src/parser/query_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ func getTime(condition *WhereCondition, isParsingStartTime bool) (*WhereConditio
return condition, nil, nil
}
case "=":
microseconds, err := parseTime(timeExpression)
nanoseconds := microseconds * 1000
nanoseconds, err := parseTime(timeExpression)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 99d8165

Please sign in to comment.