Skip to content

Commit 948b295

Browse files
committed
Adding support for non string granularities
1 parent 394c102 commit 948b295

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/DruidFamiliar/QueryGenerator/SimpleGroupByDruidQueryGenerator.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public function generateQuery(IDruidQueryParameters $params)
5757
// We always have these keys
5858
$queryKeys[] = '"queryType": "{QUERYTYPE}"';
5959
$queryKeys[] = '"dataSource": "{DATASOURCE}"';
60-
$queryKeys[] = '"granularity": "{GRANULARITYSPEC.GRAN}"';
60+
61+
if ( is_array( $params->granularity ) && count( $params->granularity ) > 0 ) {
62+
$queryKeys[] = '"granularity": {GRANULARITYSPEC.GRAN}';
63+
} else {
64+
$queryKeys[] = '"granularity": "{GRANULARITYSPEC.GRAN}"';
65+
}
66+
6167
$queryKeys[] = '"dimensions": [ "{NON_TIME_DIMENSIONS}" ]';
6268

6369
if ( count( $params->filters ) > 0 ) {
@@ -83,7 +89,12 @@ public function generateQuery(IDruidQueryParameters $params)
8389
$query = str_replace('{INTERVALS}', $params->intervals, $query);
8490

8591

86-
$query = str_replace('{GRANULARITYSPEC.GRAN}', $params->granularity, $query);
92+
if ( is_array( $params->granularity ) && count( $params->granularity ) > 0 ) {
93+
$query = str_replace('{GRANULARITYSPEC.GRAN}', json_encode( $params->granularity ), $query);
94+
} else {
95+
$query = str_replace('{GRANULARITYSPEC.GRAN}', $params->granularity, $query);
96+
}
97+
8798
$query = str_replace('{NON_TIME_DIMENSIONS}', join('","', $params->dimensions), $query);
8899
$query = str_replace('{FILTERS}', join(",", $params->filters), $query);
89100
$query = str_replace('{AGGREGATORS}', join(",", $params->aggregators), $query);

tests/DruidFamiliar/Test/QueryGenerator/SimpleGroupByDruidQueryGeneratorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,40 @@ public function testGenerateQueryHandlesNotHavingPostAggregations()
254254
$this->assertArrayNotHasKey('postAggregations', $query);
255255
}
256256

257+
public function testGenerateQueryHandlesStringGranularities()
258+
{
259+
$params = $this->getMockSimpleGroupByQueryParameters();
260+
$params->granularity = 'DAY';
261+
262+
$q = new \DruidFamiliar\QueryGenerator\SimpleGroupByDruidQueryGenerator();
263+
264+
$query = $q->generateQuery($params);
265+
266+
267+
$jsonString = json_decode( $query, true );
268+
269+
$this->assertArrayHasKey('granularity', $jsonString);
270+
$this->assertEquals( 'DAY', $jsonString['granularity'] );
271+
}
272+
273+
public function testGenerateQueryHandlesObjectGranularities()
274+
{
275+
$params = $this->getMockSimpleGroupByQueryParameters();
276+
$params->granularity = array('type'=> "period", "period"=>"P3M");
277+
278+
$q = new \DruidFamiliar\QueryGenerator\SimpleGroupByDruidQueryGenerator();
279+
280+
$query = $q->generateQuery($params);
281+
282+
283+
$jsonString = json_decode( $query, true );
284+
285+
$this->assertArrayHasKey('granularity', $jsonString);
286+
$this->assertCount( 2, $jsonString['granularity'] );
287+
$this->assertArrayHasKey('type', $jsonString['granularity']);
288+
$this->assertArrayHasKey('period', $jsonString['granularity']);
289+
290+
$this->assertEquals( 'period', $jsonString['granularity']['type'] );
291+
$this->assertEquals( 'P3M', $jsonString['granularity']['period'] );
292+
}
257293
}

0 commit comments

Comments
 (0)