-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Well format the raw response when query parameter "pretty" enabled (#…
- Loading branch information
Showing
14 changed files
with
404 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.protocol.response.format; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.Getter; | ||
import org.opensearch.sql.protocol.response.QueryResult; | ||
|
||
@Getter | ||
public class FlatResponseBase { | ||
protected static final String INTERLINE_SEPARATOR = System.lineSeparator(); | ||
|
||
private final QueryResult response; | ||
protected final String separator; | ||
|
||
private final List<String> headers; | ||
private final List<List<String>> data; | ||
|
||
FlatResponseBase(QueryResult response, String separator) { | ||
this.response = response; | ||
this.separator = separator; | ||
this.headers = getOriginalHeaders(response); | ||
this.data = getOriginalData(response); | ||
} | ||
|
||
public String format() { | ||
List<String> headersAndData = new ArrayList<>(); | ||
headersAndData.add(getHeaderLine()); | ||
headersAndData.addAll(getDataLines()); | ||
return String.join(INTERLINE_SEPARATOR, headersAndData); | ||
} | ||
|
||
protected String getHeaderLine() { | ||
return String.join(separator, headers); | ||
} | ||
|
||
private List<String> getOriginalHeaders(QueryResult response) { | ||
ImmutableList.Builder<String> headers = ImmutableList.builder(); | ||
response.columnNameTypes().forEach((column, type) -> headers.add(column)); | ||
List<String> result = headers.build(); | ||
return formatHeaders(result); | ||
} | ||
|
||
protected List<String> getDataLines() { | ||
return data.stream().map(v -> String.join(separator, v)).collect(Collectors.toList()); | ||
} | ||
|
||
private List<List<String>> getOriginalData(QueryResult response) { | ||
ImmutableList.Builder<List<String>> dataLines = new ImmutableList.Builder<>(); | ||
response | ||
.iterator() | ||
.forEachRemaining( | ||
row -> { | ||
ImmutableList.Builder<String> line = new ImmutableList.Builder<>(); | ||
// replace null values with empty string | ||
Arrays.asList(row).forEach(val -> line.add(val == null ? "" : val.toString())); | ||
dataLines.add(line.build()); | ||
}); | ||
List<List<String>> result = dataLines.build(); | ||
return formatData(result); | ||
} | ||
|
||
protected List<String> formatHeaders(List<String> headers) { | ||
return headers.stream() | ||
.map(cell -> quoteIfRequired(separator, cell)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
protected List<List<String>> formatData(List<List<String>> lines) { | ||
List<List<String>> result = new ArrayList<>(); | ||
for (List<String> line : lines) { | ||
result.add( | ||
line.stream().map(cell -> quoteIfRequired(separator, cell)).collect(Collectors.toList())); | ||
} | ||
return result; | ||
} | ||
|
||
protected String quoteIfRequired(String separator, String cell) { | ||
final String quote = "\""; | ||
return cell.contains(separator) ? quote + cell.replaceAll("\"", "\"\"") + quote : cell; | ||
} | ||
} |
149 changes: 0 additions & 149 deletions
149
...ocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.