Skip to content

Commit d842c5e

Browse files
committed
fix painless script for date parse and date time parse functions
1 parent 6f47a44 commit d842c5e

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12071207
| "field": "createdAt",
12081208
| "script": {
12091209
| "lang": "painless",
1210-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd'); (param1 == null) ? null : param2.parse(param1, LocalDate::from)"
1210+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : LocalDate.parse(param1, DateTimeFormatter.ofPattern('yyyy-MM-dd'))"
12111211
| }
12121212
| }
12131213
| }
@@ -1227,6 +1227,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12271227
.replaceAll(";", "; ")
12281228
.replaceAll(",ChronoUnit", ", ChronoUnit")
12291229
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1230+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
12301231
.replaceAll("==", " == ")
12311232
.replaceAll("!=", " != ")
12321233
.replaceAll("&&", " && ")
@@ -1373,7 +1374,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13731374
| "field": "createdAt",
13741375
| "script": {
13751376
| "lang": "painless",
1376-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX'); (param1 == null) ? null : param2.parse(param1, ZonedDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoField.YEAR)"
1377+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : ZonedDateTime.parse(param1, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX')).truncatedTo(ChronoUnit.MINUTES).get(ChronoField.YEAR)"
13771378
| }
13781379
| }
13791380
| }
@@ -1398,6 +1399,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13981399
.replaceAll(">", " > ")
13991400
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
14001401
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1402+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
14011403
.replaceAll("SSSXXX", "SSS XXX")
14021404
.replaceAll("ddHH", "dd HH")
14031405
}
@@ -1520,7 +1522,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
15201522
| "max": {
15211523
| "script": {
15221524
| "lang": "painless",
1523-
| "source": "def param1 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def param2 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param3 = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX'); def param4 = (param2 == null) ? null : param3.parse(param2, ZonedDateTime::from); (param1 == null || param2 == null) ? null : ChronoUnit.DAYS.between(param1, param4)"
1525+
| "source": "def param1 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def param2 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param3 = (param2 == null) ? null : ZonedDateTime.parse(param2, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX')); (param1 == null || param2 == null) ? null : ChronoUnit.DAYS.between(param1, param3)"
15241526
| }
15251527
| }
15261528
| }
@@ -1544,8 +1546,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
15441546
.replaceAll("!=", " != ")
15451547
.replaceAll("&&", " && ")
15461548
.replaceAll("\\|\\|", " || ")
1547-
.replaceAll("ZonedDateTime", " ZonedDateTime")
15481549
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1550+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
15491551
.replaceAll("SSSXXX", "SSS XXX")
15501552
.replaceAll("ddHH", "dd HH")
15511553
}
@@ -1956,7 +1958,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
19561958
| "c": {
19571959
| "script": {
19581960
| "lang": "painless",
1959-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from).minus(2, ChronoUnit.DAYS); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); param3 != null ? param3 : param4"
1961+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = LocalDate.parse(\"2025-09-11\", DateTimeFormatter.ofPattern('yyyy-MM-dd')).minus(2, ChronoUnit.DAYS); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); param3 != null ? param3 : param4"
19601962
| }
19611963
| }
19621964
| },
@@ -1993,7 +1995,9 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
19931995
.replaceAll(";\\s\\s", "; ")
19941996
.replaceAll("ChronoUnit", " ChronoUnit")
19951997
.replaceAll(",LocalDate", ", LocalDate")
1998+
.replaceAll("=LocalDate", " = LocalDate")
19961999
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
2000+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
19972001
.replaceAll("=ZonedDateTime", " = ZonedDateTime")
19982002
.replaceAll(":ZonedDateTime", " : ZonedDateTime")
19992003
}
@@ -2012,7 +2016,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
20122016
| "c": {
20132017
| "script": {
20142018
| "lang": "painless",
2015-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate().minus(2, ChronoUnit.HOURS); try { param3 != null ? param3 : param4 } catch (Exception e) { return null; }"
2019+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = LocalDate.parse(\"2025-09-11\", DateTimeFormatter.ofPattern('yyyy-MM-dd')); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate().minus(2, ChronoUnit.HOURS); try { param3 != null ? param3 : param4 } catch (Exception e) { return null; }"
20162020
| }
20172021
| },
20182022
| "c2": {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12111211
| "field": "createdAt",
12121212
| "script": {
12131213
| "lang": "painless",
1214-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd'); (param1 == null) ? null : param2.parse(param1, LocalDate::from)"
1214+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : LocalDate.parse(param1, DateTimeFormatter.ofPattern('yyyy-MM-dd'))"
12151215
| }
12161216
| }
12171217
| }
@@ -1231,6 +1231,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12311231
.replaceAll(";", "; ")
12321232
.replaceAll(",ChronoUnit", ", ChronoUnit")
12331233
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1234+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
12341235
.replaceAll("==", " == ")
12351236
.replaceAll("!=", " != ")
12361237
.replaceAll("&&", " && ")
@@ -1377,7 +1378,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13771378
| "field": "createdAt",
13781379
| "script": {
13791380
| "lang": "painless",
1380-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX'); (param1 == null) ? null : param2.parse(param1, ZonedDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoField.YEAR)"
1381+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : ZonedDateTime.parse(param1, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX')).truncatedTo(ChronoUnit.MINUTES).get(ChronoField.YEAR)"
13811382
| }
13821383
| }
13831384
| }
@@ -1402,6 +1403,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
14021403
.replaceAll(">", " > ")
14031404
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
14041405
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1406+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
14051407
.replaceAll("SSSXXX", "SSS XXX")
14061408
.replaceAll("ddHH", "dd HH")
14071409
}
@@ -1524,7 +1526,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
15241526
| "max": {
15251527
| "script": {
15261528
| "lang": "painless",
1527-
| "source": "def param1 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def param2 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param3 = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX'); def param4 = (param2 == null) ? null : param3.parse(param2, ZonedDateTime::from); (param1 == null || param2 == null) ? null : ChronoUnit.DAYS.between(param1, param4)"
1529+
| "source": "def param1 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def param2 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param3 = (param2 == null) ? null : ZonedDateTime.parse(param2, DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS XXX')); (param1 == null || param2 == null) ? null : ChronoUnit.DAYS.between(param1, param3)"
15281530
| }
15291531
| }
15301532
| }
@@ -1548,8 +1550,8 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
15481550
.replaceAll("!=", " != ")
15491551
.replaceAll("&&", " && ")
15501552
.replaceAll("\\|\\|", " || ")
1551-
.replaceAll("ZonedDateTime", " ZonedDateTime")
15521553
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
1554+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
15531555
.replaceAll("SSSXXX", "SSS XXX")
15541556
.replaceAll("ddHH", "dd HH")
15551557
}
@@ -1960,7 +1962,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
19601962
| "c": {
19611963
| "script": {
19621964
| "lang": "painless",
1963-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from).minus(2, ChronoUnit.DAYS); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); param3 != null ? param3 : param4"
1965+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = LocalDate.parse(\"2025-09-11\", DateTimeFormatter.ofPattern('yyyy-MM-dd')).minus(2, ChronoUnit.DAYS); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); param3 != null ? param3 : param4"
19641966
| }
19651967
| }
19661968
| },
@@ -1997,7 +1999,9 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
19971999
.replaceAll(";\\s\\s", "; ")
19982000
.replaceAll("ChronoUnit", " ChronoUnit")
19992001
.replaceAll(",LocalDate", ", LocalDate")
2002+
.replaceAll("=LocalDate", " = LocalDate")
20002003
.replaceAll("=DateTimeFormatter", " = DateTimeFormatter")
2004+
.replaceAll(",DateTimeFormatter", ", DateTimeFormatter")
20012005
.replaceAll("=ZonedDateTime", " = ZonedDateTime")
20022006
.replaceAll(":ZonedDateTime", " : ZonedDateTime")
20032007
}
@@ -2016,7 +2020,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
20162020
| "c": {
20172021
| "script": {
20182022
| "lang": "painless",
2019-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate().minus(2, ChronoUnit.HOURS); try { param3 != null ? param3 : param4 } catch (Exception e) { return null; }"
2023+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def param2 = LocalDate.parse(\"2025-09-11\", DateTimeFormatter.ofPattern('yyyy-MM-dd')); def param3 = param1 == param2 ? null : param1; def param4 = ZonedDateTime.now(ZoneId.of('Z')).toLocalDate().minus(2, ChronoUnit.HOURS); try { param3 != null ? param3 : param4 } catch (Exception e) { return null; }"
20202024
| }
20212025
| },
20222026
| "c2": {

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -522,20 +522,15 @@ package object time {
522522
case Some(ctx) =>
523523
identifier.baseType match {
524524
case SQLTypes.Varchar =>
525-
ctx.addParam(LiteralParam(s"$param.parse($arg, LocalDate::from)")) match {
525+
ctx.addParam(LiteralParam(s"LocalDate.parse($arg, $param)")) match {
526526
case Some(p) => return p
527527
case _ =>
528528
}
529529
case _ =>
530-
ctx.addParam(LiteralParam(param)) match {
531-
case Some(p) => return s"$p.parse($arg, LocalDate::from)"
532-
case _ =>
533-
}
534-
535530
}
536531
case _ =>
537532
}
538-
s"$param.parse($arg, LocalDate::from)"
533+
s"LocalDate.parse($arg, $param)"
539534
case _ => throw new IllegalArgumentException("DateParse requires exactly one argument")
540535
}
541536

@@ -647,7 +642,7 @@ package object time {
647642
with FunctionWithIdentifier
648643
with FunctionWithDateTimeFormat
649644
with DateMathScript {
650-
override def fun: Option[PainlessScript] = Some(DateTimeParse)
645+
override def fun: Option[PainlessScript] = None
651646

652647
override def args: List[PainlessScript] = List(identifier)
653648

@@ -668,20 +663,15 @@ package object time {
668663
case Some(ctx) =>
669664
identifier.baseType match {
670665
case SQLTypes.Varchar =>
671-
ctx.addParam(LiteralParam(s"$param.parse($arg, ZonedDateTime::from)")) match {
666+
ctx.addParam(LiteralParam(s"ZonedDateTime.parse($arg, $param)")) match {
672667
case Some(p) => return p
673668
case _ =>
674669
}
675670
case _ =>
676-
ctx.addParam(LiteralParam(param)) match {
677-
case Some(p) => return s"$p.parse($arg, ZonedDateTime::from)"
678-
case _ =>
679-
}
680-
681671
}
682672
case _ =>
683673
}
684-
s"$param.parse($arg, ZonedDateTime::from)"
674+
s"ZonedDateTime.parse($arg, $param)"
685675
case _ => throw new IllegalArgumentException("DateParse requires exactly one argument")
686676
}
687677

0 commit comments

Comments
 (0)