Skip to content

Commit

Permalink
Added new exception for nested fields to force it to fallback to legacy
Browse files Browse the repository at this point in the history
Signed-off-by: Guian Gumpac <[email protected]>
  • Loading branch information
GumpacG committed Jan 14, 2023
1 parent 9d4a841 commit dc17e9f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.opensearch.sql.analysis.symbol.Namespace;
import org.opensearch.sql.analysis.symbol.Symbol;
import org.opensearch.sql.analysis.symbol.SymbolTable;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.exception.UnsupportedV2NestedException;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.ReferenceExpression;
import org.opensearch.sql.expression.env.Environment;
Expand Down Expand Up @@ -48,6 +50,12 @@ public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) {
@Override
public ExprType resolve(Symbol symbol) {
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) {
if (!cur.symbolTable.lookup(symbol).isEmpty()
&& cur.symbolTable.lookup(symbol).get().equals(ExprCoreType.ARRAY)) {
throw new UnsupportedV2NestedException(
String.format("can't resolve %s type in the new engine", symbol));
}

Optional<ExprType> typeOptional = cur.symbolTable.lookup(symbol);
if (typeOptional.isPresent()) {
return typeOptional.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.exception;

/**
* Exception thrown to fall back to legacy when nested fields are queried.
*/
public class UnsupportedV2NestedException extends QueryEngineException {
public UnsupportedV2NestedException(String message) {
super(message);
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.sql;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Test;
import org.opensearch.sql.legacy.SQLIntegTestCase;
import java.io.IOException;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_NESTED_TYPE;
import static org.opensearch.sql.util.MatcherUtils.schema;
import static org.opensearch.sql.util.MatcherUtils.verifyColumn;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
import static org.opensearch.sql.util.MatcherUtils.rows;

public class NestedFallbackIT extends SQLIntegTestCase{
@Override
public void init() throws IOException {
loadIndex(Index.NESTED);
}

@Test
public void nested_function_and_without_nested_function_test() {
String query = "SELECT nested(message.info), message.info FROM " + TEST_INDEX_NESTED_TYPE;
JSONObject result = executeJdbcRequest(query);

verifyColumn(result, schema("message.info", "keyword"),
schema("message.info", "keyword"));
verifyDataRows(result, rows("a", "a"),
rows("b", "b"),
rows("c", "c"),
rows("c", "c"),
rows("a", "a"),
rows("zz", "zz"));
}

@Ignore("Legacy has a bug that returns nulls for array values. " +
"This will be fixed in the new engine. https://github.com/opensearch-project/sql/issues/1218")
@Test
public void without_nested_function_test() {
String query = "SELECT message.info FROM " + TEST_INDEX_NESTED_TYPE;
JSONObject result = executeJdbcRequest(query);

verifyColumn(result, schema("message.info", "keyword"));
verifyDataRows(result, rows("a"),
rows("b"),
rows("c"),
rows("c"),
rows("a"),
rows("zz"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.sql.common.antlr.SyntaxCheckException;
import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.common.utils.QueryContext;
import org.opensearch.sql.exception.UnsupportedV2NestedException;
import org.opensearch.sql.executor.ExecutionEngine.ExplainResponse;
import org.opensearch.sql.legacy.metrics.MetricName;
import org.opensearch.sql.legacy.metrics.Metrics;
Expand Down Expand Up @@ -126,7 +127,7 @@ public void onResponse(T response) {

@Override
public void onFailure(Exception e) {
if (e instanceof SyntaxCheckException) {
if (e instanceof SyntaxCheckException || e instanceof UnsupportedV2NestedException) {
fallBackHandler.accept(channel, e);
} else {
next.onFailure(e);
Expand Down

0 comments on commit dc17e9f

Please sign in to comment.