github.com/influxdata/influxql@v1.1.0/ast.go (about)

     1  package influxql
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"math"
     8  	"regexp"
     9  	"regexp/syntax"
    10  	"sort"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/gogo/protobuf/proto"
    16  	internal "github.com/influxdata/influxql/internal"
    17  )
    18  
    19  // DataType represents the primitive data types available in InfluxQL.
    20  type DataType int
    21  
    22  const (
    23  	// Unknown primitive data type.
    24  	Unknown DataType = 0
    25  	// Float means the data type is a float.
    26  	Float DataType = 1
    27  	// Integer means the data type is an integer.
    28  	Integer DataType = 2
    29  	// String means the data type is a string of text.
    30  	String DataType = 3
    31  	// Boolean means the data type is a boolean.
    32  	Boolean DataType = 4
    33  	// Time means the data type is a time.
    34  	Time DataType = 5
    35  	// Duration means the data type is a duration of time.
    36  	Duration DataType = 6
    37  	// Tag means the data type is a tag.
    38  	Tag DataType = 7
    39  	// AnyField means the data type is any field.
    40  	AnyField DataType = 8
    41  	// Unsigned means the data type is an unsigned integer.
    42  	Unsigned DataType = 9
    43  )
    44  
    45  const (
    46  	// MinTime is the minumum time that can be represented.
    47  	//
    48  	// 1677-09-21 00:12:43.145224194 +0000 UTC
    49  	//
    50  	// The two lowest minimum integers are used as sentinel values.  The
    51  	// minimum value needs to be used as a value lower than any other value for
    52  	// comparisons and another separate value is needed to act as a sentinel
    53  	// default value that is unusable by the user, but usable internally.
    54  	// Because these two values need to be used for a special purpose, we do
    55  	// not allow users to write points at these two times.
    56  	MinTime = int64(math.MinInt64) + 2
    57  
    58  	// MaxTime is the maximum time that can be represented.
    59  	//
    60  	// 2262-04-11 23:47:16.854775806 +0000 UTC
    61  	//
    62  	// The highest time represented by a nanosecond needs to be used for an
    63  	// exclusive range in the shard group, so the maximum time needs to be one
    64  	// less than the possible maximum number of nanoseconds representable by an
    65  	// int64 so that we don't lose a point at that one time.
    66  	MaxTime = int64(math.MaxInt64) - 1
    67  )
    68  
    69  var (
    70  	// ErrInvalidTime is returned when the timestamp string used to
    71  	// compare against time field is invalid.
    72  	ErrInvalidTime = errors.New("invalid timestamp string")
    73  )
    74  
    75  // InspectDataType returns the data type of a given value.
    76  func InspectDataType(v interface{}) DataType {
    77  	switch v.(type) {
    78  	case float64:
    79  		return Float
    80  	case int64, int32, int:
    81  		return Integer
    82  	case string:
    83  		return String
    84  	case bool:
    85  		return Boolean
    86  	case uint64:
    87  		return Unsigned
    88  	case time.Time:
    89  		return Time
    90  	case time.Duration:
    91  		return Duration
    92  	default:
    93  		return Unknown
    94  	}
    95  }
    96  
    97  // DataTypeFromString returns a data type given the string representation of that
    98  // data type.
    99  func DataTypeFromString(s string) DataType {
   100  	switch s {
   101  	case "float":
   102  		return Float
   103  	case "integer":
   104  		return Integer
   105  	case "unsigned":
   106  		return Unsigned
   107  	case "string":
   108  		return String
   109  	case "boolean":
   110  		return Boolean
   111  	case "time":
   112  		return Time
   113  	case "duration":
   114  		return Duration
   115  	case "tag":
   116  		return Tag
   117  	case "field":
   118  		return AnyField
   119  	default:
   120  		return Unknown
   121  	}
   122  }
   123  
   124  // LessThan returns true if the other DataType has greater precedence than the
   125  // current data type. Unknown has the lowest precedence.
   126  //
   127  // NOTE: This is not the same as using the `<` or `>` operator because the
   128  // integers used decrease with higher precedence, but Unknown is the lowest
   129  // precedence at the zero value.
   130  func (d DataType) LessThan(other DataType) bool {
   131  	if d == Unknown {
   132  		return true
   133  	} else if d == Unsigned {
   134  		return other != Unknown && other <= Integer
   135  	} else if other == Unsigned {
   136  		return d >= String
   137  	}
   138  	return other != Unknown && other < d
   139  }
   140  
   141  var (
   142  	zeroFloat64  interface{} = float64(0)
   143  	zeroInt64    interface{} = int64(0)
   144  	zeroUint64   interface{} = uint64(0)
   145  	zeroString   interface{} = ""
   146  	zeroBoolean  interface{} = false
   147  	zeroTime     interface{} = time.Time{}
   148  	zeroDuration interface{} = time.Duration(0)
   149  )
   150  
   151  // Zero returns the zero value for the DataType.
   152  // The return value of this method, when sent back to InspectDataType,
   153  // may not produce the same value.
   154  func (d DataType) Zero() interface{} {
   155  	switch d {
   156  	case Float:
   157  		return zeroFloat64
   158  	case Integer:
   159  		return zeroInt64
   160  	case Unsigned:
   161  		return zeroUint64
   162  	case String, Tag:
   163  		return zeroString
   164  	case Boolean:
   165  		return zeroBoolean
   166  	case Time:
   167  		return zeroTime
   168  	case Duration:
   169  		return zeroDuration
   170  	}
   171  	return nil
   172  }
   173  
   174  // String returns the human-readable string representation of the DataType.
   175  func (d DataType) String() string {
   176  	switch d {
   177  	case Float:
   178  		return "float"
   179  	case Integer:
   180  		return "integer"
   181  	case Unsigned:
   182  		return "unsigned"
   183  	case String:
   184  		return "string"
   185  	case Boolean:
   186  		return "boolean"
   187  	case Time:
   188  		return "time"
   189  	case Duration:
   190  		return "duration"
   191  	case Tag:
   192  		return "tag"
   193  	case AnyField:
   194  		return "field"
   195  	}
   196  	return "unknown"
   197  }
   198  
   199  // Node represents a node in the InfluxDB abstract syntax tree.
   200  type Node interface {
   201  	// node is unexported to ensure implementations of Node
   202  	// can only originate in this package.
   203  	node()
   204  	String() string
   205  }
   206  
   207  func (*Query) node()     {}
   208  func (Statements) node() {}
   209  
   210  func (*AlterRetentionPolicyStatement) node()       {}
   211  func (*CreateContinuousQueryStatement) node()      {}
   212  func (*CreateDatabaseStatement) node()             {}
   213  func (*CreateRetentionPolicyStatement) node()      {}
   214  func (*CreateSubscriptionStatement) node()         {}
   215  func (*CreateUserStatement) node()                 {}
   216  func (*Distinct) node()                            {}
   217  func (*DeleteSeriesStatement) node()               {}
   218  func (*DeleteStatement) node()                     {}
   219  func (*DropContinuousQueryStatement) node()        {}
   220  func (*DropDatabaseStatement) node()               {}
   221  func (*DropMeasurementStatement) node()            {}
   222  func (*DropRetentionPolicyStatement) node()        {}
   223  func (*DropSeriesStatement) node()                 {}
   224  func (*DropShardStatement) node()                  {}
   225  func (*DropSubscriptionStatement) node()           {}
   226  func (*DropUserStatement) node()                   {}
   227  func (*ExplainStatement) node()                    {}
   228  func (*GrantStatement) node()                      {}
   229  func (*GrantAdminStatement) node()                 {}
   230  func (*KillQueryStatement) node()                  {}
   231  func (*RevokeStatement) node()                     {}
   232  func (*RevokeAdminStatement) node()                {}
   233  func (*SelectStatement) node()                     {}
   234  func (*SetPasswordUserStatement) node()            {}
   235  func (*ShowContinuousQueriesStatement) node()      {}
   236  func (*ShowGrantsForUserStatement) node()          {}
   237  func (*ShowDatabasesStatement) node()              {}
   238  func (*ShowFieldKeyCardinalityStatement) node()    {}
   239  func (*ShowFieldKeysStatement) node()              {}
   240  func (*ShowRetentionPoliciesStatement) node()      {}
   241  func (*ShowMeasurementCardinalityStatement) node() {}
   242  func (*ShowMeasurementsStatement) node()           {}
   243  func (*ShowQueriesStatement) node()                {}
   244  func (*ShowSeriesStatement) node()                 {}
   245  func (*ShowSeriesCardinalityStatement) node()      {}
   246  func (*ShowShardGroupsStatement) node()            {}
   247  func (*ShowShardsStatement) node()                 {}
   248  func (*ShowStatsStatement) node()                  {}
   249  func (*ShowSubscriptionsStatement) node()          {}
   250  func (*ShowDiagnosticsStatement) node()            {}
   251  func (*ShowTagKeyCardinalityStatement) node()      {}
   252  func (*ShowTagKeysStatement) node()                {}
   253  func (*ShowTagValuesCardinalityStatement) node()   {}
   254  func (*ShowTagValuesStatement) node()              {}
   255  func (*ShowUsersStatement) node()                  {}
   256  
   257  func (*BinaryExpr) node()      {}
   258  func (*BooleanLiteral) node()  {}
   259  func (*BoundParameter) node()  {}
   260  func (*Call) node()            {}
   261  func (*Dimension) node()       {}
   262  func (Dimensions) node()       {}
   263  func (*DurationLiteral) node() {}
   264  func (*IntegerLiteral) node()  {}
   265  func (*UnsignedLiteral) node() {}
   266  func (*Field) node()           {}
   267  func (Fields) node()           {}
   268  func (*Measurement) node()     {}
   269  func (Measurements) node()     {}
   270  func (*NilLiteral) node()      {}
   271  func (*NumberLiteral) node()   {}
   272  func (*ParenExpr) node()       {}
   273  func (*RegexLiteral) node()    {}
   274  func (*ListLiteral) node()     {}
   275  func (*SortField) node()       {}
   276  func (SortFields) node()       {}
   277  func (Sources) node()          {}
   278  func (*StringLiteral) node()   {}
   279  func (*SubQuery) node()        {}
   280  func (*Target) node()          {}
   281  func (*TimeLiteral) node()     {}
   282  func (*VarRef) node()          {}
   283  func (*Wildcard) node()        {}
   284  
   285  // Query represents a collection of ordered statements.
   286  type Query struct {
   287  	Statements Statements
   288  }
   289  
   290  // String returns a string representation of the query.
   291  func (q *Query) String() string { return q.Statements.String() }
   292  
   293  // Statements represents a list of statements.
   294  type Statements []Statement
   295  
   296  // String returns a string representation of the statements.
   297  func (a Statements) String() string {
   298  	var str []string
   299  	for _, stmt := range a {
   300  		str = append(str, stmt.String())
   301  	}
   302  	return strings.Join(str, ";\n")
   303  }
   304  
   305  // Statement represents a single command in InfluxQL.
   306  type Statement interface {
   307  	Node
   308  	// stmt is unexported to ensure implementations of Statement
   309  	// can only originate in this package.
   310  	stmt()
   311  	RequiredPrivileges() (ExecutionPrivileges, error)
   312  }
   313  
   314  // HasDefaultDatabase provides an interface to get the default database from a Statement.
   315  type HasDefaultDatabase interface {
   316  	Node
   317  	// stmt is unexported to ensure implementations of HasDefaultDatabase
   318  	// can only originate in this package.
   319  	stmt()
   320  	DefaultDatabase() string
   321  }
   322  
   323  // ExecutionPrivilege is a privilege required for a user to execute
   324  // a statement on a database or resource.
   325  type ExecutionPrivilege struct {
   326  	// Admin privilege required.
   327  	Admin bool
   328  
   329  	// Name of the database.
   330  	Name string
   331  
   332  	// Database privilege required.
   333  	Privilege Privilege
   334  }
   335  
   336  // ExecutionPrivileges is a list of privileges required to execute a statement.
   337  type ExecutionPrivileges []ExecutionPrivilege
   338  
   339  func (*AlterRetentionPolicyStatement) stmt()       {}
   340  func (*CreateContinuousQueryStatement) stmt()      {}
   341  func (*CreateDatabaseStatement) stmt()             {}
   342  func (*CreateRetentionPolicyStatement) stmt()      {}
   343  func (*CreateSubscriptionStatement) stmt()         {}
   344  func (*CreateUserStatement) stmt()                 {}
   345  func (*DeleteSeriesStatement) stmt()               {}
   346  func (*DeleteStatement) stmt()                     {}
   347  func (*DropContinuousQueryStatement) stmt()        {}
   348  func (*DropDatabaseStatement) stmt()               {}
   349  func (*DropMeasurementStatement) stmt()            {}
   350  func (*DropRetentionPolicyStatement) stmt()        {}
   351  func (*DropSeriesStatement) stmt()                 {}
   352  func (*DropSubscriptionStatement) stmt()           {}
   353  func (*DropUserStatement) stmt()                   {}
   354  func (*ExplainStatement) stmt()                    {}
   355  func (*GrantStatement) stmt()                      {}
   356  func (*GrantAdminStatement) stmt()                 {}
   357  func (*KillQueryStatement) stmt()                  {}
   358  func (*ShowContinuousQueriesStatement) stmt()      {}
   359  func (*ShowGrantsForUserStatement) stmt()          {}
   360  func (*ShowDatabasesStatement) stmt()              {}
   361  func (*ShowFieldKeyCardinalityStatement) stmt()    {}
   362  func (*ShowFieldKeysStatement) stmt()              {}
   363  func (*ShowMeasurementCardinalityStatement) stmt() {}
   364  func (*ShowMeasurementsStatement) stmt()           {}
   365  func (*ShowQueriesStatement) stmt()                {}
   366  func (*ShowRetentionPoliciesStatement) stmt()      {}
   367  func (*ShowSeriesStatement) stmt()                 {}
   368  func (*ShowSeriesCardinalityStatement) stmt()      {}
   369  func (*ShowShardGroupsStatement) stmt()            {}
   370  func (*ShowShardsStatement) stmt()                 {}
   371  func (*ShowStatsStatement) stmt()                  {}
   372  func (*DropShardStatement) stmt()                  {}
   373  func (*ShowSubscriptionsStatement) stmt()          {}
   374  func (*ShowDiagnosticsStatement) stmt()            {}
   375  func (*ShowTagKeyCardinalityStatement) stmt()      {}
   376  func (*ShowTagKeysStatement) stmt()                {}
   377  func (*ShowTagValuesCardinalityStatement) stmt()   {}
   378  func (*ShowTagValuesStatement) stmt()              {}
   379  func (*ShowUsersStatement) stmt()                  {}
   380  func (*RevokeStatement) stmt()                     {}
   381  func (*RevokeAdminStatement) stmt()                {}
   382  func (*SelectStatement) stmt()                     {}
   383  func (*SetPasswordUserStatement) stmt()            {}
   384  
   385  // Expr represents an expression that can be evaluated to a value.
   386  type Expr interface {
   387  	Node
   388  	// expr is unexported to ensure implementations of Expr
   389  	// can only originate in this package.
   390  	expr()
   391  }
   392  
   393  func (*BinaryExpr) expr()      {}
   394  func (*BooleanLiteral) expr()  {}
   395  func (*BoundParameter) expr()  {}
   396  func (*Call) expr()            {}
   397  func (*Distinct) expr()        {}
   398  func (*DurationLiteral) expr() {}
   399  func (*IntegerLiteral) expr()  {}
   400  func (*UnsignedLiteral) expr() {}
   401  func (*NilLiteral) expr()      {}
   402  func (*NumberLiteral) expr()   {}
   403  func (*ParenExpr) expr()       {}
   404  func (*RegexLiteral) expr()    {}
   405  func (*ListLiteral) expr()     {}
   406  func (*StringLiteral) expr()   {}
   407  func (*TimeLiteral) expr()     {}
   408  func (*VarRef) expr()          {}
   409  func (*Wildcard) expr()        {}
   410  
   411  // Literal represents a static literal.
   412  type Literal interface {
   413  	Expr
   414  	// literal is unexported to ensure implementations of Literal
   415  	// can only originate in this package.
   416  	literal()
   417  }
   418  
   419  func (*BooleanLiteral) literal()  {}
   420  func (*BoundParameter) literal()  {}
   421  func (*DurationLiteral) literal() {}
   422  func (*IntegerLiteral) literal()  {}
   423  func (*UnsignedLiteral) literal() {}
   424  func (*NilLiteral) literal()      {}
   425  func (*NumberLiteral) literal()   {}
   426  func (*RegexLiteral) literal()    {}
   427  func (*ListLiteral) literal()     {}
   428  func (*StringLiteral) literal()   {}
   429  func (*TimeLiteral) literal()     {}
   430  
   431  // Source represents a source of data for a statement.
   432  type Source interface {
   433  	Node
   434  	// source is unexported to ensure implementations of Source
   435  	// can only originate in this package.
   436  	source()
   437  }
   438  
   439  func (*Measurement) source() {}
   440  func (*SubQuery) source()    {}
   441  
   442  // Sources represents a list of sources.
   443  type Sources []Source
   444  
   445  // String returns a string representation of a Sources array.
   446  func (a Sources) String() string {
   447  	var buf bytes.Buffer
   448  
   449  	ubound := len(a) - 1
   450  	for i, src := range a {
   451  		_, _ = buf.WriteString(src.String())
   452  		if i < ubound {
   453  			_, _ = buf.WriteString(", ")
   454  		}
   455  	}
   456  
   457  	return buf.String()
   458  }
   459  
   460  // Measurements returns all measurements including ones embedded in subqueries.
   461  func (a Sources) Measurements() []*Measurement {
   462  	mms := make([]*Measurement, 0, len(a))
   463  	for _, src := range a {
   464  		switch src := src.(type) {
   465  		case *Measurement:
   466  			mms = append(mms, src)
   467  		case *SubQuery:
   468  			mms = append(mms, src.Statement.Sources.Measurements()...)
   469  		}
   470  	}
   471  	return mms
   472  }
   473  
   474  // MarshalBinary encodes a list of sources to a binary format.
   475  func (a Sources) MarshalBinary() ([]byte, error) {
   476  	var pb internal.Measurements
   477  	pb.Items = make([]*internal.Measurement, len(a))
   478  	for i, source := range a {
   479  		pb.Items[i] = encodeMeasurement(source.(*Measurement))
   480  	}
   481  	return proto.Marshal(&pb)
   482  }
   483  
   484  // UnmarshalBinary decodes binary data into a list of sources.
   485  func (a *Sources) UnmarshalBinary(buf []byte) error {
   486  	var pb internal.Measurements
   487  	if err := proto.Unmarshal(buf, &pb); err != nil {
   488  		return err
   489  	}
   490  	*a = make(Sources, len(pb.GetItems()))
   491  	for i := range pb.GetItems() {
   492  		mm, err := decodeMeasurement(pb.GetItems()[i])
   493  		if err != nil {
   494  			return err
   495  		}
   496  		(*a)[i] = mm
   497  	}
   498  	return nil
   499  }
   500  
   501  // RequiredPrivileges recursively returns a list of execution privileges required.
   502  func (a Sources) RequiredPrivileges() (ExecutionPrivileges, error) {
   503  	var ep ExecutionPrivileges
   504  	for _, source := range a {
   505  		switch source := source.(type) {
   506  		case *Measurement:
   507  			ep = append(ep, ExecutionPrivilege{
   508  				Name:      source.Database,
   509  				Privilege: ReadPrivilege,
   510  			})
   511  		case *SubQuery:
   512  			privs, err := source.Statement.RequiredPrivileges()
   513  			if err != nil {
   514  				return nil, err
   515  			}
   516  			ep = append(ep, privs...)
   517  		default:
   518  			return nil, fmt.Errorf("invalid source: %s", source)
   519  		}
   520  	}
   521  	return ep, nil
   522  }
   523  
   524  // IsSystemName returns true if name is an internal system name.
   525  func IsSystemName(name string) bool {
   526  	switch name {
   527  	case "_fieldKeys",
   528  		"_measurements",
   529  		"_name",
   530  		"_series",
   531  		"_tagKey",
   532  		"_tagKeys",
   533  		"_tags":
   534  		return true
   535  	default:
   536  		return false
   537  	}
   538  }
   539  
   540  // SortField represents a field to sort results by.
   541  type SortField struct {
   542  	// Name of the field.
   543  	Name string
   544  
   545  	// Sort order.
   546  	Ascending bool
   547  }
   548  
   549  // String returns a string representation of a sort field.
   550  func (field *SortField) String() string {
   551  	var buf bytes.Buffer
   552  	if field.Name != "" {
   553  		_, _ = buf.WriteString(field.Name)
   554  		_, _ = buf.WriteString(" ")
   555  	}
   556  	if field.Ascending {
   557  		_, _ = buf.WriteString("ASC")
   558  	} else {
   559  		_, _ = buf.WriteString("DESC")
   560  	}
   561  	return buf.String()
   562  }
   563  
   564  // SortFields represents an ordered list of ORDER BY fields.
   565  type SortFields []*SortField
   566  
   567  // String returns a string representation of sort fields.
   568  func (a SortFields) String() string {
   569  	fields := make([]string, 0, len(a))
   570  	for _, field := range a {
   571  		fields = append(fields, field.String())
   572  	}
   573  	return strings.Join(fields, ", ")
   574  }
   575  
   576  // CreateDatabaseStatement represents a command for creating a new database.
   577  type CreateDatabaseStatement struct {
   578  	// Name of the database to be created.
   579  	Name string
   580  
   581  	// RetentionPolicyCreate indicates whether the user explicitly wants to create a retention policy.
   582  	RetentionPolicyCreate bool
   583  
   584  	// RetentionPolicyDuration indicates retention duration for the new database.
   585  	RetentionPolicyDuration *time.Duration
   586  
   587  	// RetentionPolicyReplication indicates retention replication for the new database.
   588  	RetentionPolicyReplication *int
   589  
   590  	// RetentionPolicyName indicates retention name for the new database.
   591  	RetentionPolicyName string
   592  
   593  	// RetentionPolicyShardGroupDuration indicates shard group duration for the new database.
   594  	RetentionPolicyShardGroupDuration time.Duration
   595  }
   596  
   597  // String returns a string representation of the create database statement.
   598  func (s *CreateDatabaseStatement) String() string {
   599  	var buf bytes.Buffer
   600  	_, _ = buf.WriteString("CREATE DATABASE ")
   601  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   602  	if s.RetentionPolicyCreate {
   603  		_, _ = buf.WriteString(" WITH")
   604  		if s.RetentionPolicyDuration != nil {
   605  			_, _ = buf.WriteString(" DURATION ")
   606  			_, _ = buf.WriteString(s.RetentionPolicyDuration.String())
   607  		}
   608  		if s.RetentionPolicyReplication != nil {
   609  			_, _ = buf.WriteString(" REPLICATION ")
   610  			_, _ = buf.WriteString(strconv.Itoa(*s.RetentionPolicyReplication))
   611  		}
   612  		if s.RetentionPolicyShardGroupDuration > 0 {
   613  			_, _ = buf.WriteString(" SHARD DURATION ")
   614  			_, _ = buf.WriteString(s.RetentionPolicyShardGroupDuration.String())
   615  		}
   616  		if s.RetentionPolicyName != "" {
   617  			_, _ = buf.WriteString(" NAME ")
   618  			_, _ = buf.WriteString(QuoteIdent(s.RetentionPolicyName))
   619  		}
   620  	}
   621  
   622  	return buf.String()
   623  }
   624  
   625  // RequiredPrivileges returns the privilege required to execute a CreateDatabaseStatement.
   626  func (s *CreateDatabaseStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   627  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   628  }
   629  
   630  // DropDatabaseStatement represents a command to drop a database.
   631  type DropDatabaseStatement struct {
   632  	// Name of the database to be dropped.
   633  	Name string
   634  }
   635  
   636  // String returns a string representation of the drop database statement.
   637  func (s *DropDatabaseStatement) String() string {
   638  	var buf bytes.Buffer
   639  	_, _ = buf.WriteString("DROP DATABASE ")
   640  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   641  	return buf.String()
   642  }
   643  
   644  // RequiredPrivileges returns the privilege required to execute a DropDatabaseStatement.
   645  func (s *DropDatabaseStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   646  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   647  }
   648  
   649  // DropRetentionPolicyStatement represents a command to drop a retention policy from a database.
   650  type DropRetentionPolicyStatement struct {
   651  	// Name of the policy to drop.
   652  	Name string
   653  
   654  	// Name of the database to drop the policy from.
   655  	Database string
   656  }
   657  
   658  // String returns a string representation of the drop retention policy statement.
   659  func (s *DropRetentionPolicyStatement) String() string {
   660  	var buf bytes.Buffer
   661  	_, _ = buf.WriteString("DROP RETENTION POLICY ")
   662  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   663  	_, _ = buf.WriteString(" ON ")
   664  	_, _ = buf.WriteString(QuoteIdent(s.Database))
   665  	return buf.String()
   666  }
   667  
   668  // RequiredPrivileges returns the privilege required to execute a DropRetentionPolicyStatement.
   669  func (s *DropRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   670  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: WritePrivilege}}, nil
   671  }
   672  
   673  // DefaultDatabase returns the default database from the statement.
   674  func (s *DropRetentionPolicyStatement) DefaultDatabase() string {
   675  	return s.Database
   676  }
   677  
   678  // CreateUserStatement represents a command for creating a new user.
   679  type CreateUserStatement struct {
   680  	// Name of the user to be created.
   681  	Name string
   682  
   683  	// User's password.
   684  	Password string
   685  
   686  	// User's admin privilege.
   687  	Admin bool
   688  }
   689  
   690  // String returns a string representation of the create user statement.
   691  func (s *CreateUserStatement) String() string {
   692  	var buf bytes.Buffer
   693  	_, _ = buf.WriteString("CREATE USER ")
   694  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   695  	_, _ = buf.WriteString(" WITH PASSWORD ")
   696  	_, _ = buf.WriteString("[REDACTED]")
   697  	if s.Admin {
   698  		_, _ = buf.WriteString(" WITH ALL PRIVILEGES")
   699  	}
   700  	return buf.String()
   701  }
   702  
   703  // RequiredPrivileges returns the privilege(s) required to execute a CreateUserStatement.
   704  func (s *CreateUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   705  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   706  }
   707  
   708  // DropUserStatement represents a command for dropping a user.
   709  type DropUserStatement struct {
   710  	// Name of the user to drop.
   711  	Name string
   712  }
   713  
   714  // String returns a string representation of the drop user statement.
   715  func (s *DropUserStatement) String() string {
   716  	var buf bytes.Buffer
   717  	_, _ = buf.WriteString("DROP USER ")
   718  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   719  	return buf.String()
   720  }
   721  
   722  // RequiredPrivileges returns the privilege(s) required to execute a DropUserStatement.
   723  func (s *DropUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   724  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   725  }
   726  
   727  // Privilege is a type of action a user can be granted the right to use.
   728  type Privilege int
   729  
   730  const (
   731  	// NoPrivileges means no privileges required / granted / revoked.
   732  	NoPrivileges Privilege = iota
   733  	// ReadPrivilege means read privilege required / granted / revoked.
   734  	ReadPrivilege
   735  	// WritePrivilege means write privilege required / granted / revoked.
   736  	WritePrivilege
   737  	// AllPrivileges means all privileges required / granted / revoked.
   738  	AllPrivileges
   739  )
   740  
   741  // NewPrivilege returns an initialized *Privilege.
   742  func NewPrivilege(p Privilege) *Privilege { return &p }
   743  
   744  // String returns a string representation of a Privilege.
   745  func (p Privilege) String() string {
   746  	switch p {
   747  	case NoPrivileges:
   748  		return "NO PRIVILEGES"
   749  	case ReadPrivilege:
   750  		return "READ"
   751  	case WritePrivilege:
   752  		return "WRITE"
   753  	case AllPrivileges:
   754  		return "ALL PRIVILEGES"
   755  	}
   756  	return ""
   757  }
   758  
   759  // GrantStatement represents a command for granting a privilege.
   760  type GrantStatement struct {
   761  	// The privilege to be granted.
   762  	Privilege Privilege
   763  
   764  	// Database to grant the privilege to.
   765  	On string
   766  
   767  	// Who to grant the privilege to.
   768  	User string
   769  }
   770  
   771  // String returns a string representation of the grant statement.
   772  func (s *GrantStatement) String() string {
   773  	var buf bytes.Buffer
   774  	_, _ = buf.WriteString("GRANT ")
   775  	_, _ = buf.WriteString(s.Privilege.String())
   776  	_, _ = buf.WriteString(" ON ")
   777  	_, _ = buf.WriteString(QuoteIdent(s.On))
   778  	_, _ = buf.WriteString(" TO ")
   779  	_, _ = buf.WriteString(QuoteIdent(s.User))
   780  	return buf.String()
   781  }
   782  
   783  // RequiredPrivileges returns the privilege required to execute a GrantStatement.
   784  func (s *GrantStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   785  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   786  }
   787  
   788  // DefaultDatabase returns the default database from the statement.
   789  func (s *GrantStatement) DefaultDatabase() string {
   790  	return s.On
   791  }
   792  
   793  // GrantAdminStatement represents a command for granting admin privilege.
   794  type GrantAdminStatement struct {
   795  	// Who to grant the privilege to.
   796  	User string
   797  }
   798  
   799  // String returns a string representation of the grant admin statement.
   800  func (s *GrantAdminStatement) String() string {
   801  	var buf bytes.Buffer
   802  	_, _ = buf.WriteString("GRANT ALL PRIVILEGES TO ")
   803  	_, _ = buf.WriteString(QuoteIdent(s.User))
   804  	return buf.String()
   805  }
   806  
   807  // RequiredPrivileges returns the privilege required to execute a GrantAdminStatement.
   808  func (s *GrantAdminStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   809  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   810  }
   811  
   812  // KillQueryStatement represents a command for killing a query.
   813  type KillQueryStatement struct {
   814  	// The query to kill.
   815  	QueryID uint64
   816  
   817  	// The host to delegate the kill to.
   818  	Host string
   819  }
   820  
   821  // String returns a string representation of the kill query statement.
   822  func (s *KillQueryStatement) String() string {
   823  	var buf bytes.Buffer
   824  	_, _ = buf.WriteString("KILL QUERY ")
   825  	_, _ = buf.WriteString(strconv.FormatUint(s.QueryID, 10))
   826  	if s.Host != "" {
   827  		_, _ = buf.WriteString(" ON ")
   828  		_, _ = buf.WriteString(QuoteIdent(s.Host))
   829  	}
   830  	return buf.String()
   831  }
   832  
   833  // RequiredPrivileges returns the privilege required to execute a KillQueryStatement.
   834  func (s *KillQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   835  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   836  }
   837  
   838  // SetPasswordUserStatement represents a command for changing user password.
   839  type SetPasswordUserStatement struct {
   840  	// Plain-text password.
   841  	Password string
   842  
   843  	// Who to grant the privilege to.
   844  	Name string
   845  }
   846  
   847  // String returns a string representation of the set password statement.
   848  func (s *SetPasswordUserStatement) String() string {
   849  	var buf bytes.Buffer
   850  	_, _ = buf.WriteString("SET PASSWORD FOR ")
   851  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   852  	_, _ = buf.WriteString(" = ")
   853  	_, _ = buf.WriteString("[REDACTED]")
   854  	return buf.String()
   855  }
   856  
   857  // RequiredPrivileges returns the privilege required to execute a SetPasswordUserStatement.
   858  func (s *SetPasswordUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   859  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   860  }
   861  
   862  // RevokeStatement represents a command to revoke a privilege from a user.
   863  type RevokeStatement struct {
   864  	// The privilege to be revoked.
   865  	Privilege Privilege
   866  
   867  	// Database to revoke the privilege from.
   868  	On string
   869  
   870  	// Who to revoke privilege from.
   871  	User string
   872  }
   873  
   874  // String returns a string representation of the revoke statement.
   875  func (s *RevokeStatement) String() string {
   876  	var buf bytes.Buffer
   877  	_, _ = buf.WriteString("REVOKE ")
   878  	_, _ = buf.WriteString(s.Privilege.String())
   879  	_, _ = buf.WriteString(" ON ")
   880  	_, _ = buf.WriteString(QuoteIdent(s.On))
   881  	_, _ = buf.WriteString(" FROM ")
   882  	_, _ = buf.WriteString(QuoteIdent(s.User))
   883  	return buf.String()
   884  }
   885  
   886  // RequiredPrivileges returns the privilege required to execute a RevokeStatement.
   887  func (s *RevokeStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   888  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   889  }
   890  
   891  // DefaultDatabase returns the default database from the statement.
   892  func (s *RevokeStatement) DefaultDatabase() string {
   893  	return s.On
   894  }
   895  
   896  // RevokeAdminStatement represents a command to revoke admin privilege from a user.
   897  type RevokeAdminStatement struct {
   898  	// Who to revoke admin privilege from.
   899  	User string
   900  }
   901  
   902  // String returns a string representation of the revoke admin statement.
   903  func (s *RevokeAdminStatement) String() string {
   904  	var buf bytes.Buffer
   905  	_, _ = buf.WriteString("REVOKE ALL PRIVILEGES FROM ")
   906  	_, _ = buf.WriteString(QuoteIdent(s.User))
   907  	return buf.String()
   908  }
   909  
   910  // RequiredPrivileges returns the privilege required to execute a RevokeAdminStatement.
   911  func (s *RevokeAdminStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   912  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   913  }
   914  
   915  // CreateRetentionPolicyStatement represents a command to create a retention policy.
   916  type CreateRetentionPolicyStatement struct {
   917  	// Name of policy to create.
   918  	Name string
   919  
   920  	// Name of database this policy belongs to.
   921  	Database string
   922  
   923  	// Duration data written to this policy will be retained.
   924  	Duration time.Duration
   925  
   926  	// Replication factor for data written to this policy.
   927  	Replication int
   928  
   929  	// Should this policy be set as default for the database?
   930  	Default bool
   931  
   932  	// Shard Duration.
   933  	ShardGroupDuration time.Duration
   934  }
   935  
   936  // String returns a string representation of the create retention policy.
   937  func (s *CreateRetentionPolicyStatement) String() string {
   938  	var buf bytes.Buffer
   939  	_, _ = buf.WriteString("CREATE RETENTION POLICY ")
   940  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   941  	_, _ = buf.WriteString(" ON ")
   942  	_, _ = buf.WriteString(QuoteIdent(s.Database))
   943  	_, _ = buf.WriteString(" DURATION ")
   944  	_, _ = buf.WriteString(FormatDuration(s.Duration))
   945  	_, _ = buf.WriteString(" REPLICATION ")
   946  	_, _ = buf.WriteString(strconv.Itoa(s.Replication))
   947  	if s.ShardGroupDuration > 0 {
   948  		_, _ = buf.WriteString(" SHARD DURATION ")
   949  		_, _ = buf.WriteString(FormatDuration(s.ShardGroupDuration))
   950  	}
   951  	if s.Default {
   952  		_, _ = buf.WriteString(" DEFAULT")
   953  	}
   954  	return buf.String()
   955  }
   956  
   957  // RequiredPrivileges returns the privilege required to execute a CreateRetentionPolicyStatement.
   958  func (s *CreateRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
   959  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
   960  }
   961  
   962  // DefaultDatabase returns the default database from the statement.
   963  func (s *CreateRetentionPolicyStatement) DefaultDatabase() string {
   964  	return s.Database
   965  }
   966  
   967  // AlterRetentionPolicyStatement represents a command to alter an existing retention policy.
   968  type AlterRetentionPolicyStatement struct {
   969  	// Name of policy to alter.
   970  	Name string
   971  
   972  	// Name of the database this policy belongs to.
   973  	Database string
   974  
   975  	// Duration data written to this policy will be retained.
   976  	Duration *time.Duration
   977  
   978  	// Replication factor for data written to this policy.
   979  	Replication *int
   980  
   981  	// Should this policy be set as defalut for the database?
   982  	Default bool
   983  
   984  	// Duration of the Shard.
   985  	ShardGroupDuration *time.Duration
   986  }
   987  
   988  // String returns a string representation of the alter retention policy statement.
   989  func (s *AlterRetentionPolicyStatement) String() string {
   990  	var buf bytes.Buffer
   991  	_, _ = buf.WriteString("ALTER RETENTION POLICY ")
   992  	_, _ = buf.WriteString(QuoteIdent(s.Name))
   993  	_, _ = buf.WriteString(" ON ")
   994  	_, _ = buf.WriteString(QuoteIdent(s.Database))
   995  
   996  	if s.Duration != nil {
   997  		_, _ = buf.WriteString(" DURATION ")
   998  		_, _ = buf.WriteString(FormatDuration(*s.Duration))
   999  	}
  1000  
  1001  	if s.Replication != nil {
  1002  		_, _ = buf.WriteString(" REPLICATION ")
  1003  		_, _ = buf.WriteString(strconv.Itoa(*s.Replication))
  1004  	}
  1005  
  1006  	if s.ShardGroupDuration != nil {
  1007  		_, _ = buf.WriteString(" SHARD DURATION ")
  1008  		_, _ = buf.WriteString(FormatDuration(*s.ShardGroupDuration))
  1009  	}
  1010  
  1011  	if s.Default {
  1012  		_, _ = buf.WriteString(" DEFAULT")
  1013  	}
  1014  
  1015  	return buf.String()
  1016  }
  1017  
  1018  // RequiredPrivileges returns the privilege required to execute an AlterRetentionPolicyStatement.
  1019  func (s *AlterRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  1020  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  1021  }
  1022  
  1023  // DefaultDatabase returns the default database from the statement.
  1024  func (s *AlterRetentionPolicyStatement) DefaultDatabase() string {
  1025  	return s.Database
  1026  }
  1027  
  1028  // FillOption represents different options for filling aggregate windows.
  1029  type FillOption int
  1030  
  1031  const (
  1032  	// NullFill means that empty aggregate windows will just have null values.
  1033  	NullFill FillOption = iota
  1034  	// NoFill means that empty aggregate windows will be purged from the result.
  1035  	NoFill
  1036  	// NumberFill means that empty aggregate windows will be filled with a provided number.
  1037  	NumberFill
  1038  	// PreviousFill means that empty aggregate windows will be filled with whatever the previous aggregate window had.
  1039  	PreviousFill
  1040  	// LinearFill means that empty aggregate windows will be filled with whatever a linear value between non null windows.
  1041  	LinearFill
  1042  )
  1043  
  1044  // SelectStatement represents a command for extracting data from the database.
  1045  type SelectStatement struct {
  1046  	// Expressions returned from the selection.
  1047  	Fields Fields
  1048  
  1049  	// Target (destination) for the result of a SELECT INTO query.
  1050  	Target *Target
  1051  
  1052  	// Expressions used for grouping the selection.
  1053  	Dimensions Dimensions
  1054  
  1055  	// Data sources (measurements) that fields are extracted from.
  1056  	Sources Sources
  1057  
  1058  	// An expression evaluated on data point.
  1059  	Condition Expr
  1060  
  1061  	// Fields to sort results by.
  1062  	SortFields SortFields
  1063  
  1064  	// Maximum number of rows to be returned. Unlimited if zero.
  1065  	Limit int
  1066  
  1067  	// Returns rows starting at an offset from the first row.
  1068  	Offset int
  1069  
  1070  	// Maxiumum number of series to be returned. Unlimited if zero.
  1071  	SLimit int
  1072  
  1073  	// Returns series starting at an offset from the first one.
  1074  	SOffset int
  1075  
  1076  	// Memoized group by interval from GroupBy().
  1077  	groupByInterval time.Duration
  1078  
  1079  	// Whether it's a query for raw data values (i.e. not an aggregate).
  1080  	IsRawQuery bool
  1081  
  1082  	// What fill option the select statement uses, if any.
  1083  	Fill FillOption
  1084  
  1085  	// The value to fill empty aggregate buckets with, if any.
  1086  	FillValue interface{}
  1087  
  1088  	// The timezone for the query, if any.
  1089  	Location *time.Location
  1090  
  1091  	// Renames the implicit time field name.
  1092  	TimeAlias string
  1093  
  1094  	// Removes the "time" column from the output.
  1095  	OmitTime bool
  1096  
  1097  	// Removes measurement name from resulting query. Useful for meta queries.
  1098  	StripName bool
  1099  
  1100  	// Overrides the output measurement name.
  1101  	EmitName string
  1102  
  1103  	// Removes duplicate rows from raw queries.
  1104  	Dedupe bool
  1105  }
  1106  
  1107  // TimeAscending returns true if the time field is sorted in chronological order.
  1108  func (s *SelectStatement) TimeAscending() bool {
  1109  	return len(s.SortFields) == 0 || s.SortFields[0].Ascending
  1110  }
  1111  
  1112  // TimeFieldName returns the name of the time field.
  1113  func (s *SelectStatement) TimeFieldName() string {
  1114  	if s.TimeAlias != "" {
  1115  		return s.TimeAlias
  1116  	}
  1117  	return "time"
  1118  }
  1119  
  1120  // Clone returns a deep copy of the statement.
  1121  func (s *SelectStatement) Clone() *SelectStatement {
  1122  	clone := *s
  1123  	clone.Fields = make(Fields, 0, len(s.Fields))
  1124  	clone.Dimensions = make(Dimensions, 0, len(s.Dimensions))
  1125  	clone.Sources = cloneSources(s.Sources)
  1126  	clone.SortFields = make(SortFields, 0, len(s.SortFields))
  1127  	clone.Condition = CloneExpr(s.Condition)
  1128  
  1129  	if s.Target != nil {
  1130  		clone.Target = &Target{
  1131  			Measurement: &Measurement{
  1132  				Database:        s.Target.Measurement.Database,
  1133  				RetentionPolicy: s.Target.Measurement.RetentionPolicy,
  1134  				Name:            s.Target.Measurement.Name,
  1135  				Regex:           CloneRegexLiteral(s.Target.Measurement.Regex),
  1136  			},
  1137  		}
  1138  	}
  1139  	for _, f := range s.Fields {
  1140  		clone.Fields = append(clone.Fields, &Field{Expr: CloneExpr(f.Expr), Alias: f.Alias})
  1141  	}
  1142  	for _, d := range s.Dimensions {
  1143  		clone.Dimensions = append(clone.Dimensions, &Dimension{Expr: CloneExpr(d.Expr)})
  1144  	}
  1145  	for _, f := range s.SortFields {
  1146  		clone.SortFields = append(clone.SortFields, &SortField{Name: f.Name, Ascending: f.Ascending})
  1147  	}
  1148  	return &clone
  1149  }
  1150  
  1151  func cloneSources(sources Sources) Sources {
  1152  	clone := make(Sources, 0, len(sources))
  1153  	for _, s := range sources {
  1154  		clone = append(clone, cloneSource(s))
  1155  	}
  1156  	return clone
  1157  }
  1158  
  1159  func cloneSource(s Source) Source {
  1160  	if s == nil {
  1161  		return nil
  1162  	}
  1163  
  1164  	switch s := s.(type) {
  1165  	case *Measurement:
  1166  		return s.Clone()
  1167  	case *SubQuery:
  1168  		return &SubQuery{Statement: s.Statement.Clone()}
  1169  	default:
  1170  		panic("unreachable")
  1171  	}
  1172  }
  1173  
  1174  // FieldMapper returns the data type for the field inside of the measurement.
  1175  type FieldMapper interface {
  1176  	FieldDimensions(m *Measurement) (fields map[string]DataType, dimensions map[string]struct{}, err error)
  1177  
  1178  	TypeMapper
  1179  }
  1180  
  1181  // RewriteFields returns the re-written form of the select statement. Any wildcard query
  1182  // fields are replaced with the supplied fields, and any wildcard GROUP BY fields are replaced
  1183  // with the supplied dimensions. Any fields with no type specifier are rewritten with the
  1184  // appropriate type.
  1185  func (s *SelectStatement) RewriteFields(m FieldMapper) (*SelectStatement, error) {
  1186  	// Clone the statement so we aren't rewriting the original.
  1187  	other := s.Clone()
  1188  
  1189  	// Iterate through the sources and rewrite any subqueries first.
  1190  	for _, src := range other.Sources {
  1191  		switch src := src.(type) {
  1192  		case *SubQuery:
  1193  			stmt, err := src.Statement.RewriteFields(m)
  1194  			if err != nil {
  1195  				return nil, err
  1196  			}
  1197  			src.Statement = stmt
  1198  		}
  1199  	}
  1200  
  1201  	// Rewrite all variable references in the fields with their types if one
  1202  	// hasn't been specified.
  1203  	rewrite := func(n Node) {
  1204  		ref, ok := n.(*VarRef)
  1205  		if !ok || (ref.Type != Unknown && ref.Type != AnyField) {
  1206  			return
  1207  		}
  1208  
  1209  		typ := EvalType(ref, other.Sources, m)
  1210  		if typ == Tag && ref.Type == AnyField {
  1211  			return
  1212  		}
  1213  		ref.Type = typ
  1214  	}
  1215  	WalkFunc(other.Fields, rewrite)
  1216  	WalkFunc(other.Condition, rewrite)
  1217  
  1218  	// Ignore if there are no wildcards.
  1219  	hasFieldWildcard := other.HasFieldWildcard()
  1220  	hasDimensionWildcard := other.HasDimensionWildcard()
  1221  	if !hasFieldWildcard && !hasDimensionWildcard {
  1222  		return other, nil
  1223  	}
  1224  
  1225  	fieldSet, dimensionSet, err := FieldDimensions(other.Sources, m)
  1226  	if err != nil {
  1227  		return nil, err
  1228  	}
  1229  
  1230  	// If there are no dimension wildcards then merge dimensions to fields.
  1231  	if !hasDimensionWildcard {
  1232  		// Remove the dimensions present in the group by so they don't get added as fields.
  1233  		for _, d := range other.Dimensions {
  1234  			switch expr := d.Expr.(type) {
  1235  			case *VarRef:
  1236  				delete(dimensionSet, expr.Val)
  1237  			}
  1238  		}
  1239  	}
  1240  
  1241  	// Sort the field and dimension names for wildcard expansion.
  1242  	var fields []VarRef
  1243  	if len(fieldSet) > 0 {
  1244  		fields = make([]VarRef, 0, len(fieldSet))
  1245  		for name, typ := range fieldSet {
  1246  			fields = append(fields, VarRef{Val: name, Type: typ})
  1247  		}
  1248  		if !hasDimensionWildcard {
  1249  			for name := range dimensionSet {
  1250  				fields = append(fields, VarRef{Val: name, Type: Tag})
  1251  			}
  1252  			dimensionSet = nil
  1253  		}
  1254  		sort.Sort(VarRefs(fields))
  1255  	}
  1256  	dimensions := stringSetSlice(dimensionSet)
  1257  
  1258  	// Rewrite all wildcard query fields
  1259  	if hasFieldWildcard {
  1260  		// Allocate a slice assuming there is exactly one wildcard for efficiency.
  1261  		rwFields := make(Fields, 0, len(other.Fields)+len(fields)-1)
  1262  		for _, f := range other.Fields {
  1263  			switch expr := f.Expr.(type) {
  1264  			case *Wildcard:
  1265  				for _, ref := range fields {
  1266  					if expr.Type == FIELD && ref.Type == Tag {
  1267  						continue
  1268  					} else if expr.Type == TAG && ref.Type != Tag {
  1269  						continue
  1270  					}
  1271  					rwFields = append(rwFields, &Field{Expr: &VarRef{Val: ref.Val, Type: ref.Type}})
  1272  				}
  1273  			case *RegexLiteral:
  1274  				for _, ref := range fields {
  1275  					if expr.Val.MatchString(ref.Val) {
  1276  						rwFields = append(rwFields, &Field{Expr: &VarRef{Val: ref.Val, Type: ref.Type}})
  1277  					}
  1278  				}
  1279  			case *Call:
  1280  				// Clone a template that we can modify and use for new fields.
  1281  				template := CloneExpr(expr).(*Call)
  1282  
  1283  				// Search for the call with a wildcard by continuously descending until
  1284  				// we no longer have a call.
  1285  				call := template
  1286  				for len(call.Args) > 0 {
  1287  					arg, ok := call.Args[0].(*Call)
  1288  					if !ok {
  1289  						break
  1290  					}
  1291  					call = arg
  1292  				}
  1293  
  1294  				// Check if this field value is a wildcard.
  1295  				if len(call.Args) == 0 {
  1296  					rwFields = append(rwFields, f)
  1297  					continue
  1298  				}
  1299  
  1300  				// Retrieve if this is a wildcard or a regular expression.
  1301  				var re *regexp.Regexp
  1302  				switch expr := call.Args[0].(type) {
  1303  				case *Wildcard:
  1304  					if expr.Type == TAG {
  1305  						return nil, fmt.Errorf("unable to use tag wildcard in %s()", call.Name)
  1306  					}
  1307  				case *RegexLiteral:
  1308  					re = expr.Val
  1309  				default:
  1310  					rwFields = append(rwFields, f)
  1311  					continue
  1312  				}
  1313  
  1314  				// All types that can expand wildcards support float, integer, and unsigned.
  1315  				supportedTypes := map[DataType]struct{}{
  1316  					Float:    {},
  1317  					Integer:  {},
  1318  					Unsigned: {},
  1319  				}
  1320  
  1321  				// Add additional types for certain functions.
  1322  				switch call.Name {
  1323  				case "count", "first", "last", "distinct", "elapsed", "mode", "sample":
  1324  					supportedTypes[String] = struct{}{}
  1325  					fallthrough
  1326  				case "min", "max":
  1327  					supportedTypes[Boolean] = struct{}{}
  1328  				case "holt_winters", "holt_winters_with_fit":
  1329  					delete(supportedTypes, Unsigned)
  1330  				}
  1331  
  1332  				for _, ref := range fields {
  1333  					// Do not expand tags within a function call. It likely won't do anything
  1334  					// anyway and will be the wrong thing in 99% of cases.
  1335  					if ref.Type == Tag {
  1336  						continue
  1337  					} else if _, ok := supportedTypes[ref.Type]; !ok {
  1338  						continue
  1339  					} else if re != nil && !re.MatchString(ref.Val) {
  1340  						continue
  1341  					}
  1342  
  1343  					// Make a new expression and replace the wildcard within this cloned expression.
  1344  					call.Args[0] = &VarRef{Val: ref.Val, Type: ref.Type}
  1345  					rwFields = append(rwFields, &Field{
  1346  						Expr:  CloneExpr(template),
  1347  						Alias: fmt.Sprintf("%s_%s", f.Name(), ref.Val),
  1348  					})
  1349  				}
  1350  			case *BinaryExpr:
  1351  				// Search for regexes or wildcards within the binary
  1352  				// expression. If we find any, throw an error indicating that
  1353  				// it's illegal.
  1354  				var regex, wildcard bool
  1355  				WalkFunc(expr, func(n Node) {
  1356  					switch n.(type) {
  1357  					case *RegexLiteral:
  1358  						regex = true
  1359  					case *Wildcard:
  1360  						wildcard = true
  1361  					}
  1362  				})
  1363  
  1364  				if wildcard {
  1365  					return nil, fmt.Errorf("unsupported expression with wildcard: %s", f.Expr)
  1366  				} else if regex {
  1367  					return nil, fmt.Errorf("unsupported expression with regex field: %s", f.Expr)
  1368  				}
  1369  				rwFields = append(rwFields, f)
  1370  			default:
  1371  				rwFields = append(rwFields, f)
  1372  			}
  1373  		}
  1374  		other.Fields = rwFields
  1375  	}
  1376  
  1377  	// Rewrite all wildcard GROUP BY fields
  1378  	if hasDimensionWildcard {
  1379  		// Allocate a slice assuming there is exactly one wildcard for efficiency.
  1380  		rwDimensions := make(Dimensions, 0, len(other.Dimensions)+len(dimensions)-1)
  1381  		for _, d := range other.Dimensions {
  1382  			switch expr := d.Expr.(type) {
  1383  			case *Wildcard:
  1384  				for _, name := range dimensions {
  1385  					rwDimensions = append(rwDimensions, &Dimension{Expr: &VarRef{Val: name}})
  1386  				}
  1387  			case *RegexLiteral:
  1388  				for _, name := range dimensions {
  1389  					if expr.Val.MatchString(name) {
  1390  						rwDimensions = append(rwDimensions, &Dimension{Expr: &VarRef{Val: name}})
  1391  					}
  1392  				}
  1393  			default:
  1394  				rwDimensions = append(rwDimensions, d)
  1395  			}
  1396  		}
  1397  		other.Dimensions = rwDimensions
  1398  	}
  1399  
  1400  	return other, nil
  1401  }
  1402  
  1403  // RewriteRegexConditions rewrites regex conditions to make better use of the
  1404  // database index.
  1405  //
  1406  // Conditions that can currently be simplified are:
  1407  //
  1408  //     - host =~ /^foo$/ becomes host = 'foo'
  1409  //     - host !~ /^foo$/ becomes host != 'foo'
  1410  //
  1411  // Note: if the regex contains groups, character classes, repetition or
  1412  // similar, it's likely it won't be rewritten. In order to support rewriting
  1413  // regexes with these characters would be a lot more work.
  1414  func (s *SelectStatement) RewriteRegexConditions() {
  1415  	s.Condition = RewriteExpr(s.Condition, func(e Expr) Expr {
  1416  		be, ok := e.(*BinaryExpr)
  1417  		if !ok || (be.Op != EQREGEX && be.Op != NEQREGEX) {
  1418  			// This expression is not a binary condition or doesn't have a
  1419  			// regex based operator.
  1420  			return e
  1421  		}
  1422  
  1423  		// Handle regex-based condition.
  1424  		rhs := be.RHS.(*RegexLiteral) // This must be a regex.
  1425  
  1426  		vals, ok := matchExactRegex(rhs.Val.String())
  1427  		if !ok {
  1428  			// Regex didn't match.
  1429  			return e
  1430  		}
  1431  
  1432  		// Update the condition operator.
  1433  		var concatOp Token
  1434  		if be.Op == EQREGEX {
  1435  			be.Op = EQ
  1436  			concatOp = OR
  1437  		} else {
  1438  			be.Op = NEQ
  1439  			concatOp = AND
  1440  		}
  1441  
  1442  		// Remove leading and trailing ^ and $.
  1443  		switch {
  1444  		case len(vals) == 0:
  1445  			be.RHS = &StringLiteral{}
  1446  		case len(vals) == 1:
  1447  			be.RHS = &StringLiteral{Val: vals[0]}
  1448  		default:
  1449  			expr := &BinaryExpr{
  1450  				Op:  be.Op,
  1451  				LHS: be.LHS,
  1452  				RHS: &StringLiteral{Val: vals[0]},
  1453  			}
  1454  			for i := 1; i < len(vals); i++ {
  1455  				expr = &BinaryExpr{
  1456  					Op:  concatOp,
  1457  					LHS: expr,
  1458  					RHS: &BinaryExpr{
  1459  						Op:  be.Op,
  1460  						LHS: be.LHS,
  1461  						RHS: &StringLiteral{Val: vals[i]},
  1462  					},
  1463  				}
  1464  			}
  1465  			return &ParenExpr{Expr: expr}
  1466  		}
  1467  		return be
  1468  	})
  1469  
  1470  	// Unwrap any top level parenthesis.
  1471  	if cond, ok := s.Condition.(*ParenExpr); ok {
  1472  		s.Condition = cond.Expr
  1473  	}
  1474  }
  1475  
  1476  // matchExactRegex matches regexes into literals if possible. This will match the
  1477  // pattern /^foo$/ or /^(foo|bar)$/. It considers /^$/ to be a matching regex.
  1478  func matchExactRegex(v string) ([]string, bool) {
  1479  	re, err := syntax.Parse(v, syntax.Perl)
  1480  	if err != nil {
  1481  		// Nothing we can do or log.
  1482  		return nil, false
  1483  	}
  1484  	re = re.Simplify()
  1485  
  1486  	if re.Op != syntax.OpConcat {
  1487  		return nil, false
  1488  	}
  1489  
  1490  	if len(re.Sub) < 2 {
  1491  		// Regex has too few subexpressions.
  1492  		return nil, false
  1493  	}
  1494  
  1495  	start := re.Sub[0]
  1496  	if !(start.Op == syntax.OpBeginLine || start.Op == syntax.OpBeginText) {
  1497  		// Regex does not begin with ^
  1498  		return nil, false
  1499  	}
  1500  
  1501  	end := re.Sub[len(re.Sub)-1]
  1502  	if !(end.Op == syntax.OpEndLine || end.Op == syntax.OpEndText) {
  1503  		// Regex does not end with $
  1504  		return nil, false
  1505  	}
  1506  
  1507  	// Remove the begin and end text from the regex.
  1508  	re.Sub = re.Sub[1 : len(re.Sub)-1]
  1509  
  1510  	if len(re.Sub) == 0 {
  1511  		// The regex /^$/
  1512  		return nil, true
  1513  	}
  1514  	return matchRegex(re)
  1515  }
  1516  
  1517  // matchRegex will match a regular expression to literals if possible.
  1518  func matchRegex(re *syntax.Regexp) ([]string, bool) {
  1519  
  1520  	// Maximum number of literals that the expression should be expanded to. If
  1521  	// this is exceeded, no expansion will be done. This allows reasonable
  1522  	// optimizations of regex by expansion to literals but prevents cases
  1523  	// where that expansion would result in a large number of literals.
  1524  	const maxLiterals = 100
  1525  
  1526  	// Exit if we see a case-insensitive flag as it is not something we support at this time.
  1527  	if re.Flags&syntax.FoldCase != 0 {
  1528  		return nil, false
  1529  	}
  1530  
  1531  	switch re.Op {
  1532  	case syntax.OpLiteral:
  1533  		// We can rewrite this regex.
  1534  		return []string{string(re.Rune)}, true
  1535  	case syntax.OpCapture:
  1536  		return matchRegex(re.Sub[0])
  1537  	case syntax.OpConcat:
  1538  		// Go through each of the subs and concatenate the result to each one.
  1539  		names, ok := matchRegex(re.Sub[0])
  1540  		if !ok {
  1541  			return nil, false
  1542  		}
  1543  
  1544  		for _, sub := range re.Sub[1:] {
  1545  			vals, ok := matchRegex(sub)
  1546  			if !ok {
  1547  				return nil, false
  1548  			}
  1549  
  1550  			// If there is only one value, concatenate it to all strings rather
  1551  			// than allocate a new slice.
  1552  			if len(vals) == 1 {
  1553  				for i := range names {
  1554  					names[i] += vals[0]
  1555  				}
  1556  				continue
  1557  			} else if len(names) == 1 {
  1558  				// If there is only one value, then do this concatenation in
  1559  				// the opposite direction.
  1560  				for i := range vals {
  1561  					vals[i] = names[0] + vals[i]
  1562  				}
  1563  				names = vals
  1564  				continue
  1565  			}
  1566  
  1567  			sz := len(names) * len(vals)
  1568  			if sz > maxLiterals {
  1569  				return nil, false
  1570  			}
  1571  
  1572  			// The long method of using multiple concatenations.
  1573  			concat := make([]string, sz)
  1574  			for i := range names {
  1575  				for j := range vals {
  1576  					concat[i*len(vals)+j] = names[i] + vals[j]
  1577  				}
  1578  			}
  1579  			names = concat
  1580  		}
  1581  		return names, true
  1582  	case syntax.OpCharClass:
  1583  		var sz int
  1584  		for i := 0; i < len(re.Rune); i += 2 {
  1585  			sz += int(re.Rune[i+1]) - int(re.Rune[i]) + 1
  1586  		}
  1587  
  1588  		if sz > maxLiterals {
  1589  			return nil, false
  1590  		}
  1591  
  1592  		names := make([]string, 0, sz)
  1593  		for i := 0; i < len(re.Rune); i += 2 {
  1594  			for r := int(re.Rune[i]); r <= int(re.Rune[i+1]); r++ {
  1595  				names = append(names, string([]rune{rune(r)}))
  1596  			}
  1597  		}
  1598  		return names, true
  1599  	case syntax.OpAlternate:
  1600  		var names []string
  1601  		for _, sub := range re.Sub {
  1602  			vals, ok := matchRegex(sub)
  1603  			if !ok {
  1604  				return nil, false
  1605  			}
  1606  			names = append(names, vals...)
  1607  		}
  1608  		if len(names) > maxLiterals {
  1609  			return nil, false
  1610  		}
  1611  		return names, true
  1612  	}
  1613  	return nil, false
  1614  }
  1615  
  1616  // RewriteDistinct rewrites the expression to be a call for map/reduce to work correctly.
  1617  // This method assumes all validation has passed.
  1618  func (s *SelectStatement) RewriteDistinct() {
  1619  	WalkFunc(s.Fields, func(n Node) {
  1620  		switch n := n.(type) {
  1621  		case *Field:
  1622  			if expr, ok := n.Expr.(*Distinct); ok {
  1623  				n.Expr = expr.NewCall()
  1624  				s.IsRawQuery = false
  1625  			}
  1626  		case *Call:
  1627  			for i, arg := range n.Args {
  1628  				if arg, ok := arg.(*Distinct); ok {
  1629  					n.Args[i] = arg.NewCall()
  1630  				}
  1631  			}
  1632  		}
  1633  	})
  1634  }
  1635  
  1636  // RewriteTimeFields removes any "time" field references.
  1637  func (s *SelectStatement) RewriteTimeFields() {
  1638  	for i := 0; i < len(s.Fields); i++ {
  1639  		switch expr := s.Fields[i].Expr.(type) {
  1640  		case *VarRef:
  1641  			if expr.Val == "time" {
  1642  				s.TimeAlias = s.Fields[i].Alias
  1643  				s.Fields = append(s.Fields[:i], s.Fields[i+1:]...)
  1644  			}
  1645  		}
  1646  	}
  1647  }
  1648  
  1649  // ColumnNames will walk all fields and functions and return the appropriate field names for the select statement
  1650  // while maintaining order of the field names.
  1651  func (s *SelectStatement) ColumnNames() []string {
  1652  	// First walk each field to determine the number of columns.
  1653  	columnFields := Fields{}
  1654  	for _, field := range s.Fields {
  1655  		columnFields = append(columnFields, field)
  1656  
  1657  		switch f := field.Expr.(type) {
  1658  		case *Call:
  1659  			if s.Target == nil && (f.Name == "top" || f.Name == "bottom") {
  1660  				for _, arg := range f.Args[1:] {
  1661  					ref, ok := arg.(*VarRef)
  1662  					if ok {
  1663  						columnFields = append(columnFields, &Field{Expr: ref})
  1664  					}
  1665  				}
  1666  			}
  1667  		}
  1668  	}
  1669  
  1670  	// Determine if we should add an extra column for an implicit time.
  1671  	offset := 0
  1672  	if !s.OmitTime {
  1673  		offset++
  1674  	}
  1675  
  1676  	columnNames := make([]string, len(columnFields)+offset)
  1677  	if !s.OmitTime {
  1678  		// Add the implicit time if requested.
  1679  		columnNames[0] = s.TimeFieldName()
  1680  	}
  1681  
  1682  	// Keep track of the encountered column names.
  1683  	names := make(map[string]int)
  1684  
  1685  	// Resolve aliases first.
  1686  	for i, col := range columnFields {
  1687  		if col.Alias != "" {
  1688  			columnNames[i+offset] = col.Alias
  1689  			names[col.Alias] = 1
  1690  		}
  1691  	}
  1692  
  1693  	// Resolve any generated names and resolve conflicts.
  1694  	for i, col := range columnFields {
  1695  		if columnNames[i+offset] != "" {
  1696  			continue
  1697  		}
  1698  
  1699  		name := col.Name()
  1700  		count, conflict := names[name]
  1701  		if conflict {
  1702  			for {
  1703  				resolvedName := fmt.Sprintf("%s_%d", name, count)
  1704  				_, conflict = names[resolvedName]
  1705  				if !conflict {
  1706  					names[name] = count + 1
  1707  					name = resolvedName
  1708  					break
  1709  				}
  1710  				count++
  1711  			}
  1712  		}
  1713  		names[name]++
  1714  		columnNames[i+offset] = name
  1715  	}
  1716  	return columnNames
  1717  }
  1718  
  1719  // FieldExprByName returns the expression that matches the field name and the
  1720  // index where this was found. If the name matches one of the arguments to
  1721  // "top" or "bottom", the variable reference inside of the function is returned
  1722  // and the index is of the function call rather than the variable reference.
  1723  // If no expression is found, -1 is returned for the index and the expression
  1724  // will be nil.
  1725  func (s *SelectStatement) FieldExprByName(name string) (int, Expr) {
  1726  	for i, f := range s.Fields {
  1727  		if f.Name() == name {
  1728  			return i, f.Expr
  1729  		} else if call, ok := f.Expr.(*Call); ok && (call.Name == "top" || call.Name == "bottom") && len(call.Args) > 2 {
  1730  			for _, arg := range call.Args[1 : len(call.Args)-1] {
  1731  				if arg, ok := arg.(*VarRef); ok && arg.Val == name {
  1732  					return i, arg
  1733  				}
  1734  			}
  1735  		}
  1736  	}
  1737  	return -1, nil
  1738  }
  1739  
  1740  // Reduce calls the Reduce function on the different components of the
  1741  // SelectStatement to reduce the statement.
  1742  func (s *SelectStatement) Reduce(valuer Valuer) *SelectStatement {
  1743  	stmt := s.Clone()
  1744  	stmt.Condition = Reduce(stmt.Condition, valuer)
  1745  	for _, d := range stmt.Dimensions {
  1746  		d.Expr = Reduce(d.Expr, valuer)
  1747  	}
  1748  
  1749  	for _, source := range stmt.Sources {
  1750  		switch source := source.(type) {
  1751  		case *SubQuery:
  1752  			source.Statement = source.Statement.Reduce(valuer)
  1753  		}
  1754  	}
  1755  	return stmt
  1756  }
  1757  
  1758  // String returns a string representation of the select statement.
  1759  func (s *SelectStatement) String() string {
  1760  	var buf bytes.Buffer
  1761  	_, _ = buf.WriteString("SELECT ")
  1762  	_, _ = buf.WriteString(s.Fields.String())
  1763  
  1764  	if s.Target != nil {
  1765  		_, _ = buf.WriteString(" ")
  1766  		_, _ = buf.WriteString(s.Target.String())
  1767  	}
  1768  	if len(s.Sources) > 0 {
  1769  		_, _ = buf.WriteString(" FROM ")
  1770  		_, _ = buf.WriteString(s.Sources.String())
  1771  	}
  1772  	if s.Condition != nil {
  1773  		_, _ = buf.WriteString(" WHERE ")
  1774  		_, _ = buf.WriteString(s.Condition.String())
  1775  	}
  1776  	if len(s.Dimensions) > 0 {
  1777  		_, _ = buf.WriteString(" GROUP BY ")
  1778  		_, _ = buf.WriteString(s.Dimensions.String())
  1779  	}
  1780  	switch s.Fill {
  1781  	case NoFill:
  1782  		_, _ = buf.WriteString(" fill(none)")
  1783  	case NumberFill:
  1784  		_, _ = buf.WriteString(fmt.Sprintf(" fill(%v)", s.FillValue))
  1785  	case LinearFill:
  1786  		_, _ = buf.WriteString(" fill(linear)")
  1787  	case PreviousFill:
  1788  		_, _ = buf.WriteString(" fill(previous)")
  1789  	}
  1790  	if len(s.SortFields) > 0 {
  1791  		_, _ = buf.WriteString(" ORDER BY ")
  1792  		_, _ = buf.WriteString(s.SortFields.String())
  1793  	}
  1794  	if s.Limit > 0 {
  1795  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  1796  	}
  1797  	if s.Offset > 0 {
  1798  		_, _ = buf.WriteString(" OFFSET ")
  1799  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  1800  	}
  1801  	if s.SLimit > 0 {
  1802  		_, _ = fmt.Fprintf(&buf, " SLIMIT %d", s.SLimit)
  1803  	}
  1804  	if s.SOffset > 0 {
  1805  		_, _ = fmt.Fprintf(&buf, " SOFFSET %d", s.SOffset)
  1806  	}
  1807  	if s.Location != nil {
  1808  		_, _ = fmt.Fprintf(&buf, ` TZ('%s')`, s.Location)
  1809  	}
  1810  	return buf.String()
  1811  }
  1812  
  1813  // RequiredPrivileges returns the privilege required to execute the SelectStatement.
  1814  // NOTE: Statement should be normalized first (database name(s) in Sources and
  1815  // Target should be populated). If the statement has not been normalized, an
  1816  // empty string will be returned for the database name and it is up to the caller
  1817  // to interpret that as the default database.
  1818  func (s *SelectStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  1819  	ep, err := s.Sources.RequiredPrivileges()
  1820  	if err != nil {
  1821  		return nil, err
  1822  	}
  1823  
  1824  	if s.Target != nil {
  1825  		ep = append(ep, ExecutionPrivilege{Admin: false, Name: s.Target.Measurement.Database, Privilege: WritePrivilege})
  1826  	}
  1827  	return ep, nil
  1828  }
  1829  
  1830  // HasWildcard returns whether or not the select statement has at least 1 wildcard.
  1831  func (s *SelectStatement) HasWildcard() bool {
  1832  	return s.HasFieldWildcard() || s.HasDimensionWildcard()
  1833  }
  1834  
  1835  // HasFieldWildcard returns whether or not the select statement has at least 1 wildcard in the fields.
  1836  func (s *SelectStatement) HasFieldWildcard() (hasWildcard bool) {
  1837  	WalkFunc(s.Fields, func(n Node) {
  1838  		if hasWildcard {
  1839  			return
  1840  		}
  1841  		switch n.(type) {
  1842  		case *Wildcard, *RegexLiteral:
  1843  			hasWildcard = true
  1844  		}
  1845  	})
  1846  	return hasWildcard
  1847  }
  1848  
  1849  // HasDimensionWildcard returns whether or not the select statement has
  1850  // at least 1 wildcard in the dimensions aka `GROUP BY`.
  1851  func (s *SelectStatement) HasDimensionWildcard() bool {
  1852  	for _, d := range s.Dimensions {
  1853  		switch d.Expr.(type) {
  1854  		case *Wildcard, *RegexLiteral:
  1855  			return true
  1856  		}
  1857  	}
  1858  
  1859  	return false
  1860  }
  1861  
  1862  // GroupByInterval extracts the time interval, if specified.
  1863  func (s *SelectStatement) GroupByInterval() (time.Duration, error) {
  1864  	// return if we've already pulled it out
  1865  	if s.groupByInterval != 0 {
  1866  		return s.groupByInterval, nil
  1867  	}
  1868  
  1869  	// Ignore if there are no dimensions.
  1870  	if len(s.Dimensions) == 0 {
  1871  		return 0, nil
  1872  	}
  1873  
  1874  	for _, d := range s.Dimensions {
  1875  		if call, ok := d.Expr.(*Call); ok && call.Name == "time" {
  1876  			// Make sure there is exactly one argument.
  1877  			if got := len(call.Args); got < 1 || got > 2 {
  1878  				return 0, errors.New("time dimension expected 1 or 2 arguments")
  1879  			}
  1880  
  1881  			// Ensure the argument is a duration.
  1882  			lit, ok := call.Args[0].(*DurationLiteral)
  1883  			if !ok {
  1884  				return 0, errors.New("time dimension must have duration argument")
  1885  			}
  1886  			s.groupByInterval = lit.Val
  1887  			return lit.Val, nil
  1888  		}
  1889  	}
  1890  	return 0, nil
  1891  }
  1892  
  1893  // GroupByOffset extracts the time interval offset, if specified.
  1894  func (s *SelectStatement) GroupByOffset() (time.Duration, error) {
  1895  	interval, err := s.GroupByInterval()
  1896  	if err != nil {
  1897  		return 0, err
  1898  	}
  1899  
  1900  	// Ignore if there are no dimensions.
  1901  	if len(s.Dimensions) == 0 {
  1902  		return 0, nil
  1903  	}
  1904  
  1905  	for _, d := range s.Dimensions {
  1906  		if call, ok := d.Expr.(*Call); ok && call.Name == "time" {
  1907  			if len(call.Args) == 2 {
  1908  				switch expr := call.Args[1].(type) {
  1909  				case *DurationLiteral:
  1910  					return expr.Val % interval, nil
  1911  				case *TimeLiteral:
  1912  					return expr.Val.Sub(expr.Val.Truncate(interval)), nil
  1913  				default:
  1914  					return 0, fmt.Errorf("invalid time dimension offset: %s", expr)
  1915  				}
  1916  			}
  1917  			return 0, nil
  1918  		}
  1919  	}
  1920  	return 0, nil
  1921  }
  1922  
  1923  // SetTimeRange sets the start and end time of the select statement to [start, end). i.e. start inclusive, end exclusive.
  1924  // This is used commonly for continuous queries so the start and end are in buckets.
  1925  func (s *SelectStatement) SetTimeRange(start, end time.Time) error {
  1926  	cond := fmt.Sprintf("time >= '%s' AND time < '%s'", start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano))
  1927  	if s.Condition != nil {
  1928  		cond = fmt.Sprintf("%s AND %s", s.rewriteWithoutTimeDimensions(), cond)
  1929  	}
  1930  
  1931  	expr, err := NewParser(strings.NewReader(cond)).ParseExpr()
  1932  	if err != nil {
  1933  		return err
  1934  	}
  1935  
  1936  	// Fold out any previously replaced time dimensions and set the condition.
  1937  	s.Condition = Reduce(expr, nil)
  1938  
  1939  	return nil
  1940  }
  1941  
  1942  // rewriteWithoutTimeDimensions will remove any WHERE time... clauses from the select statement.
  1943  // This is necessary when setting an explicit time range to override any that previously existed.
  1944  func (s *SelectStatement) rewriteWithoutTimeDimensions() string {
  1945  	n := RewriteFunc(s.Condition, func(n Node) Node {
  1946  		switch n := n.(type) {
  1947  		case *BinaryExpr:
  1948  			if n.LHS.String() == "time" {
  1949  				return &BooleanLiteral{Val: true}
  1950  			}
  1951  			return n
  1952  		case *Call:
  1953  			return &BooleanLiteral{Val: true}
  1954  		default:
  1955  			return n
  1956  		}
  1957  	})
  1958  
  1959  	return n.String()
  1960  }
  1961  
  1962  func encodeMeasurement(mm *Measurement) *internal.Measurement {
  1963  	pb := &internal.Measurement{
  1964  		Database:        proto.String(mm.Database),
  1965  		RetentionPolicy: proto.String(mm.RetentionPolicy),
  1966  		Name:            proto.String(mm.Name),
  1967  		IsTarget:        proto.Bool(mm.IsTarget),
  1968  	}
  1969  	if mm.Regex != nil {
  1970  		pb.Regex = proto.String(mm.Regex.Val.String())
  1971  	}
  1972  	return pb
  1973  }
  1974  
  1975  func decodeMeasurement(pb *internal.Measurement) (*Measurement, error) {
  1976  	mm := &Measurement{
  1977  		Database:        pb.GetDatabase(),
  1978  		RetentionPolicy: pb.GetRetentionPolicy(),
  1979  		Name:            pb.GetName(),
  1980  		IsTarget:        pb.GetIsTarget(),
  1981  	}
  1982  
  1983  	if pb.Regex != nil {
  1984  		regex, err := regexp.Compile(pb.GetRegex())
  1985  		if err != nil {
  1986  			return nil, fmt.Errorf("invalid binary measurement regex: value=%q, err=%s", pb.GetRegex(), err)
  1987  		}
  1988  		mm.Regex = &RegexLiteral{Val: regex}
  1989  	}
  1990  
  1991  	return mm, nil
  1992  }
  1993  
  1994  // walkNames will walk the Expr and return the identifier names used.
  1995  func walkNames(exp Expr) []string {
  1996  	switch expr := exp.(type) {
  1997  	case *VarRef:
  1998  		return []string{expr.Val}
  1999  	case *Call:
  2000  		var a []string
  2001  		for _, expr := range expr.Args {
  2002  			if ref, ok := expr.(*VarRef); ok {
  2003  				a = append(a, ref.Val)
  2004  			}
  2005  		}
  2006  		return a
  2007  	case *BinaryExpr:
  2008  		var ret []string
  2009  		ret = append(ret, walkNames(expr.LHS)...)
  2010  		ret = append(ret, walkNames(expr.RHS)...)
  2011  		return ret
  2012  	case *ParenExpr:
  2013  		return walkNames(expr.Expr)
  2014  	}
  2015  
  2016  	return nil
  2017  }
  2018  
  2019  // walkRefs will walk the Expr and return the var refs used.
  2020  func walkRefs(exp Expr) []VarRef {
  2021  	refs := make(map[VarRef]struct{})
  2022  	var walk func(exp Expr)
  2023  	walk = func(exp Expr) {
  2024  		switch expr := exp.(type) {
  2025  		case *VarRef:
  2026  			refs[*expr] = struct{}{}
  2027  		case *Call:
  2028  			for _, expr := range expr.Args {
  2029  				if ref, ok := expr.(*VarRef); ok {
  2030  					refs[*ref] = struct{}{}
  2031  				}
  2032  			}
  2033  		case *BinaryExpr:
  2034  			walk(expr.LHS)
  2035  			walk(expr.RHS)
  2036  		case *ParenExpr:
  2037  			walk(expr.Expr)
  2038  		}
  2039  	}
  2040  	walk(exp)
  2041  
  2042  	// Turn the map into a slice.
  2043  	a := make([]VarRef, 0, len(refs))
  2044  	for ref := range refs {
  2045  		a = append(a, ref)
  2046  	}
  2047  	return a
  2048  }
  2049  
  2050  // ExprNames returns a list of non-"time" field names from an expression.
  2051  func ExprNames(expr Expr) []VarRef {
  2052  	m := make(map[VarRef]struct{})
  2053  	for _, ref := range walkRefs(expr) {
  2054  		if ref.Val == "time" {
  2055  			continue
  2056  		}
  2057  		m[ref] = struct{}{}
  2058  	}
  2059  
  2060  	a := make([]VarRef, 0, len(m))
  2061  	for k := range m {
  2062  		a = append(a, k)
  2063  	}
  2064  	sort.Sort(VarRefs(a))
  2065  
  2066  	return a
  2067  }
  2068  
  2069  // Target represents a target (destination) policy, measurement, and DB.
  2070  type Target struct {
  2071  	// Measurement to write into.
  2072  	Measurement *Measurement
  2073  }
  2074  
  2075  // String returns a string representation of the Target.
  2076  func (t *Target) String() string {
  2077  	if t == nil {
  2078  		return ""
  2079  	}
  2080  
  2081  	var buf bytes.Buffer
  2082  	_, _ = buf.WriteString("INTO ")
  2083  	_, _ = buf.WriteString(t.Measurement.String())
  2084  	if t.Measurement.Name == "" {
  2085  		_, _ = buf.WriteString(":MEASUREMENT")
  2086  	}
  2087  
  2088  	return buf.String()
  2089  }
  2090  
  2091  // ExplainStatement represents a command for explaining a select statement.
  2092  type ExplainStatement struct {
  2093  	Statement *SelectStatement
  2094  
  2095  	Analyze bool
  2096  }
  2097  
  2098  // String returns a string representation of the explain statement.
  2099  func (e *ExplainStatement) String() string {
  2100  	var buf bytes.Buffer
  2101  	buf.WriteString("EXPLAIN ")
  2102  	if e.Analyze {
  2103  		buf.WriteString("ANALYZE ")
  2104  	}
  2105  	buf.WriteString(e.Statement.String())
  2106  	return buf.String()
  2107  }
  2108  
  2109  // RequiredPrivileges returns the privilege required to execute a ExplainStatement.
  2110  func (e *ExplainStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2111  	return e.Statement.RequiredPrivileges()
  2112  }
  2113  
  2114  // DeleteStatement represents a command for deleting data from the database.
  2115  type DeleteStatement struct {
  2116  	// Data source that values are removed from.
  2117  	Source Source
  2118  
  2119  	// An expression evaluated on data point.
  2120  	Condition Expr
  2121  }
  2122  
  2123  // String returns a string representation of the delete statement.
  2124  func (s *DeleteStatement) String() string {
  2125  	var buf bytes.Buffer
  2126  	_, _ = buf.WriteString("DELETE FROM ")
  2127  	_, _ = buf.WriteString(s.Source.String())
  2128  	if s.Condition != nil {
  2129  		_, _ = buf.WriteString(" WHERE ")
  2130  		_, _ = buf.WriteString(s.Condition.String())
  2131  	}
  2132  	return buf.String()
  2133  }
  2134  
  2135  // RequiredPrivileges returns the privilege required to execute a DeleteStatement.
  2136  func (s *DeleteStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2137  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil
  2138  }
  2139  
  2140  // DefaultDatabase returns the default database from the statement.
  2141  func (s *DeleteStatement) DefaultDatabase() string {
  2142  	if m, ok := s.Source.(*Measurement); ok {
  2143  		return m.Database
  2144  	}
  2145  	return ""
  2146  }
  2147  
  2148  // ShowSeriesStatement represents a command for listing series in the database.
  2149  type ShowSeriesStatement struct {
  2150  	// Database to query. If blank, use the default database.
  2151  	// The database can also be specified per source in the Sources.
  2152  	Database string
  2153  
  2154  	// Measurement(s) the series are listed for.
  2155  	Sources Sources
  2156  
  2157  	// An expression evaluated on a series name or tag.
  2158  	Condition Expr
  2159  
  2160  	// Fields to sort results by
  2161  	SortFields SortFields
  2162  
  2163  	// Maximum number of rows to be returned.
  2164  	// Unlimited if zero.
  2165  	Limit int
  2166  
  2167  	// Returns rows starting at an offset from the first row.
  2168  	Offset int
  2169  }
  2170  
  2171  // String returns a string representation of the list series statement.
  2172  func (s *ShowSeriesStatement) String() string {
  2173  	var buf bytes.Buffer
  2174  	_, _ = buf.WriteString("SHOW SERIES")
  2175  
  2176  	if s.Database != "" {
  2177  		_, _ = buf.WriteString(" ON ")
  2178  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2179  	}
  2180  	if s.Sources != nil {
  2181  		_, _ = buf.WriteString(" FROM ")
  2182  		_, _ = buf.WriteString(s.Sources.String())
  2183  	}
  2184  
  2185  	if s.Condition != nil {
  2186  		_, _ = buf.WriteString(" WHERE ")
  2187  		_, _ = buf.WriteString(s.Condition.String())
  2188  	}
  2189  	if len(s.SortFields) > 0 {
  2190  		_, _ = buf.WriteString(" ORDER BY ")
  2191  		_, _ = buf.WriteString(s.SortFields.String())
  2192  	}
  2193  	if s.Limit > 0 {
  2194  		_, _ = buf.WriteString(" LIMIT ")
  2195  		_, _ = buf.WriteString(strconv.Itoa(s.Limit))
  2196  	}
  2197  	if s.Offset > 0 {
  2198  		_, _ = buf.WriteString(" OFFSET ")
  2199  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2200  	}
  2201  	return buf.String()
  2202  }
  2203  
  2204  // RequiredPrivileges returns the privilege required to execute a ShowSeriesStatement.
  2205  func (s *ShowSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2206  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2207  }
  2208  
  2209  // DefaultDatabase returns the default database from the statement.
  2210  func (s *ShowSeriesStatement) DefaultDatabase() string {
  2211  	return s.Database
  2212  }
  2213  
  2214  // DropSeriesStatement represents a command for removing a series from the database.
  2215  type DropSeriesStatement struct {
  2216  	// Data source that fields are extracted from (optional)
  2217  	Sources Sources
  2218  
  2219  	// An expression evaluated on data point (optional)
  2220  	Condition Expr
  2221  }
  2222  
  2223  // String returns a string representation of the drop series statement.
  2224  func (s *DropSeriesStatement) String() string {
  2225  	var buf bytes.Buffer
  2226  	buf.WriteString("DROP SERIES")
  2227  
  2228  	if s.Sources != nil {
  2229  		buf.WriteString(" FROM ")
  2230  		buf.WriteString(s.Sources.String())
  2231  	}
  2232  	if s.Condition != nil {
  2233  		buf.WriteString(" WHERE ")
  2234  		buf.WriteString(s.Condition.String())
  2235  	}
  2236  
  2237  	return buf.String()
  2238  }
  2239  
  2240  // RequiredPrivileges returns the privilege required to execute a DropSeriesStatement.
  2241  func (s DropSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2242  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil
  2243  }
  2244  
  2245  // DeleteSeriesStatement represents a command for deleting all or part of a series from a database.
  2246  type DeleteSeriesStatement struct {
  2247  	// Data source that fields are extracted from (optional)
  2248  	Sources Sources
  2249  
  2250  	// An expression evaluated on data point (optional)
  2251  	Condition Expr
  2252  }
  2253  
  2254  // String returns a string representation of the delete series statement.
  2255  func (s *DeleteSeriesStatement) String() string {
  2256  	var buf bytes.Buffer
  2257  	buf.WriteString("DELETE")
  2258  
  2259  	if s.Sources != nil {
  2260  		buf.WriteString(" FROM ")
  2261  		buf.WriteString(s.Sources.String())
  2262  	}
  2263  	if s.Condition != nil {
  2264  		buf.WriteString(" WHERE ")
  2265  		buf.WriteString(s.Condition.String())
  2266  	}
  2267  
  2268  	return buf.String()
  2269  }
  2270  
  2271  // RequiredPrivileges returns the privilege required to execute a DeleteSeriesStatement.
  2272  func (s DeleteSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2273  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil
  2274  }
  2275  
  2276  // DropShardStatement represents a command for removing a shard from
  2277  // the node.
  2278  type DropShardStatement struct {
  2279  	// ID of the shard to be dropped.
  2280  	ID uint64
  2281  }
  2282  
  2283  // String returns a string representation of the drop series statement.
  2284  func (s *DropShardStatement) String() string {
  2285  	var buf bytes.Buffer
  2286  	buf.WriteString("DROP SHARD ")
  2287  	buf.WriteString(strconv.FormatUint(s.ID, 10))
  2288  	return buf.String()
  2289  }
  2290  
  2291  // RequiredPrivileges returns the privilege required to execute a
  2292  // DropShardStatement.
  2293  func (s *DropShardStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2294  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2295  }
  2296  
  2297  // ShowSeriesCardinalityStatement represents a command for listing series cardinality.
  2298  type ShowSeriesCardinalityStatement struct {
  2299  	// Database to query. If blank, use the default database.
  2300  	// The database can also be specified per source in the Sources.
  2301  	Database string
  2302  
  2303  	// Specifies whether the user requires exact counting or not.
  2304  	Exact bool
  2305  
  2306  	// Measurement(s) the series are listed for.
  2307  	Sources Sources
  2308  
  2309  	// An expression evaluated on a series name or tag.
  2310  	Condition Expr
  2311  
  2312  	// Expressions used for grouping the selection.
  2313  	Dimensions Dimensions
  2314  
  2315  	Limit, Offset int
  2316  }
  2317  
  2318  // String returns a string representation of the show continuous queries statement.
  2319  func (s *ShowSeriesCardinalityStatement) String() string {
  2320  	var buf bytes.Buffer
  2321  	_, _ = buf.WriteString("SHOW SERIES")
  2322  
  2323  	if s.Exact {
  2324  		_, _ = buf.WriteString(" EXACT")
  2325  	}
  2326  	_, _ = buf.WriteString(" CARDINALITY")
  2327  
  2328  	if s.Database != "" {
  2329  		_, _ = buf.WriteString(" ON ")
  2330  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2331  	}
  2332  
  2333  	if s.Sources != nil {
  2334  		_, _ = buf.WriteString(" FROM ")
  2335  		_, _ = buf.WriteString(s.Sources.String())
  2336  	}
  2337  	if s.Condition != nil {
  2338  		_, _ = buf.WriteString(" WHERE ")
  2339  		_, _ = buf.WriteString(s.Condition.String())
  2340  	}
  2341  	if len(s.Dimensions) > 0 {
  2342  		_, _ = buf.WriteString(" GROUP BY ")
  2343  		_, _ = buf.WriteString(s.Dimensions.String())
  2344  	}
  2345  	if s.Limit > 0 {
  2346  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  2347  	}
  2348  	if s.Offset > 0 {
  2349  		_, _ = buf.WriteString(" OFFSET ")
  2350  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2351  	}
  2352  	return buf.String()
  2353  }
  2354  
  2355  // RequiredPrivileges returns the privilege required to execute a ShowSeriesCardinalityStatement.
  2356  func (s *ShowSeriesCardinalityStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2357  	if !s.Exact {
  2358  		return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2359  	}
  2360  	return s.Sources.RequiredPrivileges()
  2361  }
  2362  
  2363  // DefaultDatabase returns the default database from the statement.
  2364  func (s *ShowSeriesCardinalityStatement) DefaultDatabase() string {
  2365  	return s.Database
  2366  }
  2367  
  2368  // ShowContinuousQueriesStatement represents a command for listing continuous queries.
  2369  type ShowContinuousQueriesStatement struct{}
  2370  
  2371  // String returns a string representation of the show continuous queries statement.
  2372  func (s *ShowContinuousQueriesStatement) String() string { return "SHOW CONTINUOUS QUERIES" }
  2373  
  2374  // RequiredPrivileges returns the privilege required to execute a ShowContinuousQueriesStatement.
  2375  func (s *ShowContinuousQueriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2376  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil
  2377  }
  2378  
  2379  // ShowGrantsForUserStatement represents a command for listing user privileges.
  2380  type ShowGrantsForUserStatement struct {
  2381  	// Name of the user to display privileges.
  2382  	Name string
  2383  }
  2384  
  2385  // String returns a string representation of the show grants for user.
  2386  func (s *ShowGrantsForUserStatement) String() string {
  2387  	var buf bytes.Buffer
  2388  	_, _ = buf.WriteString("SHOW GRANTS FOR ")
  2389  	_, _ = buf.WriteString(QuoteIdent(s.Name))
  2390  
  2391  	return buf.String()
  2392  }
  2393  
  2394  // RequiredPrivileges returns the privilege required to execute a ShowGrantsForUserStatement
  2395  func (s *ShowGrantsForUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2396  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2397  }
  2398  
  2399  // ShowDatabasesStatement represents a command for listing all databases in the cluster.
  2400  type ShowDatabasesStatement struct{}
  2401  
  2402  // String returns a string representation of the show databases command.
  2403  func (s *ShowDatabasesStatement) String() string { return "SHOW DATABASES" }
  2404  
  2405  // RequiredPrivileges returns the privilege required to execute a ShowDatabasesStatement.
  2406  func (s *ShowDatabasesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2407  	// SHOW DATABASES is one of few statements that have no required privileges.
  2408  	// Anyone is allowed to execute it, but the returned results depend on the user's
  2409  	// individual database permissions.
  2410  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: NoPrivileges}}, nil
  2411  }
  2412  
  2413  // CreateContinuousQueryStatement represents a command for creating a continuous query.
  2414  type CreateContinuousQueryStatement struct {
  2415  	// Name of the continuous query to be created.
  2416  	Name string
  2417  
  2418  	// Name of the database to create the continuous query on.
  2419  	Database string
  2420  
  2421  	// Source of data (SELECT statement).
  2422  	Source *SelectStatement
  2423  
  2424  	// Interval to resample previous queries.
  2425  	ResampleEvery time.Duration
  2426  
  2427  	// Maximum duration to resample previous queries.
  2428  	ResampleFor time.Duration
  2429  }
  2430  
  2431  // String returns a string representation of the statement.
  2432  func (s *CreateContinuousQueryStatement) String() string {
  2433  	var buf bytes.Buffer
  2434  	fmt.Fprintf(&buf, "CREATE CONTINUOUS QUERY %s ON %s ", QuoteIdent(s.Name), QuoteIdent(s.Database))
  2435  
  2436  	if s.ResampleEvery > 0 || s.ResampleFor > 0 {
  2437  		buf.WriteString("RESAMPLE ")
  2438  		if s.ResampleEvery > 0 {
  2439  			fmt.Fprintf(&buf, "EVERY %s ", FormatDuration(s.ResampleEvery))
  2440  		}
  2441  		if s.ResampleFor > 0 {
  2442  			fmt.Fprintf(&buf, "FOR %s ", FormatDuration(s.ResampleFor))
  2443  		}
  2444  	}
  2445  	fmt.Fprintf(&buf, "BEGIN %s END", s.Source.String())
  2446  	return buf.String()
  2447  }
  2448  
  2449  // DefaultDatabase returns the default database from the statement.
  2450  func (s *CreateContinuousQueryStatement) DefaultDatabase() string {
  2451  	return s.Database
  2452  }
  2453  
  2454  // RequiredPrivileges returns the privilege required to execute a CreateContinuousQueryStatement.
  2455  func (s *CreateContinuousQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2456  	ep := ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}
  2457  
  2458  	// Selecting into a database that's different from the source?
  2459  	if s.Source.Target.Measurement.Database != "" {
  2460  		// Change source database privilege requirement to read.
  2461  		ep[0].Privilege = ReadPrivilege
  2462  
  2463  		// Add destination database privilege requirement and set it to write.
  2464  		p := ExecutionPrivilege{
  2465  			Admin:     false,
  2466  			Name:      s.Source.Target.Measurement.Database,
  2467  			Privilege: WritePrivilege,
  2468  		}
  2469  		ep = append(ep, p)
  2470  	}
  2471  
  2472  	return ep, nil
  2473  }
  2474  
  2475  func (s *CreateContinuousQueryStatement) validate() error {
  2476  	interval, err := s.Source.GroupByInterval()
  2477  	if err != nil {
  2478  		return err
  2479  	}
  2480  
  2481  	if s.ResampleFor != 0 {
  2482  		if s.ResampleEvery != 0 && s.ResampleEvery > interval {
  2483  			interval = s.ResampleEvery
  2484  		}
  2485  		if interval > s.ResampleFor {
  2486  			return fmt.Errorf("FOR duration must be >= GROUP BY time duration: must be a minimum of %s, got %s", FormatDuration(interval), FormatDuration(s.ResampleFor))
  2487  		}
  2488  	}
  2489  	return nil
  2490  }
  2491  
  2492  // DropContinuousQueryStatement represents a command for removing a continuous query.
  2493  type DropContinuousQueryStatement struct {
  2494  	Name     string
  2495  	Database string
  2496  }
  2497  
  2498  // String returns a string representation of the statement.
  2499  func (s *DropContinuousQueryStatement) String() string {
  2500  	return fmt.Sprintf("DROP CONTINUOUS QUERY %s ON %s", QuoteIdent(s.Name), QuoteIdent(s.Database))
  2501  }
  2502  
  2503  // RequiredPrivileges returns the privilege(s) required to execute a DropContinuousQueryStatement
  2504  func (s *DropContinuousQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2505  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: WritePrivilege}}, nil
  2506  }
  2507  
  2508  // DefaultDatabase returns the default database from the statement.
  2509  func (s *DropContinuousQueryStatement) DefaultDatabase() string {
  2510  	return s.Database
  2511  }
  2512  
  2513  // ShowMeasurementCardinalityStatement represents a command for listing measurement cardinality.
  2514  type ShowMeasurementCardinalityStatement struct {
  2515  	Exact         bool // If false then cardinality estimation will be used.
  2516  	Database      string
  2517  	Sources       Sources
  2518  	Condition     Expr
  2519  	Dimensions    Dimensions
  2520  	Limit, Offset int
  2521  }
  2522  
  2523  // String returns a string representation of the statement.
  2524  func (s *ShowMeasurementCardinalityStatement) String() string {
  2525  	var buf bytes.Buffer
  2526  	_, _ = buf.WriteString("SHOW MEASUREMENT")
  2527  
  2528  	if s.Exact {
  2529  		_, _ = buf.WriteString(" EXACT")
  2530  	}
  2531  	_, _ = buf.WriteString(" CARDINALITY")
  2532  
  2533  	if s.Database != "" {
  2534  		_, _ = buf.WriteString(" ON ")
  2535  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2536  	}
  2537  
  2538  	if s.Sources != nil {
  2539  		_, _ = buf.WriteString(" FROM ")
  2540  		_, _ = buf.WriteString(s.Sources.String())
  2541  	}
  2542  	if s.Condition != nil {
  2543  		_, _ = buf.WriteString(" WHERE ")
  2544  		_, _ = buf.WriteString(s.Condition.String())
  2545  	}
  2546  	if len(s.Dimensions) > 0 {
  2547  		_, _ = buf.WriteString(" GROUP BY ")
  2548  		_, _ = buf.WriteString(s.Dimensions.String())
  2549  	}
  2550  	if s.Limit > 0 {
  2551  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  2552  	}
  2553  	if s.Offset > 0 {
  2554  		_, _ = buf.WriteString(" OFFSET ")
  2555  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2556  	}
  2557  	return buf.String()
  2558  }
  2559  
  2560  // RequiredPrivileges returns the privilege required to execute a ShowMeasurementCardinalityStatement.
  2561  func (s *ShowMeasurementCardinalityStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2562  	if !s.Exact {
  2563  		return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2564  	}
  2565  	return s.Sources.RequiredPrivileges()
  2566  }
  2567  
  2568  // DefaultDatabase returns the default database from the statement.
  2569  func (s *ShowMeasurementCardinalityStatement) DefaultDatabase() string {
  2570  	return s.Database
  2571  }
  2572  
  2573  // ShowMeasurementsStatement represents a command for listing measurements.
  2574  type ShowMeasurementsStatement struct {
  2575  	// Database to query. If blank, use the default database.
  2576  	Database string
  2577  
  2578  	// Measurement name or regex.
  2579  	Source Source
  2580  
  2581  	// An expression evaluated on data point.
  2582  	Condition Expr
  2583  
  2584  	// Fields to sort results by
  2585  	SortFields SortFields
  2586  
  2587  	// Maximum number of rows to be returned.
  2588  	// Unlimited if zero.
  2589  	Limit int
  2590  
  2591  	// Returns rows starting at an offset from the first row.
  2592  	Offset int
  2593  }
  2594  
  2595  // String returns a string representation of the statement.
  2596  func (s *ShowMeasurementsStatement) String() string {
  2597  	var buf bytes.Buffer
  2598  	_, _ = buf.WriteString("SHOW MEASUREMENTS")
  2599  
  2600  	if s.Database != "" {
  2601  		_, _ = buf.WriteString(" ON ")
  2602  		_, _ = buf.WriteString(s.Database)
  2603  	}
  2604  	if s.Source != nil {
  2605  		_, _ = buf.WriteString(" WITH MEASUREMENT ")
  2606  		if m, ok := s.Source.(*Measurement); ok && m.Regex != nil {
  2607  			_, _ = buf.WriteString("=~ ")
  2608  		} else {
  2609  			_, _ = buf.WriteString("= ")
  2610  		}
  2611  		_, _ = buf.WriteString(s.Source.String())
  2612  	}
  2613  	if s.Condition != nil {
  2614  		_, _ = buf.WriteString(" WHERE ")
  2615  		_, _ = buf.WriteString(s.Condition.String())
  2616  	}
  2617  	if len(s.SortFields) > 0 {
  2618  		_, _ = buf.WriteString(" ORDER BY ")
  2619  		_, _ = buf.WriteString(s.SortFields.String())
  2620  	}
  2621  	if s.Limit > 0 {
  2622  		_, _ = buf.WriteString(" LIMIT ")
  2623  		_, _ = buf.WriteString(strconv.Itoa(s.Limit))
  2624  	}
  2625  	if s.Offset > 0 {
  2626  		_, _ = buf.WriteString(" OFFSET ")
  2627  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2628  	}
  2629  	return buf.String()
  2630  }
  2631  
  2632  // RequiredPrivileges returns the privilege(s) required to execute a ShowMeasurementsStatement.
  2633  func (s *ShowMeasurementsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2634  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2635  }
  2636  
  2637  // DefaultDatabase returns the default database from the statement.
  2638  func (s *ShowMeasurementsStatement) DefaultDatabase() string {
  2639  	return s.Database
  2640  }
  2641  
  2642  // DropMeasurementStatement represents a command to drop a measurement.
  2643  type DropMeasurementStatement struct {
  2644  	// Name of the measurement to be dropped.
  2645  	Name string
  2646  }
  2647  
  2648  // String returns a string representation of the drop measurement statement.
  2649  func (s *DropMeasurementStatement) String() string {
  2650  	var buf bytes.Buffer
  2651  	_, _ = buf.WriteString("DROP MEASUREMENT ")
  2652  	_, _ = buf.WriteString(QuoteIdent(s.Name))
  2653  	return buf.String()
  2654  }
  2655  
  2656  // RequiredPrivileges returns the privilege(s) required to execute a DropMeasurementStatement
  2657  func (s *DropMeasurementStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2658  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2659  }
  2660  
  2661  // ShowQueriesStatement represents a command for listing all running queries.
  2662  type ShowQueriesStatement struct{}
  2663  
  2664  // String returns a string representation of the show queries statement.
  2665  func (s *ShowQueriesStatement) String() string {
  2666  	return "SHOW QUERIES"
  2667  }
  2668  
  2669  // RequiredPrivileges returns the privilege required to execute a ShowQueriesStatement.
  2670  func (s *ShowQueriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2671  	return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil
  2672  }
  2673  
  2674  // ShowRetentionPoliciesStatement represents a command for listing retention policies.
  2675  type ShowRetentionPoliciesStatement struct {
  2676  	// Name of the database to list policies for.
  2677  	Database string
  2678  }
  2679  
  2680  // String returns a string representation of a ShowRetentionPoliciesStatement.
  2681  func (s *ShowRetentionPoliciesStatement) String() string {
  2682  	var buf bytes.Buffer
  2683  	_, _ = buf.WriteString("SHOW RETENTION POLICIES")
  2684  	if s.Database != "" {
  2685  		_, _ = buf.WriteString(" ON ")
  2686  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2687  	}
  2688  	return buf.String()
  2689  }
  2690  
  2691  // RequiredPrivileges returns the privilege(s) required to execute a ShowRetentionPoliciesStatement
  2692  func (s *ShowRetentionPoliciesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2693  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2694  }
  2695  
  2696  // DefaultDatabase returns the default database from the statement.
  2697  func (s *ShowRetentionPoliciesStatement) DefaultDatabase() string {
  2698  	return s.Database
  2699  }
  2700  
  2701  // ShowStatsStatement displays statistics for a given module.
  2702  type ShowStatsStatement struct {
  2703  	Module string
  2704  }
  2705  
  2706  // String returns a string representation of a ShowStatsStatement.
  2707  func (s *ShowStatsStatement) String() string {
  2708  	var buf bytes.Buffer
  2709  	_, _ = buf.WriteString("SHOW STATS")
  2710  	if s.Module != "" {
  2711  		_, _ = buf.WriteString(" FOR ")
  2712  		_, _ = buf.WriteString(QuoteString(s.Module))
  2713  	}
  2714  	return buf.String()
  2715  }
  2716  
  2717  // RequiredPrivileges returns the privilege(s) required to execute a ShowStatsStatement
  2718  func (s *ShowStatsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2719  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2720  }
  2721  
  2722  // ShowShardGroupsStatement represents a command for displaying shard groups in the cluster.
  2723  type ShowShardGroupsStatement struct{}
  2724  
  2725  // String returns a string representation of the SHOW SHARD GROUPS command.
  2726  func (s *ShowShardGroupsStatement) String() string { return "SHOW SHARD GROUPS" }
  2727  
  2728  // RequiredPrivileges returns the privileges required to execute the statement.
  2729  func (s *ShowShardGroupsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2730  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2731  }
  2732  
  2733  // ShowShardsStatement represents a command for displaying shards in the cluster.
  2734  type ShowShardsStatement struct{}
  2735  
  2736  // String returns a string representation.
  2737  func (s *ShowShardsStatement) String() string { return "SHOW SHARDS" }
  2738  
  2739  // RequiredPrivileges returns the privileges required to execute the statement.
  2740  func (s *ShowShardsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2741  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2742  }
  2743  
  2744  // ShowDiagnosticsStatement represents a command for show node diagnostics.
  2745  type ShowDiagnosticsStatement struct {
  2746  	// Module
  2747  	Module string
  2748  }
  2749  
  2750  // String returns a string representation of the ShowDiagnosticsStatement.
  2751  func (s *ShowDiagnosticsStatement) String() string {
  2752  	var buf bytes.Buffer
  2753  	_, _ = buf.WriteString("SHOW DIAGNOSTICS")
  2754  	if s.Module != "" {
  2755  		_, _ = buf.WriteString(" FOR ")
  2756  		_, _ = buf.WriteString(QuoteString(s.Module))
  2757  	}
  2758  	return buf.String()
  2759  }
  2760  
  2761  // RequiredPrivileges returns the privilege required to execute a ShowDiagnosticsStatement
  2762  func (s *ShowDiagnosticsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2763  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2764  }
  2765  
  2766  // CreateSubscriptionStatement represents a command to add a subscription to the incoming data stream.
  2767  type CreateSubscriptionStatement struct {
  2768  	Name            string
  2769  	Database        string
  2770  	RetentionPolicy string
  2771  	Destinations    []string
  2772  	Mode            string
  2773  }
  2774  
  2775  // String returns a string representation of the CreateSubscriptionStatement.
  2776  func (s *CreateSubscriptionStatement) String() string {
  2777  	var buf bytes.Buffer
  2778  	_, _ = buf.WriteString("CREATE SUBSCRIPTION ")
  2779  	_, _ = buf.WriteString(QuoteIdent(s.Name))
  2780  	_, _ = buf.WriteString(" ON ")
  2781  	_, _ = buf.WriteString(QuoteIdent(s.Database))
  2782  	_, _ = buf.WriteString(".")
  2783  	_, _ = buf.WriteString(QuoteIdent(s.RetentionPolicy))
  2784  	_, _ = buf.WriteString(" DESTINATIONS ")
  2785  	_, _ = buf.WriteString(s.Mode)
  2786  	_, _ = buf.WriteString(" ")
  2787  	for i, dest := range s.Destinations {
  2788  		if i != 0 {
  2789  			_, _ = buf.WriteString(", ")
  2790  		}
  2791  		_, _ = buf.WriteString(QuoteString(dest))
  2792  	}
  2793  
  2794  	return buf.String()
  2795  }
  2796  
  2797  // RequiredPrivileges returns the privilege required to execute a CreateSubscriptionStatement.
  2798  func (s *CreateSubscriptionStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2799  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2800  }
  2801  
  2802  // DefaultDatabase returns the default database from the statement.
  2803  func (s *CreateSubscriptionStatement) DefaultDatabase() string {
  2804  	return s.Database
  2805  }
  2806  
  2807  // DropSubscriptionStatement represents a command to drop a subscription to the incoming data stream.
  2808  type DropSubscriptionStatement struct {
  2809  	Name            string
  2810  	Database        string
  2811  	RetentionPolicy string
  2812  }
  2813  
  2814  // String returns a string representation of the DropSubscriptionStatement.
  2815  func (s *DropSubscriptionStatement) String() string {
  2816  	return fmt.Sprintf(`DROP SUBSCRIPTION %s ON %s.%s`, QuoteIdent(s.Name), QuoteIdent(s.Database), QuoteIdent(s.RetentionPolicy))
  2817  }
  2818  
  2819  // RequiredPrivileges returns the privilege required to execute a DropSubscriptionStatement
  2820  func (s *DropSubscriptionStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2821  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2822  }
  2823  
  2824  // DefaultDatabase returns the default database from the statement.
  2825  func (s *DropSubscriptionStatement) DefaultDatabase() string {
  2826  	return s.Database
  2827  }
  2828  
  2829  // ShowSubscriptionsStatement represents a command to show a list of subscriptions.
  2830  type ShowSubscriptionsStatement struct {
  2831  }
  2832  
  2833  // String returns a string representation of the ShowSubscriptionsStatement.
  2834  func (s *ShowSubscriptionsStatement) String() string {
  2835  	return "SHOW SUBSCRIPTIONS"
  2836  }
  2837  
  2838  // RequiredPrivileges returns the privilege required to execute a ShowSubscriptionsStatement.
  2839  func (s *ShowSubscriptionsStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2840  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  2841  }
  2842  
  2843  // ShowTagKeysStatement represents a command for listing tag keys.
  2844  type ShowTagKeysStatement struct {
  2845  	// Database to query. If blank, use the default database.
  2846  	// The database can also be specified per source in the Sources.
  2847  	Database string
  2848  
  2849  	// Data sources that fields are extracted from.
  2850  	Sources Sources
  2851  
  2852  	// An expression evaluated on data point.
  2853  	Condition Expr
  2854  
  2855  	// Fields to sort results by.
  2856  	SortFields SortFields
  2857  
  2858  	// Maximum number of tag keys per measurement. Unlimited if zero.
  2859  	Limit int
  2860  
  2861  	// Returns tag keys starting at an offset from the first row.
  2862  	Offset int
  2863  
  2864  	// Maxiumum number of series to be returned. Unlimited if zero.
  2865  	SLimit int
  2866  
  2867  	// Returns series starting at an offset from the first one.
  2868  	SOffset int
  2869  }
  2870  
  2871  // String returns a string representation of the statement.
  2872  func (s *ShowTagKeysStatement) String() string {
  2873  	var buf bytes.Buffer
  2874  	_, _ = buf.WriteString("SHOW TAG KEYS")
  2875  
  2876  	if s.Database != "" {
  2877  		_, _ = buf.WriteString(" ON ")
  2878  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2879  	}
  2880  	if s.Sources != nil {
  2881  		_, _ = buf.WriteString(" FROM ")
  2882  		_, _ = buf.WriteString(s.Sources.String())
  2883  	}
  2884  	if s.Condition != nil {
  2885  		_, _ = buf.WriteString(" WHERE ")
  2886  		_, _ = buf.WriteString(s.Condition.String())
  2887  	}
  2888  	if len(s.SortFields) > 0 {
  2889  		_, _ = buf.WriteString(" ORDER BY ")
  2890  		_, _ = buf.WriteString(s.SortFields.String())
  2891  	}
  2892  	if s.Limit > 0 {
  2893  		_, _ = buf.WriteString(" LIMIT ")
  2894  		_, _ = buf.WriteString(strconv.Itoa(s.Limit))
  2895  	}
  2896  	if s.Offset > 0 {
  2897  		_, _ = buf.WriteString(" OFFSET ")
  2898  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2899  	}
  2900  	if s.SLimit > 0 {
  2901  		_, _ = buf.WriteString(" SLIMIT ")
  2902  		_, _ = buf.WriteString(strconv.Itoa(s.SLimit))
  2903  	}
  2904  	if s.SOffset > 0 {
  2905  		_, _ = buf.WriteString(" SOFFSET ")
  2906  		_, _ = buf.WriteString(strconv.Itoa(s.SOffset))
  2907  	}
  2908  	return buf.String()
  2909  }
  2910  
  2911  // RequiredPrivileges returns the privilege(s) required to execute a ShowTagKeysStatement.
  2912  func (s *ShowTagKeysStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2913  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  2914  }
  2915  
  2916  // DefaultDatabase returns the default database from the statement.
  2917  func (s *ShowTagKeysStatement) DefaultDatabase() string {
  2918  	return s.Database
  2919  }
  2920  
  2921  // ShowTagKeyCardinalityStatement represents a command for listing tag key cardinality.
  2922  type ShowTagKeyCardinalityStatement struct {
  2923  	Database      string
  2924  	Exact         bool
  2925  	Sources       Sources
  2926  	Condition     Expr
  2927  	Dimensions    Dimensions
  2928  	Limit, Offset int
  2929  }
  2930  
  2931  // String returns a string representation of the statement.
  2932  func (s *ShowTagKeyCardinalityStatement) String() string {
  2933  	var buf bytes.Buffer
  2934  	_, _ = buf.WriteString("SHOW TAG KEY ")
  2935  	if s.Exact {
  2936  		_, _ = buf.WriteString("EXACT ")
  2937  	}
  2938  	_, _ = buf.WriteString("CARDINALITY")
  2939  
  2940  	if s.Database != "" {
  2941  		_, _ = buf.WriteString(" ON ")
  2942  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  2943  	}
  2944  	if s.Sources != nil {
  2945  		_, _ = buf.WriteString(" FROM ")
  2946  		_, _ = buf.WriteString(s.Sources.String())
  2947  	}
  2948  	if s.Condition != nil {
  2949  		_, _ = buf.WriteString(" WHERE ")
  2950  		_, _ = buf.WriteString(s.Condition.String())
  2951  	}
  2952  	if len(s.Dimensions) > 0 {
  2953  		_, _ = buf.WriteString(" GROUP BY ")
  2954  		_, _ = buf.WriteString(s.Dimensions.String())
  2955  	}
  2956  	if s.Limit > 0 {
  2957  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  2958  	}
  2959  	if s.Offset > 0 {
  2960  		_, _ = buf.WriteString(" OFFSET ")
  2961  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  2962  	}
  2963  	return buf.String()
  2964  }
  2965  
  2966  // RequiredPrivileges returns the privilege required to execute a ShowTagKeyCardinalityStatement.
  2967  func (s *ShowTagKeyCardinalityStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  2968  	return s.Sources.RequiredPrivileges()
  2969  }
  2970  
  2971  // DefaultDatabase returns the default database from the statement.
  2972  func (s *ShowTagKeyCardinalityStatement) DefaultDatabase() string {
  2973  	return s.Database
  2974  }
  2975  
  2976  // ShowTagValuesStatement represents a command for listing tag values.
  2977  type ShowTagValuesStatement struct {
  2978  	// Database to query. If blank, use the default database.
  2979  	// The database can also be specified per source in the Sources.
  2980  	Database string
  2981  
  2982  	// Data source that fields are extracted from.
  2983  	Sources Sources
  2984  
  2985  	// Operation to use when selecting tag key(s).
  2986  	Op Token
  2987  
  2988  	// Literal to compare the tag key(s) with.
  2989  	TagKeyExpr Literal
  2990  
  2991  	// An expression evaluated on data point.
  2992  	Condition Expr
  2993  
  2994  	// Fields to sort results by.
  2995  	SortFields SortFields
  2996  
  2997  	// Maximum number of rows to be returned.
  2998  	// Unlimited if zero.
  2999  	Limit int
  3000  
  3001  	// Returns rows starting at an offset from the first row.
  3002  	Offset int
  3003  }
  3004  
  3005  // String returns a string representation of the statement.
  3006  func (s *ShowTagValuesStatement) String() string {
  3007  	var buf bytes.Buffer
  3008  	_, _ = buf.WriteString("SHOW TAG VALUES")
  3009  
  3010  	if s.Database != "" {
  3011  		_, _ = buf.WriteString(" ON ")
  3012  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  3013  	}
  3014  	if s.Sources != nil {
  3015  		_, _ = buf.WriteString(" FROM ")
  3016  		_, _ = buf.WriteString(s.Sources.String())
  3017  	}
  3018  	_, _ = buf.WriteString(" WITH KEY ")
  3019  	_, _ = buf.WriteString(s.Op.String())
  3020  	_, _ = buf.WriteString(" ")
  3021  	if lit, ok := s.TagKeyExpr.(*StringLiteral); ok {
  3022  		_, _ = buf.WriteString(QuoteIdent(lit.Val))
  3023  	} else {
  3024  		_, _ = buf.WriteString(s.TagKeyExpr.String())
  3025  	}
  3026  	if s.Condition != nil {
  3027  		_, _ = buf.WriteString(" WHERE ")
  3028  		_, _ = buf.WriteString(s.Condition.String())
  3029  	}
  3030  	if len(s.SortFields) > 0 {
  3031  		_, _ = buf.WriteString(" ORDER BY ")
  3032  		_, _ = buf.WriteString(s.SortFields.String())
  3033  	}
  3034  	if s.Limit > 0 {
  3035  		_, _ = buf.WriteString(" LIMIT ")
  3036  		_, _ = buf.WriteString(strconv.Itoa(s.Limit))
  3037  	}
  3038  	if s.Offset > 0 {
  3039  		_, _ = buf.WriteString(" OFFSET ")
  3040  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  3041  	}
  3042  	return buf.String()
  3043  }
  3044  
  3045  // RequiredPrivileges returns the privilege(s) required to execute a ShowTagValuesStatement.
  3046  func (s *ShowTagValuesStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  3047  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  3048  }
  3049  
  3050  // DefaultDatabase returns the default database from the statement.
  3051  func (s *ShowTagValuesStatement) DefaultDatabase() string {
  3052  	return s.Database
  3053  }
  3054  
  3055  // ShowTagValuesCardinalityStatement represents a command for listing tag value cardinality.
  3056  type ShowTagValuesCardinalityStatement struct {
  3057  	Database      string
  3058  	Exact         bool
  3059  	Sources       Sources
  3060  	Op            Token
  3061  	TagKeyExpr    Literal
  3062  	Condition     Expr
  3063  	Dimensions    Dimensions
  3064  	Limit, Offset int
  3065  }
  3066  
  3067  // String returns a string representation of the statement.
  3068  func (s *ShowTagValuesCardinalityStatement) String() string {
  3069  	var buf bytes.Buffer
  3070  	_, _ = buf.WriteString("SHOW TAG VALUES ")
  3071  	if s.Exact {
  3072  		_, _ = buf.WriteString("EXACT ")
  3073  	}
  3074  	_, _ = buf.WriteString("CARDINALITY")
  3075  
  3076  	if s.Database != "" {
  3077  		_, _ = buf.WriteString(" ON ")
  3078  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  3079  	}
  3080  	if s.Sources != nil {
  3081  		_, _ = buf.WriteString(" FROM ")
  3082  		_, _ = buf.WriteString(s.Sources.String())
  3083  	}
  3084  	_, _ = buf.WriteString(" WITH KEY ")
  3085  	_, _ = buf.WriteString(s.Op.String())
  3086  	_, _ = buf.WriteString(" ")
  3087  	if lit, ok := s.TagKeyExpr.(*StringLiteral); ok {
  3088  		_, _ = buf.WriteString(QuoteIdent(lit.Val))
  3089  	} else {
  3090  		_, _ = buf.WriteString(s.TagKeyExpr.String())
  3091  	}
  3092  	if s.Condition != nil {
  3093  		_, _ = buf.WriteString(" WHERE ")
  3094  		_, _ = buf.WriteString(s.Condition.String())
  3095  	}
  3096  	if len(s.Dimensions) > 0 {
  3097  		_, _ = buf.WriteString(" GROUP BY ")
  3098  		_, _ = buf.WriteString(s.Dimensions.String())
  3099  	}
  3100  	if s.Limit > 0 {
  3101  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  3102  	}
  3103  	if s.Offset > 0 {
  3104  		_, _ = buf.WriteString(" OFFSET ")
  3105  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  3106  	}
  3107  	return buf.String()
  3108  }
  3109  
  3110  // RequiredPrivileges returns the privilege required to execute a ShowTagValuesCardinalityStatement.
  3111  func (s *ShowTagValuesCardinalityStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  3112  	return s.Sources.RequiredPrivileges()
  3113  }
  3114  
  3115  // DefaultDatabase returns the default database from the statement.
  3116  func (s *ShowTagValuesCardinalityStatement) DefaultDatabase() string {
  3117  	return s.Database
  3118  }
  3119  
  3120  // ShowUsersStatement represents a command for listing users.
  3121  type ShowUsersStatement struct{}
  3122  
  3123  // String returns a string representation of the ShowUsersStatement.
  3124  func (s *ShowUsersStatement) String() string {
  3125  	return "SHOW USERS"
  3126  }
  3127  
  3128  // RequiredPrivileges returns the privilege(s) required to execute a ShowUsersStatement
  3129  func (s *ShowUsersStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  3130  	return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil
  3131  }
  3132  
  3133  // ShowFieldKeyCardinalityStatement represents a command for listing field key cardinality.
  3134  type ShowFieldKeyCardinalityStatement struct {
  3135  	Database      string
  3136  	Exact         bool
  3137  	Sources       Sources
  3138  	Condition     Expr
  3139  	Dimensions    Dimensions
  3140  	Limit, Offset int
  3141  }
  3142  
  3143  // String returns a string representation of the statement.
  3144  func (s *ShowFieldKeyCardinalityStatement) String() string {
  3145  	var buf bytes.Buffer
  3146  	_, _ = buf.WriteString("SHOW FIELD KEY ")
  3147  
  3148  	if s.Exact {
  3149  		_, _ = buf.WriteString("EXACT ")
  3150  	}
  3151  	_, _ = buf.WriteString("CARDINALITY")
  3152  
  3153  	if s.Database != "" {
  3154  		_, _ = buf.WriteString(" ON ")
  3155  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  3156  	}
  3157  	if s.Sources != nil {
  3158  		_, _ = buf.WriteString(" FROM ")
  3159  		_, _ = buf.WriteString(s.Sources.String())
  3160  	}
  3161  	if s.Condition != nil {
  3162  		_, _ = buf.WriteString(" WHERE ")
  3163  		_, _ = buf.WriteString(s.Condition.String())
  3164  	}
  3165  	if len(s.Dimensions) > 0 {
  3166  		_, _ = buf.WriteString(" GROUP BY ")
  3167  		_, _ = buf.WriteString(s.Dimensions.String())
  3168  	}
  3169  	if s.Limit > 0 {
  3170  		_, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit)
  3171  	}
  3172  	if s.Offset > 0 {
  3173  		_, _ = buf.WriteString(" OFFSET ")
  3174  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  3175  	}
  3176  	return buf.String()
  3177  }
  3178  
  3179  // RequiredPrivileges returns the privilege required to execute a ShowFieldKeyCardinalityStatement.
  3180  func (s *ShowFieldKeyCardinalityStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  3181  	return s.Sources.RequiredPrivileges()
  3182  }
  3183  
  3184  // DefaultDatabase returns the default database from the statement.
  3185  func (s *ShowFieldKeyCardinalityStatement) DefaultDatabase() string {
  3186  	return s.Database
  3187  }
  3188  
  3189  // ShowFieldKeysStatement represents a command for listing field keys.
  3190  type ShowFieldKeysStatement struct {
  3191  	// Database to query. If blank, use the default database.
  3192  	// The database can also be specified per source in the Sources.
  3193  	Database string
  3194  
  3195  	// Data sources that fields are extracted from.
  3196  	Sources Sources
  3197  
  3198  	// Fields to sort results by
  3199  	SortFields SortFields
  3200  
  3201  	// Maximum number of rows to be returned.
  3202  	// Unlimited if zero.
  3203  	Limit int
  3204  
  3205  	// Returns rows starting at an offset from the first row.
  3206  	Offset int
  3207  }
  3208  
  3209  // String returns a string representation of the statement.
  3210  func (s *ShowFieldKeysStatement) String() string {
  3211  	var buf bytes.Buffer
  3212  	_, _ = buf.WriteString("SHOW FIELD KEYS")
  3213  
  3214  	if s.Database != "" {
  3215  		_, _ = buf.WriteString(" ON ")
  3216  		_, _ = buf.WriteString(QuoteIdent(s.Database))
  3217  	}
  3218  	if s.Sources != nil {
  3219  		_, _ = buf.WriteString(" FROM ")
  3220  		_, _ = buf.WriteString(s.Sources.String())
  3221  	}
  3222  	if len(s.SortFields) > 0 {
  3223  		_, _ = buf.WriteString(" ORDER BY ")
  3224  		_, _ = buf.WriteString(s.SortFields.String())
  3225  	}
  3226  	if s.Limit > 0 {
  3227  		_, _ = buf.WriteString(" LIMIT ")
  3228  		_, _ = buf.WriteString(strconv.Itoa(s.Limit))
  3229  	}
  3230  	if s.Offset > 0 {
  3231  		_, _ = buf.WriteString(" OFFSET ")
  3232  		_, _ = buf.WriteString(strconv.Itoa(s.Offset))
  3233  	}
  3234  	return buf.String()
  3235  }
  3236  
  3237  // RequiredPrivileges returns the privilege(s) required to execute a ShowFieldKeysStatement.
  3238  func (s *ShowFieldKeysStatement) RequiredPrivileges() (ExecutionPrivileges, error) {
  3239  	return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}}, nil
  3240  }
  3241  
  3242  // DefaultDatabase returns the default database from the statement.
  3243  func (s *ShowFieldKeysStatement) DefaultDatabase() string {
  3244  	return s.Database
  3245  }
  3246  
  3247  // Fields represents a list of fields.
  3248  type Fields []*Field
  3249  
  3250  // AliasNames returns a list of calculated field names in
  3251  // order of alias, function name, then field.
  3252  func (a Fields) AliasNames() []string {
  3253  	names := []string{}
  3254  	for _, f := range a {
  3255  		names = append(names, f.Name())
  3256  	}
  3257  	return names
  3258  }
  3259  
  3260  // Names returns a list of field names.
  3261  func (a Fields) Names() []string {
  3262  	names := []string{}
  3263  	for _, f := range a {
  3264  		switch expr := f.Expr.(type) {
  3265  		case *Call:
  3266  			names = append(names, expr.Name)
  3267  		case *VarRef:
  3268  			names = append(names, expr.Val)
  3269  		case *BinaryExpr:
  3270  			names = append(names, walkNames(expr)...)
  3271  		case *ParenExpr:
  3272  			names = append(names, walkNames(expr)...)
  3273  		}
  3274  	}
  3275  	return names
  3276  }
  3277  
  3278  // String returns a string representation of the fields.
  3279  func (a Fields) String() string {
  3280  	var str []string
  3281  	for _, f := range a {
  3282  		str = append(str, f.String())
  3283  	}
  3284  	return strings.Join(str, ", ")
  3285  }
  3286  
  3287  // Field represents an expression retrieved from a select statement.
  3288  type Field struct {
  3289  	Expr  Expr
  3290  	Alias string
  3291  }
  3292  
  3293  // Name returns the name of the field. Returns alias, if set.
  3294  // Otherwise uses the function name or variable name.
  3295  func (f *Field) Name() string {
  3296  	// Return alias, if set.
  3297  	if f.Alias != "" {
  3298  		return f.Alias
  3299  	}
  3300  
  3301  	// Return the function name or variable name, if available.
  3302  	switch expr := f.Expr.(type) {
  3303  	case *Call:
  3304  		return expr.Name
  3305  	case *BinaryExpr:
  3306  		return BinaryExprName(expr)
  3307  	case *ParenExpr:
  3308  		f := Field{Expr: expr.Expr}
  3309  		return f.Name()
  3310  	case *VarRef:
  3311  		return expr.Val
  3312  	}
  3313  
  3314  	// Otherwise return a blank name.
  3315  	return ""
  3316  }
  3317  
  3318  // String returns a string representation of the field.
  3319  func (f *Field) String() string {
  3320  	str := f.Expr.String()
  3321  
  3322  	if f.Alias == "" {
  3323  		return str
  3324  	}
  3325  	return fmt.Sprintf("%s AS %s", str, QuoteIdent(f.Alias))
  3326  }
  3327  
  3328  // Len implements sort.Interface.
  3329  func (a Fields) Len() int { return len(a) }
  3330  
  3331  // Less implements sort.Interface.
  3332  func (a Fields) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
  3333  
  3334  // Swap implements sort.Interface.
  3335  func (a Fields) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  3336  
  3337  // Dimensions represents a list of dimensions.
  3338  type Dimensions []*Dimension
  3339  
  3340  // String returns a string representation of the dimensions.
  3341  func (a Dimensions) String() string {
  3342  	var str []string
  3343  	for _, d := range a {
  3344  		str = append(str, d.String())
  3345  	}
  3346  	return strings.Join(str, ", ")
  3347  }
  3348  
  3349  // Normalize returns the interval and tag dimensions separately.
  3350  // Returns 0 if no time interval is specified.
  3351  func (a Dimensions) Normalize() (time.Duration, []string) {
  3352  	var dur time.Duration
  3353  	var tags []string
  3354  
  3355  	for _, dim := range a {
  3356  		switch expr := dim.Expr.(type) {
  3357  		case *Call:
  3358  			lit, _ := expr.Args[0].(*DurationLiteral)
  3359  			dur = lit.Val
  3360  		case *VarRef:
  3361  			tags = append(tags, expr.Val)
  3362  		}
  3363  	}
  3364  
  3365  	return dur, tags
  3366  }
  3367  
  3368  // Dimension represents an expression that a select statement is grouped by.
  3369  type Dimension struct {
  3370  	Expr Expr
  3371  }
  3372  
  3373  // String returns a string representation of the dimension.
  3374  func (d *Dimension) String() string { return d.Expr.String() }
  3375  
  3376  // Measurements represents a list of measurements.
  3377  type Measurements []*Measurement
  3378  
  3379  // String returns a string representation of the measurements.
  3380  func (a Measurements) String() string {
  3381  	var str []string
  3382  	for _, m := range a {
  3383  		str = append(str, m.String())
  3384  	}
  3385  	return strings.Join(str, ", ")
  3386  }
  3387  
  3388  // Measurement represents a single measurement used as a datasource.
  3389  type Measurement struct {
  3390  	Database        string
  3391  	RetentionPolicy string
  3392  	Name            string
  3393  	Regex           *RegexLiteral
  3394  	IsTarget        bool
  3395  
  3396  	// This field indicates that the measurement should read be read from the
  3397  	// specified system iterator.
  3398  	SystemIterator string
  3399  }
  3400  
  3401  // Clone returns a deep clone of the Measurement.
  3402  func (m *Measurement) Clone() *Measurement {
  3403  	var regexp *RegexLiteral
  3404  	if m.Regex != nil && m.Regex.Val != nil {
  3405  		regexp = &RegexLiteral{Val: m.Regex.Val.Copy()}
  3406  	}
  3407  	return &Measurement{
  3408  		Database:        m.Database,
  3409  		RetentionPolicy: m.RetentionPolicy,
  3410  		Name:            m.Name,
  3411  		Regex:           regexp,
  3412  		IsTarget:        m.IsTarget,
  3413  		SystemIterator:  m.SystemIterator,
  3414  	}
  3415  }
  3416  
  3417  // String returns a string representation of the measurement.
  3418  func (m *Measurement) String() string {
  3419  	var buf bytes.Buffer
  3420  	if m.Database != "" {
  3421  		_, _ = buf.WriteString(QuoteIdent(m.Database))
  3422  		_, _ = buf.WriteString(".")
  3423  	}
  3424  
  3425  	if m.RetentionPolicy != "" {
  3426  		_, _ = buf.WriteString(QuoteIdent(m.RetentionPolicy))
  3427  	}
  3428  
  3429  	if m.Database != "" || m.RetentionPolicy != "" {
  3430  		_, _ = buf.WriteString(`.`)
  3431  	}
  3432  
  3433  	if m.Name != "" && m.SystemIterator == "" {
  3434  		_, _ = buf.WriteString(QuoteIdent(m.Name))
  3435  	} else if m.SystemIterator != "" {
  3436  		_, _ = buf.WriteString(QuoteIdent(m.SystemIterator))
  3437  	} else if m.Regex != nil {
  3438  		_, _ = buf.WriteString(m.Regex.String())
  3439  	}
  3440  
  3441  	return buf.String()
  3442  }
  3443  
  3444  // SubQuery is a source with a SelectStatement as the backing store.
  3445  type SubQuery struct {
  3446  	Statement *SelectStatement
  3447  }
  3448  
  3449  // String returns a string representation of the subquery.
  3450  func (s *SubQuery) String() string {
  3451  	return fmt.Sprintf("(%s)", s.Statement.String())
  3452  }
  3453  
  3454  // VarRef represents a reference to a variable.
  3455  type VarRef struct {
  3456  	Val  string
  3457  	Type DataType
  3458  }
  3459  
  3460  // String returns a string representation of the variable reference.
  3461  func (r *VarRef) String() string {
  3462  	buf := bytes.NewBufferString(QuoteIdent(r.Val))
  3463  	if r.Type != Unknown {
  3464  		buf.WriteString("::")
  3465  		buf.WriteString(r.Type.String())
  3466  	}
  3467  	return buf.String()
  3468  }
  3469  
  3470  // VarRefs represents a slice of VarRef types.
  3471  type VarRefs []VarRef
  3472  
  3473  // Len implements sort.Interface.
  3474  func (a VarRefs) Len() int { return len(a) }
  3475  
  3476  // Less implements sort.Interface.
  3477  func (a VarRefs) Less(i, j int) bool {
  3478  	if a[i].Val != a[j].Val {
  3479  		return a[i].Val < a[j].Val
  3480  	}
  3481  	return a[i].Type < a[j].Type
  3482  }
  3483  
  3484  // Swap implements sort.Interface.
  3485  func (a VarRefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  3486  
  3487  // Strings returns a slice of the variable names.
  3488  func (a VarRefs) Strings() []string {
  3489  	s := make([]string, len(a))
  3490  	for i, ref := range a {
  3491  		s[i] = ref.Val
  3492  	}
  3493  	return s
  3494  }
  3495  
  3496  // Call represents a function call.
  3497  type Call struct {
  3498  	Name string
  3499  	Args []Expr
  3500  }
  3501  
  3502  // String returns a string representation of the call.
  3503  func (c *Call) String() string {
  3504  	// Join arguments.
  3505  	var str []string
  3506  	for _, arg := range c.Args {
  3507  		str = append(str, arg.String())
  3508  	}
  3509  
  3510  	// Write function name and args.
  3511  	return fmt.Sprintf("%s(%s)", c.Name, strings.Join(str, ", "))
  3512  }
  3513  
  3514  // Distinct represents a DISTINCT expression.
  3515  type Distinct struct {
  3516  	// Identifier following DISTINCT
  3517  	Val string
  3518  }
  3519  
  3520  // String returns a string representation of the expression.
  3521  func (d *Distinct) String() string {
  3522  	return fmt.Sprintf("DISTINCT %s", d.Val)
  3523  }
  3524  
  3525  // NewCall returns a new call expression from this expressions.
  3526  func (d *Distinct) NewCall() *Call {
  3527  	return &Call{
  3528  		Name: "distinct",
  3529  		Args: []Expr{
  3530  			&VarRef{Val: d.Val},
  3531  		},
  3532  	}
  3533  }
  3534  
  3535  // NumberLiteral represents a numeric literal.
  3536  type NumberLiteral struct {
  3537  	Val float64
  3538  }
  3539  
  3540  // String returns a string representation of the literal.
  3541  func (l *NumberLiteral) String() string { return strconv.FormatFloat(l.Val, 'f', 3, 64) }
  3542  
  3543  // IntegerLiteral represents an integer literal.
  3544  type IntegerLiteral struct {
  3545  	Val int64
  3546  }
  3547  
  3548  // String returns a string representation of the literal.
  3549  func (l *IntegerLiteral) String() string { return fmt.Sprintf("%d", l.Val) }
  3550  
  3551  // UnsignedLiteral represents an unsigned literal. The parser will only use an unsigned literal if the parsed
  3552  // integer is greater than math.MaxInt64.
  3553  type UnsignedLiteral struct {
  3554  	Val uint64
  3555  }
  3556  
  3557  // String returns a string representation of the literal.
  3558  func (l *UnsignedLiteral) String() string { return strconv.FormatUint(l.Val, 10) }
  3559  
  3560  // BooleanLiteral represents a boolean literal.
  3561  type BooleanLiteral struct {
  3562  	Val bool
  3563  }
  3564  
  3565  // String returns a string representation of the literal.
  3566  func (l *BooleanLiteral) String() string {
  3567  	if l.Val {
  3568  		return "true"
  3569  	}
  3570  	return "false"
  3571  }
  3572  
  3573  // isTrueLiteral returns true if the expression is a literal "true" value.
  3574  func isTrueLiteral(expr Expr) bool {
  3575  	if expr, ok := expr.(*BooleanLiteral); ok {
  3576  		return expr.Val == true
  3577  	}
  3578  	return false
  3579  }
  3580  
  3581  // isFalseLiteral returns true if the expression is a literal "false" value.
  3582  func isFalseLiteral(expr Expr) bool {
  3583  	if expr, ok := expr.(*BooleanLiteral); ok {
  3584  		return expr.Val == false
  3585  	}
  3586  	return false
  3587  }
  3588  
  3589  // ListLiteral represents a list of tag key literals.
  3590  type ListLiteral struct {
  3591  	Vals []string
  3592  }
  3593  
  3594  // String returns a string representation of the literal.
  3595  func (s *ListLiteral) String() string {
  3596  	var buf bytes.Buffer
  3597  	_, _ = buf.WriteString("(")
  3598  	for idx, tagKey := range s.Vals {
  3599  		if idx != 0 {
  3600  			_, _ = buf.WriteString(", ")
  3601  		}
  3602  		_, _ = buf.WriteString(QuoteIdent(tagKey))
  3603  	}
  3604  	_, _ = buf.WriteString(")")
  3605  	return buf.String()
  3606  }
  3607  
  3608  // StringLiteral represents a string literal.
  3609  type StringLiteral struct {
  3610  	Val string
  3611  }
  3612  
  3613  // String returns a string representation of the literal.
  3614  func (l *StringLiteral) String() string { return QuoteString(l.Val) }
  3615  
  3616  // IsTimeLiteral returns if this string can be interpreted as a time literal.
  3617  func (l *StringLiteral) IsTimeLiteral() bool {
  3618  	return isDateTimeString(l.Val) || isDateString(l.Val)
  3619  }
  3620  
  3621  // ToTimeLiteral returns a time literal if this string can be converted to a time literal.
  3622  func (l *StringLiteral) ToTimeLiteral(loc *time.Location) (*TimeLiteral, error) {
  3623  	if loc == nil {
  3624  		loc = time.UTC
  3625  	}
  3626  
  3627  	if isDateTimeString(l.Val) {
  3628  		t, err := time.ParseInLocation(DateTimeFormat, l.Val, loc)
  3629  		if err != nil {
  3630  			// try to parse it as an RFCNano time
  3631  			t, err = time.ParseInLocation(time.RFC3339Nano, l.Val, loc)
  3632  			if err != nil {
  3633  				return nil, ErrInvalidTime
  3634  			}
  3635  		}
  3636  		return &TimeLiteral{Val: t}, nil
  3637  	} else if isDateString(l.Val) {
  3638  		t, err := time.ParseInLocation(DateFormat, l.Val, loc)
  3639  		if err != nil {
  3640  			return nil, ErrInvalidTime
  3641  		}
  3642  		return &TimeLiteral{Val: t}, nil
  3643  	}
  3644  	return nil, ErrInvalidTime
  3645  }
  3646  
  3647  // TimeLiteral represents a point-in-time literal.
  3648  type TimeLiteral struct {
  3649  	Val time.Time
  3650  }
  3651  
  3652  // String returns a string representation of the literal.
  3653  func (l *TimeLiteral) String() string {
  3654  	return `'` + l.Val.UTC().Format(time.RFC3339Nano) + `'`
  3655  }
  3656  
  3657  // DurationLiteral represents a duration literal.
  3658  type DurationLiteral struct {
  3659  	Val time.Duration
  3660  }
  3661  
  3662  // String returns a string representation of the literal.
  3663  func (l *DurationLiteral) String() string { return FormatDuration(l.Val) }
  3664  
  3665  // NilLiteral represents a nil literal.
  3666  // This is not available to the query language itself. It's only used internally.
  3667  type NilLiteral struct{}
  3668  
  3669  // String returns a string representation of the literal.
  3670  func (l *NilLiteral) String() string { return `nil` }
  3671  
  3672  // BoundParameter represents a bound parameter literal.
  3673  // This is not available to the query language itself, but can be used when
  3674  // constructing a query string from an AST.
  3675  type BoundParameter struct {
  3676  	Name string
  3677  }
  3678  
  3679  // String returns a string representation of the bound parameter.
  3680  func (bp *BoundParameter) String() string {
  3681  	return fmt.Sprintf("$%s", QuoteIdent(bp.Name))
  3682  }
  3683  
  3684  // BinaryExpr represents an operation between two expressions.
  3685  type BinaryExpr struct {
  3686  	Op  Token
  3687  	LHS Expr
  3688  	RHS Expr
  3689  }
  3690  
  3691  // String returns a string representation of the binary expression.
  3692  func (e *BinaryExpr) String() string {
  3693  	return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op.String(), e.RHS.String())
  3694  }
  3695  
  3696  // BinaryExprName returns the name of a binary expression by concatenating
  3697  // the variables in the binary expression with underscores.
  3698  func BinaryExprName(expr *BinaryExpr) string {
  3699  	v := binaryExprNameVisitor{}
  3700  	Walk(&v, expr)
  3701  	return strings.Join(v.names, "_")
  3702  }
  3703  
  3704  type binaryExprNameVisitor struct {
  3705  	names []string
  3706  }
  3707  
  3708  func (v *binaryExprNameVisitor) Visit(n Node) Visitor {
  3709  	switch n := n.(type) {
  3710  	case *VarRef:
  3711  		v.names = append(v.names, n.Val)
  3712  	case *Call:
  3713  		v.names = append(v.names, n.Name)
  3714  		return nil
  3715  	}
  3716  	return v
  3717  }
  3718  
  3719  // ParenExpr represents a parenthesized expression.
  3720  type ParenExpr struct {
  3721  	Expr Expr
  3722  }
  3723  
  3724  // String returns a string representation of the parenthesized expression.
  3725  func (e *ParenExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
  3726  
  3727  // RegexLiteral represents a regular expression.
  3728  type RegexLiteral struct {
  3729  	Val *regexp.Regexp
  3730  }
  3731  
  3732  // String returns a string representation of the literal.
  3733  func (r *RegexLiteral) String() string {
  3734  	if r.Val != nil {
  3735  		return fmt.Sprintf("/%s/", strings.Replace(r.Val.String(), `/`, `\/`, -1))
  3736  	}
  3737  	return ""
  3738  }
  3739  
  3740  // CloneRegexLiteral returns a clone of the RegexLiteral.
  3741  func CloneRegexLiteral(r *RegexLiteral) *RegexLiteral {
  3742  	if r == nil {
  3743  		return nil
  3744  	}
  3745  
  3746  	clone := &RegexLiteral{}
  3747  	if r.Val != nil {
  3748  		clone.Val = regexp.MustCompile(r.Val.String())
  3749  	}
  3750  
  3751  	return clone
  3752  }
  3753  
  3754  // Wildcard represents a wild card expression.
  3755  type Wildcard struct {
  3756  	Type Token
  3757  }
  3758  
  3759  // String returns a string representation of the wildcard.
  3760  func (e *Wildcard) String() string {
  3761  	switch e.Type {
  3762  	case FIELD:
  3763  		return "*::field"
  3764  	case TAG:
  3765  		return "*::tag"
  3766  	default:
  3767  		return "*"
  3768  	}
  3769  }
  3770  
  3771  // CloneExpr returns a deep copy of the expression.
  3772  func CloneExpr(expr Expr) Expr {
  3773  	if expr == nil {
  3774  		return nil
  3775  	}
  3776  	switch expr := expr.(type) {
  3777  	case *BinaryExpr:
  3778  		return &BinaryExpr{Op: expr.Op, LHS: CloneExpr(expr.LHS), RHS: CloneExpr(expr.RHS)}
  3779  	case *BooleanLiteral:
  3780  		return &BooleanLiteral{Val: expr.Val}
  3781  	case *Call:
  3782  		args := make([]Expr, len(expr.Args))
  3783  		for i, arg := range expr.Args {
  3784  			args[i] = CloneExpr(arg)
  3785  		}
  3786  		return &Call{Name: expr.Name, Args: args}
  3787  	case *Distinct:
  3788  		return &Distinct{Val: expr.Val}
  3789  	case *DurationLiteral:
  3790  		return &DurationLiteral{Val: expr.Val}
  3791  	case *IntegerLiteral:
  3792  		return &IntegerLiteral{Val: expr.Val}
  3793  	case *UnsignedLiteral:
  3794  		return &UnsignedLiteral{Val: expr.Val}
  3795  	case *NumberLiteral:
  3796  		return &NumberLiteral{Val: expr.Val}
  3797  	case *ParenExpr:
  3798  		return &ParenExpr{Expr: CloneExpr(expr.Expr)}
  3799  	case *RegexLiteral:
  3800  		return &RegexLiteral{Val: expr.Val}
  3801  	case *StringLiteral:
  3802  		return &StringLiteral{Val: expr.Val}
  3803  	case *TimeLiteral:
  3804  		return &TimeLiteral{Val: expr.Val}
  3805  	case *VarRef:
  3806  		return &VarRef{Val: expr.Val, Type: expr.Type}
  3807  	case *Wildcard:
  3808  		return &Wildcard{Type: expr.Type}
  3809  	}
  3810  	panic("unreachable")
  3811  }
  3812  
  3813  // HasTimeExpr returns true if the expression has a time term.
  3814  func HasTimeExpr(expr Expr) bool {
  3815  	switch n := expr.(type) {
  3816  	case *BinaryExpr:
  3817  		if n.Op == AND || n.Op == OR {
  3818  			return HasTimeExpr(n.LHS) || HasTimeExpr(n.RHS)
  3819  		}
  3820  		if ref, ok := n.LHS.(*VarRef); ok && strings.ToLower(ref.Val) == "time" {
  3821  			return true
  3822  		}
  3823  		return false
  3824  	case *ParenExpr:
  3825  		// walk down the tree
  3826  		return HasTimeExpr(n.Expr)
  3827  	default:
  3828  		return false
  3829  	}
  3830  }
  3831  
  3832  // Visitor can be called by Walk to traverse an AST hierarchy.
  3833  // The Visit() function is called once per node.
  3834  type Visitor interface {
  3835  	Visit(Node) Visitor
  3836  }
  3837  
  3838  // Walk traverses a node hierarchy in depth-first order.
  3839  func Walk(v Visitor, node Node) {
  3840  	if node == nil {
  3841  		return
  3842  	}
  3843  
  3844  	if v = v.Visit(node); v == nil {
  3845  		return
  3846  	}
  3847  
  3848  	switch n := node.(type) {
  3849  	case *BinaryExpr:
  3850  		Walk(v, n.LHS)
  3851  		Walk(v, n.RHS)
  3852  
  3853  	case *Call:
  3854  		for _, expr := range n.Args {
  3855  			Walk(v, expr)
  3856  		}
  3857  
  3858  	case *CreateContinuousQueryStatement:
  3859  		Walk(v, n.Source)
  3860  
  3861  	case *Dimension:
  3862  		Walk(v, n.Expr)
  3863  
  3864  	case Dimensions:
  3865  		for _, c := range n {
  3866  			Walk(v, c)
  3867  		}
  3868  
  3869  	case *DeleteSeriesStatement:
  3870  		Walk(v, n.Sources)
  3871  		Walk(v, n.Condition)
  3872  
  3873  	case *DropSeriesStatement:
  3874  		Walk(v, n.Sources)
  3875  		Walk(v, n.Condition)
  3876  
  3877  	case *ExplainStatement:
  3878  		Walk(v, n.Statement)
  3879  
  3880  	case *Field:
  3881  		Walk(v, n.Expr)
  3882  
  3883  	case Fields:
  3884  		for _, c := range n {
  3885  			Walk(v, c)
  3886  		}
  3887  
  3888  	case *ParenExpr:
  3889  		Walk(v, n.Expr)
  3890  
  3891  	case *Query:
  3892  		Walk(v, n.Statements)
  3893  
  3894  	case *SelectStatement:
  3895  		Walk(v, n.Fields)
  3896  		Walk(v, n.Target)
  3897  		Walk(v, n.Dimensions)
  3898  		Walk(v, n.Sources)
  3899  		Walk(v, n.Condition)
  3900  		Walk(v, n.SortFields)
  3901  
  3902  	case *ShowFieldKeyCardinalityStatement:
  3903  		Walk(v, n.Sources)
  3904  		Walk(v, n.Condition)
  3905  
  3906  	case *ShowSeriesStatement:
  3907  		Walk(v, n.Sources)
  3908  		Walk(v, n.Condition)
  3909  
  3910  	case *ShowSeriesCardinalityStatement:
  3911  		Walk(v, n.Sources)
  3912  		Walk(v, n.Condition)
  3913  
  3914  	case *ShowMeasurementCardinalityStatement:
  3915  		Walk(v, n.Sources)
  3916  		Walk(v, n.Condition)
  3917  
  3918  	case *ShowTagKeyCardinalityStatement:
  3919  		Walk(v, n.Sources)
  3920  		Walk(v, n.Condition)
  3921  
  3922  	case *ShowTagKeysStatement:
  3923  		Walk(v, n.Sources)
  3924  		Walk(v, n.Condition)
  3925  		Walk(v, n.SortFields)
  3926  
  3927  	case *ShowTagValuesCardinalityStatement:
  3928  		Walk(v, n.Sources)
  3929  		Walk(v, n.Condition)
  3930  
  3931  	case *ShowTagValuesStatement:
  3932  		Walk(v, n.Sources)
  3933  		Walk(v, n.Condition)
  3934  		Walk(v, n.SortFields)
  3935  
  3936  	case *ShowFieldKeysStatement:
  3937  		Walk(v, n.Sources)
  3938  		Walk(v, n.SortFields)
  3939  
  3940  	case SortFields:
  3941  		for _, sf := range n {
  3942  			Walk(v, sf)
  3943  		}
  3944  
  3945  	case Sources:
  3946  		for _, s := range n {
  3947  			Walk(v, s)
  3948  		}
  3949  
  3950  	case *SubQuery:
  3951  		Walk(v, n.Statement)
  3952  
  3953  	case Statements:
  3954  		for _, s := range n {
  3955  			Walk(v, s)
  3956  		}
  3957  
  3958  	case *Target:
  3959  		if n != nil {
  3960  			Walk(v, n.Measurement)
  3961  		}
  3962  	}
  3963  }
  3964  
  3965  // WalkFunc traverses a node hierarchy in depth-first order.
  3966  func WalkFunc(node Node, fn func(Node)) {
  3967  	Walk(walkFuncVisitor(fn), node)
  3968  }
  3969  
  3970  type walkFuncVisitor func(Node)
  3971  
  3972  func (fn walkFuncVisitor) Visit(n Node) Visitor { fn(n); return fn }
  3973  
  3974  // Rewriter can be called by Rewrite to replace nodes in the AST hierarchy.
  3975  // The Rewrite() function is called once per node.
  3976  type Rewriter interface {
  3977  	Rewrite(Node) Node
  3978  }
  3979  
  3980  // Rewrite recursively invokes the rewriter to replace each node.
  3981  // Nodes are traversed depth-first and rewritten from leaf to root.
  3982  func Rewrite(r Rewriter, node Node) Node {
  3983  	switch n := node.(type) {
  3984  	case *Query:
  3985  		n.Statements = Rewrite(r, n.Statements).(Statements)
  3986  
  3987  	case Statements:
  3988  		for i, s := range n {
  3989  			n[i] = Rewrite(r, s).(Statement)
  3990  		}
  3991  
  3992  	case *SelectStatement:
  3993  		n.Fields = Rewrite(r, n.Fields).(Fields)
  3994  		n.Dimensions = Rewrite(r, n.Dimensions).(Dimensions)
  3995  		n.Sources = Rewrite(r, n.Sources).(Sources)
  3996  
  3997  		// Rewrite may return nil. Nil does not satisfy the Expr
  3998  		// interface. We only assert the rewritten result to be an
  3999  		// Expr if it is not nil:
  4000  		if cond := Rewrite(r, n.Condition); cond != nil {
  4001  			n.Condition = cond.(Expr)
  4002  		} else {
  4003  			n.Condition = nil
  4004  		}
  4005  
  4006  	case *SubQuery:
  4007  		n.Statement = Rewrite(r, n.Statement).(*SelectStatement)
  4008  
  4009  	case Fields:
  4010  		for i, f := range n {
  4011  			n[i] = Rewrite(r, f).(*Field)
  4012  		}
  4013  
  4014  	case *Field:
  4015  		n.Expr = Rewrite(r, n.Expr).(Expr)
  4016  
  4017  	case Dimensions:
  4018  		for i, d := range n {
  4019  			n[i] = Rewrite(r, d).(*Dimension)
  4020  		}
  4021  
  4022  	case *Dimension:
  4023  		n.Expr = Rewrite(r, n.Expr).(Expr)
  4024  
  4025  	case *BinaryExpr:
  4026  		n.LHS = Rewrite(r, n.LHS).(Expr)
  4027  		n.RHS = Rewrite(r, n.RHS).(Expr)
  4028  
  4029  	case *ParenExpr:
  4030  		n.Expr = Rewrite(r, n.Expr).(Expr)
  4031  
  4032  	case *Call:
  4033  		for i, expr := range n.Args {
  4034  			n.Args[i] = Rewrite(r, expr).(Expr)
  4035  		}
  4036  	}
  4037  
  4038  	return r.Rewrite(node)
  4039  }
  4040  
  4041  // RewriteFunc rewrites a node hierarchy.
  4042  func RewriteFunc(node Node, fn func(Node) Node) Node {
  4043  	return Rewrite(rewriterFunc(fn), node)
  4044  }
  4045  
  4046  type rewriterFunc func(Node) Node
  4047  
  4048  func (fn rewriterFunc) Rewrite(n Node) Node { return fn(n) }
  4049  
  4050  // RewriteExpr recursively invokes the function to replace each expr.
  4051  // Nodes are traversed depth-first and rewritten from leaf to root.
  4052  func RewriteExpr(expr Expr, fn func(Expr) Expr) Expr {
  4053  	switch e := expr.(type) {
  4054  	case *BinaryExpr:
  4055  		e.LHS = RewriteExpr(e.LHS, fn)
  4056  		e.RHS = RewriteExpr(e.RHS, fn)
  4057  		if e.LHS != nil && e.RHS == nil {
  4058  			expr = e.LHS
  4059  		} else if e.RHS != nil && e.LHS == nil {
  4060  			expr = e.RHS
  4061  		} else if e.LHS == nil && e.RHS == nil {
  4062  			return nil
  4063  		}
  4064  
  4065  	case *ParenExpr:
  4066  		e.Expr = RewriteExpr(e.Expr, fn)
  4067  		if e.Expr == nil {
  4068  			return nil
  4069  		}
  4070  
  4071  	case *Call:
  4072  		for i, expr := range e.Args {
  4073  			e.Args[i] = RewriteExpr(expr, fn)
  4074  		}
  4075  	}
  4076  
  4077  	return fn(expr)
  4078  }
  4079  
  4080  // Eval evaluates expr against a map.
  4081  func Eval(expr Expr, m map[string]interface{}) interface{} {
  4082  	eval := ValuerEval{Valuer: MapValuer(m)}
  4083  	return eval.Eval(expr)
  4084  }
  4085  
  4086  // MapValuer is a valuer that substitutes values for the mapped interface.
  4087  type MapValuer map[string]interface{}
  4088  
  4089  // Value returns the value for a key in the MapValuer.
  4090  func (m MapValuer) Value(key string) (interface{}, bool) {
  4091  	v, ok := m[key]
  4092  	return v, ok
  4093  }
  4094  
  4095  // ValuerEval will evaluate an expression using the Valuer.
  4096  type ValuerEval struct {
  4097  	Valuer Valuer
  4098  
  4099  	// IntegerFloatDivision will set the eval system to treat
  4100  	// a division between two integers as a floating point division.
  4101  	IntegerFloatDivision bool
  4102  }
  4103  
  4104  // Eval evaluates an expression and returns a value.
  4105  func (v *ValuerEval) Eval(expr Expr) interface{} {
  4106  	if expr == nil {
  4107  		return nil
  4108  	}
  4109  
  4110  	switch expr := expr.(type) {
  4111  	case *BinaryExpr:
  4112  		return v.evalBinaryExpr(expr)
  4113  	case *BooleanLiteral:
  4114  		return expr.Val
  4115  	case *IntegerLiteral:
  4116  		return expr.Val
  4117  	case *NumberLiteral:
  4118  		return expr.Val
  4119  	case *UnsignedLiteral:
  4120  		return expr.Val
  4121  	case *ParenExpr:
  4122  		return v.Eval(expr.Expr)
  4123  	case *RegexLiteral:
  4124  		return expr.Val
  4125  	case *StringLiteral:
  4126  		return expr.Val
  4127  	case *Call:
  4128  		if valuer, ok := v.Valuer.(CallValuer); ok {
  4129  			var args []interface{}
  4130  			if len(expr.Args) > 0 {
  4131  				args = make([]interface{}, len(expr.Args))
  4132  				for i := range expr.Args {
  4133  					args[i] = v.Eval(expr.Args[i])
  4134  				}
  4135  			}
  4136  			val, _ := valuer.Call(expr.Name, args)
  4137  			return val
  4138  		}
  4139  		return nil
  4140  	case *VarRef:
  4141  		val, _ := v.Valuer.Value(expr.Val)
  4142  		return val
  4143  	default:
  4144  		return nil
  4145  	}
  4146  }
  4147  
  4148  // EvalBool evaluates expr and returns true if result is a boolean true.
  4149  // Otherwise returns false.
  4150  func (v *ValuerEval) EvalBool(expr Expr) bool {
  4151  	val, _ := v.Eval(expr).(bool)
  4152  	return val
  4153  }
  4154  
  4155  func (v *ValuerEval) evalBinaryExpr(expr *BinaryExpr) interface{} {
  4156  	lhs := v.Eval(expr.LHS)
  4157  	rhs := v.Eval(expr.RHS)
  4158  	if lhs == nil && rhs != nil {
  4159  		// When the LHS is nil and the RHS is a boolean, implicitly cast the
  4160  		// nil to false.
  4161  		if _, ok := rhs.(bool); ok {
  4162  			lhs = false
  4163  		}
  4164  	} else if lhs != nil && rhs == nil {
  4165  		// Implicit cast of the RHS nil to false when the LHS is a boolean.
  4166  		if _, ok := lhs.(bool); ok {
  4167  			rhs = false
  4168  		}
  4169  	}
  4170  
  4171  	// Evaluate if both sides are simple types.
  4172  	switch lhs := lhs.(type) {
  4173  	case bool:
  4174  		rhs, ok := rhs.(bool)
  4175  		switch expr.Op {
  4176  		case AND:
  4177  			return ok && (lhs && rhs)
  4178  		case OR:
  4179  			return ok && (lhs || rhs)
  4180  		case BITWISE_AND:
  4181  			return ok && (lhs && rhs)
  4182  		case BITWISE_OR:
  4183  			return ok && (lhs || rhs)
  4184  		case BITWISE_XOR:
  4185  			return ok && (lhs != rhs)
  4186  		case EQ:
  4187  			return ok && (lhs == rhs)
  4188  		case NEQ:
  4189  			return ok && (lhs != rhs)
  4190  		}
  4191  	case float64:
  4192  		// Try the rhs as a float64, int64, or uint64
  4193  		rhsf, ok := rhs.(float64)
  4194  		if !ok {
  4195  			switch val := rhs.(type) {
  4196  			case int64:
  4197  				rhsf, ok = float64(val), true
  4198  			case uint64:
  4199  				rhsf, ok = float64(val), true
  4200  			}
  4201  		}
  4202  
  4203  		rhs := rhsf
  4204  		switch expr.Op {
  4205  		case EQ:
  4206  			return ok && (lhs == rhs)
  4207  		case NEQ:
  4208  			return ok && (lhs != rhs)
  4209  		case LT:
  4210  			return ok && (lhs < rhs)
  4211  		case LTE:
  4212  			return ok && (lhs <= rhs)
  4213  		case GT:
  4214  			return ok && (lhs > rhs)
  4215  		case GTE:
  4216  			return ok && (lhs >= rhs)
  4217  		case ADD:
  4218  			if !ok {
  4219  				return nil
  4220  			}
  4221  			return lhs + rhs
  4222  		case SUB:
  4223  			if !ok {
  4224  				return nil
  4225  			}
  4226  			return lhs - rhs
  4227  		case MUL:
  4228  			if !ok {
  4229  				return nil
  4230  			}
  4231  			return lhs * rhs
  4232  		case DIV:
  4233  			if !ok {
  4234  				return nil
  4235  			} else if rhs == 0 {
  4236  				return float64(0)
  4237  			}
  4238  			return lhs / rhs
  4239  		case MOD:
  4240  			if !ok {
  4241  				return nil
  4242  			}
  4243  			return math.Mod(lhs, rhs)
  4244  		}
  4245  	case int64:
  4246  		// Try as a float64 to see if a float cast is required.
  4247  		switch rhs := rhs.(type) {
  4248  		case float64:
  4249  			lhs := float64(lhs)
  4250  			switch expr.Op {
  4251  			case EQ:
  4252  				return lhs == rhs
  4253  			case NEQ:
  4254  				return lhs != rhs
  4255  			case LT:
  4256  				return lhs < rhs
  4257  			case LTE:
  4258  				return lhs <= rhs
  4259  			case GT:
  4260  				return lhs > rhs
  4261  			case GTE:
  4262  				return lhs >= rhs
  4263  			case ADD:
  4264  				return lhs + rhs
  4265  			case SUB:
  4266  				return lhs - rhs
  4267  			case MUL:
  4268  				return lhs * rhs
  4269  			case DIV:
  4270  				if rhs == 0 {
  4271  					return float64(0)
  4272  				}
  4273  				return lhs / rhs
  4274  			case MOD:
  4275  				return math.Mod(lhs, rhs)
  4276  			}
  4277  		case int64:
  4278  			switch expr.Op {
  4279  			case EQ:
  4280  				return lhs == rhs
  4281  			case NEQ:
  4282  				return lhs != rhs
  4283  			case LT:
  4284  				return lhs < rhs
  4285  			case LTE:
  4286  				return lhs <= rhs
  4287  			case GT:
  4288  				return lhs > rhs
  4289  			case GTE:
  4290  				return lhs >= rhs
  4291  			case ADD:
  4292  				return lhs + rhs
  4293  			case SUB:
  4294  				return lhs - rhs
  4295  			case MUL:
  4296  				return lhs * rhs
  4297  			case DIV:
  4298  				if v.IntegerFloatDivision {
  4299  					if rhs == 0 {
  4300  						return float64(0)
  4301  					}
  4302  					return float64(lhs) / float64(rhs)
  4303  				}
  4304  
  4305  				if rhs == 0 {
  4306  					return int64(0)
  4307  				}
  4308  				return lhs / rhs
  4309  			case MOD:
  4310  				if rhs == 0 {
  4311  					return int64(0)
  4312  				}
  4313  				return lhs % rhs
  4314  			case BITWISE_AND:
  4315  				return lhs & rhs
  4316  			case BITWISE_OR:
  4317  				return lhs | rhs
  4318  			case BITWISE_XOR:
  4319  				return lhs ^ rhs
  4320  			}
  4321  		case uint64:
  4322  			switch expr.Op {
  4323  			case EQ:
  4324  				return uint64(lhs) == rhs
  4325  			case NEQ:
  4326  				return uint64(lhs) != rhs
  4327  			case LT:
  4328  				if lhs < 0 {
  4329  					return true
  4330  				}
  4331  				return uint64(lhs) < rhs
  4332  			case LTE:
  4333  				if lhs < 0 {
  4334  					return true
  4335  				}
  4336  				return uint64(lhs) <= rhs
  4337  			case GT:
  4338  				if lhs < 0 {
  4339  					return false
  4340  				}
  4341  				return uint64(lhs) > rhs
  4342  			case GTE:
  4343  				if lhs < 0 {
  4344  					return false
  4345  				}
  4346  				return uint64(lhs) >= rhs
  4347  			case ADD:
  4348  				return uint64(lhs) + rhs
  4349  			case SUB:
  4350  				return uint64(lhs) - rhs
  4351  			case MUL:
  4352  				return uint64(lhs) * rhs
  4353  			case DIV:
  4354  				if rhs == 0 {
  4355  					return uint64(0)
  4356  				}
  4357  				return uint64(lhs) / rhs
  4358  			case MOD:
  4359  				if rhs == 0 {
  4360  					return uint64(0)
  4361  				}
  4362  				return uint64(lhs) % rhs
  4363  			case BITWISE_AND:
  4364  				return uint64(lhs) & rhs
  4365  			case BITWISE_OR:
  4366  				return uint64(lhs) | rhs
  4367  			case BITWISE_XOR:
  4368  				return uint64(lhs) ^ rhs
  4369  			}
  4370  		}
  4371  	case uint64:
  4372  		// Try as a float64 to see if a float cast is required.
  4373  		switch rhs := rhs.(type) {
  4374  		case float64:
  4375  			lhs := float64(lhs)
  4376  			switch expr.Op {
  4377  			case EQ:
  4378  				return lhs == rhs
  4379  			case NEQ:
  4380  				return lhs != rhs
  4381  			case LT:
  4382  				return lhs < rhs
  4383  			case LTE:
  4384  				return lhs <= rhs
  4385  			case GT:
  4386  				return lhs > rhs
  4387  			case GTE:
  4388  				return lhs >= rhs
  4389  			case ADD:
  4390  				return lhs + rhs
  4391  			case SUB:
  4392  				return lhs - rhs
  4393  			case MUL:
  4394  				return lhs * rhs
  4395  			case DIV:
  4396  				if rhs == 0 {
  4397  					return float64(0)
  4398  				}
  4399  				return lhs / rhs
  4400  			case MOD:
  4401  				return math.Mod(lhs, rhs)
  4402  			}
  4403  		case int64:
  4404  			switch expr.Op {
  4405  			case EQ:
  4406  				return lhs == uint64(rhs)
  4407  			case NEQ:
  4408  				return lhs != uint64(rhs)
  4409  			case LT:
  4410  				if rhs < 0 {
  4411  					return false
  4412  				}
  4413  				return lhs < uint64(rhs)
  4414  			case LTE:
  4415  				if rhs < 0 {
  4416  					return false
  4417  				}
  4418  				return lhs <= uint64(rhs)
  4419  			case GT:
  4420  				if rhs < 0 {
  4421  					return true
  4422  				}
  4423  				return lhs > uint64(rhs)
  4424  			case GTE:
  4425  				if rhs < 0 {
  4426  					return true
  4427  				}
  4428  				return lhs >= uint64(rhs)
  4429  			case ADD:
  4430  				return lhs + uint64(rhs)
  4431  			case SUB:
  4432  				return lhs - uint64(rhs)
  4433  			case MUL:
  4434  				return lhs * uint64(rhs)
  4435  			case DIV:
  4436  				if rhs == 0 {
  4437  					return uint64(0)
  4438  				}
  4439  				return lhs / uint64(rhs)
  4440  			case MOD:
  4441  				if rhs == 0 {
  4442  					return uint64(0)
  4443  				}
  4444  				return lhs % uint64(rhs)
  4445  			case BITWISE_AND:
  4446  				return lhs & uint64(rhs)
  4447  			case BITWISE_OR:
  4448  				return lhs | uint64(rhs)
  4449  			case BITWISE_XOR:
  4450  				return lhs ^ uint64(rhs)
  4451  			}
  4452  		case uint64:
  4453  			switch expr.Op {
  4454  			case EQ:
  4455  				return lhs == rhs
  4456  			case NEQ:
  4457  				return lhs != rhs
  4458  			case LT:
  4459  				return lhs < rhs
  4460  			case LTE:
  4461  				return lhs <= rhs
  4462  			case GT:
  4463  				return lhs > rhs
  4464  			case GTE:
  4465  				return lhs >= rhs
  4466  			case ADD:
  4467  				return lhs + rhs
  4468  			case SUB:
  4469  				return lhs - rhs
  4470  			case MUL:
  4471  				return lhs * rhs
  4472  			case DIV:
  4473  				if rhs == 0 {
  4474  					return uint64(0)
  4475  				}
  4476  				return lhs / rhs
  4477  			case MOD:
  4478  				if rhs == 0 {
  4479  					return uint64(0)
  4480  				}
  4481  				return lhs % rhs
  4482  			case BITWISE_AND:
  4483  				return lhs & rhs
  4484  			case BITWISE_OR:
  4485  				return lhs | rhs
  4486  			case BITWISE_XOR:
  4487  				return lhs ^ rhs
  4488  			}
  4489  		}
  4490  	case string:
  4491  		switch expr.Op {
  4492  		case EQ:
  4493  			rhs, ok := rhs.(string)
  4494  			if !ok {
  4495  				return false
  4496  			}
  4497  			return lhs == rhs
  4498  		case NEQ:
  4499  			rhs, ok := rhs.(string)
  4500  			if !ok {
  4501  				return false
  4502  			}
  4503  			return lhs != rhs
  4504  		case EQREGEX:
  4505  			rhs, ok := rhs.(*regexp.Regexp)
  4506  			if !ok {
  4507  				return false
  4508  			}
  4509  			return rhs.MatchString(lhs)
  4510  		case NEQREGEX:
  4511  			rhs, ok := rhs.(*regexp.Regexp)
  4512  			if !ok {
  4513  				return false
  4514  			}
  4515  			return !rhs.MatchString(lhs)
  4516  		}
  4517  	}
  4518  
  4519  	// The types were not comparable. If our operation was an equality operation,
  4520  	// return false instead of true.
  4521  	switch expr.Op {
  4522  	case EQ, NEQ, LT, LTE, GT, GTE:
  4523  		return false
  4524  	}
  4525  	return nil
  4526  }
  4527  
  4528  // EvalBool evaluates expr and returns true if result is a boolean true.
  4529  // Otherwise returns false.
  4530  func EvalBool(expr Expr, m map[string]interface{}) bool {
  4531  	v, _ := Eval(expr, m).(bool)
  4532  	return v
  4533  }
  4534  
  4535  // TypeMapper maps a data type to the measurement and field.
  4536  type TypeMapper interface {
  4537  	MapType(measurement *Measurement, field string) DataType
  4538  }
  4539  
  4540  // CallTypeMapper maps a data type to the function call.
  4541  type CallTypeMapper interface {
  4542  	TypeMapper
  4543  
  4544  	CallType(name string, args []DataType) (DataType, error)
  4545  }
  4546  
  4547  type nilTypeMapper struct{}
  4548  
  4549  func (nilTypeMapper) MapType(*Measurement, string) DataType { return Unknown }
  4550  
  4551  type multiTypeMapper []TypeMapper
  4552  
  4553  // MultiTypeMapper combines multiple TypeMappers into a single one.
  4554  // The MultiTypeMapper will return the first type that is not Unknown.
  4555  // It will not iterate through all of them to find the highest priority one.
  4556  func MultiTypeMapper(mappers ...TypeMapper) TypeMapper {
  4557  	return multiTypeMapper(mappers)
  4558  }
  4559  
  4560  func (a multiTypeMapper) MapType(measurement *Measurement, field string) DataType {
  4561  	for _, m := range a {
  4562  		if typ := m.MapType(measurement, field); typ != Unknown {
  4563  			return typ
  4564  		}
  4565  	}
  4566  	return Unknown
  4567  }
  4568  
  4569  func (a multiTypeMapper) CallType(name string, args []DataType) (DataType, error) {
  4570  	for _, m := range a {
  4571  		call, ok := m.(CallTypeMapper)
  4572  		if ok {
  4573  			typ, err := call.CallType(name, args)
  4574  			if err != nil {
  4575  				return Unknown, err
  4576  			} else if typ != Unknown {
  4577  				return typ, nil
  4578  			}
  4579  		}
  4580  	}
  4581  	return Unknown, nil
  4582  }
  4583  
  4584  // TypeValuerEval evaluates an expression to determine its output type.
  4585  type TypeValuerEval struct {
  4586  	TypeMapper TypeMapper
  4587  	Sources    Sources
  4588  }
  4589  
  4590  // EvalType returns the type for an expression. If the expression cannot
  4591  // be evaluated for some reason, like incompatible types, it is returned
  4592  // as a TypeError in the error. If the error is non-fatal so we can continue
  4593  // even though an error happened, true will be returned.
  4594  // This function assumes that the expression has already been reduced.
  4595  func (v *TypeValuerEval) EvalType(expr Expr) (DataType, error) {
  4596  	switch expr := expr.(type) {
  4597  	case *VarRef:
  4598  		return v.evalVarRefExprType(expr)
  4599  	case *Call:
  4600  		return v.evalCallExprType(expr)
  4601  	case *BinaryExpr:
  4602  		return v.evalBinaryExprType(expr)
  4603  	case *ParenExpr:
  4604  		return v.EvalType(expr.Expr)
  4605  	case *NumberLiteral:
  4606  		return Float, nil
  4607  	case *IntegerLiteral:
  4608  		return Integer, nil
  4609  	case *UnsignedLiteral:
  4610  		return Unsigned, nil
  4611  	case *StringLiteral:
  4612  		return String, nil
  4613  	case *BooleanLiteral:
  4614  		return Boolean, nil
  4615  	}
  4616  	return Unknown, nil
  4617  }
  4618  
  4619  func (v *TypeValuerEval) evalVarRefExprType(expr *VarRef) (DataType, error) {
  4620  	// If this variable already has an assigned type, just use that.
  4621  	if expr.Type != Unknown && expr.Type != AnyField {
  4622  		return expr.Type, nil
  4623  	}
  4624  
  4625  	var typ DataType
  4626  	if v.TypeMapper != nil {
  4627  		for _, src := range v.Sources {
  4628  			switch src := src.(type) {
  4629  			case *Measurement:
  4630  				if t := v.TypeMapper.MapType(src, expr.Val); typ.LessThan(t) {
  4631  					typ = t
  4632  				}
  4633  			case *SubQuery:
  4634  				_, e := src.Statement.FieldExprByName(expr.Val)
  4635  				if e != nil {
  4636  					valuer := TypeValuerEval{
  4637  						TypeMapper: v.TypeMapper,
  4638  						Sources:    src.Statement.Sources,
  4639  					}
  4640  					if t, err := valuer.EvalType(e); err != nil {
  4641  						return Unknown, err
  4642  					} else if typ.LessThan(t) {
  4643  						typ = t
  4644  					}
  4645  				}
  4646  
  4647  				if typ == Unknown {
  4648  					for _, d := range src.Statement.Dimensions {
  4649  						if d, ok := d.Expr.(*VarRef); ok && expr.Val == d.Val {
  4650  							typ = Tag
  4651  						}
  4652  					}
  4653  				}
  4654  			}
  4655  		}
  4656  	}
  4657  	return typ, nil
  4658  }
  4659  
  4660  func (v *TypeValuerEval) evalCallExprType(expr *Call) (DataType, error) {
  4661  	typmap, ok := v.TypeMapper.(CallTypeMapper)
  4662  	if !ok {
  4663  		return Unknown, nil
  4664  	}
  4665  
  4666  	// Evaluate all of the data types for the arguments.
  4667  	args := make([]DataType, len(expr.Args))
  4668  	for i, arg := range expr.Args {
  4669  		typ, err := v.EvalType(arg)
  4670  		if err != nil {
  4671  			return Unknown, err
  4672  		}
  4673  		args[i] = typ
  4674  	}
  4675  
  4676  	// Pass in the data types for the call so it can be type checked and
  4677  	// the resulting type can be returned.
  4678  	return typmap.CallType(expr.Name, args)
  4679  }
  4680  
  4681  func (v *TypeValuerEval) evalBinaryExprType(expr *BinaryExpr) (DataType, error) {
  4682  	// Find the data type for both sides of the expression.
  4683  	lhs, err := v.EvalType(expr.LHS)
  4684  	if err != nil {
  4685  		return Unknown, err
  4686  	}
  4687  	rhs, err := v.EvalType(expr.RHS)
  4688  	if err != nil {
  4689  		return Unknown, err
  4690  	}
  4691  
  4692  	// If one of the two is unsigned and the other is an integer, we cannot add
  4693  	// the two without an explicit cast unless the integer is a literal.
  4694  	if lhs == Unsigned && rhs == Integer {
  4695  		if isLiteral(expr.LHS) {
  4696  			return Unknown, &TypeError{
  4697  				Expr:    expr,
  4698  				Message: fmt.Sprintf("cannot use %s with an integer and unsigned literal", expr.Op),
  4699  			}
  4700  		} else if !isLiteral(expr.RHS) {
  4701  			return Unknown, &TypeError{
  4702  				Expr:    expr,
  4703  				Message: fmt.Sprintf("cannot use %s between an integer and unsigned, an explicit cast is required", expr.Op),
  4704  			}
  4705  		}
  4706  	} else if lhs == Integer && rhs == Unsigned {
  4707  		if isLiteral(expr.RHS) {
  4708  			return Unknown, &TypeError{
  4709  				Expr:    expr,
  4710  				Message: fmt.Sprintf("cannot use %s with an integer and unsigned literal", expr.Op),
  4711  			}
  4712  		} else if !isLiteral(expr.LHS) {
  4713  			return Unknown, &TypeError{
  4714  				Expr:    expr,
  4715  				Message: fmt.Sprintf("cannot use %s between an integer and unsigned, an explicit cast is required", expr.Op),
  4716  			}
  4717  		}
  4718  	}
  4719  
  4720  	// If one of the two is unknown, then return the other as the type.
  4721  	if lhs == Unknown {
  4722  		return rhs, nil
  4723  	} else if rhs == Unknown {
  4724  		return lhs, nil
  4725  	}
  4726  
  4727  	// Rather than re-implement the ValuerEval here, we create a dummy binary
  4728  	// expression with the zero values and inspect the resulting value back into
  4729  	// a data type to determine the output.
  4730  	e := BinaryExpr{
  4731  		LHS: &VarRef{Val: "lhs"},
  4732  		RHS: &VarRef{Val: "rhs"},
  4733  		Op:  expr.Op,
  4734  	}
  4735  	result := Eval(&e, map[string]interface{}{
  4736  		"lhs": lhs.Zero(),
  4737  		"rhs": rhs.Zero(),
  4738  	})
  4739  
  4740  	typ := InspectDataType(result)
  4741  	if typ == Unknown {
  4742  		// If the type is unknown, then the two types were not compatible.
  4743  		return Unknown, &TypeError{
  4744  			Expr:    expr,
  4745  			Message: fmt.Sprintf("incompatible types: %s and %s", lhs, rhs),
  4746  		}
  4747  	}
  4748  	return typ, nil
  4749  }
  4750  
  4751  // TypeError is an error when two types are incompatible.
  4752  type TypeError struct {
  4753  	// Expr contains the expression that generated the type error.
  4754  	Expr Expr
  4755  	// Message contains the informational message about the type error.
  4756  	Message string
  4757  }
  4758  
  4759  func (e *TypeError) Error() string {
  4760  	return fmt.Sprintf("type error: %s: %s", e.Expr, e.Message)
  4761  }
  4762  
  4763  // EvalType evaluates the expression's type.
  4764  func EvalType(expr Expr, sources Sources, typmap TypeMapper) DataType {
  4765  	if typmap == nil {
  4766  		typmap = nilTypeMapper{}
  4767  	}
  4768  
  4769  	valuer := TypeValuerEval{
  4770  		TypeMapper: typmap,
  4771  		Sources:    sources,
  4772  	}
  4773  	typ, _ := valuer.EvalType(expr)
  4774  	return typ
  4775  }
  4776  
  4777  func FieldDimensions(sources Sources, m FieldMapper) (fields map[string]DataType, dimensions map[string]struct{}, err error) {
  4778  	fields = make(map[string]DataType)
  4779  	dimensions = make(map[string]struct{})
  4780  
  4781  	for _, src := range sources {
  4782  		switch src := src.(type) {
  4783  		case *Measurement:
  4784  			f, d, err := m.FieldDimensions(src)
  4785  			if err != nil {
  4786  				return nil, nil, err
  4787  			}
  4788  
  4789  			for k, typ := range f {
  4790  				if fields[k].LessThan(typ) {
  4791  					fields[k] = typ
  4792  				}
  4793  			}
  4794  			for k := range d {
  4795  				dimensions[k] = struct{}{}
  4796  			}
  4797  		case *SubQuery:
  4798  			for _, f := range src.Statement.Fields {
  4799  				k := f.Name()
  4800  				typ := EvalType(f.Expr, src.Statement.Sources, m)
  4801  
  4802  				if fields[k].LessThan(typ) {
  4803  					fields[k] = typ
  4804  				}
  4805  			}
  4806  
  4807  			for _, d := range src.Statement.Dimensions {
  4808  				if expr, ok := d.Expr.(*VarRef); ok {
  4809  					dimensions[expr.Val] = struct{}{}
  4810  				}
  4811  			}
  4812  		}
  4813  	}
  4814  	return
  4815  }
  4816  
  4817  // Reduce evaluates expr using the available values in valuer.
  4818  // References that don't exist in valuer are ignored.
  4819  func Reduce(expr Expr, valuer Valuer) Expr {
  4820  	expr = reduce(expr, valuer)
  4821  
  4822  	// Unwrap parens at top level.
  4823  	if expr, ok := expr.(*ParenExpr); ok {
  4824  		return expr.Expr
  4825  	}
  4826  	return expr
  4827  }
  4828  
  4829  func reduce(expr Expr, valuer Valuer) Expr {
  4830  	if expr == nil {
  4831  		return nil
  4832  	}
  4833  
  4834  	switch expr := expr.(type) {
  4835  	case *BinaryExpr:
  4836  		return reduceBinaryExpr(expr, valuer)
  4837  	case *Call:
  4838  		return reduceCall(expr, valuer)
  4839  	case *ParenExpr:
  4840  		return reduceParenExpr(expr, valuer)
  4841  	case *VarRef:
  4842  		return reduceVarRef(expr, valuer)
  4843  	case *NilLiteral:
  4844  		return expr
  4845  	default:
  4846  		return CloneExpr(expr)
  4847  	}
  4848  }
  4849  
  4850  func reduceBinaryExpr(expr *BinaryExpr, valuer Valuer) Expr {
  4851  	// Reduce both sides first.
  4852  	op := expr.Op
  4853  	lhs := reduce(expr.LHS, valuer)
  4854  	rhs := reduce(expr.RHS, valuer)
  4855  
  4856  	loc := time.UTC
  4857  	if valuer, ok := valuer.(ZoneValuer); ok {
  4858  		if l := valuer.Zone(); l != nil {
  4859  			loc = l
  4860  		}
  4861  	}
  4862  
  4863  	// Do not evaluate if one side is nil.
  4864  	if lhs == nil || rhs == nil {
  4865  		return &BinaryExpr{LHS: lhs, RHS: rhs, Op: expr.Op}
  4866  	}
  4867  
  4868  	// If we have a logical operator (AND, OR) and one side is a boolean literal
  4869  	// then we need to have special handling.
  4870  	if op == AND {
  4871  		if isFalseLiteral(lhs) || isFalseLiteral(rhs) {
  4872  			return &BooleanLiteral{Val: false}
  4873  		} else if isTrueLiteral(lhs) {
  4874  			return rhs
  4875  		} else if isTrueLiteral(rhs) {
  4876  			return lhs
  4877  		}
  4878  	} else if op == OR {
  4879  		if isTrueLiteral(lhs) || isTrueLiteral(rhs) {
  4880  			return &BooleanLiteral{Val: true}
  4881  		} else if isFalseLiteral(lhs) {
  4882  			return rhs
  4883  		} else if isFalseLiteral(rhs) {
  4884  			return lhs
  4885  		}
  4886  	}
  4887  
  4888  	// Evaluate if both sides are simple types.
  4889  	switch lhs := lhs.(type) {
  4890  	case *BooleanLiteral:
  4891  		return reduceBinaryExprBooleanLHS(op, lhs, rhs)
  4892  	case *DurationLiteral:
  4893  		return reduceBinaryExprDurationLHS(op, lhs, rhs, loc)
  4894  	case *IntegerLiteral:
  4895  		return reduceBinaryExprIntegerLHS(op, lhs, rhs, loc)
  4896  	case *UnsignedLiteral:
  4897  		return reduceBinaryExprUnsignedLHS(op, lhs, rhs)
  4898  	case *NilLiteral:
  4899  		return reduceBinaryExprNilLHS(op, lhs, rhs)
  4900  	case *NumberLiteral:
  4901  		return reduceBinaryExprNumberLHS(op, lhs, rhs)
  4902  	case *StringLiteral:
  4903  		return reduceBinaryExprStringLHS(op, lhs, rhs, loc)
  4904  	case *TimeLiteral:
  4905  		return reduceBinaryExprTimeLHS(op, lhs, rhs, loc)
  4906  	default:
  4907  		return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  4908  	}
  4909  }
  4910  
  4911  func reduceBinaryExprBooleanLHS(op Token, lhs *BooleanLiteral, rhs Expr) Expr {
  4912  	switch rhs := rhs.(type) {
  4913  	case *BooleanLiteral:
  4914  		switch op {
  4915  		case EQ:
  4916  			return &BooleanLiteral{Val: lhs.Val == rhs.Val}
  4917  		case NEQ:
  4918  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  4919  		case AND:
  4920  			return &BooleanLiteral{Val: lhs.Val && rhs.Val}
  4921  		case OR:
  4922  			return &BooleanLiteral{Val: lhs.Val || rhs.Val}
  4923  		case BITWISE_AND:
  4924  			return &BooleanLiteral{Val: lhs.Val && rhs.Val}
  4925  		case BITWISE_OR:
  4926  			return &BooleanLiteral{Val: lhs.Val || rhs.Val}
  4927  		case BITWISE_XOR:
  4928  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  4929  		}
  4930  	case *NilLiteral:
  4931  		return &BooleanLiteral{Val: false}
  4932  	}
  4933  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  4934  }
  4935  
  4936  func reduceBinaryExprDurationLHS(op Token, lhs *DurationLiteral, rhs Expr, loc *time.Location) Expr {
  4937  	switch rhs := rhs.(type) {
  4938  	case *DurationLiteral:
  4939  		switch op {
  4940  		case ADD:
  4941  			return &DurationLiteral{Val: lhs.Val + rhs.Val}
  4942  		case SUB:
  4943  			return &DurationLiteral{Val: lhs.Val - rhs.Val}
  4944  		case EQ:
  4945  			return &BooleanLiteral{Val: lhs.Val == rhs.Val}
  4946  		case NEQ:
  4947  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  4948  		case GT:
  4949  			return &BooleanLiteral{Val: lhs.Val > rhs.Val}
  4950  		case GTE:
  4951  			return &BooleanLiteral{Val: lhs.Val >= rhs.Val}
  4952  		case LT:
  4953  			return &BooleanLiteral{Val: lhs.Val < rhs.Val}
  4954  		case LTE:
  4955  			return &BooleanLiteral{Val: lhs.Val <= rhs.Val}
  4956  		}
  4957  	case *NumberLiteral:
  4958  		switch op {
  4959  		case MUL:
  4960  			return &DurationLiteral{Val: lhs.Val * time.Duration(rhs.Val)}
  4961  		case DIV:
  4962  			if rhs.Val == 0 {
  4963  				return &DurationLiteral{Val: 0}
  4964  			}
  4965  			return &DurationLiteral{Val: lhs.Val / time.Duration(rhs.Val)}
  4966  		}
  4967  	case *IntegerLiteral:
  4968  		switch op {
  4969  		case MUL:
  4970  			return &DurationLiteral{Val: lhs.Val * time.Duration(rhs.Val)}
  4971  		case DIV:
  4972  			if rhs.Val == 0 {
  4973  				return &DurationLiteral{Val: 0}
  4974  			}
  4975  			return &DurationLiteral{Val: lhs.Val / time.Duration(rhs.Val)}
  4976  		}
  4977  	case *TimeLiteral:
  4978  		switch op {
  4979  		case ADD:
  4980  			return &TimeLiteral{Val: rhs.Val.Add(lhs.Val)}
  4981  		}
  4982  	case *StringLiteral:
  4983  		t, err := rhs.ToTimeLiteral(loc)
  4984  		if err != nil {
  4985  			break
  4986  		}
  4987  		expr := reduceBinaryExprDurationLHS(op, lhs, t, loc)
  4988  
  4989  		// If the returned expression is still a binary expr, that means
  4990  		// we couldn't reduce it so this wasn't used in a time literal context.
  4991  		if _, ok := expr.(*BinaryExpr); !ok {
  4992  			return expr
  4993  		}
  4994  	case *NilLiteral:
  4995  		return &BooleanLiteral{Val: false}
  4996  	}
  4997  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  4998  }
  4999  
  5000  func reduceBinaryExprIntegerLHS(op Token, lhs *IntegerLiteral, rhs Expr, loc *time.Location) Expr {
  5001  	switch rhs := rhs.(type) {
  5002  	case *NumberLiteral:
  5003  		return reduceBinaryExprNumberLHS(op, &NumberLiteral{Val: float64(lhs.Val)}, rhs)
  5004  	case *IntegerLiteral:
  5005  		switch op {
  5006  		case ADD:
  5007  			return &IntegerLiteral{Val: lhs.Val + rhs.Val}
  5008  		case SUB:
  5009  			return &IntegerLiteral{Val: lhs.Val - rhs.Val}
  5010  		case MUL:
  5011  			return &IntegerLiteral{Val: lhs.Val * rhs.Val}
  5012  		case DIV:
  5013  			if rhs.Val == 0 {
  5014  				return &NumberLiteral{Val: 0}
  5015  			}
  5016  			return &NumberLiteral{Val: float64(lhs.Val) / float64(rhs.Val)}
  5017  		case MOD:
  5018  			if rhs.Val == 0 {
  5019  				return &IntegerLiteral{Val: 0}
  5020  			}
  5021  			return &IntegerLiteral{Val: lhs.Val % rhs.Val}
  5022  		case BITWISE_AND:
  5023  			return &IntegerLiteral{Val: lhs.Val & rhs.Val}
  5024  		case BITWISE_OR:
  5025  			return &IntegerLiteral{Val: lhs.Val | rhs.Val}
  5026  		case BITWISE_XOR:
  5027  			return &IntegerLiteral{Val: lhs.Val ^ rhs.Val}
  5028  		case EQ:
  5029  			return &BooleanLiteral{Val: lhs.Val == rhs.Val}
  5030  		case NEQ:
  5031  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  5032  		case GT:
  5033  			return &BooleanLiteral{Val: lhs.Val > rhs.Val}
  5034  		case GTE:
  5035  			return &BooleanLiteral{Val: lhs.Val >= rhs.Val}
  5036  		case LT:
  5037  			return &BooleanLiteral{Val: lhs.Val < rhs.Val}
  5038  		case LTE:
  5039  			return &BooleanLiteral{Val: lhs.Val <= rhs.Val}
  5040  		}
  5041  	case *UnsignedLiteral:
  5042  		// Comparisons between an unsigned and integer literal will not involve
  5043  		// a cast if the integer is negative as that will have an improper result.
  5044  		// Look for those situations here.
  5045  		if lhs.Val < 0 {
  5046  			switch op {
  5047  			case LT, LTE:
  5048  				return &BooleanLiteral{Val: true}
  5049  			case GT, GTE:
  5050  				return &BooleanLiteral{Val: false}
  5051  			}
  5052  		}
  5053  		return reduceBinaryExprUnsignedLHS(op, &UnsignedLiteral{Val: uint64(lhs.Val)}, rhs)
  5054  	case *DurationLiteral:
  5055  		// Treat the integer as a timestamp.
  5056  		switch op {
  5057  		case ADD:
  5058  			return &TimeLiteral{Val: time.Unix(0, lhs.Val).Add(rhs.Val)}
  5059  		case SUB:
  5060  			return &TimeLiteral{Val: time.Unix(0, lhs.Val).Add(-rhs.Val)}
  5061  		}
  5062  	case *TimeLiteral:
  5063  		d := &DurationLiteral{Val: time.Duration(lhs.Val)}
  5064  		expr := reduceBinaryExprDurationLHS(op, d, rhs, loc)
  5065  		if _, ok := expr.(*BinaryExpr); !ok {
  5066  			return expr
  5067  		}
  5068  	case *StringLiteral:
  5069  		t, err := rhs.ToTimeLiteral(loc)
  5070  		if err != nil {
  5071  			break
  5072  		}
  5073  		d := &DurationLiteral{Val: time.Duration(lhs.Val)}
  5074  		expr := reduceBinaryExprDurationLHS(op, d, t, loc)
  5075  		if _, ok := expr.(*BinaryExpr); !ok {
  5076  			return expr
  5077  		}
  5078  	case *NilLiteral:
  5079  		return &BooleanLiteral{Val: false}
  5080  	}
  5081  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5082  }
  5083  
  5084  func reduceBinaryExprUnsignedLHS(op Token, lhs *UnsignedLiteral, rhs Expr) Expr {
  5085  	switch rhs := rhs.(type) {
  5086  	case *NumberLiteral:
  5087  		return reduceBinaryExprNumberLHS(op, &NumberLiteral{Val: float64(lhs.Val)}, rhs)
  5088  	case *IntegerLiteral:
  5089  		// Comparisons between an unsigned and integer literal will not involve
  5090  		// a cast if the integer is negative as that will have an improper result.
  5091  		// Look for those situations here.
  5092  		if rhs.Val < 0 {
  5093  			switch op {
  5094  			case LT, LTE:
  5095  				return &BooleanLiteral{Val: false}
  5096  			case GT, GTE:
  5097  				return &BooleanLiteral{Val: true}
  5098  			}
  5099  		}
  5100  		return reduceBinaryExprUnsignedLHS(op, lhs, &UnsignedLiteral{Val: uint64(rhs.Val)})
  5101  	case *UnsignedLiteral:
  5102  		switch op {
  5103  		case ADD:
  5104  			return &UnsignedLiteral{Val: lhs.Val + rhs.Val}
  5105  		case SUB:
  5106  			return &UnsignedLiteral{Val: lhs.Val - rhs.Val}
  5107  		case MUL:
  5108  			return &UnsignedLiteral{Val: lhs.Val * rhs.Val}
  5109  		case DIV:
  5110  			if rhs.Val == 0 {
  5111  				return &UnsignedLiteral{Val: 0}
  5112  			}
  5113  			return &UnsignedLiteral{Val: lhs.Val / rhs.Val}
  5114  		case MOD:
  5115  			if rhs.Val == 0 {
  5116  				return &UnsignedLiteral{Val: 0}
  5117  			}
  5118  			return &UnsignedLiteral{Val: lhs.Val % rhs.Val}
  5119  		case EQ:
  5120  			return &BooleanLiteral{Val: lhs.Val == rhs.Val}
  5121  		case NEQ:
  5122  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  5123  		case GT:
  5124  			return &BooleanLiteral{Val: lhs.Val > rhs.Val}
  5125  		case GTE:
  5126  			return &BooleanLiteral{Val: lhs.Val >= rhs.Val}
  5127  		case LT:
  5128  			return &BooleanLiteral{Val: lhs.Val < rhs.Val}
  5129  		case LTE:
  5130  			return &BooleanLiteral{Val: lhs.Val <= rhs.Val}
  5131  		}
  5132  	}
  5133  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5134  }
  5135  
  5136  func reduceBinaryExprNilLHS(op Token, lhs *NilLiteral, rhs Expr) Expr {
  5137  	switch op {
  5138  	case EQ, NEQ:
  5139  		return &BooleanLiteral{Val: false}
  5140  	}
  5141  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5142  }
  5143  
  5144  func reduceBinaryExprNumberLHS(op Token, lhs *NumberLiteral, rhs Expr) Expr {
  5145  	switch rhs := rhs.(type) {
  5146  	case *NumberLiteral:
  5147  		switch op {
  5148  		case ADD:
  5149  			return &NumberLiteral{Val: lhs.Val + rhs.Val}
  5150  		case SUB:
  5151  			return &NumberLiteral{Val: lhs.Val - rhs.Val}
  5152  		case MUL:
  5153  			return &NumberLiteral{Val: lhs.Val * rhs.Val}
  5154  		case DIV:
  5155  			if rhs.Val == 0 {
  5156  				return &NumberLiteral{Val: 0}
  5157  			}
  5158  			return &NumberLiteral{Val: lhs.Val / rhs.Val}
  5159  		case MOD:
  5160  			return &NumberLiteral{Val: math.Mod(lhs.Val, rhs.Val)}
  5161  		case EQ:
  5162  			return &BooleanLiteral{Val: lhs.Val == rhs.Val}
  5163  		case NEQ:
  5164  			return &BooleanLiteral{Val: lhs.Val != rhs.Val}
  5165  		case GT:
  5166  			return &BooleanLiteral{Val: lhs.Val > rhs.Val}
  5167  		case GTE:
  5168  			return &BooleanLiteral{Val: lhs.Val >= rhs.Val}
  5169  		case LT:
  5170  			return &BooleanLiteral{Val: lhs.Val < rhs.Val}
  5171  		case LTE:
  5172  			return &BooleanLiteral{Val: lhs.Val <= rhs.Val}
  5173  		}
  5174  	case *IntegerLiteral:
  5175  		switch op {
  5176  		case ADD:
  5177  			return &NumberLiteral{Val: lhs.Val + float64(rhs.Val)}
  5178  		case SUB:
  5179  			return &NumberLiteral{Val: lhs.Val - float64(rhs.Val)}
  5180  		case MUL:
  5181  			return &NumberLiteral{Val: lhs.Val * float64(rhs.Val)}
  5182  		case DIV:
  5183  			if float64(rhs.Val) == 0 {
  5184  				return &NumberLiteral{Val: 0}
  5185  			}
  5186  			return &NumberLiteral{Val: lhs.Val / float64(rhs.Val)}
  5187  		case MOD:
  5188  			return &NumberLiteral{Val: math.Mod(lhs.Val, float64(rhs.Val))}
  5189  		case EQ:
  5190  			return &BooleanLiteral{Val: lhs.Val == float64(rhs.Val)}
  5191  		case NEQ:
  5192  			return &BooleanLiteral{Val: lhs.Val != float64(rhs.Val)}
  5193  		case GT:
  5194  			return &BooleanLiteral{Val: lhs.Val > float64(rhs.Val)}
  5195  		case GTE:
  5196  			return &BooleanLiteral{Val: lhs.Val >= float64(rhs.Val)}
  5197  		case LT:
  5198  			return &BooleanLiteral{Val: lhs.Val < float64(rhs.Val)}
  5199  		case LTE:
  5200  			return &BooleanLiteral{Val: lhs.Val <= float64(rhs.Val)}
  5201  		}
  5202  	case *UnsignedLiteral:
  5203  		return reduceBinaryExprNumberLHS(op, lhs, &NumberLiteral{Val: float64(rhs.Val)})
  5204  	case *NilLiteral:
  5205  		return &BooleanLiteral{Val: false}
  5206  	}
  5207  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5208  }
  5209  
  5210  func reduceBinaryExprStringLHS(op Token, lhs *StringLiteral, rhs Expr, loc *time.Location) Expr {
  5211  	switch rhs := rhs.(type) {
  5212  	case *StringLiteral:
  5213  		switch op {
  5214  		case EQ:
  5215  			var expr Expr = &BooleanLiteral{Val: lhs.Val == rhs.Val}
  5216  			// This might be a comparison between time literals.
  5217  			// If it is, parse the time literals and then compare since it
  5218  			// could be a different result if they use different formats
  5219  			// for the same time.
  5220  			if lhs.IsTimeLiteral() && rhs.IsTimeLiteral() {
  5221  				tlhs, err := lhs.ToTimeLiteral(loc)
  5222  				if err != nil {
  5223  					return expr
  5224  				}
  5225  
  5226  				trhs, err := rhs.ToTimeLiteral(loc)
  5227  				if err != nil {
  5228  					return expr
  5229  				}
  5230  
  5231  				t := reduceBinaryExprTimeLHS(op, tlhs, trhs, loc)
  5232  				if _, ok := t.(*BinaryExpr); !ok {
  5233  					expr = t
  5234  				}
  5235  			}
  5236  			return expr
  5237  		case NEQ:
  5238  			var expr Expr = &BooleanLiteral{Val: lhs.Val != rhs.Val}
  5239  			// This might be a comparison between time literals.
  5240  			// If it is, parse the time literals and then compare since it
  5241  			// could be a different result if they use different formats
  5242  			// for the same time.
  5243  			if lhs.IsTimeLiteral() && rhs.IsTimeLiteral() {
  5244  				tlhs, err := lhs.ToTimeLiteral(loc)
  5245  				if err != nil {
  5246  					return expr
  5247  				}
  5248  
  5249  				trhs, err := rhs.ToTimeLiteral(loc)
  5250  				if err != nil {
  5251  					return expr
  5252  				}
  5253  
  5254  				t := reduceBinaryExprTimeLHS(op, tlhs, trhs, loc)
  5255  				if _, ok := t.(*BinaryExpr); !ok {
  5256  					expr = t
  5257  				}
  5258  			}
  5259  			return expr
  5260  		case ADD:
  5261  			return &StringLiteral{Val: lhs.Val + rhs.Val}
  5262  		default:
  5263  			// Attempt to convert the string literal to a time literal.
  5264  			t, err := lhs.ToTimeLiteral(loc)
  5265  			if err != nil {
  5266  				break
  5267  			}
  5268  			expr := reduceBinaryExprTimeLHS(op, t, rhs, loc)
  5269  
  5270  			// If the returned expression is still a binary expr, that means
  5271  			// we couldn't reduce it so this wasn't used in a time literal context.
  5272  			if _, ok := expr.(*BinaryExpr); !ok {
  5273  				return expr
  5274  			}
  5275  		}
  5276  	case *DurationLiteral:
  5277  		// Attempt to convert the string literal to a time literal.
  5278  		t, err := lhs.ToTimeLiteral(loc)
  5279  		if err != nil {
  5280  			break
  5281  		}
  5282  		expr := reduceBinaryExprTimeLHS(op, t, rhs, loc)
  5283  
  5284  		// If the returned expression is still a binary expr, that means
  5285  		// we couldn't reduce it so this wasn't used in a time literal context.
  5286  		if _, ok := expr.(*BinaryExpr); !ok {
  5287  			return expr
  5288  		}
  5289  	case *TimeLiteral:
  5290  		// Attempt to convert the string literal to a time literal.
  5291  		t, err := lhs.ToTimeLiteral(loc)
  5292  		if err != nil {
  5293  			break
  5294  		}
  5295  		expr := reduceBinaryExprTimeLHS(op, t, rhs, loc)
  5296  
  5297  		// If the returned expression is still a binary expr, that means
  5298  		// we couldn't reduce it so this wasn't used in a time literal context.
  5299  		if _, ok := expr.(*BinaryExpr); !ok {
  5300  			return expr
  5301  		}
  5302  	case *IntegerLiteral:
  5303  		// Attempt to convert the string literal to a time literal.
  5304  		t, err := lhs.ToTimeLiteral(loc)
  5305  		if err != nil {
  5306  			break
  5307  		}
  5308  		expr := reduceBinaryExprTimeLHS(op, t, rhs, loc)
  5309  
  5310  		// If the returned expression is still a binary expr, that means
  5311  		// we couldn't reduce it so this wasn't used in a time literal context.
  5312  		if _, ok := expr.(*BinaryExpr); !ok {
  5313  			return expr
  5314  		}
  5315  	case *NilLiteral:
  5316  		switch op {
  5317  		case EQ, NEQ:
  5318  			return &BooleanLiteral{Val: false}
  5319  		}
  5320  	}
  5321  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5322  }
  5323  
  5324  func reduceBinaryExprTimeLHS(op Token, lhs *TimeLiteral, rhs Expr, loc *time.Location) Expr {
  5325  	switch rhs := rhs.(type) {
  5326  	case *DurationLiteral:
  5327  		switch op {
  5328  		case ADD:
  5329  			return &TimeLiteral{Val: lhs.Val.Add(rhs.Val)}
  5330  		case SUB:
  5331  			return &TimeLiteral{Val: lhs.Val.Add(-rhs.Val)}
  5332  		}
  5333  	case *IntegerLiteral:
  5334  		d := &DurationLiteral{Val: time.Duration(rhs.Val)}
  5335  		expr := reduceBinaryExprTimeLHS(op, lhs, d, loc)
  5336  		if _, ok := expr.(*BinaryExpr); !ok {
  5337  			return expr
  5338  		}
  5339  	case *TimeLiteral:
  5340  		switch op {
  5341  		case SUB:
  5342  			return &DurationLiteral{Val: lhs.Val.Sub(rhs.Val)}
  5343  		case EQ:
  5344  			return &BooleanLiteral{Val: lhs.Val.Equal(rhs.Val)}
  5345  		case NEQ:
  5346  			return &BooleanLiteral{Val: !lhs.Val.Equal(rhs.Val)}
  5347  		case GT:
  5348  			return &BooleanLiteral{Val: lhs.Val.After(rhs.Val)}
  5349  		case GTE:
  5350  			return &BooleanLiteral{Val: lhs.Val.After(rhs.Val) || lhs.Val.Equal(rhs.Val)}
  5351  		case LT:
  5352  			return &BooleanLiteral{Val: lhs.Val.Before(rhs.Val)}
  5353  		case LTE:
  5354  			return &BooleanLiteral{Val: lhs.Val.Before(rhs.Val) || lhs.Val.Equal(rhs.Val)}
  5355  		}
  5356  	case *StringLiteral:
  5357  		t, err := rhs.ToTimeLiteral(loc)
  5358  		if err != nil {
  5359  			break
  5360  		}
  5361  		expr := reduceBinaryExprTimeLHS(op, lhs, t, loc)
  5362  
  5363  		// If the returned expression is still a binary expr, that means
  5364  		// we couldn't reduce it so this wasn't used in a time literal context.
  5365  		if _, ok := expr.(*BinaryExpr); !ok {
  5366  			return expr
  5367  		}
  5368  	case *NilLiteral:
  5369  		return &BooleanLiteral{Val: false}
  5370  	}
  5371  	return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs}
  5372  }
  5373  
  5374  func reduceCall(expr *Call, valuer Valuer) Expr {
  5375  	// Otherwise reduce arguments.
  5376  	var args []Expr
  5377  	literalsOnly := true
  5378  	if len(expr.Args) > 0 {
  5379  		args = make([]Expr, len(expr.Args))
  5380  		for i, arg := range expr.Args {
  5381  			args[i] = reduce(arg, valuer)
  5382  			if !isLiteral(args[i]) {
  5383  				literalsOnly = false
  5384  			}
  5385  		}
  5386  	}
  5387  
  5388  	// Evaluate a function call if the valuer is a CallValuer and
  5389  	// the arguments are only literals.
  5390  	if literalsOnly {
  5391  		if valuer, ok := valuer.(CallValuer); ok {
  5392  			argVals := make([]interface{}, len(args))
  5393  			for i := range args {
  5394  				argVals[i] = Eval(args[i], nil)
  5395  			}
  5396  			if v, ok := valuer.Call(expr.Name, argVals); ok {
  5397  				return asLiteral(v)
  5398  			}
  5399  		}
  5400  	}
  5401  	return &Call{Name: expr.Name, Args: args}
  5402  }
  5403  
  5404  func reduceParenExpr(expr *ParenExpr, valuer Valuer) Expr {
  5405  	subexpr := reduce(expr.Expr, valuer)
  5406  	if subexpr, ok := subexpr.(*BinaryExpr); ok {
  5407  		return &ParenExpr{Expr: subexpr}
  5408  	}
  5409  	return subexpr
  5410  }
  5411  
  5412  func reduceVarRef(expr *VarRef, valuer Valuer) Expr {
  5413  	// Ignore if there is no valuer.
  5414  	if valuer == nil {
  5415  		return &VarRef{Val: expr.Val, Type: expr.Type}
  5416  	}
  5417  
  5418  	// Retrieve the value of the ref.
  5419  	// Ignore if the value doesn't exist.
  5420  	v, ok := valuer.Value(expr.Val)
  5421  	if !ok {
  5422  		return &VarRef{Val: expr.Val, Type: expr.Type}
  5423  	}
  5424  
  5425  	// Return the value as a literal.
  5426  	return asLiteral(v)
  5427  }
  5428  
  5429  // asLiteral takes an interface and converts it into an influxql literal.
  5430  func asLiteral(v interface{}) Literal {
  5431  	switch v := v.(type) {
  5432  	case bool:
  5433  		return &BooleanLiteral{Val: v}
  5434  	case time.Duration:
  5435  		return &DurationLiteral{Val: v}
  5436  	case float64:
  5437  		return &NumberLiteral{Val: v}
  5438  	case int64:
  5439  		return &IntegerLiteral{Val: v}
  5440  	case string:
  5441  		return &StringLiteral{Val: v}
  5442  	case time.Time:
  5443  		return &TimeLiteral{Val: v}
  5444  	default:
  5445  		return &NilLiteral{}
  5446  	}
  5447  }
  5448  
  5449  // isLiteral returns if the expression is a literal.
  5450  func isLiteral(expr Expr) bool {
  5451  	_, ok := expr.(Literal)
  5452  	return ok
  5453  }
  5454  
  5455  // Valuer is the interface that wraps the Value() method.
  5456  type Valuer interface {
  5457  	// Value returns the value and existence flag for a given key.
  5458  	Value(key string) (interface{}, bool)
  5459  }
  5460  
  5461  // CallValuer implements the Call method for evaluating function calls.
  5462  type CallValuer interface {
  5463  	Valuer
  5464  
  5465  	// Call is invoked to evaluate a function call (if possible).
  5466  	Call(name string, args []interface{}) (interface{}, bool)
  5467  }
  5468  
  5469  // ZoneValuer is the interface that specifies the current time zone.
  5470  type ZoneValuer interface {
  5471  	Valuer
  5472  
  5473  	// Zone returns the time zone location. This function may return nil
  5474  	// if no time zone is known.
  5475  	Zone() *time.Location
  5476  }
  5477  
  5478  var _ CallValuer = (*NowValuer)(nil)
  5479  var _ ZoneValuer = (*NowValuer)(nil)
  5480  
  5481  // NowValuer returns only the value for "now()".
  5482  type NowValuer struct {
  5483  	Now      time.Time
  5484  	Location *time.Location
  5485  }
  5486  
  5487  // Value is a method that returns the value and existence flag for a given key.
  5488  func (v *NowValuer) Value(key string) (interface{}, bool) {
  5489  	if !v.Now.IsZero() && key == "now()" {
  5490  		return v.Now, true
  5491  	}
  5492  	return nil, false
  5493  }
  5494  
  5495  // Call evaluates the now() function to replace now() with the current time.
  5496  func (v *NowValuer) Call(name string, args []interface{}) (interface{}, bool) {
  5497  	if name == "now" && len(args) == 0 {
  5498  		return v.Now, true
  5499  	}
  5500  	return nil, false
  5501  }
  5502  
  5503  // Zone is a method that returns the time.Location.
  5504  func (v *NowValuer) Zone() *time.Location {
  5505  	if v.Location != nil {
  5506  		return v.Location
  5507  	}
  5508  	return nil
  5509  }
  5510  
  5511  // MultiValuer returns a Valuer that iterates over multiple Valuer instances
  5512  // to find a match.
  5513  func MultiValuer(valuers ...Valuer) Valuer {
  5514  	return multiValuer(valuers)
  5515  }
  5516  
  5517  type multiValuer []Valuer
  5518  
  5519  var _ CallValuer = multiValuer(nil)
  5520  var _ ZoneValuer = multiValuer(nil)
  5521  
  5522  func (a multiValuer) Value(key string) (interface{}, bool) {
  5523  	for _, valuer := range a {
  5524  		if v, ok := valuer.Value(key); ok {
  5525  			return v, true
  5526  		}
  5527  	}
  5528  	return nil, false
  5529  }
  5530  
  5531  func (a multiValuer) Call(name string, args []interface{}) (interface{}, bool) {
  5532  	for _, valuer := range a {
  5533  		if valuer, ok := valuer.(CallValuer); ok {
  5534  			if v, ok := valuer.Call(name, args); ok {
  5535  				return v, true
  5536  			}
  5537  		}
  5538  	}
  5539  	return nil, false
  5540  }
  5541  
  5542  func (a multiValuer) Zone() *time.Location {
  5543  	for _, valuer := range a {
  5544  		if valuer, ok := valuer.(ZoneValuer); ok {
  5545  			if v := valuer.Zone(); v != nil {
  5546  				return v
  5547  			}
  5548  		}
  5549  	}
  5550  	return nil
  5551  }
  5552  
  5553  // ContainsVarRef returns true if expr is a VarRef or contains one.
  5554  func ContainsVarRef(expr Expr) bool {
  5555  	var v containsVarRefVisitor
  5556  	Walk(&v, expr)
  5557  	return v.contains
  5558  }
  5559  
  5560  type containsVarRefVisitor struct {
  5561  	contains bool
  5562  }
  5563  
  5564  func (v *containsVarRefVisitor) Visit(n Node) Visitor {
  5565  	switch n.(type) {
  5566  	case *Call:
  5567  		return nil
  5568  	case *VarRef:
  5569  		v.contains = true
  5570  	}
  5571  	return v
  5572  }
  5573  
  5574  func IsSelector(expr Expr) bool {
  5575  	if call, ok := expr.(*Call); ok {
  5576  		switch call.Name {
  5577  		case "first", "last", "min", "max", "percentile", "sample", "top", "bottom":
  5578  			return true
  5579  		}
  5580  	}
  5581  	return false
  5582  }
  5583  
  5584  // stringSetSlice returns a sorted slice of keys from a string set.
  5585  func stringSetSlice(m map[string]struct{}) []string {
  5586  	if m == nil {
  5587  		return nil
  5588  	}
  5589  
  5590  	a := make([]string, 0, len(m))
  5591  	for k := range m {
  5592  		a = append(a, k)
  5593  	}
  5594  	sort.Strings(a)
  5595  	return a
  5596  }
  5597  
  5598  // TimeRange represents a range of time from Min to Max. The times are inclusive.
  5599  type TimeRange struct {
  5600  	Min, Max time.Time
  5601  }
  5602  
  5603  // Intersect joins this TimeRange with another TimeRange.
  5604  func (t TimeRange) Intersect(other TimeRange) TimeRange {
  5605  	if !other.Min.IsZero() {
  5606  		if t.Min.IsZero() || other.Min.After(t.Min) {
  5607  			t.Min = other.Min
  5608  		}
  5609  	}
  5610  	if !other.Max.IsZero() {
  5611  		if t.Max.IsZero() || other.Max.Before(t.Max) {
  5612  			t.Max = other.Max
  5613  		}
  5614  	}
  5615  	return t
  5616  }
  5617  
  5618  // IsZero is true if the min and max of the time range are zero.
  5619  func (t TimeRange) IsZero() bool {
  5620  	return t.Min.IsZero() && t.Max.IsZero()
  5621  }
  5622  
  5623  // Used by TimeRange methods.
  5624  var minTime = time.Unix(0, MinTime)
  5625  var maxTime = time.Unix(0, MaxTime)
  5626  
  5627  // MinTime returns the minimum time of the TimeRange.
  5628  // If the minimum time is zero, this returns the minimum possible time.
  5629  func (t TimeRange) MinTime() time.Time {
  5630  	if t.Min.IsZero() {
  5631  		return minTime
  5632  	}
  5633  	return t.Min
  5634  }
  5635  
  5636  // MaxTime returns the maximum time of the TimeRange.
  5637  // If the maximum time is zero, this returns the maximum possible time.
  5638  func (t TimeRange) MaxTime() time.Time {
  5639  	if t.Max.IsZero() {
  5640  		return maxTime
  5641  	}
  5642  	return t.Max
  5643  }
  5644  
  5645  // MinTimeNano returns the minimum time in nanoseconds since the epoch.
  5646  // If the minimum time is zero, this returns the minimum possible time.
  5647  func (t TimeRange) MinTimeNano() int64 {
  5648  	if t.Min.IsZero() {
  5649  		return MinTime
  5650  	}
  5651  	return t.Min.UnixNano()
  5652  }
  5653  
  5654  // MaxTimeNano returns the maximum time in nanoseconds since the epoch.
  5655  // If the maximum time is zero, this returns the maximum possible time.
  5656  func (t TimeRange) MaxTimeNano() int64 {
  5657  	if t.Max.IsZero() {
  5658  		return MaxTime
  5659  	}
  5660  	return t.Max.UnixNano()
  5661  }
  5662  
  5663  // ConditionExpr extracts the time range and the condition from an expression.
  5664  // We only support simple time ranges that are constrained with AND and are not nested.
  5665  // This throws an error when we encounter a time condition that is combined with OR
  5666  // to prevent returning unexpected results that we do not support.
  5667  func ConditionExpr(cond Expr, valuer Valuer) (Expr, TimeRange, error) {
  5668  	expr, tr, err := conditionExpr(cond, valuer)
  5669  
  5670  	// Remove top level parentheses
  5671  	if e, ok := expr.(*ParenExpr); ok {
  5672  		expr = e.Expr
  5673  	}
  5674  
  5675  	if e, ok := expr.(*BooleanLiteral); ok && e.Val {
  5676  		// If the condition is true, return nil instead to indicate there
  5677  		// is no condition.
  5678  		expr = nil
  5679  	}
  5680  	return expr, tr, err
  5681  }
  5682  
  5683  func conditionExpr(cond Expr, valuer Valuer) (Expr, TimeRange, error) {
  5684  	if cond == nil {
  5685  		return nil, TimeRange{}, nil
  5686  	}
  5687  
  5688  	switch cond := cond.(type) {
  5689  	case *BinaryExpr:
  5690  		if cond.Op == AND || cond.Op == OR {
  5691  			lhsExpr, lhsTime, err := conditionExpr(cond.LHS, valuer)
  5692  			if err != nil {
  5693  				return nil, TimeRange{}, err
  5694  			}
  5695  
  5696  			rhsExpr, rhsTime, err := conditionExpr(cond.RHS, valuer)
  5697  			if err != nil {
  5698  				return nil, TimeRange{}, err
  5699  			}
  5700  
  5701  			// Always intersect the time range even if it makes no sense.
  5702  			// There is no such thing as using OR with a time range.
  5703  			timeRange := lhsTime.Intersect(rhsTime)
  5704  
  5705  			// Combine the left and right expression.
  5706  			if rhsExpr == nil {
  5707  				return lhsExpr, timeRange, nil
  5708  			} else if lhsExpr == nil {
  5709  				return rhsExpr, timeRange, nil
  5710  			}
  5711  			return reduce(&BinaryExpr{
  5712  				Op:  cond.Op,
  5713  				LHS: lhsExpr,
  5714  				RHS: rhsExpr,
  5715  			}, nil), timeRange, nil
  5716  		}
  5717  
  5718  		// If either the left or the right side is "time", we are looking at
  5719  		// a time range.
  5720  		if lhs, ok := cond.LHS.(*VarRef); ok && strings.ToLower(lhs.Val) == "time" {
  5721  			timeRange, err := getTimeRange(cond.Op, cond.RHS, valuer)
  5722  			return nil, timeRange, err
  5723  		} else if rhs, ok := cond.RHS.(*VarRef); ok && strings.ToLower(rhs.Val) == "time" {
  5724  			// Swap the op for the opposite if it is a comparison.
  5725  			op := cond.Op
  5726  			switch op {
  5727  			case GT:
  5728  				op = LT
  5729  			case LT:
  5730  				op = GT
  5731  			case GTE:
  5732  				op = LTE
  5733  			case LTE:
  5734  				op = GTE
  5735  			}
  5736  			timeRange, err := getTimeRange(op, cond.LHS, valuer)
  5737  			return nil, timeRange, err
  5738  		}
  5739  		return reduce(cond, valuer), TimeRange{}, nil
  5740  	case *ParenExpr:
  5741  		expr, timeRange, err := conditionExpr(cond.Expr, valuer)
  5742  		if err != nil {
  5743  			return nil, TimeRange{}, err
  5744  		} else if expr == nil {
  5745  			return nil, timeRange, nil
  5746  		}
  5747  		return reduce(&ParenExpr{Expr: expr}, nil), timeRange, nil
  5748  	case *BooleanLiteral:
  5749  		return cond, TimeRange{}, nil
  5750  	default:
  5751  		return nil, TimeRange{}, fmt.Errorf("invalid condition expression: %s", cond)
  5752  	}
  5753  }
  5754  
  5755  // getTimeRange returns the time range associated with this comparison.
  5756  // op is the operation that is used for comparison and rhs is the right hand side
  5757  // of the expression. The left hand side is always assumed to be "time".
  5758  func getTimeRange(op Token, rhs Expr, valuer Valuer) (TimeRange, error) {
  5759  	// If literal looks like a date time then parse it as a time literal.
  5760  	if strlit, ok := rhs.(*StringLiteral); ok {
  5761  		if strlit.IsTimeLiteral() {
  5762  			var loc *time.Location
  5763  			if valuer, ok := valuer.(ZoneValuer); ok {
  5764  				loc = valuer.Zone()
  5765  			}
  5766  			t, err := strlit.ToTimeLiteral(loc)
  5767  			if err != nil {
  5768  				return TimeRange{}, err
  5769  			}
  5770  			rhs = t
  5771  		}
  5772  	}
  5773  
  5774  	// Evaluate the RHS to replace "now()" with the current time.
  5775  	rhs = Reduce(rhs, valuer)
  5776  
  5777  	var value time.Time
  5778  	switch lit := rhs.(type) {
  5779  	case *TimeLiteral:
  5780  		if lit.Val.After(time.Unix(0, MaxTime)) {
  5781  			return TimeRange{}, fmt.Errorf("time %s overflows time literal", lit.Val.Format(time.RFC3339))
  5782  		} else if lit.Val.Before(time.Unix(0, MinTime+1)) {
  5783  			// The minimum allowable time literal is one greater than the minimum time because the minimum time
  5784  			// is a sentinel value only used internally.
  5785  			return TimeRange{}, fmt.Errorf("time %s underflows time literal", lit.Val.Format(time.RFC3339))
  5786  		}
  5787  		value = lit.Val
  5788  	case *DurationLiteral:
  5789  		value = time.Unix(0, int64(lit.Val)).UTC()
  5790  	case *NumberLiteral:
  5791  		value = time.Unix(0, int64(lit.Val)).UTC()
  5792  	case *IntegerLiteral:
  5793  		value = time.Unix(0, lit.Val).UTC()
  5794  	default:
  5795  		return TimeRange{}, fmt.Errorf("invalid operation: time and %T are not compatible", lit)
  5796  	}
  5797  
  5798  	timeRange := TimeRange{}
  5799  	switch op {
  5800  	case GT:
  5801  		timeRange.Min = value.Add(time.Nanosecond)
  5802  	case GTE:
  5803  		timeRange.Min = value
  5804  	case LT:
  5805  		timeRange.Max = value.Add(-time.Nanosecond)
  5806  	case LTE:
  5807  		timeRange.Max = value
  5808  	case EQ:
  5809  		timeRange.Min, timeRange.Max = value, value
  5810  	default:
  5811  		return TimeRange{}, fmt.Errorf("invalid time comparison operator: %s", op)
  5812  	}
  5813  	return timeRange, nil
  5814  }