[ASTERIXDB-2978][FUN] Fix substr1() handling of offset 0

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Fix substr1(x,0) to return input string instead of null

Change-Id: I5c313c163165f03b2c55d730438a2723f15dcc3d
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/13803
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substr1/substr1.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substr1/substr1.1.adm
index ba46923..f524208 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substr1/substr1.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substr1/substr1.1.adm
@@ -1 +1 @@
-[ "g", null, "ab", "bc", "cd" ]
\ No newline at end of file
+[ "g", "ab", "ab", "bc", "cd" ]
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substring1/substring1.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substring1/substring1.1.adm
index ba46923..f524208 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substring1/substring1.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substr01/offset1/substring1/substring1.1.adm
@@ -1 +1 @@
-[ "g", null, "ab", "bc", "cd" ]
\ No newline at end of file
+[ "g", "ab", "ab", "bc", "cd" ]
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substr1/substr1.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substr1/substr1.1.adm
index 772cf57..fb038ae 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substr1/substr1.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substr1/substr1.1.adm
@@ -1 +1 @@
-[ "g", null, "abcdefg", "bcdefg", "cdefg" ]
\ No newline at end of file
+[ "g", "abcdefg", "abcdefg", "bcdefg", "cdefg" ]
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substring1/substring1.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substring1/substring1.1.adm
index 772cf57..fb038ae 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substring1/substring1.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring2-1/offset1/substring1/substring1.1.adm
@@ -1 +1 @@
-[ "g", null, "abcdefg", "bcdefg", "cdefg" ]
\ No newline at end of file
+[ "g", "abcdefg", "abcdefg", "bcdefg", "cdefg" ]
\ No newline at end of file
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/Substring2Eval.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/Substring2Eval.java
index 124ae40..e5a6853 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/Substring2Eval.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/Substring2Eval.java
@@ -96,7 +96,7 @@
         string.set(bytes, offset + 1, len - 1);
         array.reset();
         try {
-            int actualStart = start >= 0 ? start - baseOffset : string.getStringLength() + start;
+            int actualStart = start >= 0 ? Math.max(start - baseOffset, 0) : string.getStringLength() + start;
             boolean success = UTF8StringPointable.substr(string, actualStart, Integer.MAX_VALUE, builder, array);
             if (success) {
                 out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/SubstringEval.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/SubstringEval.java
index e8c2ff8..dec6cce 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/SubstringEval.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/SubstringEval.java
@@ -108,7 +108,7 @@
         string.set(bytes, offset + 1, length - 1);
         array.reset();
         try {
-            int actualStart = start >= 0 ? start - baseOffset : string.getStringLength() + start;
+            int actualStart = start >= 0 ? Math.max(start - baseOffset, 0) : string.getStringLength() + start;
             boolean success = UTF8StringPointable.substr(string, actualStart, len, builder, array);
             if (success) {
                 out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);