Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class PaginationIT extends SQLIntegTestCase {
public void init() throws IOException {
loadIndex(Index.CALCS);
loadIndex(Index.ONLINE);
loadIndex(Index.DOG);
loadIndex(Index.DOGS2);
}

@Test
Expand Down Expand Up @@ -251,6 +253,45 @@ public void testAlias() throws Exception {
assertEquals(aliasFilteredResponse.getInt("size"), 4);
}

@Test
public void testAliasResultConsistency() throws Exception {
String aliasName = "alias_dog_consistency";

// Create an alias that maps to both DOG and DOGS2 indices
String createAliasQuery =
String.format(
"{ \"actions\": [ "
+ "{ \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } }, "
+ "{ \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } } "
+ "] }",
Index.DOG.getName(), aliasName, Index.DOGS2.getName(), aliasName);
Request createAliasRequest = new Request("POST", "/_aliases");
createAliasRequest.setJsonEntity(createAliasQuery);
JSONObject aliasResponse = new JSONObject(executeRequest(createAliasRequest));
assertTrue(aliasResponse.getBoolean("acknowledged"));

// Query both indices directly (same indices the alias points to)
String directQuery =
String.format("SELECT * FROM %s, %s", Index.DOG.getName(), Index.DOGS2.getName());
JSONObject directResponse = new JSONObject(executeFetchQuery(directQuery, 10, "jdbc"));

// Query using alias (which points to the same two indices)
String aliasQuery = String.format("SELECT * FROM %s", aliasName);
JSONObject aliasQueryResponse = new JSONObject(executeFetchQuery(aliasQuery, 10, "jdbc"));

assertEquals(directResponse.getInt("size"), aliasQueryResponse.getInt("size"));

// Clean up alias
String deleteAliasQuery =
String.format(
"{ \"actions\": [ { \"remove\": { \"index\": \"%s\", \"alias\": \"%s\" } }, {"
+ " \"remove\": { \"index\": \"%s\", \"alias\": \"%s\" } } ] }",
Index.DOG.getName(), aliasName, Index.DOGS2.getName(), aliasName);
Request deleteAliasRequest = new Request("POST", "/_aliases");
deleteAliasRequest.setJsonEntity(deleteAliasQuery);
executeRequest(deleteAliasRequest);
}

private String executeFetchQuery(String query, int fetchSize, String requestType, String filter)
throws IOException {
String endpoint = "/_plugins/_sql?format=" + requestType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.stream.StreamSupport;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.opensearch.action.admin.indices.alias.get.GetAliasesResponse;
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.opensearch.common.document.DocumentField;
Expand Down Expand Up @@ -163,11 +161,6 @@ private void populateResultSetFromDefaultCursor(DefaultCursor cursor) {
private void loadFromEsState(Query query) {
String indexName = fetchIndexName(query);
String[] fieldNames = fetchFieldsAsArray(query);
GetAliasesResponse getAliasesResponse =
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
if (getAliasesResponse != null && !getAliasesResponse.getAliases().isEmpty()) {
indexName = getAliasesResponse.getAliases().keySet().iterator().next();
}
// Reset boolean in the case of JOIN query where multiple calls to loadFromEsState() are made
selectAll = isSimpleQuerySelectAll(query) || isJoinQuerySelectAll(query, fieldNames);

Expand All @@ -180,11 +173,11 @@ private void loadFromEsState(Query query) {
client.admin().indices().getFieldMappings(request).actionGet();

Map<String, Map<String, FieldMappingMetadata>> mappings = response.mappings();
if (mappings.isEmpty() || !mappings.containsKey(indexName)) {
if (mappings.isEmpty()) {
throw new IllegalArgumentException(
String.format("Index type %s does not exist", query.getFrom()));
}
Map<String, FieldMappingMetadata> typeMappings = mappings.get(indexName);
Map<String, FieldMappingMetadata> typeMappings = mappings.values().iterator().next();

this.indexName = this.indexName == null ? indexName : (this.indexName + "|" + indexName);
this.columns.addAll(
Expand Down
Loading