Skip to content

Commit

Permalink
[Spotless] Applying Google Code Format for sql files #11 (#1968)
Browse files Browse the repository at this point in the history
* [Spotless] Applying Google Code Format for sql files #11 (#330)

* Spotless apply, ignore checkstyle, manual java doc fixes.

Signed-off-by: Mitchell Gale <[email protected]>

* Spotless apply

Signed-off-by: Mitchell Gale <[email protected]>

---------

Signed-off-by: Mitchell Gale <[email protected]>

* Apply suggestions from code review

Co-authored-by: Guian Gumpac <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

---------

Signed-off-by: Mitchell Gale <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>
Co-authored-by: Guian Gumpac <[email protected]>
  • Loading branch information
MitchellGale and GumpacG authored Aug 16, 2023
1 parent 491a73b commit b035b13
Show file tree
Hide file tree
Showing 32 changed files with 1,221 additions and 1,638 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ repositories {
spotless {
java {
target fileTree('.') {
include 'common/**/*.java',
'datasources/**/*.java',
include 'datasources/**/*.java',
'core/**/*.java',
'sql/**/*.java',
'common/**/*.java',
'ppl/**/*.java'
exclude '**/build/**', '**/build-*/**'
}
Expand Down
5 changes: 5 additions & 0 deletions sql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ dependencies {
testImplementation(testFixtures(project(":core")))
}

// Being ignored as a temporary measure before being removed in favour of
// spotless https://github.com/opensearch-project/sql/issues/1101
checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

test {
useJUnitPlatform()
testLogging {
Expand Down
24 changes: 12 additions & 12 deletions sql/src/main/java/org/opensearch/sql/sql/SQLService.java
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.sql;

import java.util.Optional;
Expand All @@ -21,9 +20,7 @@
import org.opensearch.sql.sql.parser.AstBuilder;
import org.opensearch.sql.sql.parser.AstStatementBuilder;

/**
* SQL service.
*/
/** SQL service. */
@RequiredArgsConstructor
public class SQLService {

Expand Down Expand Up @@ -69,15 +66,19 @@ private AbstractPlan plan(
if (request.getCursor().isPresent()) {
// Handle v2 cursor here -- legacy cursor was handled earlier.
if (isExplainRequest) {
throw new UnsupportedOperationException("Explain of a paged query continuation "
+ "is not supported. Use `explain` for the initial query request.");
throw new UnsupportedOperationException(
"Explain of a paged query continuation "
+ "is not supported. Use `explain` for the initial query request.");
}
if (request.isCursorCloseRequest()) {
return queryExecutionFactory.createCloseCursor(request.getCursor().get(),
queryListener.orElse(null));
return queryExecutionFactory.createCloseCursor(
request.getCursor().get(), queryListener.orElse(null));
}
return queryExecutionFactory.create(request.getCursor().get(),
isExplainRequest, queryListener.orElse(null), explainListener.orElse(null));
return queryExecutionFactory.create(
request.getCursor().get(),
isExplainRequest,
queryListener.orElse(null),
explainListener.orElse(null));
} else {
// 1.Parse query and convert parse tree (CST) to abstract syntax tree (AST)
ParseTree cst = parser.parse(request.getQuery());
Expand All @@ -90,8 +91,7 @@ private AbstractPlan plan(
.fetchSize(request.getFetchSize())
.build()));

return queryExecutionFactory.create(
statement, queryListener, explainListener);
return queryExecutionFactory.create(statement, queryListener, explainListener);
}
}
}
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.sql.antlr;

import static org.opensearch.sql.sql.antlr.parser.OpenSearchSQLLexer.BACKTICK_QUOTE_ID;
Expand Down Expand Up @@ -31,21 +30,17 @@
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;

/**
* Parse tree listener for anonymizing SQL requests.
*/
/** Parse tree listener for anonymizing SQL requests. */
public class AnonymizerListener implements ParseTreeListener {
private String anonymizedQueryString = "";
private static final int NO_TYPE = -1;
private int previousType = NO_TYPE;

@Override
public void enterEveryRule(ParserRuleContext ctx) {
}
public void enterEveryRule(ParserRuleContext ctx) {}

@Override
public void exitEveryRule(ParserRuleContext ctx) {
}
public void exitEveryRule(ParserRuleContext ctx) {}

@Override
public void visitTerminal(TerminalNode node) {
Expand All @@ -57,10 +52,11 @@ public void visitTerminal(TerminalNode node) {
int token = node.getSymbol().getType();
boolean isDotIdentifiers = token == DOT || previousType == DOT;
boolean isComma = token == COMMA;
boolean isEqualComparison = ((token == EQUAL_SYMBOL)
boolean isEqualComparison =
((token == EQUAL_SYMBOL)
&& (previousType == LESS_SYMBOL
|| previousType == GREATER_SYMBOL
|| previousType == EXCLAMATION_SYMBOL));
|| previousType == GREATER_SYMBOL
|| previousType == EXCLAMATION_SYMBOL));
boolean isNotEqualComparisonAlternative =
previousType == LESS_SYMBOL && token == GREATER_SYMBOL;
if (!isDotIdentifiers && !isComma && !isEqualComparison && !isNotEqualComparisonAlternative) {
Expand Down Expand Up @@ -103,9 +99,7 @@ public void visitTerminal(TerminalNode node) {
}

@Override
public void visitErrorNode(ErrorNode node) {

}
public void visitErrorNode(ErrorNode node) {}

public String getAnonymizedQueryString() {
return "(" + anonymizedQueryString + ")";
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.sql.antlr;

import org.antlr.v4.runtime.CommonTokenStream;
Expand All @@ -16,16 +15,15 @@
import org.opensearch.sql.sql.antlr.parser.OpenSearchSQLLexer;
import org.opensearch.sql.sql.antlr.parser.OpenSearchSQLParser;

/**
* SQL syntax parser which encapsulates an ANTLR parser.
*/
/** SQL syntax parser which encapsulates an ANTLR parser. */
public class SQLSyntaxParser implements Parser {
private static final Logger LOG = LogManager.getLogger(SQLSyntaxParser.class);

/**
* Parse a SQL query by ANTLR parser.
* @param query a SQL query
* @return parse tree root
*
* @param query a SQL query
* @return parse tree root
*/
@Override
public ParseTree parse(String query) {
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.sql.domain;

import java.util.Collections;
Expand All @@ -20,43 +19,30 @@
import org.json.JSONObject;
import org.opensearch.sql.protocol.response.format.Format;

/**
* SQL query request.
*/
/** SQL query request. */
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class SQLQueryRequest {
private static final String QUERY_FIELD_CURSOR = "cursor";
private static final Set<String> SUPPORTED_FIELDS = Set.of(
"query", "fetch_size", "parameters", QUERY_FIELD_CURSOR);
private static final Set<String> SUPPORTED_FIELDS =
Set.of("query", "fetch_size", "parameters", QUERY_FIELD_CURSOR);
private static final String QUERY_PARAMS_FORMAT = "format";
private static final String QUERY_PARAMS_SANITIZE = "sanitize";

/**
* JSON payload in REST request.
*/
/** JSON payload in REST request. */
private final JSONObject jsonContent;

/**
* SQL query.
*/
@Getter
private final String query;
/** SQL query. */
@Getter private final String query;

/**
* Request path.
*/
/** Request path. */
private final String path;

/**
* Request format.
*/
/** Request format. */
private final String format;

/**
* Request params.
*/
/** Request params. */
private Map<String, String> params = Collections.emptyMap();

@Getter
Expand All @@ -65,11 +51,13 @@ public class SQLQueryRequest {

private String cursor;

/**
* Constructor of SQLQueryRequest that passes request params.
*/
public SQLQueryRequest(JSONObject jsonContent, String query, String path,
Map<String, String> params, String cursor) {
/** Constructor of SQLQueryRequest that passes request params. */
public SQLQueryRequest(
JSONObject jsonContent,
String query,
String path,
Map<String, String> params,
String cursor) {
this.jsonContent = jsonContent;
this.query = query;
this.path = path;
Expand All @@ -80,24 +68,30 @@ public SQLQueryRequest(JSONObject jsonContent, String query, String path,
}

/**
*
*
* <pre>
* Pre-check if the request can be supported by meeting ALL the following criteria:
* 1.Only supported fields present in request body, ex. "filter" and "cursor" are not supported
* 2.Response format is default or can be supported.
* </pre>
*
* @return true if supported.
*/
public boolean isSupported() {
var noCursor = !isCursor();
var noQuery = query == null;
var noUnsupportedParams = params.isEmpty()
|| (params.size() == 1 && params.containsKey(QUERY_PARAMS_FORMAT));
var noUnsupportedParams =
params.isEmpty() || (params.size() == 1 && params.containsKey(QUERY_PARAMS_FORMAT));
var noContent = jsonContent == null || jsonContent.isEmpty();

return ((!noCursor && noQuery
&& noUnsupportedParams && noContent) // if cursor is given, but other things
|| (noCursor && !noQuery)) // or if cursor is not given, but query
&& isOnlySupportedFieldInPayload() // and request has supported fields only
&& isSupportedFormat(); // and request is in supported format
return ((!noCursor
&& noQuery
&& noUnsupportedParams
&& noContent) // if cursor is given, but other things
|| (noCursor && !noQuery)) // or if cursor is not given, but query
&& isOnlySupportedFieldInPayload() // and request has supported fields only
&& isSupportedFormat(); // and request is in supported format
}

private boolean isCursor() {
Expand All @@ -106,6 +100,7 @@ private boolean isCursor() {

/**
* Check if request is to explain rather than execute the query.
*
* @return true if it is an explain request
*/
public boolean isExplainRequest() {
Expand All @@ -116,16 +111,14 @@ public boolean isCursorCloseRequest() {
return path.endsWith("/close");
}

/**
* Decide on the formatter by the requested format.
*/
/** Decide on the formatter by the requested format. */
public Format format() {
Optional<Format> optionalFormat = Format.of(format);
if (optionalFormat.isPresent()) {
return optionalFormat.get();
} else {
throw new IllegalArgumentException(
String.format(Locale.ROOT,"response in %s format is not supported.", format));
String.format(Locale.ROOT, "response in %s format is not supported.", format));
}
}

Expand Down Expand Up @@ -155,5 +148,4 @@ private boolean shouldSanitize(Map<String, String> params) {
}
return true;
}

}
Loading

0 comments on commit b035b13

Please sign in to comment.