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

Refactoring influxql package to enable golint #5366

Merged
merged 1 commit into from
Feb 18, 2016
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

- [#5541](https://github.com/influxdata/influxdb/pull/5541): Client: Support for adding custom TLS Config for HTTP client.
- [#4299](https://github.com/influxdata/influxdb/pull/4299): Client: Reject uint64 Client.Point.Field values. Thanks @arussellsaw
- [#5550](https://github.com/influxdata/influxdb/pull/5550): Enabled golint for tsdb/engine/wal.
- [#5550](https://github.com/influxdata/influxdb/pull/5550): Enabled golint for tsdb/engine/wal. @gabelev
- [#5419](https://github.com/influxdata/influxdb/pull/5419): Graphite: Support matching tags multiple times Thanks @m4ce
- [#5598](https://github.com/influxdata/influxdb/pull/5598): Client: Add Ping to v2 client @PSUdaemon
- [#4125](https://github.com/influxdata/influxdb/pull/4125): Admin UI: Fetch and display server version on connect. Thanks @alexiri!
- [#5602](https://github.com/influxdata/influxdb/pull/5602): Simplify cluster startup for scripting and deployment
- [#5562](https://github.com/influxdata/influxdb/pull/5562): Graphite: Support matching fields multiple times (@chrusty)
- [#5666](https://github.com/influxdata/influxdb/pull/5666): Manage dependencies with gdm
- [#5512](https://github.com/influxdata/influxdb/pull/5512): HTTP: Add config option to enable HTTP JSON write path which is now disabled by default.
- [#5336](https://github.com/influxdata/influxdb/pull/5366): Enabled golint for influxql. @gabelev

### Bugfixes

Expand Down
18 changes: 11 additions & 7 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ func (s *AlterRetentionPolicyStatement) RequiredPrivileges() ExecutionPrivileges
return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}
}

// FillOption represents different options for aggregate windows.
type FillOption int

const (
Expand Down Expand Up @@ -1406,6 +1407,7 @@ func (s *SelectStatement) validateAggregates(tr targetRequirement) error {
return nil
}

// HasDistinct checks if a select statement contains DISTINCT
func (s *SelectStatement) HasDistinct() bool {
// determine if we have a call named distinct
for _, f := range s.Fields {
Expand Down Expand Up @@ -1443,6 +1445,7 @@ func (s *SelectStatement) validateDistinct() error {
return nil
}

// HasCountDistinct checks if a select statement contains COUNT and DISTINCT
func (s *SelectStatement) HasCountDistinct() bool {
for _, f := range s.Fields {
if c, ok := f.Expr.(*Call); ok {
Expand Down Expand Up @@ -1539,7 +1542,7 @@ func (s *SelectStatement) validateDerivative() error {
return nil
}

// GroupByIterval extracts the time interval, if specified.
// GroupByInterval extracts the time interval, if specified.
func (s *SelectStatement) GroupByInterval() (time.Duration, error) {
// return if we've already pulled it out
if s.groupByInterval != 0 {
Expand Down Expand Up @@ -2262,7 +2265,7 @@ func (s *ShowRetentionPoliciesStatement) RequiredPrivileges() ExecutionPrivilege
return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}
}

// ShowStats statement displays statistics for a given module.
// ShowStatsStatement displays statistics for a given module.
type ShowStatsStatement struct {
// Module
Module string
Expand Down Expand Up @@ -2658,9 +2661,9 @@ func (f *Field) String() string {
}

// Sort Interface for Fields
func (f Fields) Len() int { return len(f) }
func (f Fields) Less(i, j int) bool { return f[i].Name() < f[j].Name() }
func (f Fields) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (a Fields) Len() int { return len(a) }
func (a Fields) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
func (a Fields) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// Dimensions represents a list of dimensions.
type Dimensions []*Dimension
Expand Down Expand Up @@ -3094,7 +3097,7 @@ func TimeRange(expr Expr) (min, max time.Time) {
return
}

// TimeRange returns the minimum and maximum times, as epoch nano, specified by
// TimeRangeAsEpochNano returns the minimum and maximum times, as epoch nano, specified by
// and expression. If there is no lower bound, the start of the epoch is returned
// for minimum. If there is no higher bound, now is returned for maximum.
func TimeRangeAsEpochNano(expr Expr) (min, max int64) {
Expand Down Expand Up @@ -3721,11 +3724,12 @@ type Valuer interface {
Value(key string) (interface{}, bool)
}

// nowValuer returns only the value for "now()".
// NowValuer returns only the value for "now()".
type NowValuer struct {
Now time.Time
}

// Value is a method that returns the value and existence flag for a given key.
func (v *NowValuer) Value(key string) (interface{}, bool) {
if key == "now()" {
return v.Now, true
Expand Down
10 changes: 5 additions & 5 deletions influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (p *Parser) parseUInt64() (uint64, error) {
// This function assumes the DURATION token has already been consumed.
func (p *Parser) parseDuration() (time.Duration, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != DURATION_VAL && tok != INF {
if tok != DURATIONVAL && tok != INF {
return 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos)
}

Expand Down Expand Up @@ -1509,7 +1509,7 @@ func (p *Parser) parseCreateDatabaseStatement() (*CreateDatabaseStatement, error
stmt.RetentionPolicyDuration = rpDuration

// Look for "REPLICATION"
var rpReplication int = 1 // default is 1
var rpReplication = 1 // default is 1
if err := p.parseTokens([]Token{REPLICATION}); err != nil {
p.unscan()
} else {
Expand Down Expand Up @@ -2331,7 +2331,7 @@ func (p *Parser) parseUnaryExpr() (Expr, error) {
return &NumberLiteral{Val: v}, nil
case TRUE, FALSE:
return &BooleanLiteral{Val: (tok == TRUE)}, nil
case DURATION_VAL:
case DURATIONVAL:
v, _ := ParseDuration(lit)
return &DurationLiteral{Val: v}, nil
case MUL:
Expand Down Expand Up @@ -2422,7 +2422,7 @@ func (p *Parser) parseResample() (time.Duration, time.Duration, error) {
var interval time.Duration
if p.parseTokenMaybe(EVERY) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != DURATION_VAL {
if tok != DURATIONVAL {
return 0, 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos)
}

Expand All @@ -2436,7 +2436,7 @@ func (p *Parser) parseResample() (time.Duration, time.Duration, error) {
var maxDuration time.Duration
if p.parseTokenMaybe(FOR) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != DURATION_VAL {
if tok != DURATIONVAL {
return 0, 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos)
}

Expand Down
5 changes: 4 additions & 1 deletion influxql/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (t *TagSet) AddFilter(key string, filter Expr) {
t.Filters = append(t.Filters, filter)
}

// Rows represents a list of rows that can be sorted consistently by name/tag.
// Result represents a resultset returned from a single statement.
// Rows represents a list of rows that can be sorted consistently by name/tag.
type Result struct {
// StatementID is just the statement's position in the query. It's used
// to combine statement results if they're being buffered in memory.
Expand Down Expand Up @@ -67,6 +67,8 @@ func (r *Result) UnmarshalJSON(b []byte) error {
return nil
}

// GetProcessor is a Method that returns processor type and index
// based on the type of expression.
func GetProcessor(expr Expr, startIndex int) (Processor, int) {
switch expr := expr.(type) {
case *VarRef:
Expand All @@ -91,6 +93,7 @@ func GetProcessor(expr Expr, startIndex int) (Processor, int) {
panic("unreachable")
}

// Processor is a prcessor type returned by GetProcessor
type Processor func(values []interface{}) interface{}

func newEchoProcessor(index int) Processor {
Expand Down
6 changes: 4 additions & 2 deletions influxql/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (s *Scanner) scanString() (tok Token, pos Pos, lit string) {
return STRING, pos, lit
}

// ScanRegex consumes a token to find escapes
func (s *Scanner) ScanRegex() (tok Token, pos Pos, lit string) {
_, pos = s.r.curr()

Expand Down Expand Up @@ -255,15 +256,15 @@ func (s *Scanner) scanNumber() (tok Token, pos Pos, lit string) {
// If the next rune is a duration unit (u,µ,ms,s) then return a duration token
if ch0, _ := s.r.read(); ch0 == 'u' || ch0 == 'µ' || ch0 == 's' || ch0 == 'h' || ch0 == 'd' || ch0 == 'w' {
_, _ = buf.WriteRune(ch0)
return DURATION_VAL, pos, buf.String()
return DURATIONVAL, pos, buf.String()
} else if ch0 == 'm' {
_, _ = buf.WriteRune(ch0)
if ch1, _ := s.r.read(); ch1 == 's' {
_, _ = buf.WriteRune(ch1)
} else {
s.r.unread()
}
return DURATION_VAL, pos, buf.String()
return DURATIONVAL, pos, buf.String()
}
s.r.unread()
}
Expand Down Expand Up @@ -444,6 +445,7 @@ func (r *reader) curr() (ch rune, pos Pos) {
// eof is a marker code point to signify that the reader can't read any more.
const eof = rune(0)

// ScanDelimited reads a delimited set of runes
func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error) {
// Scan start delimiter.
if ch, _, err := r.ReadRune(); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions influxql/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func TestScanner_Scan(t *testing.T) {
{s: `10.3s`, tok: influxql.NUMBER, lit: `10.3`},

// Durations
{s: `10u`, tok: influxql.DURATION_VAL, lit: `10u`},
{s: `10µ`, tok: influxql.DURATION_VAL, lit: `10µ`},
{s: `10ms`, tok: influxql.DURATION_VAL, lit: `10ms`},
{s: `-1s`, tok: influxql.DURATION_VAL, lit: `-1s`},
{s: `10m`, tok: influxql.DURATION_VAL, lit: `10m`},
{s: `10h`, tok: influxql.DURATION_VAL, lit: `10h`},
{s: `10d`, tok: influxql.DURATION_VAL, lit: `10d`},
{s: `10w`, tok: influxql.DURATION_VAL, lit: `10w`},
{s: `10u`, tok: influxql.DURATIONVAL, lit: `10u`},
{s: `10µ`, tok: influxql.DURATIONVAL, lit: `10µ`},
{s: `10ms`, tok: influxql.DURATIONVAL, lit: `10ms`},
{s: `-1s`, tok: influxql.DURATIONVAL, lit: `-1s`},
{s: `10m`, tok: influxql.DURATIONVAL, lit: `10m`},
{s: `10h`, tok: influxql.DURATIONVAL, lit: `10h`},
{s: `10d`, tok: influxql.DURATIONVAL, lit: `10d`},
{s: `10w`, tok: influxql.DURATIONVAL, lit: `10w`},
{s: `10x`, tok: influxql.NUMBER, lit: `10`}, // non-duration unit

// Keywords
Expand Down
63 changes: 32 additions & 31 deletions influxql/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ import (
// Token is a lexical token of the InfluxQL language.
type Token int

// These are a comprehensive list of InfluxQL language tokens.
const (
// Special tokens
// ILLEGAL Token, EOF, WS are Special InfluxQL tokens.
ILLEGAL Token = iota
EOF
WS

literal_beg
// Literals
IDENT // main
NUMBER // 12345.67
DURATION_VAL // 13h
STRING // "abc"
BADSTRING // "abc
BADESCAPE // \q
TRUE // true
FALSE // false
REGEX // Regular expressions
BADREGEX // `.*
literal_end
literalBeg
// IDENT and the following are InfluxQL literal tokens.
IDENT // main
NUMBER // 12345.67
DURATIONVAL // 13h
STRING // "abc"
BADSTRING // "abc
BADESCAPE // \q
TRUE // true
FALSE // false
REGEX // Regular expressions
BADREGEX // `.*
literalEnd

operator_beg
// Operators
operatorBeg
// ADD and the following are InfluxQL Operators
ADD // +
SUB // -
MUL // *
Expand All @@ -45,7 +46,7 @@ const (
LTE // <=
GT // >
GTE // >=
operator_end
operatorEnd

LPAREN // (
RPAREN // )
Expand All @@ -54,8 +55,8 @@ const (
SEMICOLON // ;
DOT // .

keyword_beg
// Keywords
keywordBeg
// ALL and the following are InfluxQL Keywords
ALL
ALTER
ANY
Expand Down Expand Up @@ -137,23 +138,23 @@ const (
WHERE
WITH
WRITE
keyword_end
keywordEnd
)

var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
WS: "WS",

IDENT: "IDENT",
NUMBER: "NUMBER",
DURATION_VAL: "DURATION_VAL",
STRING: "STRING",
BADSTRING: "BADSTRING",
BADESCAPE: "BADESCAPE",
TRUE: "TRUE",
FALSE: "FALSE",
REGEX: "REGEX",
IDENT: "IDENT",
NUMBER: "NUMBER",
DURATIONVAL: "DURATIONVAL",
STRING: "STRING",
BADSTRING: "BADSTRING",
BADESCAPE: "BADESCAPE",
TRUE: "TRUE",
FALSE: "FALSE",
REGEX: "REGEX",

ADD: "+",
SUB: "-",
Expand Down Expand Up @@ -266,7 +267,7 @@ var keywords map[string]Token

func init() {
keywords = make(map[string]Token)
for tok := keyword_beg + 1; tok < keyword_end; tok++ {
for tok := keywordBeg + 1; tok < keywordEnd; tok++ {
keywords[strings.ToLower(tokens[tok])] = tok
}
for _, tok := range []Token{AND, OR} {
Expand Down Expand Up @@ -302,7 +303,7 @@ func (tok Token) Precedence() int {
}

// isOperator returns true for operator tokens.
func (tok Token) isOperator() bool { return tok > operator_beg && tok < operator_end }
func (tok Token) isOperator() bool { return tok > operatorBeg && tok < operatorEnd }

// tokstr returns a literal if provided, otherwise returns the token string.
func tokstr(tok Token, lit string) string {
Expand Down