Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit cc96ba1

Browse files
authored
Merge pull request #276 from doitintl/feature/lior/partitonTable
Add partition and macros support at raw sql mode
2 parents 3a05791 + 2d06ba3 commit cc96ba1

File tree

7 files changed

+59
-29
lines changed

7 files changed

+59
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ The query builder provides a simple yet a user-friendly interface to help you qu
144144
WHERE `createDate` < TIMESTAMP_MILLIS (1592828659681) AND _PARTITIONTIME >= '2020-06-15 15:24:19' AND _PARTITIONTIME < '2020-06-22 15:24:19'
145145
```
146146

147+
You can now use timeFilter macro in raw sql mode
148+
147149
4. GROUP BY option - You can use a pre-defined macro or use one of the fields from your query
148150
a. time ($__interval,none)
149151
5. ORDER BY option

dist/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ The query builder provides a simple yet a user-friendly interface to help you qu
144144
WHERE `createDate` < TIMESTAMP_MILLIS (1592828659681) AND _PARTITIONTIME >= '2020-06-15 15:24:19' AND _PARTITIONTIME < '2020-06-22 15:24:19'
145145
```
146146

147+
You can now use pre-defined macros in raw sql mode
148+
147149
4. GROUP BY option - You can use a pre-defined macro or use one of the fields from your query
148150
a. time ($__interval,none)
149151
5. ORDER BY option

dist/module.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bigquery_query.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default class BigQueryQuery {
9393
public static replaceTimeShift(q) {
9494
return q.replace(/(\$__timeShifting\().*?(?=\))./g, "");
9595
}
96-
private static convertToUtc(d) {
96+
static convertToUtc(d) {
9797
return new Date(d.getTime() + d.getTimezoneOffset() * 60000);
9898
}
9999

@@ -454,8 +454,16 @@ export default class BigQueryQuery {
454454
return tag.params.join(" ");
455455
}
456456
});
457-
if (conditions.length > 0) {
458-
query = "\nWHERE\n " + conditions.join(" AND\n ");
457+
if (this.target.partitioned) {
458+
const partitionedField = this.target.partitionedField ? this.target.partitionedField : "_PARTITIONTIME";
459+
if (this.templateSrv.timeRange && this.templateSrv.timeRange.from) {
460+
const from = `${partitionedField} >= '${BigQueryQuery.formatDateToString(this.templateSrv.timeRange.from._d, "-", true)}'`;
461+
conditions.push(from);
462+
}
463+
if (this.templateSrv.timeRange && this.templateSrv.timeRange.to) {
464+
const to = `${partitionedField} < '${BigQueryQuery.formatDateToString(this.templateSrv.timeRange.to._d, "-", true)}'`;
465+
conditions.push(to);
466+
}
459467
}
460468
if (this.target.sharded) {
461469
const from = BigQueryQuery.formatDateToString(
@@ -464,23 +472,11 @@ export default class BigQueryQuery {
464472
const to = BigQueryQuery.formatDateToString(
465473
this.templateSrv.timeRange.to._d
466474
);
467-
query += " AND _TABLE_SUFFIX BETWEEN '" + from + "' AND '" + to + "' ";
468-
}
469-
if (this.target.partitioned && this.target.partitionedField === "") {
470-
query +=
471-
" AND _PARTITIONTIME >= '" +
472-
BigQueryQuery.formatDateToString(
473-
this.templateSrv.timeRange.from._d,
474-
"-",
475-
true
476-
) +
477-
"' AND _PARTITIONTIME < '" +
478-
BigQueryQuery.formatDateToString(
479-
this.templateSrv.timeRange.to._d,
480-
"-",
481-
true
482-
) +
483-
"'";
475+
const sharded = "_TABLE_SUFFIX BETWEEN '" + from + "' AND '" + to + "' ";
476+
conditions.push(sharded);
477+
}
478+
if (conditions.length > 0) {
479+
query = "\nWHERE\n " + conditions.join(" AND\n ");
484480
}
485481
return query;
486482
}
@@ -657,7 +653,7 @@ export default class BigQueryQuery {
657653
BigQueryQuery.quoteFiledName(this.target.timeColumn) + " > " + from + " ";
658654
const toRange =
659655
BigQueryQuery.quoteFiledName(this.target.timeColumn) + " < " + to + " ";
660-
q = q.replace(/\$__timeFilter\(([\w_.]+)\)/g, range);
656+
q = q.replace(/\$__timeFilter\((.*?)\)/g, range);
661657
q = q.replace(/\$__timeFrom\(([\w_.]+)\)/g, fromRange);
662658
q = q.replace(/\$__timeTo\(([\w_.]+)\)/g, toRange);
663659
q = q.replace(/\$__millisTimeTo\(([\w_.]+)\)/g, to);

src/datasource.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ export class BigQueryDatasource {
228228
this.authenticationType =
229229
instanceSettings.jsonData.authenticationType || "jwt";
230230
(async () => {
231-
this.projectName =
232-
instanceSettings.jsonData.defaultProject ||
233-
(await this.getDefaultProject());
231+
this.projectName = instanceSettings.jsonData.defaultProject || (await this.getDefaultProject());
234232
})();
235233
this.mixpanel = require("mixpanel-browser");
236234
if (this.jsonData.sendUsageData !== false) {
@@ -525,6 +523,7 @@ export class BigQueryDatasource {
525523
}
526524
private setUpQ(modOptions, options, query) {
527525
let q = this.queryModel.expend_macros(modOptions);
526+
q = this.setUpPartition(q, query.partitioned, query.partitionedField, modOptions);
528527
q = BigQueryDatasource._updatePartition(q, modOptions);
529528
q = BigQueryDatasource._updateTableSuffix(q, modOptions);
530529
if (query.refId.search(Shifted) > -1) {
@@ -543,6 +542,31 @@ export class BigQueryDatasource {
543542
}
544543
return q;
545544
}
545+
/**
546+
* Add partition to query unless it has one
547+
* @param query
548+
* @param isPartitioned
549+
* @param partitionedField
550+
* @param options
551+
*/
552+
private setUpPartition(query, isPartitioned, partitionedField, options){
553+
partitionedField = partitionedField ? partitionedField : '_PARTITIONTIME';
554+
if(isPartitioned && !query.match(partitionedField)) {
555+
let fromD = BigQueryQuery.convertToUtc(options.range.from._d);
556+
let toD = BigQueryQuery.convertToUtc(options.range.to._d);
557+
const from = `${partitionedField} >= '${BigQueryQuery.formatDateToString(fromD, "-", true)}'`;
558+
const to = `${partitionedField} < '${BigQueryQuery.formatDateToString(toD, "-", true)}'`;
559+
const partition = `where ${from} AND ${to} AND `;
560+
if(query.match(/where/i))
561+
query = query.replace(/where/i, partition);
562+
else {
563+
const reg = /from ('|`|"|){1}(.*?)('|`|"|){1} as ('|`|"|)(\S*)('|`|"|){1}|from ('|`|"|){1}(\S*)('|`|"|){1}/i;
564+
const fromMatch = query.match(reg);
565+
query = query.replace(reg, `${fromMatch} ${fromMatch}`);
566+
}
567+
}
568+
return query;
569+
}
546570
private async doRequest(url, requestId = "requestId", maxRetries = 3) {
547571
return this.backendSrv
548572
.datasourceRequest({

src/specs/bigquery_query.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ describe("BigQueryQuery", () => {
195195
expect(query.buildWhereClause()).toBe(
196196
"\nWHERE\n $__timeFilter(t) AND\n v = 1"
197197
);
198+
query.target.where = [];
199+
query.target.partitioned = true;
200+
const time = { from: { _d: "1987-06-30" }, to: { _d: "1987-06-30" } };
201+
query.templateSrv.timeRange = time;
202+
const whereClause = query.buildWhereClause();
203+
expect(whereClause).toBe("\nWHERE\n _PARTITIONTIME >= '1987-06-30 03:00:00' AND\n _PARTITIONTIME < '1987-06-30 03:00:00'");
198204
});
199205

200206
describe("When generating GROUP BY clause", () => {

0 commit comments

Comments
 (0)