Skip to content

Commit 336af08

Browse files
committed
to fix aggregations with multiple partition by, update README.md + keywords and documentation related to aggregate functions, add query normalization before parsing the latter
1 parent aac63c4 commit 336af08

File tree

10 files changed

+1029
-442
lines changed

10 files changed

+1029
-442
lines changed

README.md

Lines changed: 573 additions & 12 deletions
Large diffs are not rendered by default.

bridge/src/main/scala/app/softnetwork/elastic/sql/bridge/ElasticAggregation.scala

Lines changed: 188 additions & 177 deletions
Large diffs are not rendered by default.

core/src/main/scala/app/softnetwork/elastic/client/ScrollApi.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ trait ScrollApi extends ElasticClientHelpers {
122122
)(implicit system: ActorSystem): Source[(Map[String, Any], ScrollMetrics), NotUsed] = {
123123
sql.request match {
124124
case Some(Left(single)) =>
125-
if (single.windowFunctions.nonEmpty)
125+
if (single.windowFunctions.nonEmpty && single.fields.nonEmpty)
126126
return scrollWithWindowEnrichment(sql, single, config)
127127

128128
val sqlRequest = single.copy(score = sql.score)

core/src/main/scala/app/softnetwork/elastic/client/SearchApi.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ trait SearchApi extends ElasticConversion with ElasticClientHelpers {
6868
collection.immutable.Seq(single.sources: _*),
6969
sql = Some(sql.query)
7070
)
71-
if (single.windowFunctions.exists(_.isWindowing))
71+
if (single.windowFunctions.exists(_.isWindowing) && single.fields.nonEmpty)
7272
searchWithWindowEnrichment(sql, single)
7373
else
7474
singleSearch(elasticQuery, single.fieldAliases, single.sqlAggregations)

documentation/sql/functions_aggregate.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Count rows or non-null expressions. With `DISTINCT` counts distinct values.
4343
COUNT(*)
4444
COUNT(expr)
4545
COUNT(DISTINCT expr)
46+
COUNT(*) OVER (PARTITION BY partition_expr, ...) --(since v0.14.0)
4647
```
4748

4849
**Inputs:**
@@ -195,6 +196,7 @@ Sum of values.
195196
```sql
196197
SUM(expr)
197198
SUM(DISTINCT expr)
199+
SUM(expr) OVER (PARTITION BY partition_expr, ...) --(since v0.14.0)
198200
```
199201

200202
**Inputs:**
@@ -342,6 +344,7 @@ Average of values.
342344
```sql
343345
AVG(expr)
344346
AVG(DISTINCT expr)
347+
AVG(expr) OVER (PARTITION BY partition_expr, ...) --(since v0.14.0)
345348
```
346349

347350
**Inputs:**
@@ -491,6 +494,7 @@ Minimum value in group.
491494
**Syntax:**
492495
```sql
493496
MIN(expr)
497+
MIN(expr) OVER (PARTITION BY partition_expr, ...) --(since v0.14.0)
494498
```
495499

496500
**Inputs:**
@@ -618,6 +622,7 @@ Maximum value in group.
618622
**Syntax:**
619623
```sql
620624
MAX(expr)
625+
MAX(expr) OVER (PARTITION BY partition_expr, ...) --(since v0.14.0)
621626
```
622627

623628
**Inputs:**
@@ -1075,9 +1080,9 @@ SELECT
10751080
ARRAY_AGG(name) OVER (
10761081
PARTITION BY department
10771082
ORDER BY hire_date ASC
1083+
LIMIT 100
10781084
) AS employees
1079-
FROM emp
1080-
LIMIT 100;
1085+
FROM emp;
10811086
-- Result: employees as an array of name values per department (sorted and limited)
10821087
```
10831088

@@ -1192,27 +1197,9 @@ SELECT
11921197
ARRAY_AGG(name) OVER (
11931198
PARTITION BY department
11941199
ORDER BY hire_date ASC
1200+
LIMIT 100 -- Important: limits result set size
11951201
) AS employees
1196-
FROM emp
1197-
LIMIT 100; -- Important: limits result set size
1198-
```
1199-
1200-
**Comparison with STRING_AGG (if available):**
1201-
```sql
1202-
-- ARRAY_AGG returns array
1203-
SELECT
1204-
department,
1205-
ARRAY_AGG(name) OVER (PARTITION BY department) AS name_array
12061202
FROM emp;
1207-
-- Result: ['John', 'Jane', 'Bob']
1208-
1209-
-- STRING_AGG returns string (if supported)
1210-
SELECT
1211-
department,
1212-
STRING_AGG(name, ', ') AS name_string
1213-
FROM emp
1214-
GROUP BY department;
1215-
-- Result: 'John, Jane, Bob'
12161203
```
12171204

12181205
---

documentation/sql/keywords.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GROUP BY
1414
HAVING
1515
ORDER BY
1616
OFFSET
17+
LIMIT
1718

1819
## Aliases and type conversion
1920
AS
@@ -31,6 +32,7 @@ AVG
3132
MIN
3233
MAX
3334
OVER
35+
PARTITION BY
3436
FIRST_VALUE
3537
LAST_VALUE
3638
ARRAY_AGG
@@ -87,22 +89,23 @@ ISNOTNULL
8789
NULLIF
8890

8991
## Date/Time/Datetime/Timestamp functions
90-
YEAR
91-
QUARTER
92-
MONTH
93-
WEEK
94-
DAY
95-
HOUR
96-
MINUTE
97-
SECOND
98-
MILLISECOND
99-
MICROSECOND
100-
NANOSECOND
101-
EPOCHDAY
102-
OFFSET_SECONDS
103-
LAST_DAY
104-
WEEKDAY
105-
YEARDAY
92+
[//]: # (YEAR )
93+
[//]: # (QUARTER )
94+
[//]: # (MONTH )
95+
[//]: # (WEEK )
96+
[//]: # (DAY )
97+
[//]: # (HOUR )
98+
[//]: # (MINUTE )
99+
[//]: # (SECOND )
100+
[//]: # (MILLISECOND )
101+
[//]: # (MICROSECOND )
102+
[//]: # (NANOSECOND )
103+
[//]: # (EPOCHDAY )
104+
[//]: # (OFFSET_SECONDS )
105+
[//]: # (LAST_DAY )
106+
[//]: # (LASTDAY)
107+
[//]: # (WEEKDAY )
108+
[//]: # (YEARDAY )
106109
INTERVAL
107110
CURRENT_DATE
108111
CURDATE

0 commit comments

Comments
 (0)