Skip to content

Commit

Permalink
[Spotless] Applying Google Code Format for legacy directory (pt 3/4) #21
Browse files Browse the repository at this point in the history
 (opensearch-project#1991)

* Spotless apply for legacy pt 3

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

* spotless apply

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

---------

Signed-off-by: Mitchell Gale <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>
  • Loading branch information
MitchellGale authored Aug 21, 2023
1 parent 7e3a718 commit 445c9e6
Show file tree
Hide file tree
Showing 113 changed files with 9,183 additions and 10,019 deletions.
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.legacy.antlr;

import java.util.Optional;
Expand All @@ -25,84 +24,77 @@
import org.opensearch.sql.legacy.antlr.visitor.EarlyExitAnalysisException;
import org.opensearch.sql.legacy.esdomain.LocalClusterState;

/**
* Entry point for ANTLR generated parser to perform strict syntax and semantic analysis.
*/
/** Entry point for ANTLR generated parser to perform strict syntax and semantic analysis. */
public class OpenSearchLegacySqlAnalyzer {

private static final Logger LOG = LogManager.getLogger();

/** Original sql query */
private final SqlAnalysisConfig config;

public OpenSearchLegacySqlAnalyzer(SqlAnalysisConfig config) {
this.config = config;
}

public Optional<Type> analyze(String sql, LocalClusterState clusterState) {
// Perform analysis for SELECT only for now because of extra code changes required for SHOW/DESCRIBE.
if (!isSelectStatement(sql) || !config.isAnalyzerEnabled()) {
return Optional.empty();
}
private static final Logger LOG = LogManager.getLogger();

try {
return Optional.of(analyzeSemantic(
analyzeSyntax(sql),
clusterState
));
} catch (EarlyExitAnalysisException e) {
// Expected if configured so log on debug level to avoid always logging stack trace
LOG.debug("Analysis exits early and will skip remaining process", e);
return Optional.empty();
}
}
/** Original sql query */
private final SqlAnalysisConfig config;

/**
* Build lexer and parser to perform syntax analysis only.
* Runtime exception with clear message is thrown for any verification error.
*
* @return parse tree
*/
public ParseTree analyzeSyntax(String sql) {
OpenSearchLegacySqlParser parser = createParser(createLexer(sql));
parser.addErrorListener(new SyntaxAnalysisErrorListener());
return parser.root();
}
public OpenSearchLegacySqlAnalyzer(SqlAnalysisConfig config) {
this.config = config;
}

/**
* Perform semantic analysis based on syntax analysis output - parse tree.
*
* @param tree parse tree
* @param clusterState cluster state required for index mapping query
*/
public Type analyzeSemantic(ParseTree tree, LocalClusterState clusterState) {
return tree.accept(new AntlrSqlParseTreeVisitor<>(createAnalyzer(clusterState)));
public Optional<Type> analyze(String sql, LocalClusterState clusterState) {
// Perform analysis for SELECT only for now because of extra code changes required for
// SHOW/DESCRIBE.
if (!isSelectStatement(sql) || !config.isAnalyzerEnabled()) {
return Optional.empty();
}

/** Factory method for semantic analyzer to help assemble all required components together */
private SemanticAnalyzer createAnalyzer(LocalClusterState clusterState) {
SemanticContext context = new SemanticContext();
OpenSearchMappingLoader
mappingLoader = new OpenSearchMappingLoader(context, clusterState, config.getAnalysisThreshold());
TypeChecker typeChecker = new TypeChecker(context, config.isFieldSuggestionEnabled());
return new SemanticAnalyzer(mappingLoader, typeChecker);
try {
return Optional.of(analyzeSemantic(analyzeSyntax(sql), clusterState));
} catch (EarlyExitAnalysisException e) {
// Expected if configured so log on debug level to avoid always logging stack trace
LOG.debug("Analysis exits early and will skip remaining process", e);
return Optional.empty();
}

private OpenSearchLegacySqlParser createParser(Lexer lexer) {
return new OpenSearchLegacySqlParser(
new CommonTokenStream(lexer));
}

private OpenSearchLegacySqlLexer createLexer(String sql) {
return new OpenSearchLegacySqlLexer(
new CaseInsensitiveCharStream(sql));
}

private boolean isSelectStatement(String sql) {
sql = sql.replaceAll("\\R", " ").trim();
int endOfFirstWord = sql.indexOf(' ');
String firstWord = sql.substring(0, endOfFirstWord > 0 ? endOfFirstWord : sql.length());
return "SELECT".equalsIgnoreCase(firstWord);
}

}

/**
* Build lexer and parser to perform syntax analysis only. Runtime exception with clear message is
* thrown for any verification error.
*
* @return parse tree
*/
public ParseTree analyzeSyntax(String sql) {
OpenSearchLegacySqlParser parser = createParser(createLexer(sql));
parser.addErrorListener(new SyntaxAnalysisErrorListener());
return parser.root();
}

/**
* Perform semantic analysis based on syntax analysis output - parse tree.
*
* @param tree parse tree
* @param clusterState cluster state required for index mapping query
*/
public Type analyzeSemantic(ParseTree tree, LocalClusterState clusterState) {
return tree.accept(new AntlrSqlParseTreeVisitor<>(createAnalyzer(clusterState)));
}

/** Factory method for semantic analyzer to help assemble all required components together */
private SemanticAnalyzer createAnalyzer(LocalClusterState clusterState) {
SemanticContext context = new SemanticContext();
OpenSearchMappingLoader mappingLoader =
new OpenSearchMappingLoader(context, clusterState, config.getAnalysisThreshold());
TypeChecker typeChecker = new TypeChecker(context, config.isFieldSuggestionEnabled());
return new SemanticAnalyzer(mappingLoader, typeChecker);
}

private OpenSearchLegacySqlParser createParser(Lexer lexer) {
return new OpenSearchLegacySqlParser(new CommonTokenStream(lexer));
}

private OpenSearchLegacySqlLexer createLexer(String sql) {
return new OpenSearchLegacySqlLexer(new CaseInsensitiveCharStream(sql));
}

private boolean isSelectStatement(String sql) {
sql = sql.replaceAll("\\R", " ").trim();
int endOfFirstWord = sql.indexOf(' ');
String firstWord = sql.substring(0, endOfFirstWord > 0 ? endOfFirstWord : sql.length());
return "SELECT".equalsIgnoreCase(firstWord);
}
}
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.legacy.antlr.semantic.types.base;

import static org.opensearch.sql.legacy.antlr.semantic.types.base.OpenSearchIndex.IndexType.NESTED_FIELD;
Expand All @@ -13,105 +12,102 @@
import java.util.Map;
import org.opensearch.sql.legacy.antlr.semantic.types.Type;

/**
* Base type hierarchy based on OpenSearch data type
*/
/** Base type hierarchy based on OpenSearch data type */
public enum OpenSearchDataType implements BaseType {

TYPE_ERROR,
UNKNOWN,

SHORT, LONG,
INTEGER(SHORT, LONG),
FLOAT(INTEGER),
DOUBLE(FLOAT),
NUMBER(DOUBLE),

KEYWORD,
TEXT(KEYWORD),
STRING(TEXT),

DATE_NANOS,
DATE(DATE_NANOS, STRING),

BOOLEAN,

OBJECT, NESTED,
COMPLEX(OBJECT, NESTED),

GEO_POINT,

OPENSEARCH_TYPE(
NUMBER,
//STRING, move to under DATE because DATE is compatible
DATE,
BOOLEAN,
COMPLEX,
GEO_POINT
);


/**
* Java Enum's valueOf() may thrown "enum constant not found" exception.
* And Java doesn't provide a contains method.
* So this static map is necessary for check and efficiency.
*/
private static final Map<String, OpenSearchDataType> ALL_BASE_TYPES;
static {
ImmutableMap.Builder<String, OpenSearchDataType> builder = new ImmutableMap.Builder<>();
for (OpenSearchDataType type : OpenSearchDataType.values()) {
builder.put(type.name(), type);
}
ALL_BASE_TYPES = builder.build();
TYPE_ERROR,
UNKNOWN,

SHORT,
LONG,
INTEGER(SHORT, LONG),
FLOAT(INTEGER),
DOUBLE(FLOAT),
NUMBER(DOUBLE),

KEYWORD,
TEXT(KEYWORD),
STRING(TEXT),

DATE_NANOS,
DATE(DATE_NANOS, STRING),

BOOLEAN,

OBJECT,
NESTED,
COMPLEX(OBJECT, NESTED),

GEO_POINT,

OPENSEARCH_TYPE(
NUMBER,
// STRING, move to under DATE because DATE is compatible
DATE,
BOOLEAN,
COMPLEX,
GEO_POINT);

/**
* Java Enum's valueOf() may thrown "enum constant not found" exception. And Java doesn't provide
* a contains method. So this static map is necessary for check and efficiency.
*/
private static final Map<String, OpenSearchDataType> ALL_BASE_TYPES;

static {
ImmutableMap.Builder<String, OpenSearchDataType> builder = new ImmutableMap.Builder<>();
for (OpenSearchDataType type : OpenSearchDataType.values()) {
builder.put(type.name(), type);
}
ALL_BASE_TYPES = builder.build();
}

public static OpenSearchDataType typeOf(String str) {
return ALL_BASE_TYPES.getOrDefault(toUpper(str), UNKNOWN);
}
public static OpenSearchDataType typeOf(String str) {
return ALL_BASE_TYPES.getOrDefault(toUpper(str), UNKNOWN);
}

/** Parent of current base type */
private OpenSearchDataType parent;
/** Parent of current base type */
private OpenSearchDataType parent;

OpenSearchDataType(OpenSearchDataType... compatibleTypes) {
for (OpenSearchDataType subType : compatibleTypes) {
subType.parent = this;
}
OpenSearchDataType(OpenSearchDataType... compatibleTypes) {
for (OpenSearchDataType subType : compatibleTypes) {
subType.parent = this;
}

@Override
public String getName() {
return name();
}

@Override
public String getName() {
return name();
}

/**
* For base type, compatibility means this (current type) is ancestor of other in the base type
* hierarchy.
*/
@Override
public boolean isCompatible(Type other) {
// Skip compatibility check if type is unknown
if (this == UNKNOWN || other == UNKNOWN) {
return true;
}

/**
* For base type, compatibility means this (current type) is ancestor of other
* in the base type hierarchy.
*/
@Override
public boolean isCompatible(Type other) {
// Skip compatibility check if type is unknown
if (this == UNKNOWN || other == UNKNOWN) {
return true;
}

if (!(other instanceof OpenSearchDataType)) {
// Nested data type is compatible with nested index type for type expression use
if (other instanceof OpenSearchIndex && ((OpenSearchIndex) other).type() == NESTED_FIELD) {
return isCompatible(NESTED);
}
return false;
}

// One way compatibility: parent base type is compatible with children
OpenSearchDataType cur = (OpenSearchDataType) other;
while (cur != null && cur != this) {
cur = cur.parent;
}
return cur != null;
if (!(other instanceof OpenSearchDataType)) {
// Nested data type is compatible with nested index type for type expression use
if (other instanceof OpenSearchIndex && ((OpenSearchIndex) other).type() == NESTED_FIELD) {
return isCompatible(NESTED);
}
return false;
}

@Override
public String toString() {
return "OpenSearch Data Type [" + getName() + "]";
// One way compatibility: parent base type is compatible with children
OpenSearchDataType cur = (OpenSearchDataType) other;
while (cur != null && cur != this) {
cur = cur.parent;
}
return cur != null;
}

@Override
public String toString() {
return "OpenSearch Data Type [" + getName() + "]";
}
}
Loading

0 comments on commit 445c9e6

Please sign in to comment.