Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 4cd81b5

Browse files
committed
Do not change formatting when all rows are sampled.
1 parent c9cb5a8 commit 4cd81b5

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

cypher-shell/src/main/java/org/neo4j/shell/prettyprint/TableOutputFormatter.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private int formatResultAndCountRows(String[] columns,
5454
LinePrinter output) {
5555

5656
List<Record> topRecords = take(records, numSampleRows);
57-
int[] columnSizes = calculateColumnSizes(columns, topRecords);
57+
int[] columnSizes = calculateColumnSizes(columns, topRecords, records.hasNext());
5858

5959
int totalWidth = 1;
6060
for (int columnSize : columnSizes) {
@@ -83,14 +83,21 @@ private int formatResultAndCountRows(String[] columns,
8383
return numberOfRows;
8484
}
8585

86-
private int[] calculateColumnSizes(@Nonnull String[] columns, @Nonnull List<Record> data) {
86+
/**
87+
* Calculate the size of the columns for table formatting
88+
* @param columns the column names
89+
* @param data (sample) data
90+
* @param moreDataAfterSamples if there is more data that should be written into the table after `data`
91+
* @return the column sizes
92+
*/
93+
private int[] calculateColumnSizes(@Nonnull String[] columns, @Nonnull List<Record> data, boolean moreDataAfterSamples) {
8794
int[] columnSizes = new int[columns.length];
8895
for (int i = 0; i < columns.length; i++) {
8996
columnSizes[i] = columns[i].length();
9097
}
9198
for (Record record : data) {
9299
for (int i = 0; i < columns.length; i++) {
93-
int len = columnLengthForValue(record.get(i));
100+
int len = columnLengthForValue(record.get(i), moreDataAfterSamples);
94101
if (columnSizes[i] < len) {
95102
columnSizes[i] = len;
96103
}
@@ -101,9 +108,13 @@ private int[] calculateColumnSizes(@Nonnull String[] columns, @Nonnull List<Reco
101108

102109
/**
103110
* The length of a column, where Numbers are always getting enough space to fit the highest number possible.
111+
*
112+
* @param value the value to calculate the length for
113+
* @param moreDataAfterSamples if there is more data that should be written into the table after `data`
114+
* @return the column size for this value.
104115
*/
105-
private int columnLengthForValue(Value value) {
106-
if (value instanceof NumberValueAdapter ) {
116+
private int columnLengthForValue(Value value, boolean moreDataAfterSamples) {
117+
if (value instanceof NumberValueAdapter && moreDataAfterSamples) {
107118
return 19; // The number of digits of Long.Max
108119
} else {
109120
return formatValue(value).length();

cypher-shell/src/test/java/org/neo4j/shell/prettyprint/TableOutputFormatterTest.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public void prettyPrintPlanInformationWithNewlines() {
105105

106106
// THEN
107107
assertThat(actual, startsWith(String.join(NEWLINE,
108-
"+-----------------------------------------------------------------------------------+",
109-
"| Plan | Statement | Version | Planner | Runtime | Time |",
110-
"+-----------------------------------------------------------------------------------+",
111-
"| \"EXPLAIN\" | \"READ_ONLY\" | \"3.1\" | \"COST\" | \"INTERPRETED\" | 12 |",
112-
"+-----------------------------------------------------------------------------------+",
108+
"+--------------------------------------------------------------------+",
109+
"| Plan | Statement | Version | Planner | Runtime | Time |",
110+
"+--------------------------------------------------------------------+",
111+
"| \"EXPLAIN\" | \"READ_ONLY\" | \"3.1\" | \"COST\" | \"INTERPRETED\" | 12 |",
112+
"+--------------------------------------------------------------------+",
113113
NEWLINE)));
114114
}
115115

@@ -303,19 +303,33 @@ public void basicTable() {
303303
// WHEN
304304
String table = formatResult(result);
305305
// THEN
306-
assertThat(table, containsString("| c1 | c2 |"));
307-
assertThat(table, containsString("| \"a\" | 42 |"));
306+
assertThat(table, containsString("| c1 | c2 |"));
307+
assertThat(table, containsString("| \"a\" | 42 |"));
308308
}
309309

310310
@Test
311-
public void twoRows() {
311+
public void twoRowsWithNumbersAllSampled() {
312312
// GIVEN
313313
StatementResult result = mockResult(asList("c1", "c2"), "a", 42, "b", 43);
314314
// WHEN
315315
String table = formatResult(result);
316316
// THEN
317-
assertThat(table, containsString("| \"a\" | 42 |"));
318-
assertThat(table, containsString("| \"b\" | 43 |"));
317+
assertThat(table, containsString("| \"a\" | 42 |"));
318+
assertThat(table, containsString("| \"b\" | 43 |"));
319+
}
320+
321+
@Test
322+
public void fiveRowsWithNumbersNotAllSampled() {
323+
// GIVEN
324+
StatementResult result = mockResult(asList("c1", "c2"), "a", 42, "b", 43, "c", 44, "d", 45, "e", 46);
325+
// WHEN
326+
String table = formatResult(result);
327+
// THEN
328+
assertThat(table, containsString("| \"a\" | 42 |"));
329+
assertThat(table, containsString("| \"b\" | 43 |"));
330+
assertThat(table, containsString("| \"c\" | 44 |"));
331+
assertThat(table, containsString("| \"d\" | 45 |"));
332+
assertThat(table, containsString("| \"e\" | 46 |"));
319333
}
320334

321335
@Test

0 commit comments

Comments
 (0)