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

parser: allow select from multiple series [#112] #567

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/integration/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,48 @@ func (self *DataTestSuite) RegexMatching(c *C) (Fun, Fun) {
}
}

// issue #112
func (self *DataTestSuite) SelectFromMultipleSeries(c *C) (Fun, Fun) {
return func(client Client) {
serieses := CreatePoints("cpu1.load_one", 1, 5)
client.WriteData(serieses, c)
serieses = CreatePoints("cpu2.load_one", 1, 3)
client.WriteData(serieses, c)
}, func(client Client) {
results := client.RunQuery("select * from cpu1.load_one, cpu2.load_one", c, "m")
series := map[string][]map[string]interface{}{}
for _, s := range results {
series[s.Name] = ToMap(s)
}
c.Assert(series, HasLen, 2)
c.Assert(series["cpu1.load_one"], HasLen, 5)
c.Assert(series["cpu2.load_one"], HasLen, 3)
}
}

func (self *DataTestSuite) SelectFromMultipleSeriesWithLimit(c *C) (Fun, Fun) {
return func(client Client) {
i := 0.0
serieses := CreatePointsFromFunc("cpu1.load_one", 1, 5, func(_ int) float64 { i++; return i })
client.WriteData(serieses, c)
serieses = CreatePointsFromFunc("cpu2.load_one", 1, 3, func(_ int) float64 { i++; return i })
client.WriteData(serieses, c)
}, func(client Client) {
results := client.RunQuery("select * from cpu1.load_one, cpu2.load_one limit 2", c, "m")
series := map[string][]map[string]interface{}{}
for _, s := range results {
series[s.Name] = ToMap(s)
}
c.Assert(series, HasLen, 2)
c.Assert(series["cpu1.load_one"], HasLen, 2)
c.Assert(series["cpu2.load_one"], HasLen, 2)
c.Assert(series["cpu1.load_one"][0]["column0"], Equals, float64(5.0))
c.Assert(series["cpu1.load_one"][1]["column0"], Equals, float64(4.0))
c.Assert(series["cpu2.load_one"][0]["column0"], Equals, float64(8.0))
c.Assert(series["cpu2.load_one"][1]["column0"], Equals, float64(7.0))
}
}

// issue #392
func (self *DataTestSuite) DifferentColumnsAcrossShards(c *C) (Fun, Fun) {
return func(client Client) {
Expand Down
14 changes: 13 additions & 1 deletion src/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package parser

import (
"fmt"
. "launchpad.net/gocheck"
"testing"
"time"
. "launchpad.net/gocheck"
)

// Hook up gocheck into the gotest runner.
Expand Down Expand Up @@ -336,6 +336,18 @@ func (self *QueryParserSuite) TestParseSelectWithRegexTables(c *C) {
c.Assert(regex.MatchString("usersfoobar"), Equals, true)
}

func (self *QueryParserSuite) TestParseSelectWithMultipleTables(c *C) {
q, err := ParseSelectQuery("select email from first_series, second_series, third_series where time>now()-2d;")
c.Assert(err, IsNil)

fromClause := q.GetFromClause()
c.Assert(fromClause.Type, Equals, FromClauseArray)
c.Assert(fromClause.Names, HasLen, 3)
c.Assert(fromClause.Names[0].Name.Name, Equals, "first_series")
c.Assert(fromClause.Names[1].Name.Name, Equals, "second_series")
c.Assert(fromClause.Names[2].Name.Name, Equals, "third_series")
}

func (self *QueryParserSuite) TestMergeFromClause(c *C) {
q, err := ParseSelectQuery("select value from t1 merge t2 where c = '5';")
c.Assert(err, IsNil)
Expand Down
31 changes: 31 additions & 0 deletions src/parser/query.yacc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ value *create_expression_value(char *operator, size_t size, ...) {
drop_series_query* drop_series_query;
drop_query* drop_query;
groupby_clause* groupby_clause;
table_name_array* table_name_array;
struct {
int limit;
char ascending;
Expand Down Expand Up @@ -94,6 +95,7 @@ value *create_expression_value(char *operator, size_t size, ...) {
%type <v> BOOL_EXPRESSION
%type <value_array> VALUES
%type <v> VALUE TABLE_VALUE SIMPLE_TABLE_VALUE TABLE_NAME_VALUE SIMPLE_NAME_VALUE INTO_VALUE INTO_NAME_VALUE
%type <table_name_array> SIMPLE_TABLE_VALUES
%type <v> WILDCARD REGEX_VALUE DURATION_VALUE FUNCTION_CALL
%type <groupby_clause> GROUP_BY_CLAUSE
%type <integer> LIMIT_CLAUSE
Expand Down Expand Up @@ -355,6 +357,13 @@ FROM_CLAUSE:
$$->from_clause_type = FROM_ARRAY;
}
|
FROM SIMPLE_TABLE_VALUES
{
$$ = malloc(sizeof(from_clause));
$$->names = $2;
$$->from_clause_type = FROM_ARRAY;
}
|
FROM SIMPLE_TABLE_VALUE
{
$$ = malloc(sizeof(from_clause));
Expand Down Expand Up @@ -526,6 +535,28 @@ TABLE_VALUE:
SIMPLE_TABLE_VALUE:
SIMPLE_NAME_VALUE | TABLE_NAME_VALUE

SIMPLE_TABLE_VALUES:
SIMPLE_TABLE_VALUE
{
$$ = malloc(sizeof(table_name_array));
$$->size = 1;
$$->elems = malloc(sizeof(table_name*));
$$->elems[0] = malloc(sizeof(table_name));
$$->elems[0]->name = $1;
$$->elems[0]->alias = NULL;
}
|
SIMPLE_TABLE_VALUES ',' SIMPLE_TABLE_VALUE
{
size_t new_size = $1->size + 1;
$1->elems = realloc($$->elems, sizeof(table_name*) * new_size);
$1->elems[$1->size] = malloc(sizeof(table_name));
$1->elems[$1->size]->name = $3;
$1->elems[$1->size]->alias = NULL;
$1->size = new_size;
$$ = $1;
}

INTO_VALUE:
SIMPLE_NAME_VALUE | TABLE_NAME_VALUE | INTO_NAME_VALUE

Expand Down