Skip to content

Commit 74b2fb3

Browse files
Remove GetAlias Call (#4981)
1 parent 669a4f9 commit 74b2fb3

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class PaginationIT extends SQLIntegTestCase {
2828
public void init() throws IOException {
2929
loadIndex(Index.CALCS);
3030
loadIndex(Index.ONLINE);
31+
loadIndex(Index.DOG);
32+
loadIndex(Index.DOGS2);
3133
}
3234

3335
@Test
@@ -251,6 +253,45 @@ public void testAlias() throws Exception {
251253
assertEquals(aliasFilteredResponse.getInt("size"), 4);
252254
}
253255

256+
@Test
257+
public void testAliasResultConsistency() throws Exception {
258+
String aliasName = "alias_dog_consistency";
259+
260+
// Create an alias that maps to both DOG and DOGS2 indices
261+
String createAliasQuery =
262+
String.format(
263+
"{ \"actions\": [ "
264+
+ "{ \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } }, "
265+
+ "{ \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } } "
266+
+ "] }",
267+
Index.DOG.getName(), aliasName, Index.DOGS2.getName(), aliasName);
268+
Request createAliasRequest = new Request("POST", "/_aliases");
269+
createAliasRequest.setJsonEntity(createAliasQuery);
270+
JSONObject aliasResponse = new JSONObject(executeRequest(createAliasRequest));
271+
assertTrue(aliasResponse.getBoolean("acknowledged"));
272+
273+
// Query both indices directly (same indices the alias points to)
274+
String directQuery =
275+
String.format("SELECT * FROM %s, %s", Index.DOG.getName(), Index.DOGS2.getName());
276+
JSONObject directResponse = new JSONObject(executeFetchQuery(directQuery, 10, "jdbc"));
277+
278+
// Query using alias (which points to the same two indices)
279+
String aliasQuery = String.format("SELECT * FROM %s", aliasName);
280+
JSONObject aliasQueryResponse = new JSONObject(executeFetchQuery(aliasQuery, 10, "jdbc"));
281+
282+
assertEquals(directResponse.getInt("size"), aliasQueryResponse.getInt("size"));
283+
284+
// Clean up alias
285+
String deleteAliasQuery =
286+
String.format(
287+
"{ \"actions\": [ { \"remove\": { \"index\": \"%s\", \"alias\": \"%s\" } }, {"
288+
+ " \"remove\": { \"index\": \"%s\", \"alias\": \"%s\" } } ] }",
289+
Index.DOG.getName(), aliasName, Index.DOGS2.getName(), aliasName);
290+
Request deleteAliasRequest = new Request("POST", "/_aliases");
291+
deleteAliasRequest.setJsonEntity(deleteAliasQuery);
292+
executeRequest(deleteAliasRequest);
293+
}
294+
254295
private String executeFetchQuery(String query, int fetchSize, String requestType, String filter)
255296
throws IOException {
256297
String endpoint = "/_plugins/_sql?format=" + requestType;

legacy/src/main/java/org/opensearch/sql/legacy/executor/format/SelectResultSet.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.stream.StreamSupport;
2727
import org.apache.logging.log4j.LogManager;
2828
import org.apache.logging.log4j.Logger;
29-
import org.opensearch.action.admin.indices.alias.get.GetAliasesRequest;
30-
import org.opensearch.action.admin.indices.alias.get.GetAliasesResponse;
3129
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
3230
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
3331
import org.opensearch.common.document.DocumentField;
@@ -163,11 +161,6 @@ private void populateResultSetFromDefaultCursor(DefaultCursor cursor) {
163161
private void loadFromEsState(Query query) {
164162
String indexName = fetchIndexName(query);
165163
String[] fieldNames = fetchFieldsAsArray(query);
166-
GetAliasesResponse getAliasesResponse =
167-
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
168-
if (getAliasesResponse != null && !getAliasesResponse.getAliases().isEmpty()) {
169-
indexName = getAliasesResponse.getAliases().keySet().iterator().next();
170-
}
171164
// Reset boolean in the case of JOIN query where multiple calls to loadFromEsState() are made
172165
selectAll = isSimpleQuerySelectAll(query) || isJoinQuerySelectAll(query, fieldNames);
173166

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

182175
Map<String, Map<String, FieldMappingMetadata>> mappings = response.mappings();
183-
if (mappings.isEmpty() || !mappings.containsKey(indexName)) {
176+
if (mappings.isEmpty()) {
184177
throw new IllegalArgumentException(
185178
String.format("Index type %s does not exist", query.getFrom()));
186179
}
187-
Map<String, FieldMappingMetadata> typeMappings = mappings.get(indexName);
180+
Map<String, FieldMappingMetadata> typeMappings = mappings.values().iterator().next();
188181

189182
this.indexName = this.indexName == null ? indexName : (this.indexName + "|" + indexName);
190183
this.columns.addAll(

0 commit comments

Comments
 (0)