SQL: Fix BasicFormatter NPE (#37804)

This commit is contained in:
Andrei Stefan 2019-01-24 15:40:51 +02:00 committed by GitHub
parent 7517e3a7bd
commit 163a27b93c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 18 deletions

View File

@ -36,7 +36,7 @@ public class BasicFormatter implements Writeable {
private final Function<Object, String> apply; private final Function<Object, String> apply;
FormatOption(Function<Object, String> apply) { FormatOption(Function<Object, String> apply) {
this.apply = l -> l == null ? null : apply.apply(l); this.apply = apply;
} }
public final String apply(Object l) { public final String apply(Object l) {

View File

@ -22,12 +22,14 @@ public class BasicFormatterTests extends ESTestCase {
new ColumnInfo("", "foo", "string", 0), new ColumnInfo("", "foo", "string", 0),
new ColumnInfo("", "bar", "long", 15), new ColumnInfo("", "bar", "long", 15),
new ColumnInfo("", "15charwidename!", "double", 25), new ColumnInfo("", "15charwidename!", "double", 25),
new ColumnInfo("", "null_field1", "integer", 0),
new ColumnInfo("", "superduperwidename!!!", "double", 25), new ColumnInfo("", "superduperwidename!!!", "double", 25),
new ColumnInfo("", "baz", "keyword", 0), new ColumnInfo("", "baz", "keyword", 0),
new ColumnInfo("", "date", "datetime", 24)), new ColumnInfo("", "date", "datetime", 24),
new ColumnInfo("", "null_field2", "keyword", 0)),
Arrays.asList( Arrays.asList(
Arrays.asList("15charwidedata!", 1, 6.888, 12, "rabbit", "1953-09-02T00:00:00.000Z"), Arrays.asList("15charwidedata!", 1, 6.888, null, 12, "rabbit", "1953-09-02T00:00:00.000Z", null),
Arrays.asList("dog", 1.7976931348623157E308, 123124.888, 9912, "goat", "2000-03-15T21:34:37.443Z"))); Arrays.asList("dog", 1.7976931348623157E308, 123124.888, null, 9912, "goat", "2000-03-15T21:34:37.443Z", null)));
private final BasicFormatter formatter = new BasicFormatter(firstResponse.columns(), firstResponse.rows(), format); private final BasicFormatter formatter = new BasicFormatter(firstResponse.columns(), firstResponse.rows(), format);
/** /**
@ -40,14 +42,14 @@ public class BasicFormatterTests extends ESTestCase {
public void testFormatWithHeader() { public void testFormatWithHeader() {
String[] result = formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).split("\n"); String[] result = formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).split("\n");
assertThat(result, arrayWithSize(4)); assertThat(result, arrayWithSize(4));
assertEquals(" foo | bar |15charwidename!|superduperwidename!!!| baz |" assertEquals(" foo | bar |15charwidename!| null_field1 |superduperwidename!!!| baz |"
+ " date ", result[0]); + " date | null_field2 ", result[0]);
assertEquals("---------------+----------------------+---------------+---------------------+---------------+" assertEquals("---------------+----------------------+---------------+---------------+---------------------+---------------+"
+ "------------------------", result[1]); + "------------------------+---------------", result[1]);
assertEquals("15charwidedata!|1 |6.888 |12 |rabbit |" assertEquals("15charwidedata!|1 |6.888 |null |12 |rabbit |"
+ "1953-09-02T00:00:00.000Z", result[2]); + "1953-09-02T00:00:00.000Z|null ", result[2]);
assertEquals("dog |1.7976931348623157E308|123124.888 |9912 |goat |" assertEquals("dog |1.7976931348623157E308|123124.888 |null |9912 |goat |"
+ "2000-03-15T21:34:37.443Z", result[3]); + "2000-03-15T21:34:37.443Z|null ", result[3]);
} }
/** /**
@ -57,13 +59,13 @@ public class BasicFormatterTests extends ESTestCase {
public void testFormatWithoutHeader() { public void testFormatWithoutHeader() {
String[] result = formatter.formatWithoutHeader( String[] result = formatter.formatWithoutHeader(
Arrays.asList( Arrays.asList(
Arrays.asList("ohnotruncateddata", 4, 1, 77, "wombat", "1955-01-21T01:02:03.342Z"), Arrays.asList("ohnotruncateddata", 4, 1, null, 77, "wombat", "1955-01-21T01:02:03.342Z", null),
Arrays.asList("dog", 2, 123124.888, 9912, "goat", "2231-12-31T23:59:59.999Z"))).split("\n"); Arrays.asList("dog", 2, 123124.888, null, 9912, "goat", "2231-12-31T23:59:59.999Z", null))).split("\n");
assertThat(result, arrayWithSize(2)); assertThat(result, arrayWithSize(2));
assertEquals("ohnotruncatedd~|4 |1 |77 |wombat |" assertEquals("ohnotruncatedd~|4 |1 |null |77 |wombat |"
+ "1955-01-21T01:02:03.342Z", result[0]); + "1955-01-21T01:02:03.342Z|null ", result[0]);
assertEquals("dog |2 |123124.888 |9912 |goat |" assertEquals("dog |2 |123124.888 |null |9912 |goat |"
+ "2231-12-31T23:59:59.999Z", result[1]); + "2231-12-31T23:59:59.999Z|null ", result[1]);
} }
/** /**