Skip to content

Commit

Permalink
Merge pull request influxdata#6038 from influxdata/js-6034-empty-quer…
Browse files Browse the repository at this point in the history
…y-result-when-using-name

Rename the special condition "name" to "_name" to reduce conflicts
  • Loading branch information
jsternberg committed Mar 17, 2016
2 parents 6a461d8 + d75428f commit 9f2b1ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
27 changes: 4 additions & 23 deletions influxql/statement_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,14 @@ func RewriteStatement(stmt Statement) (Statement, error) {
}

func rewriteShowFieldKeysStatement(stmt *ShowFieldKeysStatement) (Statement, error) {
var condition Expr
if len(stmt.Sources) > 0 {
if source, ok := stmt.Sources[0].(*Measurement); ok {
if source.Regex != nil {
condition = &BinaryExpr{
Op: EQREGEX,
LHS: &VarRef{Val: "name"},
RHS: &RegexLiteral{Val: source.Regex.Val},
}
} else if source.Name != "" {
condition = &BinaryExpr{
Op: EQ,
LHS: &VarRef{Val: "name"},
RHS: &StringLiteral{Val: source.Name},
}
}
}
}

return &SelectStatement{
Fields: Fields([]*Field{
{Expr: &VarRef{Val: "fieldKey"}},
}),
Sources: Sources([]Source{
&Measurement{Name: "_fieldKeys"},
}),
Condition: condition,
Condition: rewriteSourcesCondition(stmt.Sources, nil),
Offset: stmt.Offset,
Limit: stmt.Limit,
SortFields: stmt.SortFields,
Expand All @@ -69,7 +50,7 @@ func rewriteShowMeasurementsStatement(stmt *ShowMeasurementsStatement) (Statemen

return &SelectStatement{
Fields: Fields([]*Field{
{Expr: &VarRef{Val: "name"}},
{Expr: &VarRef{Val: "_name"}, Alias: "name"},
}),
Sources: Sources([]Source{
&Measurement{Name: "_measurements"},
Expand Down Expand Up @@ -201,13 +182,13 @@ func rewriteSourcesCondition(sources Sources, cond Expr) Expr {
if mm.Regex != nil {
expr = &BinaryExpr{
Op: EQREGEX,
LHS: &VarRef{Val: "name"},
LHS: &VarRef{Val: "_name"},
RHS: &RegexLiteral{Val: mm.Regex.Val},
}
} else if mm.Name != "" {
expr = &BinaryExpr{
Op: EQ,
LHS: &VarRef{Val: "name"},
LHS: &VarRef{Val: "_name"},
RHS: &StringLiteral{Val: mm.Name},
}
}
Expand Down
24 changes: 12 additions & 12 deletions influxql/statement_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,59 @@ func TestRewriteStatement(t *testing.T) {
},
{
stmt: `SHOW FIELD KEYS FROM cpu`,
s: `SELECT fieldKey FROM _fieldKeys WHERE "name" = 'cpu'`,
s: `SELECT fieldKey FROM _fieldKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW FIELD KEYS FROM /c.*/`,
s: `SELECT fieldKey FROM _fieldKeys WHERE "name" =~ /c.*/`,
s: `SELECT fieldKey FROM _fieldKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW MEASUREMENTS`,
s: `SELECT "name" FROM _measurements`,
s: `SELECT _name AS "name" FROM _measurements`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT = cpu`,
s: `SELECT "name" FROM _measurements WHERE "name" = 'cpu'`,
s: `SELECT _name AS "name" FROM _measurements WHERE _name = 'cpu'`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT =~ /c.*/`,
s: `SELECT "name" FROM _measurements WHERE "name" =~ /c.*/`,
s: `SELECT _name AS "name" FROM _measurements WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW MEASUREMENTS WHERE region = 'uswest'`,
s: `SELECT "name" FROM _measurements WHERE region = 'uswest'`,
s: `SELECT _name AS "name" FROM _measurements WHERE region = 'uswest'`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT = cpu WHERE region = 'uswest'`,
s: `SELECT "name" FROM _measurements WHERE ("name" = 'cpu') AND (region = 'uswest')`,
s: `SELECT _name AS "name" FROM _measurements WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW TAG KEYS`,
s: `SELECT tagKey FROM _tagKeys`,
},
{
stmt: `SHOW TAG KEYS FROM cpu`,
s: `SELECT tagKey FROM _tagKeys WHERE "name" = 'cpu'`,
s: `SELECT tagKey FROM _tagKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW TAG KEYS FROM /c.*/`,
s: `SELECT tagKey FROM _tagKeys WHERE "name" =~ /c.*/`,
s: `SELECT tagKey FROM _tagKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS FROM cpu WHERE region = 'uswest'`,
s: `SELECT tagKey FROM _tagKeys WHERE ("name" = 'cpu') AND (region = 'uswest')`,
s: `SELECT tagKey FROM _tagKeys WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW TAG VALUES WITH KEY = region`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE _tagKey = 'region'`,
},
{
stmt: `SHOW TAG VALUES FROM cpu WITH KEY = region`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE ("name" = 'cpu') AND (_tagKey = 'region')`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE (_name = 'cpu') AND (_tagKey = 'region')`,
},
{
stmt: `SHOW TAG VALUES FROM cpu WITH KEY IN (region, host)`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE ("name" = 'cpu') AND (_tagKey = 'region' OR _tagKey = 'host')`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE (_name = 'cpu') AND (_tagKey = 'region' OR _tagKey = 'host')`,
},
{
stmt: `SELECT value FROM cpu`,
Expand Down
14 changes: 7 additions & 7 deletions tsdb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (d *DatabaseIndex) measurementsByExpr(expr influxql.Expr) (Measurements, bo
}

// Match on name, if specified.
if tag.Val == "name" {
if tag.Val == "_name" {
return d.measurementsByNameFilter(tf.Op, tf.Value, tf.Regex), true, nil
} else if influxql.IsSystemName(tag.Val) {
return nil, false, nil
Expand Down Expand Up @@ -743,21 +743,21 @@ func (m *Measurement) idsForExpr(n *influxql.BinaryExpr) (SeriesIDs, influxql.Ex

// For fields, return all series IDs from this measurement and return
// the expression passed in, as the filter.
if name.Val != "name" && m.HasField(name.Val) {
if name.Val != "_name" && m.HasField(name.Val) {
return m.seriesIDs, n, nil
}

tagVals, ok := m.seriesByTagKeyValue[name.Val]
if name.Val != "name" && !ok {
if name.Val != "_name" && !ok {
return nil, nil, nil
}

// if we're looking for series with a specific tag value
if str, ok := value.(*influxql.StringLiteral); ok {
var ids SeriesIDs

// Special handling for "name" to match measurement name.
if name.Val == "name" {
// Special handling for "_name" to match measurement name.
if name.Val == "_name" {
if (n.Op == influxql.EQ && str.Val == m.Name) || (n.Op == influxql.NEQ && str.Val != m.Name) {
return m.seriesIDs, &influxql.BooleanLiteral{Val: true}, nil
}
Expand All @@ -777,8 +777,8 @@ func (m *Measurement) idsForExpr(n *influxql.BinaryExpr) (SeriesIDs, influxql.Ex
if re, ok := value.(*influxql.RegexLiteral); ok {
var ids SeriesIDs

// Special handling for "name" to match measurement name.
if name.Val == "name" {
// Special handling for "_name" to match measurement name.
if name.Val == "_name" {
match := re.Val.MatchString(m.Name)
if (n.Op == influxql.EQREGEX && match) || (n.Op == influxql.NEQREGEX && !match) {
return m.seriesIDs, &influxql.BooleanLiteral{Val: true}, nil
Expand Down

0 comments on commit 9f2b1ee

Please sign in to comment.