Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the implicit time field to be renamed #6374

Merged
merged 1 commit into from
Apr 14, 2016

Conversation

jsternberg
Copy link
Contributor

Fixes #6296.

@jsternberg
Copy link
Contributor Author

@benbjohnson

@benbjohnson
Copy link
Contributor

@jsternberg Can you set TimeFieldName to "time" instead of "" if it's not aliases. Then we won't have to worry about checking default values during processing.

@jsternberg
Copy link
Contributor Author

The reason why I didn't do that is so there was less code to change and so I didn't have to add that logic to the parser. The diff would be a lot larger if that field is set to "time" by default.

I think I'm going to rename TimeFieldName to TimeAlias so that it's more consistent with the Alias attribute in Field. This might also make it more clear that the alias may be empty.

@jsternberg jsternberg force-pushed the js-6296-rename-time-field branch from 49a80fb to ed9f8c8 Compare April 13, 2016 23:19
@benbjohnson
Copy link
Contributor

Can you give some examples of what needs to change? It seems like adding time alias checking everywhere in the future would be error prone.

@jsternberg
Copy link
Contributor Author

It depends on where we set the TimeAlias. If we set it as part of parseSelectStatement, then I have to modify all of the select statement tests inside of parser_test.go. If we set it in RewriteTimeFields, then I don't have to change it anywhere since that's not really used during tests, but I also don't think it's the appropriate place to put it.

I also think that checking for the empty string in one place isn't too hard and it only has to be done in one place.

@benbjohnson
Copy link
Contributor

If it's just test code that's not that bad. We may only have one place to do the if statement right now but queries are going to get more complicated as we deal with referencing times in subqueries and that's just going to grow.

@jsternberg
Copy link
Contributor Author

I don't think that really applies for this. This code does not rename the time field. It is merely a placeholder to hold onto the alias if there is a time present in the query when the query is rewritten. Another location that has to be changed is the code that sets this to begin with. Currently it's just this:

func (s *SelectStatement) RewriteTimeFields() {
    for i := 0; i < len(s.Fields); i++ {
        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:]...)
            }
        }
    }
}

This has to be changed to:

func (s *SelectStatement) RewriteTimeFields() {
    for i := 0; i < len(s.Fields); i++ {
        switch expr := s.Fields[i].Expr.(type) {
        case *VarRef:
            if expr.Val == "time" {
                if alias := s.Fields[i].Alias; alias != "" {
                    s.TimeAlias = alias
                }
                s.Fields = append(s.Fields[:i], s.Fields[i+1:]...)
            }
        }
    }
}

We don't allow aliases to be used in conditions at the moment. Is that something we're supposed to be doing? I don't really see this as affecting subqueries because these values are only used for setting the column names.

> select * from cpu
name: cpu
---------
time                    value
1460579388339842107     2
1460597252611368284     5

> select value as v from cpu where v > 2
>

If it makes it better, I can add a function to the SelectStatement that does the if statement. That might be a better solution as then it matches the same code we use for other fields. Something like:

func (s *SelectStatement) TimeFieldName() string {
    if s.TimeAlias != "" {
        return s.TimeAlias
    }
    return "time"
}

@jsternberg jsternberg force-pushed the js-6296-rename-time-field branch 2 times, most recently from a3a236e to 0a03164 Compare April 14, 2016 14:44
@benbjohnson
Copy link
Contributor

OK, leave off the "time" for now. We can revisit if it becomes an issue.

@benbjohnson
Copy link
Contributor

Otherwise lgtm

@jsternberg jsternberg force-pushed the js-6296-rename-time-field branch from 0a03164 to 9d01f3a Compare April 14, 2016 15:04
@e-dard
Copy link
Contributor

e-dard commented Apr 14, 2016

LGTM 👍

@jsternberg jsternberg merged commit 64dd2c8 into master Apr 14, 2016
@jsternberg jsternberg deleted the js-6296-rename-time-field branch April 14, 2016 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants