Skip to content

Commit a4677af

Browse files
author
Lukas Wegmann
authored
SQL: Fix txt format for empty result sets (#83376) (#83400)
Addresses #83371 The issue is caused by `org/elasticsearch/xpack/sql/plugin/TextFormatterCursor.java:57` which unwraps empty cursors in `TextFormatterCursor` before being processed by `TextFormat.PLAIN_TEXT.format()`.
1 parent 339227c commit a4677af

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docs/changelog/83376.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 83376
2+
summary: Fix txt format for empty result sets
3+
area: SQL
4+
type: bug
5+
issues: []

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ String format(RestRequest request, SqlQueryResponse response) {
7979
} else if (response.hasId()) {
8080
// an async request has no results yet
8181
return StringUtils.EMPTY;
82+
} else if (response.rows().isEmpty()) {
83+
// no data and no headers to return
84+
return StringUtils.EMPTY;
8285
}
8386
// if this code is reached, it means it's a next page without cursor wrapping
8487
throw new SqlIllegalArgumentException("Cannot find text formatter - this is likely a bug");

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xpack.sql.action.SqlQueryResponse;
1414
import org.elasticsearch.xpack.sql.proto.ColumnInfo;
1515
import org.elasticsearch.xpack.sql.proto.Mode;
16+
import org.elasticsearch.xpack.sql.proto.StringUtils;
1617

1718
import java.util.ArrayList;
1819
import java.util.Arrays;
@@ -25,6 +26,7 @@
2526
import static java.util.Collections.singletonList;
2627
import static java.util.Collections.singletonMap;
2728
import static org.elasticsearch.xpack.sql.plugin.TextFormat.CSV;
29+
import static org.elasticsearch.xpack.sql.plugin.TextFormat.PLAIN_TEXT;
2830
import static org.elasticsearch.xpack.sql.plugin.TextFormat.TSV;
2931
import static org.elasticsearch.xpack.sql.proto.SqlVersion.DATE_NANOS_SUPPORT_VERSION;
3032

@@ -163,9 +165,26 @@ public void testInvalidCsvDelims() {
163165
}
164166
}
165167

168+
public void testPlainTextEmptyCursorWithColumns() {
169+
assertEquals("""
170+
name \s
171+
---------------
172+
""", PLAIN_TEXT.format(req(), emptyData()));
173+
}
174+
175+
public void testPlainTextEmptyCursorWithoutColumns() {
176+
assertEquals(
177+
StringUtils.EMPTY,
178+
PLAIN_TEXT.format(
179+
req(),
180+
new SqlQueryResponse(StringUtils.EMPTY, Mode.JDBC, DATE_NANOS_SUPPORT_VERSION, false, null, emptyList())
181+
)
182+
);
183+
}
184+
166185
private static SqlQueryResponse emptyData() {
167186
return new SqlQueryResponse(
168-
null,
187+
StringUtils.EMPTY,
169188
Mode.JDBC,
170189
DATE_NANOS_SUPPORT_VERSION,
171190
false,

0 commit comments

Comments
 (0)