Skip to content

Commit 473cfcc

Browse files
committed
fix order by with buckets and aggregations
1 parent c80405a commit 473cfcc

File tree

7 files changed

+42
-42
lines changed

7 files changed

+42
-42
lines changed

es6/sql-bridge/src/test/scala/app/softnetwork/elastic/sql/SQLQuerySpec.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
538538
| "must_not": [
539539
| {
540540
| "term": {
541-
| "country": {
542-
| "value": "usa"
541+
| "Country": {
542+
| "value": "USA"
543543
| }
544544
| }
545545
| }
@@ -551,8 +551,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
551551
| "must_not": [
552552
| {
553553
| "term": {
554-
| "city": {
555-
| "value": "berlin"
554+
| "City": {
555+
| "value": "Berlin"
556556
| }
557557
| }
558558
| }
@@ -566,25 +566,25 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
566566
| }
567567
| },
568568
| "aggs": {
569-
| "country": {
569+
| "Country": {
570570
| "terms": {
571-
| "field": "country.keyword",
571+
| "field": "Country.keyword",
572572
| "order": {
573-
| "country": "asc"
573+
| "Country": "asc"
574574
| }
575575
| },
576576
| "aggs": {
577-
| "city": {
577+
| "City": {
578578
| "terms": {
579-
| "field": "city.keyword",
579+
| "field": "City.keyword",
580580
| "order": {
581581
| "cnt": "desc"
582582
| }
583583
| },
584584
| "aggs": {
585585
| "cnt": {
586586
| "value_count": {
587-
| "field": "customerid"
587+
| "field": "CustomerID"
588588
| }
589589
| },
590590
| "having_filter": {
@@ -1117,7 +1117,10 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
11171117
| "aggs": {
11181118
| "Country": {
11191119
| "terms": {
1120-
| "field": "Country.keyword"
1120+
| "field": "Country.keyword",
1121+
| "order": {
1122+
| "Country": "asc"
1123+
| }
11211124
| },
11221125
| "aggs": {
11231126
| "City": {

sql/bridge/src/test/scala/app/softnetwork/elastic/sql/SQLQuerySpec.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
538538
| "must_not": [
539539
| {
540540
| "term": {
541-
| "country": {
542-
| "value": "usa"
541+
| "Country": {
542+
| "value": "USA"
543543
| }
544544
| }
545545
| }
@@ -551,8 +551,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
551551
| "must_not": [
552552
| {
553553
| "term": {
554-
| "city": {
555-
| "value": "berlin"
554+
| "City": {
555+
| "value": "Berlin"
556556
| }
557557
| }
558558
| }
@@ -566,25 +566,25 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
566566
| }
567567
| },
568568
| "aggs": {
569-
| "country": {
569+
| "Country": {
570570
| "terms": {
571-
| "field": "country.keyword",
571+
| "field": "Country.keyword",
572572
| "order": {
573-
| "country": "asc"
573+
| "Country": "asc"
574574
| }
575575
| },
576576
| "aggs": {
577-
| "city": {
577+
| "City": {
578578
| "terms": {
579-
| "field": "city.keyword",
579+
| "field": "City.keyword",
580580
| "order": {
581581
| "cnt": "desc"
582582
| }
583583
| },
584584
| "aggs": {
585585
| "cnt": {
586586
| "value_count": {
587-
| "field": "customerid"
587+
| "field": "CustomerID"
588588
| }
589589
| },
590590
| "having_filter": {
@@ -1115,7 +1115,10 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
11151115
| "aggs": {
11161116
| "Country": {
11171117
| "terms": {
1118-
| "field": "Country.keyword"
1118+
| "field": "Country.keyword",
1119+
| "order": {
1120+
| "Country": "asc"
1121+
| }
11191122
| },
11201123
| "aggs": {
11211124
| "City": {

sql/src/main/scala/app/softnetwork/elastic/sql/SQLGroupBy.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package app.softnetwork.elastic.sql
33
case object GroupBy extends SQLExpr("group by") with SQLRegex
44

55
case class SQLGroupBy(buckets: Seq[SQLBucket]) extends Updateable {
6-
override def sql: String = s" $GroupBy ${buckets.mkString(",")}"
6+
override def sql: String = s" $GroupBy ${buckets.mkString(", ")}"
77
def update(request: SQLSearchRequest): SQLGroupBy =
88
this.copy(buckets = buckets.map(_.update(request)))
99
lazy val bucketNames: Map[String, SQLBucket] = buckets.map { b =>

sql/src/main/scala/app/softnetwork/elastic/sql/SQLOrderBy.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ case class SQLFieldSort(
2323
}
2424

2525
case class SQLOrderBy(sorts: Seq[SQLFieldSort]) extends SQLToken {
26-
override def sql: String = s" $OrderBy ${sorts.mkString(",")}"
26+
override def sql: String = s" $OrderBy ${sorts.mkString(", ")}"
2727
}

sql/src/main/scala/app/softnetwork/elastic/sql/SQLParser.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,14 +534,10 @@ trait SQLWhereParser {
534534
trait SQLGroupByParser {
535535
self: SQLParser with SQLWhereParser =>
536536

537-
private def having: PackratParser[SQLHaving] = Having.regex ~> whereCriteria ^^ { rawTokens =>
538-
SQLHaving(
539-
processTokens(rawTokens)
540-
)
537+
def bucket: PackratParser[SQLBucket] = identifier ^^ { i =>
538+
SQLBucket(i)
541539
}
542540

543-
def bucket: PackratParser[SQLBucket] = identifier ^^ (i => SQLBucket(i))
544-
545541
def groupBy: PackratParser[SQLGroupBy] =
546542
GroupBy.regex ~ rep1sep(bucket, separator) ^^ { case _ ~ buckets =>
547543
SQLGroupBy(buckets)

sql/src/main/scala/app/softnetwork/elastic/sql/package.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,11 @@ package object sql {
288288
with SQLSource
289289
with SQLTokenWithFunction {
290290

291-
lazy val aggregationName: Option[String] =
292-
if (aggregation) fieldAlias.orElse(Option(name)) else None
293-
294291
lazy val identifierName: String =
295-
(function match {
292+
function match {
296293
case Some(f) => s"${f.sql}($name)"
297294
case _ => name
298-
}).toLowerCase
295+
}
299296

300297
lazy val nestedType: Option[String] = if (nested) Some(name.split('.').head) else None
301298

sql/src/test/scala/app/softnetwork/elastic/sql/SQLParserSpec.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ object Queries {
7474
|order by identifier2 desc
7575
|limit 10""".stripMargin.replaceAll("\n", " ")
7676
val groupByWithHaving: String =
77-
"""SELECT COUNT(CustomerID) as cnt, City, Country
78-
|FROM Customers
79-
|GROUP BY Country,City
80-
|HAVING Country <> "USA" AND City <> "Berlin" AND COUNT(CustomerID) > 1
81-
|ORDER BY COUNT(CustomerID) DESC,Country asc""".stripMargin.replaceAll("\n", " ").toLowerCase
77+
"""select count(CustomerID) as cnt, City, Country
78+
|from Customers
79+
|group by Country, City
80+
|having Country <> "USA" and City <> "Berlin" and count(CustomerID) > 1
81+
|order by count(CustomerID) desc, Country asc""".stripMargin.replaceAll("\n", " ")
8282
val dateTimeWithIntervalFields: String =
8383
"select current_timestamp() - interval 3 day as ct, current_date as cd, current_time as t, now as n from dual"
8484
val fieldsWithInterval: String =
@@ -92,8 +92,9 @@ object Queries {
9292
val groupByWithHavingAndDateTimeFunctions: String =
9393
"""select count(CustomerID) as cnt, City, Country, max(createdAt) as lastSeen
9494
|from Table
95-
|group by Country,City
96-
|having Country <> "USA" and City <> "Berlin" and count(CustomerID) > 1 and lastSeen > now - interval 7 day""".stripMargin
95+
|group by Country, City
96+
|having Country <> "USA" and City <> "Berlin" and count(CustomerID) > 1 and lastSeen > now - interval 7 day
97+
|order by Country asc""".stripMargin
9798
.replaceAll("\n", " ")
9899
}
99100

0 commit comments

Comments
 (0)