Skip to content

Commit 334c326

Browse files
committed
add SQLType and SQLTypedFunction[IN, OUT], add validations for functions using sql type OUT -> IN, add YEAR, MONTH, DAY, HOUR, MINUTE, SECOND extractors
1 parent c931b70 commit 334c326

File tree

12 files changed

+420
-66
lines changed

12 files changed

+420
-66
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ object ElasticAggregation {
102102
): Aggregation = {
103103
if (transformFuncs.nonEmpty) {
104104
val base = s"doc['$sourceField'].value"
105-
val scriptSrc = transformFuncs.foldLeft(base) {
106-
case (expr, f: SQLTransformFunction) => f.toPainless(expr)
107-
case (expr, f) => f.toSQL(expr) // fallback
105+
val orderedTransforms = transformFuncs.reverse
106+
val scriptSrc = orderedTransforms.foldLeft(base) {
107+
case (expr, f: SQLTransformFunction[_, _]) => f.toPainless(expr)
108+
case (expr, f) => f.toSQL(expr) // fallback
108109
}
109110
val script = Script(scriptSrc).lang("painless")
110111
buildScript(aggName, script)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
882882
| "ct": {
883883
| "script": {
884884
| "lang": "painless",
885-
| "source": "doc['createdAt'].value.minus(35, ChronoUnit.MINUTE)"
885+
| "source": "doc['createdAt'].value.minus(35, ChronoUnit.MINUTES)"
886886
| }
887887
| }
888888
| },
@@ -984,7 +984,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
984984
| "script": {
985985
| "script": {
986986
| "lang": "painless",
987-
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTE);"
987+
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTES);"
988988
| }
989989
| }
990990
| }
@@ -1041,7 +1041,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
10411041
| "lastSeen": "lastSeen"
10421042
| },
10431043
| "script": {
1044-
| "source": "(params.lastSeen != null) && (params.lastSeen > ZonedDateTime.now(ZoneId.of('Z')).minus(7, ChronoUnit.DAY).toInstant().toEpochMilli())"
1044+
| "source": "(params.lastSeen != null) && (params.lastSeen > ZonedDateTime.now(ZoneId.of('Z')).minus(7, ChronoUnit.DAYS).toInstant().toEpochMilli())"
10451045
| }
10461046
| }
10471047
| }
@@ -1212,7 +1212,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12121212
| }
12131213
|}""".stripMargin
12141214
.replaceAll("\\s", "")
1215-
.replaceAll("ChronoUnit", " ChronoUnit")
1215+
.replaceAll(",ChronoUnit", ", ChronoUnit")
12161216
.replaceAll("==", " == ")
12171217
.replaceAll("!=", " != ")
12181218
.replaceAll("&&", " && ")
@@ -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)"
1262+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, LocalDateTime::from).truncatedTo(ChronoUnit.MINUTES)"
12631263
| }
12641264
| }
12651265
| }
@@ -1268,7 +1268,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12681268
| }
12691269
|}""".stripMargin
12701270
.replaceAll("\\s", "")
1271-
.replaceAll("ChronoUnit", " ChronoUnit")
1271+
.replaceAll(",ChronoUnit", ", ChronoUnit")
12721272
.replaceAll("==", " == ")
12731273
.replaceAll("!=", " != ")
12741274
.replaceAll("&&", " && ")

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ object ElasticAggregation {
101101
): Aggregation = {
102102
if (transformFuncs.nonEmpty) {
103103
val base = s"doc['$sourceField'].value"
104-
val scriptSrc = transformFuncs.foldLeft(base) {
105-
case (expr, f: SQLTransformFunction) => f.toPainless(expr)
106-
case (expr, f) => f.toSQL(expr) // fallback
104+
val orderedTransforms = transformFuncs.reverse
105+
val scriptSrc = orderedTransforms.foldLeft(base) {
106+
case (expr, f: SQLTransformFunction[_, _]) => f.toPainless(expr)
107+
case (expr, f) => f.toSQL(expr) // fallback
107108
}
108109
val script = Script(scriptSrc).lang("painless")
109110
buildScript(aggName, script)

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
@@ -881,7 +881,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
881881
| "ct": {
882882
| "script": {
883883
| "lang": "painless",
884-
| "source": "doc['createdAt'].value.minus(35, ChronoUnit.MINUTE)"
884+
| "source": "doc['createdAt'].value.minus(35, ChronoUnit.MINUTES)"
885885
| }
886886
| }
887887
| },
@@ -983,7 +983,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
983983
| "script": {
984984
| "script": {
985985
| "lang": "painless",
986-
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTE);"
986+
| "source": "return doc['createdAt'].value.toLocalTime() >= LocalTime.now().minus(10, ChronoUnit.MINUTES);"
987987
| }
988988
| }
989989
| }
@@ -1040,7 +1040,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
10401040
| "lastSeen": "lastSeen"
10411041
| },
10421042
| "script": {
1043-
| "source": "(params.lastSeen != null) && (params.lastSeen > ZonedDateTime.now(ZoneId.of('Z')).minus(7, ChronoUnit.DAY).toInstant().toEpochMilli())"
1043+
| "source": "(params.lastSeen != null) && (params.lastSeen > ZonedDateTime.now(ZoneId.of('Z')).minus(7, ChronoUnit.DAYS).toInstant().toEpochMilli())"
10441044
| }
10451045
| }
10461046
| }
@@ -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)"
1259+
| "source": "DateTimeFormatter.ofPattern('yyyy-MM-ddTHH:mm:ssZ').parse(doc['createdAt'].value, LocalDateTime::from).truncatedTo(ChronoUnit.MINUTES)"
12601260
| }
12611261
| }
12621262
| }

0 commit comments

Comments
 (0)