Skip to content

Commit 1d2426b

Browse files
committed
fix substring using inline
1 parent d0f14ed commit 1d2426b

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
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
@@ -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 : def _start = (1 - 1); def _end = _start + 3; (_start < 0 || _end > arg0.length()) ? null : arg0.substring(_start, _end) )"
2365+
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : ((1 - 1) < 0 || (1 - 1 + 3) > arg0.length()) ? null : arg0.substring((1 - 1), (1 - 1 + 3)))"
23662366
| }
23672367
| },
23682368
| "trim": {
@@ -2392,6 +2392,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23922392
.replaceAll("def_", "def _")
23932393
.replaceAll("=_", " = _")
23942394
.replaceAll(",_", ", _")
2395+
.replaceAll(",\\(", ", (")
23952396
.replaceAll("if\\(", "if (")
23962397
.replaceAll("=\\(", " = (")
23972398
.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
@@ -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 : def _start = (1 - 1); def _end = _start + 3; (_start < 0 || _end > arg0.length()) ? null : arg0.substring(_start, _end) )"
2354+
| "source": "(def arg0 = (!doc.containsKey('identifier2') || doc['identifier2'].empty ? null : doc['identifier2'].value); (arg0 == null) ? null : ((1 - 1) < 0 || (1 - 1 + 3) > arg0.length()) ? null : arg0.substring((1 - 1), (1 - 1 + 3)))"
23552355
| }
23562356
| },
23572357
| "trim": {
@@ -2381,6 +2381,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
23812381
.replaceAll("def_", "def _")
23822382
.replaceAll("=_", " = _")
23832383
.replaceAll(",_", ", _")
2384+
.replaceAll(",\\(", ", (")
23842385
.replaceAll("if\\(", "if (")
23852386
.replaceAll("=\\(", " = (")
23862387
.replaceAll(":\\(", " : (")

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,27 +1068,21 @@ case class SQLSubstring(str: PainlessScript, start: Int, length: Option[Int])
10681068
callArgs match {
10691069
// SUBSTRING(expr, start, length)
10701070
case List(arg0, 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", " ")
1071+
s"(($arg1 - 1) < 0 || ($arg1 - 1 + $arg2) > $arg0.length()) ? null : $arg0.substring(($arg1 - 1), ($arg1 - 1 + $arg2))"
10751072

10761073
// SUBSTRING(expr, start)
10771074
case List(arg0, arg1) =>
1078-
s"""def _start = ($arg1 - 1);
1079-
|(_start < 0 || _start >= $arg0.length()) ? null : $arg0.substring(_start)""".stripMargin
1080-
.replaceAll("\n", " ")
1075+
s"(($arg1 - 1) < 0 || ($arg1 - 1) >= $arg0.length()) ? null : $arg0.substring(($arg1 - 1))"
10811076

10821077
case _ => throw new IllegalArgumentException("SUBSTRING requires 2 or 3 arguments")
10831078
}
10841079
}
1080+
10851081
override def validate(): Either[String, Unit] =
1086-
if (start < 0)
1087-
Left("SUBSTRING start position must be greater than or equal to 0")
1082+
if (start < 1)
1083+
Left("SUBSTRING start position must be greater than or equal to 1 (SQL is 1-based)")
10881084
else if (length.exists(_ < 0))
10891085
Left("SUBSTRING length must be non-negative")
1090-
else if (length.exists(_ < start))
1091-
Left("SUBSTRING length must be greater than or equal to start position")
10921086
else Right(())
10931087

10941088
override def toSQL(base: String): String = sql

0 commit comments

Comments
 (0)