Skip to content

Commit 2d3d457

Browse files
committed
fix painless script for day of week function
1 parent 93c7b47 commit 2d3d457

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2945,7 +2945,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
29452945
| "wd": {
29462946
| "script": {
29472947
| "lang": "painless",
2948-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value.get(ChronoField.DAY_OF_WEEK)); param1"
2948+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : (param1.get(ChronoField.DAY_OF_WEEK) + 6) % 7"
29492949
| }
29502950
| },
29512951
| "yd": {
@@ -3047,6 +3047,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
30473047
.replaceAll("-", " - ")
30483048
.replaceAll("\\*", " * ")
30493049
.replaceAll("/", " / ")
3050+
.replaceAll("%", " % ")
30503051
.replaceAll(">", " > ")
30513052
.replaceAll("<", " < ")
30523053
.replaceAll("!=", " != ")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2949,7 +2949,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
29492949
| "wd": {
29502950
| "script": {
29512951
| "lang": "painless",
2952-
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value.get(ChronoField.DAY_OF_WEEK)); param1"
2952+
| "source": "def param1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (param1 == null) ? null : (param1.get(ChronoField.DAY_OF_WEEK) + 6) % 7"
29532953
| }
29542954
| },
29552955
| "yd": {
@@ -3051,6 +3051,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
30513051
.replaceAll("-", " - ")
30523052
.replaceAll("\\*", " * ")
30533053
.replaceAll("/", " / ")
3054+
.replaceAll("%", " % ")
30543055
.replaceAll(">", " > ")
30553056
.replaceAll("<", " < ")
30563057
.replaceAll("!=", " != ")

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,23 @@ package object time {
294294

295295
class DayOfMonth extends TimeFieldExtract(DAY_OF_MONTH)
296296

297-
class DayOfWeek extends TimeFieldExtract(DAY_OF_WEEK)
297+
class DayOfWeek(date: Identifier)
298+
extends TimeFieldExtract(DAY_OF_WEEK)
299+
with FunctionWithIdentifier {
300+
override def identifier: Identifier = date
301+
override def args: List[PainlessScript] = List(identifier)
302+
override def toPainlessCall(
303+
callArgs: List[String],
304+
context: Option[PainlessContext]
305+
): String = {
306+
callArgs match {
307+
case arg :: Nil =>
308+
s"($arg.get(${field.painless(context)}) + 6) % 7"
309+
case _ => throw new IllegalArgumentException("DayOfWeek requires exactly one argument")
310+
}
311+
}
312+
313+
}
298314

299315
class DayOfYear extends TimeFieldExtract(DAY_OF_YEAR)
300316

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,21 @@ package object time {
191191

192192
import TimeField._
193193

194+
def day_of_week_tr: PackratParser[FunctionWithIdentifier] =
195+
DAY_OF_WEEK.regex ~ start ~ (identifierWithTransformation | identifierWithIntervalFunction | identifierWithFunction | identifier) ~ end ^^ {
196+
case _ ~ _ ~ i ~ _ => new DayOfWeek(i)
197+
}
198+
199+
def day_of_week_identifier: PackratParser[Identifier] = day_of_week_tr ^^ { dw =>
200+
dw.identifier.withFunctions(dw +: dw.identifier.functions)
201+
}
202+
194203
def year_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
195204
YEAR.regex ^^ (_ => new Year)
196205
def month_of_year_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
197206
MONTH_OF_YEAR.regex ^^ (_ => new MonthOfYear)
198207
def day_of_month_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
199208
DAY_OF_MONTH.regex ^^ (_ => new DayOfMonth)
200-
def day_of_week_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
201-
DAY_OF_WEEK.regex ^^ (_ => new DayOfWeek)
202209
def day_of_year_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
203210
DAY_OF_YEAR.regex ^^ (_ => new DayOfYear)
204211
def hour_of_day_tr: PackratParser[TransformFunction[SQLTemporal, SQLNumeric]] =
@@ -228,7 +235,6 @@ package object time {
228235
year_tr |
229236
month_of_year_tr |
230237
day_of_month_tr |
231-
day_of_week_tr |
232238
day_of_year_tr |
233239
hour_of_day_tr |
234240
minute_of_hour_tr |
@@ -250,6 +256,7 @@ package object time {
250256
dateTimeFunctionWithIdentifier |
251257
date_diff_identifier |
252258
date_trunc_identifier |
259+
day_of_week_identifier |
253260
extract_identifier) ~ rep(intervalFunction) ^^ { case i ~ f =>
254261
i.withFunctions(f ++ i.functions)
255262
}

0 commit comments

Comments
 (0)