@@ -1332,7 +1332,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13321332 | "diff": {
13331333 | "script": {
13341334 | "lang": "painless",
1335- | "source": "(def s = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def e = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); s != null && e != null ? ChronoUnit.DAYS.between(s, e) : null )"
1335+ | "source": "(def arg0 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def arg1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (arg0 == null || arg1 == null) ? null : ChronoUnit.DAYS.between(arg0, arg1) )"
13361336 | }
13371337 | }
13381338 | },
@@ -1345,14 +1345,14 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13451345 .replaceAll(" \\ s" , " " )
13461346 .replaceAll(" defv" , " def v" )
13471347 .replaceAll(" defe" , " def e" )
1348- .replaceAll(" defs " , " def s " )
1348+ .replaceAll(" defa " , " def a " )
13491349 .replaceAll(" if\\ (" , " if (" )
13501350 .replaceAll(" =\\ (" , " = (" )
13511351 .replaceAll(" \\ ?" , " ? " )
13521352 .replaceAll(" :null" , " : null" )
13531353 .replaceAll(" null:" , " null : " )
13541354 .replaceAll(" return" , " return " )
1355- .replaceAll(" between \\ (s, " , " between(s, " )
1355+ .replaceAll(" ,a " , " , a " )
13561356 .replaceAll(" ;" , " ; " )
13571357 .replaceAll(" ==" , " == " )
13581358 .replaceAll(" !=" , " != " )
@@ -1382,7 +1382,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13821382 | "max": {
13831383 | "script": {
13841384 | "lang": "painless",
1385- | "source": "(def s = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def e = (def e0 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); e0 != null ? DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(e0, ZonedDateTime::from) : null); s != null && e != null ? ChronoUnit.DAYS.between(s, e) : null )"
1385+ | "source": "(def arg0 = (!doc.containsKey('updatedAt') || doc['updatedAt'].empty ? null : doc['updatedAt'].value); def arg1 = (def e0 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); e0 != null ? DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(e0, ZonedDateTime::from) : null); (arg0 == null || arg1 == null) ? null : ChronoUnit.DAYS.between(arg0, arg1) )"
13861386 | }
13871387 | }
13881388 | }
@@ -1393,14 +1393,14 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
13931393 .replaceAll(" \\ s" , " " )
13941394 .replaceAll(" defv" , " def v" )
13951395 .replaceAll(" defe" , " def e" )
1396- .replaceAll(" defs " , " def s " )
1396+ .replaceAll(" defa " , " def a " )
13971397 .replaceAll(" if\\ (" , " if (" )
13981398 .replaceAll(" =\\ (" , " = (" )
13991399 .replaceAll(" \\ ?" , " ? " )
14001400 .replaceAll(" :null" , " : null" )
14011401 .replaceAll(" null:" , " null : " )
14021402 .replaceAll(" return" , " return " )
1403- .replaceAll(" between \\ (s, " , " between(s, " )
1403+ .replaceAll(" ,a " , " , a " )
14041404 .replaceAll(" ;" , " ; " )
14051405 .replaceAll(" ==" , " == " )
14061406 .replaceAll(" !=" , " != " )
@@ -1808,7 +1808,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
18081808 | "c": {
18091809 | "script": {
18101810 | "lang": "painless",
1811- | "source": "{ def v0 = ({ def e1= (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def e2= DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from).minus(2, ChronoUnit.DAYS); return e1 == e2 ? null : e1; } );if (v0 != null) return v0; return ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); }"
1811+ | "source": "{ def v0 = (( def arg0 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (arg0 == null) ? null : arg0 == DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from).minus(2, ChronoUnit.DAYS) ? null : arg0) );if (v0 != null) return v0; return ZonedDateTime.now(ZoneId.of('Z')).toLocalDate(); }"
18121812 | }
18131813 | }
18141814 | },
@@ -1820,7 +1820,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
18201820 |}""" .stripMargin
18211821 .replaceAll(" \\ s+" , " " )
18221822 .replaceAll(" defv" , " def v" )
1823- .replaceAll(" defe " , " def e " )
1823+ .replaceAll(" defa " , " def a " )
18241824 .replaceAll(" if\\ (" , " if (" )
18251825 .replaceAll(" =\\ (" , " = (" )
18261826 .replaceAll(" \\ ?" , " ? " )
@@ -1832,6 +1832,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
18321832 .replaceAll(" returnv" , " return v" )
18331833 .replaceAll(" returne" , " return e" )
18341834 .replaceAll(" ;}" , " ; }" )
1835+ .replaceAll(" ;\\ (" , " ; (" )
18351836 .replaceAll(" ==" , " == " )
18361837 .replaceAll(" !=" , " != " )
18371838 .replaceAll(" &&" , " && " )
@@ -1857,7 +1858,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
18571858 | "c": {
18581859 | "script": {
18591860 | "lang": "painless",
1860- | "source": "{ def v0 = ({ def e1 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); def e2 = DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from); return e1 == e2 ? null : e1; } );if (v0 != null) return v0; return (ZonedDateTime.now(ZoneId.of('Z')).toLocalDate()).atStartOfDay(ZoneId.of('Z')).minus(2, ChronoUnit.HOURS); }.toInstant().toEpochMilli()"
1861+ | "source": "{ def v0 = (( def arg0 = (!doc.containsKey('createdAt') || doc['createdAt'].empty ? null : doc['createdAt'].value); (arg0 == null) ? null : arg0 == DateTimeFormatter.ofPattern('yyyy-MM-dd').parse(\"2025-09-11\", LocalDate::from) ? null : arg0) );if (v0 != null) return v0; return (ZonedDateTime.now(ZoneId.of('Z')).toLocalDate()).atStartOfDay(ZoneId.of('Z')).minus(2, ChronoUnit.HOURS); }.toInstant().toEpochMilli()"
18611862 | }
18621863 | }
18631864 | },
@@ -1869,7 +1870,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
18691870 |}""" .stripMargin
18701871 .replaceAll(" \\ s+" , " " )
18711872 .replaceAll(" defv" , " def v" )
1872- .replaceAll(" defe " , " def e " )
1873+ .replaceAll(" defa " , " def a " )
18731874 .replaceAll(" if\\ (" , " if (" )
18741875 .replaceAll(" =\\ (" , " = (" )
18751876 .replaceAll(" \\ ?" , " ? " )
@@ -2147,4 +2148,175 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
21472148 .replaceAll(" ==" , " == " )
21482149 .replaceAll(" \\ |\\ |" , " || " )
21492150 }
2151+
2152+ it should " handle mathematic function as script field and condition" in {
2153+ val select : ElasticSearchRequest =
2154+ SQLQuery (mathematical)
2155+ val query = select.query
2156+ println(query)
2157+ query shouldBe
2158+ """ {
2159+ | "query": {
2160+ | "bool": {
2161+ | "filter": [
2162+ | {
2163+ | "script": {
2164+ | "script": {
2165+ | "lang": "painless",
2166+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.sqrt(arg0)) > 100.0"
2167+ | }
2168+ | }
2169+ | }
2170+ | ]
2171+ | }
2172+ | },
2173+ | "script_fields": {
2174+ | "abs_identifier_plus_1_0_mul_2": {
2175+ | "script": {
2176+ | "lang": "painless",
2177+ | "source": "((def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.abs(arg0)) + 1.0) * ((double) 2)"
2178+ | }
2179+ | },
2180+ | "ceil_identifier": {
2181+ | "script": {
2182+ | "lang": "painless",
2183+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.ceil(arg0))"
2184+ | }
2185+ | },
2186+ | "floor_identifier": {
2187+ | "script": {
2188+ | "lang": "painless",
2189+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.floor(arg0))"
2190+ | }
2191+ | },
2192+ | "sqrt_identifier": {
2193+ | "script": {
2194+ | "lang": "painless",
2195+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.sqrt(arg0))"
2196+ | }
2197+ | },
2198+ | "exp_identifier": {
2199+ | "script": {
2200+ | "lang": "painless",
2201+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.exp(arg0))"
2202+ | }
2203+ | },
2204+ | "log_identifier": {
2205+ | "script": {
2206+ | "lang": "painless",
2207+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.log(arg0))"
2208+ | }
2209+ | },
2210+ | "log10_identifier": {
2211+ | "script": {
2212+ | "lang": "painless",
2213+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.log10(arg0))"
2214+ | }
2215+ | },
2216+ | "pow_identifier_3": {
2217+ | "script": {
2218+ | "lang": "painless",
2219+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.pow(arg0, 3))"
2220+ | }
2221+ | },
2222+ | "round_identifier_0": {
2223+ | "script": {
2224+ | "lang": "painless",
2225+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : (def p = Math.pow(10, 0); Math.round((arg0 * p) / p)))"
2226+ | }
2227+ | },
2228+ | "round_identifier_2": {
2229+ | "script": {
2230+ | "lang": "painless",
2231+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : (def p = Math.pow(10, 2); Math.round((arg0 * p) / p)))"
2232+ | }
2233+ | },
2234+ | "sign_identifier": {
2235+ | "script": {
2236+ | "lang": "painless",
2237+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); arg0 != null ? (arg0 > 0 ? 1 : (arg0 < 0 ? -1 : 0)) : null)"
2238+ | }
2239+ | },
2240+ | "cos_identifier": {
2241+ | "script": {
2242+ | "lang": "painless",
2243+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.cos(arg0))"
2244+ | }
2245+ | },
2246+ | "acos_identifier": {
2247+ | "script": {
2248+ | "lang": "painless",
2249+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.acos(arg0))"
2250+ | }
2251+ | },
2252+ | "sin_identifier": {
2253+ | "script": {
2254+ | "lang": "painless",
2255+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.sin(arg0))"
2256+ | }
2257+ | },
2258+ | "asin_identifier": {
2259+ | "script": {
2260+ | "lang": "painless",
2261+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.asin(arg0))"
2262+ | }
2263+ | },
2264+ | "tan_identifier": {
2265+ | "script": {
2266+ | "lang": "painless",
2267+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.tan(arg0))"
2268+ | }
2269+ | },
2270+ | "atan_identifier": {
2271+ | "script": {
2272+ | "lang": "painless",
2273+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.atan(arg0))"
2274+ | }
2275+ | },
2276+ | "atan2_identifier_3_0": {
2277+ | "script": {
2278+ | "lang": "painless",
2279+ | "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.atan2(arg0, 3.0))"
2280+ | }
2281+ | }
2282+ | },
2283+ | "_source": {
2284+ | "includes": [
2285+ | "identifier"
2286+ | ]
2287+ | }
2288+ |}""" .stripMargin
2289+ .replaceAll(" \\ s+" , " " )
2290+ .replaceAll(" defv" , " def v" )
2291+ .replaceAll(" defa" , " def a" )
2292+ .replaceAll(" defp" , " def p" )
2293+ .replaceAll(" if\\ (" , " if (" )
2294+ .replaceAll(" =\\ (" , " = (" )
2295+ .replaceAll(" :\\ (" , " : (" )
2296+ .replaceAll(" :0" , " : 0" )
2297+ .replaceAll(" =Math" , " = Math" )
2298+ .replaceAll(" ,(\\ d)" , " , $1" )
2299+ .replaceAll(" \\ ?" , " ? " )
2300+ .replaceAll(" :null" , " : null" )
2301+ .replaceAll(" null:" , " null : " )
2302+ .replaceAll(" return" , " return " )
2303+ .replaceAll(" between\\ (s," , " between(s, " )
2304+ .replaceAll(" ;" , " ; " )
2305+ .replaceAll(" ; if" , " ;if" )
2306+ .replaceAll(" ==" , " == " )
2307+ .replaceAll(" \\ +" , " + " )
2308+ .replaceAll(" \\ *" , " * " )
2309+ .replaceAll(" /" , " / " )
2310+ .replaceAll(" >" , " > " )
2311+ .replaceAll(" <" , " < " )
2312+ .replaceAll(" !=" , " != " )
2313+ .replaceAll(" &&" , " && " )
2314+ .replaceAll(" \\ |\\ |" , " || " )
2315+ .replaceAll(" ;\\ s\\ s" , " ; " )
2316+ .replaceAll(" ChronoUnit" , " ChronoUnit" )
2317+ .replaceAll(" ,LocalDate" , " , LocalDate" )
2318+ .replaceAll(" =DateTimeFormatter" , " = DateTimeFormatter" )
2319+ .replaceAll(" \\ (double\\ )(\\ d)" , " (double) $1" )
2320+ }
2321+
21502322}
0 commit comments