Skip to content

Commit d08ce77

Browse files
committed
to fix painless script with date_diff
1 parent 7c30aa6 commit d08ce77

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12031203
| "field": "createdAt",
12041204
| "script": {
12051205
| "lang": "painless",
1206-
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(doc['createdAt'].value, LocalDate::from)"
1206+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(doc['createdAt'].value, ZonedDateTime::from)"
12071207
| }
12081208
| }
12091209
| }
@@ -1217,7 +1217,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12171217
.replaceAll("!=", " != ")
12181218
.replaceAll("&&", " && ")
12191219
.replaceAll(">", " > ")
1220-
.replaceAll(",LocalDate", ", LocalDate")
1220+
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
12211221
}
12221222

12231223
it should "handle parse_datetime function" in {
@@ -1325,7 +1325,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13251325
| "max": {
13261326
| "script": {
13271327
| "lang": "painless",
1328-
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1328+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, ZonedDateTime::from))"
13291329
| }
13301330
| }
13311331
| }
@@ -1335,6 +1335,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13351335
|}""".stripMargin
13361336
.replaceAll("\\s", "")
13371337
.replaceAll(",doc", ", doc")
1338+
.replaceAll("DateTimeFormatter", " DateTimeFormatter")
1339+
.replaceAll("ZonedDateTime", " ZonedDateTime")
13381340
}
13391341

13401342
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12561256
| "field": "createdAt",
12571257
| "script": {
12581258
| "lang": "painless",
1259-
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, LocalDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoUnit.YEARS)"
1259+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, ZonedDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoUnit.YEARS)"
12601260
| }
12611261
| }
12621262
| }
@@ -1269,7 +1269,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12691269
.replaceAll("!=", " != ")
12701270
.replaceAll("&&", " && ")
12711271
.replaceAll(">", " > ")
1272-
.replaceAll(",LocalDate", ", LocalDate")
1272+
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
12731273
}
12741274

12751275
it should "handle date_diff function as script field" in {
@@ -1322,7 +1322,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13221322
| "max": {
13231323
| "script": {
13241324
| "lang": "painless",
1325-
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1325+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, ZonedDateTime::from))"
13261326
| }
13271327
| }
13281328
| }
@@ -1332,6 +1332,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13321332
|}""".stripMargin
13331333
.replaceAll("\\s", "")
13341334
.replaceAll(",doc", ", doc")
1335+
.replaceAll("DateTimeFormatter", " DateTimeFormatter")
1336+
.replaceAll("ZonedDateTime", " ZonedDateTime")
13351337
}
13361338

13371339
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,15 @@ case class DateDiff(end: PainlessScript, start: PainlessScript, unit: TimeUnit)
287287
override def toSQL(base: String): String = {
288288
s"$sql(${end.sql}, ${start.sql}, ${unit.sql})"
289289
}
290-
override def painless: String = s"${unit.painless}.between(${start.painless}, ${end.painless})"
290+
lazy val startPainless: String = start match {
291+
case i: Identifier => SQLFunctionUtils.buildPainless(i)
292+
case _ => start.painless
293+
}
294+
lazy val endPainless: String = end match {
295+
case i: Identifier => SQLFunctionUtils.buildPainless(i)
296+
case _ => end.painless
297+
}
298+
override def painless: String = s"${unit.painless}.between($startPainless, $endPainless)"
291299
}
292300

293301
case class DateAdd(interval: TimeInterval)
@@ -370,7 +378,7 @@ case class ParseDateTime(format: String)
370378
override def params: Seq[String] = Seq(s"'$format'")
371379
override def painless: String = throw new NotImplementedError("Use toPainless instead")
372380
override def toPainless(base: String): String =
373-
s"DateTimeFormatter.ofPattern('$format').parse($base, LocalDateTime::from)"
381+
s"DateTimeFormatter.ofPattern('$format').parse($base, ZonedDateTime::from)"
374382
}
375383

376384
case class FormatDateTime(format: String)

0 commit comments

Comments
 (0)