Skip to content

Commit

Permalink
Merge pull request #206 from Altinity/fix_join_parsing
Browse files Browse the repository at this point in the history
fix #202
  • Loading branch information
Slach authored Jun 25, 2020
2 parents 7b73be0 + bd8a987 commit 0bc50d0
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 96 deletions.
2 changes: 1 addition & 1 deletion datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func parseResponse(body []byte, refId string) (*datasource.DatasourceResponse, e
for _, dataPoint := range parsedBody.Data {
timestamp, err := strconv.ParseInt(dataPoint[tsMetaName].(string), 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse timestamp with alias t: %w", err)
return nil, fmt.Errorf("unable to parse timestamp with alias `%s`: %w", tsMetaName, err)
}
for k, v := range dataPoint {
if k != tsMetaName {
Expand Down
4 changes: 2 additions & 2 deletions dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

Binary file modified dist/vertamedia-clickhouse-plugin_linux_amd64
Binary file not shown.
214 changes: 178 additions & 36 deletions spec/scanner_specs.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,43 @@ describe("scanner:", () => {
"col1"
]
},
"join": {
"type": "GLOBAL ANY LEFT JOIN",
"source": {
"root": [],
"select": [
"EventDate",
"col1",
"countIf(col2 GLOBAL IN some_table) AS col2_shared",
"count() AS col_count",
"uniqCombinedIf(col3, col3 GLOBAL IN some_table) AS col3_shared",
"uniqCombined(col3) AS unique_col3"
],
"from": [
"general_table_all"
],
"prewhere": [
"Event IN ('type2')",
"AND EventDate <= '2016-12-20'"
],
"where": [
"(EventDate, col1) GLOBAL IN some_table",
"AND col4 GLOBAL IN some_table"
],
"group by": [
"join": [
{
"aliases": [],
"on": [],
"source": {
"from": [
"general_table_all"
],
"group by": [
"EventDate",
"col1"
],
"prewhere": [
"Event IN ('type2')",
"AND EventDate <= '2016-12-20'"
],
"root": [],
"select": [
"EventDate",
"col1",
"countIf(col2 GLOBAL IN some_table) AS col2_shared",
"count() AS col_count",
"uniqCombinedIf(col3, col3 GLOBAL IN some_table) AS col3_shared",
"uniqCombined(col3) AS unique_col3"
],
"where": [
"(EventDate, col1) GLOBAL IN some_table",
"AND col4 GLOBAL IN some_table"
]
},
"type": "GLOBAL ANY LEFT JOIN",
"using": [
"EventDate",
"col1"
]
},
"using": [
"EventDate",
"col1",
]
},
}
],
"order by": [
"EventDate",
"col1"
Expand Down Expand Up @@ -173,21 +177,23 @@ describe("scanner:", () => {
"from": [
"$table"
],
"join": {
"type": "ANY LEFT JOIN",
"join": [{
"aliases": [],
"on": [],
"source": {
"from": [
"default.log_events"
],
"root": [],
"select": [
"*"
],
"from": [
"default.log_events"
]
},
"type": "ANY LEFT JOIN",
"using": [
"EventCode"
]
},
}],
"where": [
"$timeFilter"
],
Expand Down Expand Up @@ -526,4 +532,140 @@ describe("scanner:", () => {
expect(scanner.toAST()).toEqual(expectedAST);
});
});
describe("AST case 16 (subquery + alias)", () => {
let query = "SELECT t2.service_name, sum(1.05*rand()) AS test " +
"FROM (SELECT event_time, service_name FROM default.test_grafana) AS t2 " +
"WHERE $timeFilter " +
"GROUP BY service_name " +
"ORDER BY test DESC",
scanner = new Scanner(query);

let expectedAST = {
"from": {
"root": [],
"select": [
"event_time",
"service_name"
],
"from": [
"default.test_grafana"
],
"aliases": [
"AS t2"
],
},
"group by": [
"service_name"
],
"order by": [
"test DESC"
],
"root": [],
"select": [
"t2.service_name",
"sum(1.05 * rand()) AS test"
],
"where": [
"$timeFilter"
]
};
it("expects equality", () => {
expect(scanner.toAST()).toEqual(expectedAST);
});
});
describe("AST case 17 (subquery + multiple joins)", () => {
let query = "SELECT t1.service_name, sum(1.05*rand()) AS test " +
"FROM (SELECT DISTINCT service_name FROM default.test_grafana) AS t2 " +
"INNER JOIN $table AS t1 " +
"ON (t2.service_name=t1.service_name AND 1=1) " +
"CROSS JOIN (SELECT DISTINCT service_name FROM default.test_grafana) AS t3 " +
"ON t3.service_name=t1.service_name AND 1=1 " +
"ANY JOIN default.test_grafana AS t4 " +
"USING service_name " +
"WHERE $timeFilter " +
"GROUP BY t1.service_name ORDER BY test DESC",
scanner = new Scanner(query);

let expectedAST = {
"root": [],
"select": [
"t1.service_name",
"sum(1.05 * rand()) AS test"
],
"from": {
"from": [
"default.test_grafana"
],
"root": [],
"select": [
"DISTINCT service_name"
],
"aliases": [
"AS t2",
],
},
"join": [
{
"source": [
"$table"
],
"type": "INNER JOIN",
"aliases": [
"AS",
"t1"
],
"on": [
"(t2.service_name=t1.service_name AND 1=1)"
],
"using": [],
},
{
"source": {
"root": [],
"select": [
"DISTINCT service_name",
],
"from": [
"default.test_grafana",
],
},
"type": "CROSS JOIN",
"aliases": [
"AS",
"t3"
],
"on": [
"t3.service_name=t1.service_name AND 1=1"
],
"using": [],
},
{
"source": [
"default.test_grafana",
],
"type": "ANY JOIN",
"aliases": [
"AS",
"t4"
],
"on": [],
"using": ["service_name"],

},
],
"where": [
"$timeFilter"
],
"group by": [
"t1.service_name"
],
"order by": [
"test DESC"
],
};
it("expects equality", () => {
expect(scanner.toAST()).toEqual(expectedAST);
});
});

});
Loading

0 comments on commit 0bc50d0

Please sign in to comment.