Skip to content

Commit 75c88f2

Browse files
committed
update painless within identifier
1 parent d08ce77 commit 75c88f2

File tree

6 files changed

+17
-28
lines changed

6 files changed

+17
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ object ElasticAggregation {
9898
buildScript: (String, Script) => Aggregation
9999
): Aggregation = {
100100
if (transformFuncs.nonEmpty) {
101-
val scriptSrc = SQLFunctionUtils.buildPainless(identifier)
101+
val scriptSrc = identifier.painless
102102
val script = Script(scriptSrc).lang("painless")
103103
buildScript(aggName, script)
104104
} else {

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

Lines changed: 4 additions & 4 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, ZonedDateTime::from)"
1206+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(doc['createdAt'].value, LocalDate::from)"
12071207
| }
12081208
| }
12091209
| }
@@ -1217,7 +1217,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12171217
.replaceAll("!=", " != ")
12181218
.replaceAll("&&", " && ")
12191219
.replaceAll(">", " > ")
1220-
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
1220+
.replaceAll(",LocalDate", ", LocalDate")
12211221
}
12221222

12231223
it should "handle parse_datetime function" in {
@@ -1259,7 +1259,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12591259
| "field": "createdAt",
12601260
| "script": {
12611261
| "lang": "painless",
1262-
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, LocalDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoUnit.YEARS)"
1262+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, ZonedDateTime::from).truncatedTo(ChronoUnit.MINUTES).get(ChronoUnit.YEARS)"
12631263
| }
12641264
| }
12651265
| }
@@ -1272,7 +1272,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12721272
.replaceAll("!=", " != ")
12731273
.replaceAll("&&", " && ")
12741274
.replaceAll(">", " > ")
1275-
.replaceAll(",LocalDate", ", LocalDate")
1275+
.replaceAll(",ZonedDateTime", ", ZonedDateTime")
12761276
}
12771277

12781278
it should "handle date_diff function as script field" in {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object ElasticAggregation {
9797
buildScript: (String, Script) => Aggregation
9898
): Aggregation = {
9999
if (transformFuncs.nonEmpty) {
100-
val scriptSrc = SQLFunctionUtils.buildPainless(identifier)
100+
val scriptSrc = identifier.painless
101101
val script = Script(scriptSrc).lang("painless")
102102
buildScript(aggName, script)
103103
} else {

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ object SQLFunctionUtils {
2020
aggregateAndTransformFunctions(identifier)._2
2121
}
2222

23-
def buildPainless(
24-
identifier: Identifier
25-
): String = {
26-
val base = identifier.painless
27-
val orderedFunctions = transformFunctions(identifier).reverse
28-
orderedFunctions.foldLeft(base) {
29-
case (expr, f: SQLTransformFunction[_, _]) => f.toPainless(expr)
30-
case (expr, f: PainlessScript) => s"$expr${f.painless}"
31-
case (expr, f) => f.toSQL(expr) // fallback
32-
}
33-
}
3423
}
3524

3625
trait SQLFunctionChain extends SQLFunction with SQLValidation {
@@ -287,15 +276,7 @@ case class DateDiff(end: PainlessScript, start: PainlessScript, unit: TimeUnit)
287276
override def toSQL(base: String): String = {
288277
s"$sql(${end.sql}, ${start.sql}, ${unit.sql})"
289278
}
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)"
279+
override def painless: String = s"${unit.painless}.between(${start.painless}, ${end.painless})"
299280
}
300281

301282
case class DateAdd(interval: TimeInterval)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sealed trait Field extends Updateable with SQLFunctionChain with PainlessScript
2626

2727
def update(request: SQLSearchRequest): Field
2828

29-
def painless: String = SQLFunctionUtils.buildPainless(identifier)
29+
def painless: String = identifier.painless
3030

3131
lazy val scriptName: String = fieldAlias.map(_.alias).getOrElse(sourceField)
3232
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,15 @@ package object sql {
267267

268268
lazy val aliasOrName: String = fieldAlias.getOrElse(name)
269269

270-
override def painless: String = if (name.nonEmpty) s"doc['$name'].value" else ""
270+
override def painless: String = {
271+
val base = if (name.nonEmpty) s"doc['$name'].value" else ""
272+
val orderedFunctions = SQLFunctionUtils.transformFunctions(this).reverse
273+
orderedFunctions.foldLeft(base) {
274+
case (expr, f: SQLTransformFunction[_, _]) => f.toPainless(expr)
275+
case (expr, f: PainlessScript) => s"$expr${f.painless}"
276+
case (expr, f) => f.toSQL(expr) // fallback
277+
}
278+
}
271279

272280
}
273281

0 commit comments

Comments
 (0)