LuciferYang opened a new pull request #34313:
URL: https://github.com/apache/spark/pull/34313
### What changes were proposed in this pull request?
The following sql has different behavior when using Java 8 and Java 17
```
--PostgreSQL throw ERROR: format specifies argument 0, but arguments are
numbered from 1
select format_string('%0$s', 'Hello');
```
**Use Java 8**
```
-- !query
select format_string('%0$s', 'Hello')
-- !query schema
struct<format_string(%0$s, Hello):string>
-- !query output
Hello
```
**Use Java 17**
```
-- !query
select format_string('%0$s', 'Hello')
-- !query schema
struct<>
-- !query output
java.util.IllegalFormatArgumentIndexException
Illegal format argument index = 0
```
The difference in this behavior comes from the change of
`j.u.Formatter.FormatSpecifier.index` method:
Java 8
```
private int index(String s) {
if (s != null) {
try {
index = Integer.parseInt(s.substring(0, s.length() - 1));
} catch (NumberFormatException x) {
assert(false);
}
} else {
index = 0;
}
return index;
}
```
Java 17
```java
private void index(String s, int start, int end) {
if (start >= 0) {
try {
// skip the trailing '$'
index = Integer.parseInt(s, start, end - 1, 10);
if (index <= 0) {
throw new IllegalFormatArgumentIndexException(index);
}
} catch (NumberFormatException x) {
throw new
IllegalFormatArgumentIndexException(Integer.MIN_VALUE);
}
}
}
```
A `index <= 0` condition is added here to ensure `%0$` as a
`IllegalFormatArgumentIndexException` expression.
So the main change of this pr is add the manually replacing `%0$` with `%1$`
to ensure that Java 17 and Java 8 have the same behavior.
### Why are the changes needed?
Pass UT with JDK 17
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
- Pass the Jenkins or GitHub Action
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]