Skip to content

Commit 337a245

Browse files
Remove GetAlias Call (#4981) (#5050)
(cherry picked from commit 74b2fb3) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 6dd3dd5 commit 337a245

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

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.action.search.ClearScrollResponse;
@@ -166,11 +164,6 @@ private void populateResultSetFromDefaultCursor(DefaultCursor cursor) {
166164
private void loadFromEsState(Query query) {
167165
String indexName = fetchIndexName(query);
168166
String[] fieldNames = fetchFieldsAsArray(query);
169-
GetAliasesResponse getAliasesResponse =
170-
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
171-
if (getAliasesResponse != null && !getAliasesResponse.getAliases().isEmpty()) {
172-
indexName = getAliasesResponse.getAliases().keySet().iterator().next();
173-
}
174167
// Reset boolean in the case of JOIN query where multiple calls to loadFromEsState() are made
175168
selectAll = isSimpleQuerySelectAll(query) || isJoinQuerySelectAll(query, fieldNames);
176169

@@ -183,11 +176,11 @@ private void loadFromEsState(Query query) {
183176
client.admin().indices().getFieldMappings(request).actionGet();
184177

185178
Map<String, Map<String, FieldMappingMetadata>> mappings = response.mappings();
186-
if (mappings.isEmpty() || !mappings.containsKey(indexName)) {
179+
if (mappings.isEmpty()) {
187180
throw new IllegalArgumentException(
188181
String.format("Index type %s does not exist", query.getFrom()));
189182
}
190-
Map<String, FieldMappingMetadata> typeMappings = mappings.get(indexName);
183+
Map<String, FieldMappingMetadata> typeMappings = mappings.values().iterator().next();
191184

192185
this.indexName = this.indexName == null ? indexName : (this.indexName + "|" + indexName);
193186
this.columns.addAll(

0 commit comments

Comments
 (0)