Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spotless] Applying Google Code Format for protocol files #14 #1971

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ spotless {
target fileTree('.') {
include 'datasources/**/*.java',
'core/**/*.java',
'protocol/**/*.java',
'prometheus/**/*.java',
'sql/**/*.java',
'common/**/*.java',
Expand Down Expand Up @@ -118,9 +119,8 @@ allprojects {
sourceCompatibility = targetCompatibility = "11"
}
configurations.all {
resolutionStrategy.force "com.squareup.okio:okio:3.5.0"
resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib:1.9.0"
resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib:1.6.0"
resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-common:1.6.0"
}
}

Expand Down
3 changes: 3 additions & 0 deletions protocol/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dependencies {
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.12.4'
}

checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

configurations.all {
resolutionStrategy.force "com.fasterxml.jackson.core:jackson-databind:${versions.jackson_databind}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response;

import java.util.Collection;
Expand All @@ -20,29 +19,26 @@
import org.opensearch.sql.executor.pagination.Cursor;

/**
* Query response that encapsulates query results and isolate {@link ExprValue}
* related from formatter implementation.
* Query response that encapsulates query results and isolate {@link ExprValue} related from
* formatter implementation.
*/
@RequiredArgsConstructor
public class QueryResult implements Iterable<Object[]> {

@Getter
private final ExecutionEngine.Schema schema;
@Getter private final ExecutionEngine.Schema schema;

/**
* Results which are collection of expression.
*/
/** Results which are collection of expression. */
private final Collection<ExprValue> exprValues;

@Getter
private final Cursor cursor;
@Getter private final Cursor cursor;

public QueryResult(ExecutionEngine.Schema schema, Collection<ExprValue> exprValues) {
this(schema, exprValues, Cursor.None);
}

/**
* size of results.
*
* @return size of results
*/
public int size() {
Expand All @@ -52,14 +48,18 @@ public int size() {
/**
* Parse column name from results.
*
* @return mapping from column names to its expression type.
* note that column name could be original name or its alias if any.
* @return mapping from column names to its expression type. note that column name could be
* original name or its alias if any.
*/
public Map<String, String> columnNameTypes() {
Map<String, String> colNameTypes = new LinkedHashMap<>();
schema.getColumns().forEach(column -> colNameTypes.put(
getColumnName(column),
column.getExprType().typeName().toLowerCase(Locale.ROOT)));
schema
.getColumns()
.forEach(
column ->
colNameTypes.put(
getColumnName(column),
column.getExprType().typeName().toLowerCase(Locale.ROOT)));
return colNameTypes;
}

Expand All @@ -78,9 +78,6 @@ private String getColumnName(Column column) {
}

private Object[] convertExprValuesToValues(Collection<ExprValue> exprValues) {
return exprValues
.stream()
.map(ExprValue::value)
.toArray(Object[]::new);
return exprValues.stream().map(ExprValue::value).toArray(Object[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.opensearch.sql.protocol.response.QueryResult;

/**
* A simple response formatter which contains no data.
* Supposed to use with {@link CommandPlan} only.
* A simple response formatter which contains no data. Supposed to use with {@link CommandPlan}
* only.
*/
public class CommandResponseFormatter extends JsonResponseFormatter<QueryResult> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response.format;

public class CsvResponseFormatter extends FlatResponseFormatter {
Expand All @@ -14,5 +13,4 @@ public CsvResponseFormatter() {
public CsvResponseFormatter(boolean sanitize) {
super(",", sanitize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response.format;

import com.google.gson.Gson;
Expand All @@ -17,35 +16,28 @@
@UtilityClass
public class ErrorFormatter {

private static final Gson PRETTY_PRINT_GSON = AccessController.doPrivileged(
(PrivilegedAction<Gson>) () -> new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.create());
private static final Gson GSON = AccessController.doPrivileged(
(PrivilegedAction<Gson>) () -> new GsonBuilder().disableHtmlEscaping().create());

/**
* Util method to format {@link Throwable} response to JSON string in compact printing.
*/
private static final Gson PRETTY_PRINT_GSON =
AccessController.doPrivileged(
(PrivilegedAction<Gson>)
() -> new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create());
private static final Gson GSON =
AccessController.doPrivileged(
(PrivilegedAction<Gson>) () -> new GsonBuilder().disableHtmlEscaping().create());

/** Util method to format {@link Throwable} response to JSON string in compact printing. */
public static String compactFormat(Throwable t) {
JsonError error = new ErrorFormatter.JsonError(t.getClass().getSimpleName(),
t.getMessage());
JsonError error = new ErrorFormatter.JsonError(t.getClass().getSimpleName(), t.getMessage());
return compactJsonify(error);
}

/**
* Util method to format {@link Throwable} response to JSON string in pretty printing.
*/
public static String prettyFormat(Throwable t) {
JsonError error = new ErrorFormatter.JsonError(t.getClass().getSimpleName(),
t.getMessage());
/** Util method to format {@link Throwable} response to JSON string in pretty printing. */
public static String prettyFormat(Throwable t) {
JsonError error = new ErrorFormatter.JsonError(t.getClass().getSimpleName(), t.getMessage());
return prettyJsonify(error);
}

public static String compactJsonify(Object jsonObject) {
return AccessController.doPrivileged(
(PrivilegedAction<String>) () -> GSON.toJson(jsonObject));
return AccessController.doPrivileged((PrivilegedAction<String>) () -> GSON.toJson(jsonObject));
}

public static String prettyJsonify(Object jsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response.format;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -48,9 +47,8 @@ public String format(Throwable t) {
}

/**
* Sanitize methods are migrated from legacy CSV result.
* Sanitize both headers and data lines by:
* 1) Second double quote entire cell if any comma is found.
* Sanitize methods are migrated from legacy CSV result. Sanitize both headers and data lines by:
* 1) Second double quote entire cell if any comma is found.
*/
@Getter
@RequiredArgsConstructor
Expand Down Expand Up @@ -84,44 +82,47 @@ private List<String> getHeaders(QueryResult response, boolean sanitize) {

private List<List<String>> getData(QueryResult response, boolean sanitize) {
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());
});
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 sanitizeData(result);
}

/**
* Sanitize headers because OpenSearch allows special character present in field names.
*/
/** Sanitize headers because OpenSearch allows special character present in field names. */
private List<String> sanitizeHeaders(List<String> headers) {
if (sanitize) {
return headers.stream()
.map(this::sanitizeCell)
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList());
.map(this::sanitizeCell)
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList());
} else {
return headers.stream()
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList());
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList());
}
}

private List<List<String>> sanitizeData(List<List<String>> lines) {
List<List<String>> result = new ArrayList<>();
if (sanitize) {
for (List<String> line : lines) {
result.add(line.stream()
result.add(
line.stream()
.map(this::sanitizeCell)
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList()));
}
} else {
for (List<String> line : lines) {
result.add(line.stream()
result.add(
line.stream()
.map(cell -> quoteIfRequired(INLINE_SEPARATOR, cell))
.collect(Collectors.toList()));
}
Expand All @@ -138,13 +139,11 @@ private String sanitizeCell(String cell) {

private String quoteIfRequired(String separator, String cell) {
final String quote = "\"";
return cell.contains(separator)
? quote + cell.replaceAll("\"", "\"\"") + quote : cell;
return cell.contains(separator) ? quote + cell.replaceAll("\"", "\"\"") + quote : cell;
}

private boolean isStartWithSensitiveChar(String cell) {
return SENSITIVE_CHAR.stream().anyMatch(cell::startsWith);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response.format;

import com.google.common.base.Strings;
Expand All @@ -20,8 +19,7 @@ public enum Format {
RAW("raw"),
VIZ("viz");

@Getter
private final String formatName;
@Getter private final String formatName;

private static final Map<String, Format> ALL_FORMATS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.protocol.response.format;

import java.util.List;
Expand Down Expand Up @@ -40,9 +39,7 @@ protected Object buildJsonObject(QueryResult response) {
json.datarows(fetchDataRows(response));

// Populate other fields
json.total(response.size())
.size(response.size())
.status(200);
json.total(response.size()).size(response.size()).status(200);
if (!response.getCursor().equals(Cursor.None)) {
json.cursor(response.getCursor().toString());
}
Expand All @@ -54,10 +51,7 @@ protected Object buildJsonObject(QueryResult response) {
public String format(Throwable t) {
int status = getStatus(t);
ErrorMessage message = ErrorMessageFactory.createErrorMessage(t, status);
Error error = new Error(
message.getType(),
message.getReason(),
message.getDetails());
Error error = new Error(message.getType(), message.getReason(), message.getDetails());
return jsonify(new JdbcErrorResponse(error, status));
}

Expand All @@ -66,8 +60,8 @@ private Column fetchColumn(Schema.Column col) {
}

/**
* Convert type that exists in both legacy and new engine but has different name.
* Return old type name to avoid breaking impact on client-side.
* Convert type that exists in both legacy and new engine but has different name. Return old type
* name to avoid breaking impact on client-side.
*/
private String convertToLegacyType(ExprType type) {
return type.legacyTypeName().toLowerCase();
Expand All @@ -83,18 +77,16 @@ private Object[][] fetchDataRows(QueryResult response) {
}

private int getStatus(Throwable t) {
return (t instanceof SyntaxCheckException
|| t instanceof QueryEngineException) ? 400 : 503;
return (t instanceof SyntaxCheckException || t instanceof QueryEngineException) ? 400 : 503;
}

/**
* org.json requires these inner data classes be public (and static)
*/
/** org.json requires these inner data classes be public (and static) */
@Builder
@Getter
public static class JdbcResponse {
@Singular("column")
private final List<Column> schema;

private final Object[][] datarows;
private final long total;
private final long size;
Expand Down Expand Up @@ -125,5 +117,4 @@ public static class Error {
private final String reason;
private final String details;
}

}
Loading
Loading