Skip to content

Commit d0f14ed

Browse files
committed
fix substring
1 parent 38a61e5 commit d0f14ed

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23622362
| "substr": {
23632363
| "script": {
23642364
| "lang": "painless",
2365-
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : (arg0.length() < 3) ? null : arg0.substring(1, 3))"
2365+
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : def _start = (1 - 1); def _end = _start + 3; (_start < 0 || _end > arg0.length()) ? null : arg0.substring(_start, _end) )"
23662366
| }
23672367
| },
23682368
| "trim": {
@@ -2389,6 +2389,9 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23892389
.replaceAll("defa", "def a")
23902390
.replaceAll("defe", "def e")
23912391
.replaceAll("defl", "def l")
2392+
.replaceAll("def_", "def _")
2393+
.replaceAll("=_", " = _")
2394+
.replaceAll(",_", ", _")
23922395
.replaceAll("if\\(", "if (")
23932396
.replaceAll("=\\(", " = (")
23942397
.replaceAll(":\\(", " : (")
@@ -2402,6 +2405,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
24022405
.replaceAll("; if", ";if")
24032406
.replaceAll("==", " == ")
24042407
.replaceAll("\\+", " + ")
2408+
.replaceAll("-", " - ")
24052409
.replaceAll("\\*", " * ")
24062410
.replaceAll("/", " / ")
24072411
.replaceAll(">", " > ")

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23512351
| "substr": {
23522352
| "script": {
23532353
| "lang": "painless",
2354-
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : (arg0.length() < 3) ? null : arg0.substring(1, 3))"
2354+
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : def _start = (1 - 1); def _end = _start + 3; (_start < 0 || _end > arg0.length()) ? null : arg0.substring(_start, _end) )"
23552355
| }
23562356
| },
23572357
| "trim": {
@@ -2378,6 +2378,9 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23782378
.replaceAll("defa", "def a")
23792379
.replaceAll("defe", "def e")
23802380
.replaceAll("defl", "def l")
2381+
.replaceAll("def_", "def _")
2382+
.replaceAll("=_", " = _")
2383+
.replaceAll(",_", ", _")
23812384
.replaceAll("if\\(", "if (")
23822385
.replaceAll("=\\(", " = (")
23832386
.replaceAll(":\\(", " : (")
@@ -2391,6 +2394,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23912394
.replaceAll("; if", ";if")
23922395
.replaceAll("==", " == ")
23932396
.replaceAll("\\+", " + ")
2397+
.replaceAll("-", " - ")
23942398
.replaceAll("\\*", " * ")
23952399
.replaceAll("/", " / ")
23962400
.replaceAll(">", " > ")

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,14 +1066,22 @@ case class SQLSubstring(str: PainlessScript, start: Int, length: Option[Int])
10661066

10671067
override def toPainlessCall(callArgs: List[String]): String = {
10681068
callArgs match {
1069+
// SUBSTRING(expr, start, length)
10691070
case List(arg0, arg1, arg2) =>
1070-
s"($arg0${Length.painless} < $arg2) ? null : $arg0${operator.painless}($arg1, $arg2)"
1071+
s"""def _start = ($arg1 - 1);
1072+
|def _end = _start + $arg2;
1073+
|(_start < 0 || _end > $arg0.length()) ? null : $arg0.substring(_start, _end)""".stripMargin
1074+
.replaceAll("\n", " ")
1075+
1076+
// SUBSTRING(expr, start)
10711077
case List(arg0, arg1) =>
1072-
s"($arg0${Length.painless} < $arg1) ? null : $arg0${operator.painless}($arg1)"
1078+
s"""def _start = ($arg1 - 1);
1079+
|(_start < 0 || _start >= $arg0.length()) ? null : $arg0.substring(_start)""".stripMargin
1080+
.replaceAll("\n", " ")
1081+
10731082
case _ => throw new IllegalArgumentException("SUBSTRING requires 2 or 3 arguments")
10741083
}
10751084
}
1076-
10771085
override def validate(): Either[String, Unit] =
10781086
if (start < 0)
10791087
Left("SUBSTRING start position must be greater than or equal to 0")
@@ -1111,7 +1119,6 @@ case class SQLConcat(values: List[PainlessScript]) extends StringFunction[SQLVar
11111119
else Right(())
11121120

11131121
override def toSQL(base: String): String = sql
1114-
11151122
}
11161123

11171124
case object SQLLength extends StringFunction[SQLBigInt] {

0 commit comments

Comments
 (0)