Skip to content

Commit 4abc29d

Browse files
committed
apply sql type and retrieve math script for a chain of functions, remove SQLComparisonDateMath, update identifier, expressions and implicits accordingly, override painless for like, in and match
1 parent bfdf95d commit 4abc29d

File tree

12 files changed

+189
-209
lines changed

12 files changed

+189
-209
lines changed

es6/sql-bridge/src/main/scala/app/softnetwork/elastic/sql/bridge/ElasticQuery.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import app.softnetwork.elastic.sql.{
99
ElasticNested,
1010
ElasticParent,
1111
SQLBetween,
12-
SQLComparisonDateMath,
1312
SQLExpression,
1413
SQLIn,
1514
SQLIsNotNull,
@@ -72,7 +71,6 @@ case class ElasticQuery(filter: ElasticFilter) {
7271
case between: SQLBetween[Double] => between
7372
case geoDistance: ElasticGeoDistance => geoDistance
7473
case matchExpression: ElasticMatch => matchExpression
75-
case dateMath: SQLComparisonDateMath => dateMath
7674
case isNull: SQLIsNullCriteria => isNull
7775
case isNotNull: SQLIsNotNullCriteria => isNotNull
7876
case other =>

es6/sql-bridge/src/main/scala/app/softnetwork/elastic/sql/bridge/package.scala

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ package object bridge {
143143

144144
implicit def expressionToQuery(expression: SQLExpression): Query = {
145145
import expression._
146+
if (aggregation)
147+
return matchAllQuery()
148+
if (identifier.functions.nonEmpty) {
149+
return scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
150+
}
146151
value match {
147-
case n: SQLNumeric[_] if !aggregation =>
152+
case n: SQLNumeric[_] =>
148153
operator match {
149154
case Ge =>
150155
maybeNot match {
@@ -226,7 +231,7 @@ package object bridge {
226231
}
227232
case _ => matchAllQuery()
228233
}
229-
case l: SQLLiteral if !aggregation =>
234+
case l: SQLLiteral =>
230235
operator match {
231236
case Like =>
232237
maybeNot match {
@@ -279,7 +284,7 @@ package object bridge {
279284
}
280285
case _ => matchAllQuery()
281286
}
282-
case b: SQLBoolean if !aggregation =>
287+
case b: SQLBoolean =>
283288
operator match {
284289
case Eq =>
285290
maybeNot match {
@@ -297,27 +302,27 @@ package object bridge {
297302
}
298303
case _ => matchAllQuery()
299304
}
300-
case _ => matchAllQuery()
301-
}
302-
}
303-
304-
implicit def dateMathToQuery(dateMath: SQLComparisonDateMath): Query = {
305-
import dateMath._
306-
if (aggregation)
307-
return matchAllQuery()
308-
dateTimeFunction match {
309-
case _: CurrentTimeFunction =>
310-
scriptQuery(Script(script = script).lang("painless").scriptType("source"))
311-
case _ =>
312-
val op = if (maybeNot.isDefined) operator.not else operator
313-
op match {
314-
case Gt => rangeQuery(identifier.name) gt script
315-
case Ge => rangeQuery(identifier.name) gte script
316-
case Lt => rangeQuery(identifier.name) lt script
317-
case Le => rangeQuery(identifier.name) lte script
318-
case Eq => rangeQuery(identifier.name) gte script lte script
319-
case Ne | Diff => not(rangeQuery(identifier.name) gte script lte script)
305+
case i: SQLIdentifier =>
306+
operator match {
307+
case op: SQLComparisonOperator =>
308+
i.toScript match {
309+
case Some(script) =>
310+
val o = if (maybeNot.isDefined) op.not else op
311+
o match {
312+
case Gt => rangeQuery(identifier.name) gt script
313+
case Ge => rangeQuery(identifier.name) gte script
314+
case Lt => rangeQuery(identifier.name) lt script
315+
case Le => rangeQuery(identifier.name) lte script
316+
case Eq => rangeQuery(identifier.name) gte script lte script
317+
case Ne | Diff => not(rangeQuery(identifier.name) gte script lte script)
318+
}
319+
case _ =>
320+
scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
321+
}
322+
case _ =>
323+
scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
320324
}
325+
case _ => matchAllQuery()
321326
}
322327
}
323328

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,15 +976,15 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
976976
| "script": {
977977
| "script": {
978978
| "lang": "painless",
979-
| "source": "return doc['createdAt'].value.toLocalTime() < LocalTime.now();"
979+
| "source": "doc['createdAt'].value.toLocalTime() < ZonedDateTime.now(ZoneId.of('Z')).toLocalTime()"
980980
| }
981981
| }
982982
| },
983983
| {
984984
| "script": {
985985
| "script": {
986986
| "lang": "painless",
987-
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTES);"
987+
| "source": "doc['createdAt'].value.toLocalTime() >= ZonedDateTime.now(ZoneId.of('Z')).toLocalTime().minus(10, ChronoUnit.MINUTES)"
988988
| }
989989
| }
990990
| }
@@ -1001,7 +1001,6 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
10011001
.replaceAll("ChronoUnit", " ChronoUnit")
10021002
.replaceAll(">=", " >= ")
10031003
.replaceAll("<", " < ")
1004-
.replaceAll("return", "return ")
10051004
}
10061005

10071006
it should "handle having with date functions" in {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import app.softnetwork.elastic.sql.{
99
ElasticNested,
1010
ElasticParent,
1111
SQLBetween,
12-
SQLComparisonDateMath,
1312
SQLExpression,
1413
SQLIn,
1514
SQLIsNotNull,
@@ -70,7 +69,6 @@ case class ElasticQuery(filter: ElasticFilter) {
7069
case between: SQLBetween[Double] => between
7170
case geoDistance: ElasticGeoDistance => geoDistance
7271
case matchExpression: ElasticMatch => matchExpression
73-
case dateMath: SQLComparisonDateMath => dateMath
7472
case other =>
7573
throw new IllegalArgumentException(s"Unsupported filter type: ${other.getClass.getName}")
7674
}

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ package object bridge {
144144

145145
implicit def expressionToQuery(expression: SQLExpression): Query = {
146146
import expression._
147+
if (aggregation)
148+
return matchAllQuery()
149+
if (identifier.functions.nonEmpty) {
150+
return scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
151+
}
147152
value match {
148-
case n: SQLNumeric[_] if !aggregation =>
153+
case n: SQLNumeric[_] =>
149154
operator match {
150155
case Ge =>
151156
maybeNot match {
@@ -227,7 +232,7 @@ package object bridge {
227232
}
228233
case _ => matchAllQuery()
229234
}
230-
case l: SQLLiteral if !aggregation =>
235+
case l: SQLLiteral =>
231236
operator match {
232237
case Like =>
233238
maybeNot match {
@@ -280,7 +285,7 @@ package object bridge {
280285
}
281286
case _ => matchAllQuery()
282287
}
283-
case b: SQLBoolean if !aggregation =>
288+
case b: SQLBoolean =>
284289
operator match {
285290
case Eq =>
286291
maybeNot match {
@@ -298,27 +303,27 @@ package object bridge {
298303
}
299304
case _ => matchAllQuery()
300305
}
301-
case _ => matchAllQuery()
302-
}
303-
}
304-
305-
implicit def dateMathToQuery(dateMath: SQLComparisonDateMath): Query = {
306-
import dateMath._
307-
if (aggregation)
308-
return matchAllQuery()
309-
dateTimeFunction match {
310-
case _: CurrentTimeFunction =>
311-
scriptQuery(Script(script = script).lang("painless").scriptType("source"))
312-
case _ =>
313-
val op = if (maybeNot.isDefined) operator.not else operator
314-
op match {
315-
case Gt => rangeQuery(identifier.name) gt script
316-
case Ge => rangeQuery(identifier.name) gte script
317-
case Lt => rangeQuery(identifier.name) lt script
318-
case Le => rangeQuery(identifier.name) lte script
319-
case Eq => rangeQuery(identifier.name) gte script lte script
320-
case Ne | Diff => not(rangeQuery(identifier.name) gte script lte script)
306+
case i: SQLIdentifier =>
307+
operator match {
308+
case op: SQLComparisonOperator =>
309+
i.toScript match {
310+
case Some(script) =>
311+
val o = if (maybeNot.isDefined) op.not else op
312+
o match {
313+
case Gt => rangeQuery(identifier.name) gt script
314+
case Ge => rangeQuery(identifier.name) gte script
315+
case Lt => rangeQuery(identifier.name) lt script
316+
case Le => rangeQuery(identifier.name) lte script
317+
case Eq => rangeQuery(identifier.name) gte script lte script
318+
case Ne | Diff => not(rangeQuery(identifier.name) gte script lte script)
319+
}
320+
case _ =>
321+
scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
322+
}
323+
case _ =>
324+
scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
321325
}
326+
case _ => matchAllQuery()
322327
}
323328
}
324329

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,15 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
975975
| "script": {
976976
| "script": {
977977
| "lang": "painless",
978-
| "source": "return doc['createdAt'].value.toLocalTime() < LocalTime.now();"
978+
| "source": "doc['createdAt'].value.toLocalTime() < ZonedDateTime.now(ZoneId.of('Z')).toLocalTime()"
979979
| }
980980
| }
981981
| },
982982
| {
983983
| "script": {
984984
| "script": {
985985
| "lang": "painless",
986-
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTES);"
986+
| "source": "doc['createdAt'].value.toLocalTime() >= ZonedDateTime.now(ZoneId.of('Z')).toLocalTime().minus(10, ChronoUnit.MINUTES)"
987987
| }
988988
| }
989989
| }
@@ -1000,7 +1000,6 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
10001000
.replaceAll("ChronoUnit", " ChronoUnit")
10011001
.replaceAll(">=", " >= ")
10021002
.replaceAll("<", " < ")
1003-
.replaceAll("return", "return ")
10041003
}
10051004

10061005
it should "handle having with date functions" in {

0 commit comments

Comments
 (0)