Skip to content

Commit

Permalink
Merge pull request #6374 from influxdata/js-6296-rename-time-field
Browse files Browse the repository at this point in the history
Allow the implicit time field to be renamed
  • Loading branch information
jsternberg committed Apr 14, 2016
2 parents f029d80 + 9d01f3a commit 64dd2c8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [#6287](https://github.com/influxdata/influxdb/issues/6287): Fix data race in Influx Client.
- [#6252](https://github.com/influxdata/influxdb/pull/6252): Remove TSDB listener accept message @simnv
- [#6202](https://github.com/influxdata/influxdb/pull/6202): Check default SHARD DURATION when recreating the same database.
- [#6296](https://github.com/influxdata/influxdb/issues/6296): Allow the implicit time field to be renamed again.

## v0.12.0 [2016-04-05]
### Release Notes
Expand Down
14 changes: 13 additions & 1 deletion influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ type SelectStatement struct {
// The value to fill empty aggregate buckets with, if any
FillValue interface{}

// Renames the implicit time field name.
TimeAlias string

// Removes the "time" column from the output.
OmitTime bool

Expand Down Expand Up @@ -968,6 +971,14 @@ func (s *SelectStatement) TimeAscending() bool {
return len(s.SortFields) == 0 || s.SortFields[0].Ascending
}

// TimeFieldName returns the name of the time field.
func (s *SelectStatement) TimeFieldName() string {
if s.TimeAlias != "" {
return s.TimeAlias
}
return "time"
}

// Clone returns a deep copy of the statement.
func (s *SelectStatement) Clone() *SelectStatement {
clone := &SelectStatement{
Expand Down Expand Up @@ -1133,6 +1144,7 @@ func (s *SelectStatement) RewriteTimeFields() {
switch expr := s.Fields[i].Expr.(type) {
case *VarRef:
if expr.Val == "time" {
s.TimeAlias = s.Fields[i].Alias
s.Fields = append(s.Fields[:i], s.Fields[i+1:]...)
}
}
Expand Down Expand Up @@ -1169,7 +1181,7 @@ func (s *SelectStatement) ColumnNames() []string {
columnNames := make([]string, len(columnFields)+offset)
if !s.OmitTime {
// Add the implicit time if requested.
columnNames[0] = "time"
columnNames[0] = s.TimeFieldName()
}

// Keep track of the encountered column names.
Expand Down
59 changes: 59 additions & 0 deletions influxql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,56 @@ func TestSelectStatement_RewriteWildcards(t *testing.T) {
}
}

// Test SELECT statement time field rewrite.
func TestSelectStatement_RewriteTimeFields(t *testing.T) {
var tests = []struct {
s string
stmt influxql.Statement
}{
{
s: `SELECT time, field1 FROM cpu`,
stmt: &influxql.SelectStatement{
IsRawQuery: true,
Fields: []*influxql.Field{
{Expr: &influxql.VarRef{Val: "field1"}},
},
Sources: []influxql.Source{
&influxql.Measurement{Name: "cpu"},
},
},
},
{
s: `SELECT time AS timestamp, field1 FROM cpu`,
stmt: &influxql.SelectStatement{
IsRawQuery: true,
Fields: []*influxql.Field{
{Expr: &influxql.VarRef{Val: "field1"}},
},
Sources: []influxql.Source{
&influxql.Measurement{Name: "cpu"},
},
TimeAlias: "timestamp",
},
},
}

for i, tt := range tests {
// Parse statement.
stmt, err := influxql.NewParser(strings.NewReader(tt.s)).ParseStatement()
if err != nil {
t.Fatalf("invalid statement: %q: %s", tt.s, err)
}

// Rewrite statement.
stmt.(*influxql.SelectStatement).RewriteTimeFields()
if !reflect.DeepEqual(tt.stmt, stmt) {
t.Logf("\n# %s\nexp=%s\ngot=%s\n", tt.s, mustMarshalJSON(tt.stmt), mustMarshalJSON(stmt))
t.Logf("\nSQL exp=%s\nSQL got=%s\n", tt.stmt.String(), stmt.String())
t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
}
}
}

// Ensure that the IsRawQuery flag gets set properly
func TestSelectStatement_IsRawQuerySet(t *testing.T) {
var tests = []struct {
Expand Down Expand Up @@ -1248,6 +1298,15 @@ func TestSelect_ColumnNames(t *testing.T) {
},
columns: []string{"time", "value_1", "value", "value_2"},
},
{
stmt: &influxql.SelectStatement{
Fields: influxql.Fields([]*influxql.Field{
{Expr: &influxql.VarRef{Val: "value"}},
}),
TimeAlias: "timestamp",
},
columns: []string{"timestamp", "value"},
},
} {
columns := tt.stmt.ColumnNames()
if !reflect.DeepEqual(columns, tt.columns) {
Expand Down

0 comments on commit 64dd2c8

Please sign in to comment.