Skip to content

Commit

Permalink
Fix wildcard tests
Browse files Browse the repository at this point in the history
Signed-off-by: Simeon Widdis <[email protected]>
  • Loading branch information
Swiddis committed Oct 18, 2024
1 parent 0c4e03b commit 5ed850a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testJoinWithInvalidCondition() throws IOException, ParseException {
Assert.assertEquals(400, errMsg.getInt("status"));
}

public void testWildcardInSubquery() throws IOException, ParseException {
public void testWrappedWildcardInSubquery() throws IOException, ParseException {
ResponseException result =
assertThrows(
"Expected wildcard subquery to raise error, but didn't",
Expand All @@ -44,13 +44,34 @@ public void testWildcardInSubquery() throws IOException, ParseException {
executeQuery(
String.format(
Locale.ROOT,
"SELECT a.first_name FROM %s AS a WHERE a.age IN (SELECT age FROM * WHERE age > 30)",
TestsConstants.TEST_INDEX_BANK)));
"SELECT a.first_name FROM %s AS a WHERE a.age IN (SELECT age FROM"
+ " `opensearch-sql_test_index_*` WHERE age > 30)",
TestsConstants.TEST_INDEX_BANK,
TestsConstants.TEST_INDEX_BANK_TWO)));
var errMsg = new JSONObject(EntityUtils.toString(result.getResponse().getEntity()));
System.err.println("Full response: " + errMsg);

Assert.assertEquals(
"SqlParseException", errMsg.getJSONObject("error").getString("type"));
Assert.assertEquals(400, errMsg.getInt("status"));
Assert.assertEquals("IndexNotFoundException", errMsg.getJSONObject("error").getString("type"));
Assert.assertEquals(404, errMsg.getInt("status"));
}

public void testUnwrappedWildcardInSubquery() throws IOException, ParseException {
ResponseException result =
assertThrows(
"Expected wildcard subquery to raise error, but didn't",
ResponseException.class,
() ->
executeQuery(
String.format(
Locale.ROOT,
"SELECT a.first_name FROM %s AS a WHERE a.age IN (SELECT age FROM * WHERE"
+ " age > 30)",
TestsConstants.TEST_INDEX_BANK,
TestsConstants.TEST_INDEX_BANK_TWO)));
var errMsg = new JSONObject(EntityUtils.toString(result.getResponse().getEntity()));
System.err.println("Full response: " + errMsg);

Assert.assertEquals("IndexNotFoundException", errMsg.getJSONObject("error").getString("type"));
Assert.assertEquals(404, errMsg.getInt("status"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.google.common.annotations.VisibleForTesting;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.List;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.sql.legacy.esdomain.LocalClusterState;
import org.opensearch.sql.legacy.esdomain.mapping.FieldMappings;
import org.opensearch.sql.legacy.esdomain.mapping.IndexMappings;
Expand Down Expand Up @@ -122,7 +123,23 @@ public boolean visit(SQLIdentifierExpr expr) {
String fullFieldName = arr[1];

String index = curScope().getAliases().get(alias);
if (index == null) {
throw new IndexNotFoundException(
String.format(
"The requested table '%s' does not correspond to any known index. Only indices or"
+ " table aliases are allowed.",
alias.replaceFirst("_\\d+$", "")));
}

FieldMappings fieldMappings = curScope().getMapper().mapping(index);
if (fieldMappings == null) {
throw new IndexNotFoundException(
String.format(
"The index '%s' could not be found. Note that wildcard indices are not permitted"
+ " in SQL.",
index));
}

if (fieldMappings.has(fullFieldName)) {
source = fieldMappings.mapping(fullFieldName);
} else {
Expand Down

0 comments on commit 5ed850a

Please sign in to comment.