Repository: partiql/partiql-lang-kotlin (kotlin)

Issue:
REPL exits for `INTERVAL` datum in some case
## Description
REPL exits when `INTERVAL` datum underflows.

## To Reproduce
run such a command of below, cli will throw a `java.lang.StringIndexOutOfBoundsException` and exit

```
interval '2:3.00000005' minute to second(6);
interval '3:4' minute to second(3);
```

## Expected Behavior
Give the expected result. 
If this case is not allowed, print a error and continue processing.

## Additional Context
- Java version: 23
- PartiQL version: 1.2.0-4ac06dcb3

Pull request description:
## Relevant Issues
- fix Issue #1777 

## Description
For the `INTERVAL` datum, when the `value` of nanos is less than $10^{(10 - scale)}$, formatter function will throw a exception, which causes a unexpected REPL exit.
I changed the formatter function logic to handle this case correctly.

## Other Information
- Updated Unreleased Section in CHANGELOG: **[YES]**: <Explain if NO>
- Any backward-incompatible changes? **[NO]**: <Explain if YES>
- Any new external dependencies? **[NO]**: <Explain if YES>
- Do your changes comply with the [contributing][cg] and [code style][csg] guidelines? **[YES]**

## License Information

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

<!-- DO NOT DELETE BELOW -->

[cg]: https://github.com/partiql/partiql-lang-kotlin/blob/main/CONTRIBUTING.md
[csg]: https://github.com/partiql/partiql-lang-kotlin/blob/main/CODE_STYLE.md

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f9e1a05a..33b67941e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,7 @@ rewritten to their query representation
 
 ### Fixed
 - Specify the correct qualifier for `INTERVAL` datum with `MINUTE TO SECOND`
+- Handle the formatting when the nanoseconds of `INTERVAL` datum underflow
 - CLI printing of negative interval literals
 
 ### Removed
diff --git a/partiql-cli/src/main/kotlin/org/partiql/cli/io/DatumWriterTextPretty.kt b/partiql-cli/src/main/kotlin/org/partiql/cli/io/DatumWriterTextPretty.kt
index 91c56654d..337263e64 100644
--- a/partiql-cli/src/main/kotlin/org/partiql/cli/io/DatumWriterTextPretty.kt
+++ b/partiql-cli/src/main/kotlin/org/partiql/cli/io/DatumWriterTextPretty.kt
@@ -348,14 +348,8 @@ class DatumWriterTextPretty(
             if (scale == 0) {
                 return@buildString
             }
-            val before = value.toString()
-            val beforeLength = before.length
-            val toPadBefore = nanoLength - beforeLength
-            if (toPadBefore > 0) {
-                append("0".repeat(toPadBefore))
-            }
-            val remaining = before.substring(0, scale - toPadBefore)
-            append(remaining)
+            val paddedValue = value.toString().padStart(nanoLength, '0')
+            append(paddedValue.substring(0, minOf(scale, paddedValue.length)))
         }
     }
 


Test patch:
diff --git a/partiql-cli/src/test/kotlin/org/partiql/cli/io/DatumWriterTextPrettyTests.kt b/partiql-cli/src/test/kotlin/org/partiql/cli/io/DatumWriterTextPrettyTests.kt
index f9fb222d7..0700a04c3 100644
--- a/partiql-cli/src/test/kotlin/org/partiql/cli/io/DatumWriterTextPrettyTests.kt
+++ b/partiql-cli/src/test/kotlin/org/partiql/cli/io/DatumWriterTextPrettyTests.kt
@@ -72,6 +72,8 @@ class DatumWriterTextPrettyTests {
           INTERVAL '-1:2' HOUR (2) TO MINUTE,
           INTERVAL '-1:2:3.40' HOUR (2) TO SECOND (2),
           INTERVAL '-1:2.30' MINUTE (2) TO SECOND (2),
+          INTERVAL '3:4.00' MINUTE (2) TO SECOND (2),
+          INTERVAL '3:4.012345678' MINUTE (2) TO SECOND (9),
           {
             'bar': [
               1,
@@ -141,6 +143,8 @@ class DatumWriterTextPrettyTests {
             Datum.intervalHourMinute(-1, -2, 2),
             Datum.intervalHourSecond(-1, -2, -3, -400000000, 2, 2),
             Datum.intervalMinuteSecond(-1, -2, -300000000, 2, 2),
+            Datum.intervalMinuteSecond(3, 4, 0, 2, 2),
+            Datum.intervalMinuteSecond(3, 4, 12345678, 2, 9),
             // TODO: Technically, structs and bags are unordered. This may or may not lead to issues in the future
             //  when testing. We should potentially instead rewrite these tests to re-parse the output and do a full-on
             //  comparison between the values instead. This approach, however, would not take into account the indents.