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

Reconstruct query strings from the ast #244

Merged
merged 4 commits into from
Apr 16, 2014
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
6 changes: 6 additions & 0 deletions src/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"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
Expand Down
2 changes: 1 addition & 1 deletion src/coordinator/raft_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func (s *RaftServer) runContinuousQuery(db string, query *parser.SelectQuery, st
clusterAdmin := s.clusterConfig.GetClusterAdmin(adminName)
intoClause := query.GetIntoClause()
targetName := intoClause.Target.Name
queryString := query.GetQueryStringForContinuousQuery(start, end)
queryString := query.GetQueryStringWithTimesAndNoIntoClause(start, end)

f := func(series *protocol.Series) error {
return s.coordinator.InterpolateValuesAndCommit(query.GetQueryString(), db, series, targetName, true)
Expand Down
65 changes: 65 additions & 0 deletions src/parser/from_clause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package parser

// #include "query_types.h"
// #include <stdlib.h>
import "C"
import (
"bytes"
"strings"
)
import "fmt"

type FromClauseType int

const (
FromClauseArray FromClauseType = C.FROM_ARRAY
FromClauseMerge FromClauseType = C.FROM_MERGE
FromClauseInnerJoin FromClauseType = C.FROM_INNER_JOIN
)

func (self *TableName) GetAlias() string {
if self.Alias != "" {
return self.Alias
}
return self.Name.Name
}

func (self *TableName) GetAliasString() string {
if self.Alias != "" {
return fmt.Sprintf(" as %s", self.Alias)
}
return ""
}

type TableName struct {
Name *Value
Alias string
}

type FromClause struct {
Type FromClauseType
Names []*TableName
}

func (self *FromClause) GetString() string {
buffer := bytes.NewBufferString("")
switch self.Type {
case FromClauseMerge:
fmt.Fprintf(buffer, "%s%s merge %s %s", self.Names[0].Name.GetString(), self.Names[1].GetAliasString(),
self.Names[1].Name.GetString(), self.Names[1].GetAliasString())
case FromClauseInnerJoin:
fmt.Fprintf(buffer, "%s%s inner join %s%s", self.Names[0].Name.GetString(), self.Names[0].GetAliasString(),
self.Names[1].Name.GetString(), self.Names[1].GetAliasString())
default:
names := make([]string, 0, len(self.Names))
for _, t := range self.Names {
alias := ""
if t.Alias != "" {
alias = fmt.Sprintf(" as %s", t.Alias)
}
names = append(names, fmt.Sprintf("%s%s", t.Name.GetString(), alias))
}
buffer.WriteString(strings.Join(names, ","))
}
return buffer.String()
}
46 changes: 46 additions & 0 deletions src/parser/group_by.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package parser

import (
"bytes"
"common"
"fmt"
"time"
)

type GroupByClause struct {
FillWithZero bool
FillValue *Value
Elems []*Value
}

func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
for _, groupBy := range self.Elems {
if groupBy.IsFunctionCall() {
// TODO: check the number of arguments and return an error
if len(groupBy.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
}
// TODO: check the function name
// TODO: error checking
arg := groupBy.Elems[0].Name
durationInt, err := common.ParseTimeDuration(arg)
if err != nil {
return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
}
duration := time.Duration(durationInt)
return &duration, nil
}
}
return nil, nil
}

func (self *GroupByClause) GetString() string {
buffer := bytes.NewBufferString("")

buffer.WriteString(Values(self.Elems).GetString())

if self.FillWithZero {
fmt.Fprintf(buffer, " fill(%s)", self.FillValue.GetString())
}
return buffer.String()
}
Loading