Merge pull request #7304 from influxdata/js-remove-substatement-method
Remove defunct `Substatement()` callpull/7268/head
commit
635ce337f0
|
@ -1921,71 +1921,6 @@ func (s *SelectStatement) rewriteWithoutTimeDimensions() string {
|
|||
return n.String()
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
BinaryExpr
|
||||
|
||||
SELECT mean(xxx.value) + avg(yyy.value) FROM xxx JOIN yyy WHERE xxx.host = 123
|
||||
|
||||
from xxx where host = 123
|
||||
select avg(value) from yyy where host = 123
|
||||
|
||||
SELECT xxx.value FROM xxx WHERE xxx.host = 123
|
||||
SELECT yyy.value FROM yyy
|
||||
|
||||
---
|
||||
|
||||
SELECT MEAN(xxx.value) + MEAN(cpu.load.value)
|
||||
FROM xxx JOIN yyy
|
||||
GROUP BY host
|
||||
WHERE (xxx.region == "uswest" OR yyy.region == "uswest") AND xxx.otherfield == "XXX"
|
||||
|
||||
select * from (
|
||||
select mean + mean from xxx join yyy
|
||||
group by time(5m), host
|
||||
) (xxx.region == "uswest" OR yyy.region == "uswest") AND xxx.otherfield == "XXX"
|
||||
|
||||
(seriesIDS for xxx.region = 'uswest' union seriesIDs for yyy.regnion = 'uswest') | seriesIDS xxx.otherfield = 'XXX'
|
||||
|
||||
WHERE xxx.region == "uswest" AND xxx.otherfield == "XXX"
|
||||
WHERE yyy.region == "uswest"
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// Substatement returns a single-series statement for a given variable reference.
|
||||
func (s *SelectStatement) Substatement(ref *VarRef) (*SelectStatement, error) {
|
||||
// Copy dimensions and properties to new statement.
|
||||
other := &SelectStatement{
|
||||
Fields: Fields{{Expr: ref}},
|
||||
Dimensions: s.Dimensions,
|
||||
Limit: s.Limit,
|
||||
Offset: s.Offset,
|
||||
SortFields: s.SortFields,
|
||||
}
|
||||
|
||||
// If there is only one series source then return it with the whole condition.
|
||||
if len(s.Sources) == 1 {
|
||||
other.Sources = s.Sources
|
||||
other.Condition = s.Condition
|
||||
return other, nil
|
||||
}
|
||||
|
||||
// Find the matching source.
|
||||
name := MatchSource(s.Sources, ref.Val)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("field source not found: %s", ref.Val)
|
||||
}
|
||||
other.Sources = append(other.Sources, &Measurement{Name: name})
|
||||
|
||||
// Filter out conditions.
|
||||
if s.Condition != nil {
|
||||
other.Condition = filterExprBySource(name, s.Condition)
|
||||
}
|
||||
|
||||
return other, nil
|
||||
}
|
||||
|
||||
// NamesInWhere returns the field and tag names (idents) referenced in the where clause
|
||||
func (s *SelectStatement) NamesInWhere() []string {
|
||||
var a []string
|
||||
|
|
|
@ -61,77 +61,6 @@ func TestDataType_String(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Ensure the SELECT statement can extract substatements.
|
||||
func TestSelectStatement_Substatement(t *testing.T) {
|
||||
var tests = []struct {
|
||||
stmt string
|
||||
expr *influxql.VarRef
|
||||
sub string
|
||||
err string
|
||||
}{
|
||||
// 0. Single series
|
||||
{
|
||||
stmt: `SELECT value FROM myseries WHERE value > 1`,
|
||||
expr: &influxql.VarRef{Val: "value"},
|
||||
sub: `SELECT value FROM myseries WHERE value > 1`,
|
||||
},
|
||||
|
||||
// 1. Simple join
|
||||
{
|
||||
stmt: `SELECT sum(aa.value) + sum(bb.value) FROM aa, bb`,
|
||||
expr: &influxql.VarRef{Val: "aa.value"},
|
||||
sub: `SELECT "aa.value" FROM aa`,
|
||||
},
|
||||
|
||||
// 2. Simple merge
|
||||
{
|
||||
stmt: `SELECT sum(aa.value) + sum(bb.value) FROM aa, bb`,
|
||||
expr: &influxql.VarRef{Val: "bb.value"},
|
||||
sub: `SELECT "bb.value" FROM bb`,
|
||||
},
|
||||
|
||||
// 3. Join with condition
|
||||
{
|
||||
stmt: `SELECT sum(aa.value) + sum(bb.value) FROM aa, bb WHERE aa.host = 'servera' AND bb.host = 'serverb'`,
|
||||
expr: &influxql.VarRef{Val: "bb.value"},
|
||||
sub: `SELECT "bb.value" FROM bb WHERE "bb.host" = 'serverb'`,
|
||||
},
|
||||
|
||||
// 4. Join with complex condition
|
||||
{
|
||||
stmt: `SELECT sum(aa.value) + sum(bb.value) FROM aa, bb WHERE aa.host = 'servera' AND (bb.host = 'serverb' OR bb.host = 'serverc') AND 1 = 2`,
|
||||
expr: &influxql.VarRef{Val: "bb.value"},
|
||||
sub: `SELECT "bb.value" FROM bb WHERE ("bb.host" = 'serverb' OR "bb.host" = 'serverc') AND 1 = 2`,
|
||||
},
|
||||
|
||||
// 5. 4 with different condition order
|
||||
{
|
||||
stmt: `SELECT sum(aa.value) + sum(bb.value) FROM aa, bb WHERE ((bb.host = 'serverb' OR bb.host = 'serverc') AND aa.host = 'servera') AND 1 = 2`,
|
||||
expr: &influxql.VarRef{Val: "bb.value"},
|
||||
sub: `SELECT "bb.value" FROM bb WHERE (("bb.host" = 'serverb' OR "bb.host" = 'serverc')) AND 1 = 2`,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
// Parse statement.
|
||||
stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement()
|
||||
if err != nil {
|
||||
t.Fatalf("invalid statement: %q: %s", tt.stmt, err)
|
||||
}
|
||||
|
||||
// Extract substatement.
|
||||
sub, err := stmt.(*influxql.SelectStatement).Substatement(tt.expr)
|
||||
if err != nil {
|
||||
t.Errorf("%d. %q: unexpected error: %s", i, tt.stmt, err)
|
||||
continue
|
||||
}
|
||||
if substr := sub.String(); tt.sub != substr {
|
||||
t.Errorf("%d. %q: unexpected substatement:\n\nexp=%s\n\ngot=%s\n\n", i, tt.stmt, tt.sub, substr)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the SELECT statement can extract GROUP BY interval.
|
||||
func TestSelectStatement_GroupByInterval(t *testing.T) {
|
||||
q := "SELECT sum(value) from foo where time < now() GROUP BY time(10m)"
|
||||
|
|
Loading…
Reference in New Issue