|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.remote; |
| 7 | + |
| 8 | +import static org.opensearch.sql.util.MatcherUtils.rows; |
| 9 | +import static org.opensearch.sql.util.MatcherUtils.schema; |
| 10 | +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; |
| 11 | +import static org.opensearch.sql.util.MatcherUtils.verifySchema; |
| 12 | +import static org.opensearch.sql.util.MatcherUtils.verifySchemaInOrder; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.List; |
| 16 | +import org.json.JSONObject; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.opensearch.client.Request; |
| 19 | +import org.opensearch.client.ResponseException; |
| 20 | +import org.opensearch.sql.ppl.PPLIntegTestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Integration tests for aggregation functions (MIN, MAX, FIRST, LAST, TAKE) with alias fields. |
| 24 | + * Tests the fix for issue #4595. |
| 25 | + */ |
| 26 | +public class CalciteAliasFieldAggregationIT extends PPLIntegTestCase { |
| 27 | + |
| 28 | + private static final String TEST_INDEX_ALIAS = "test_alias_bug"; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void init() throws Exception { |
| 32 | + super.init(); |
| 33 | + enableCalcite(); |
| 34 | + createTestIndexWithAliasFields(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Create test index with alias fields mapping and insert sample data. This mirrors the |
| 39 | + * reproduction steps from issue #4595. |
| 40 | + */ |
| 41 | + private void createTestIndexWithAliasFields() throws IOException { |
| 42 | + // Delete the index if it exists (for test isolation) |
| 43 | + try { |
| 44 | + Request deleteIndex = new Request("DELETE", "/" + TEST_INDEX_ALIAS); |
| 45 | + client().performRequest(deleteIndex); |
| 46 | + } catch (ResponseException e) { |
| 47 | + // Index doesn't exist, which is fine |
| 48 | + } |
| 49 | + |
| 50 | + // Create index with alias fields |
| 51 | + Request createIndex = new Request("PUT", "/" + TEST_INDEX_ALIAS); |
| 52 | + createIndex.setJsonEntity( |
| 53 | + "{\n" |
| 54 | + + " \"mappings\": {\n" |
| 55 | + + " \"properties\": {\n" |
| 56 | + + " \"created_at\": {\"type\": \"date\"},\n" |
| 57 | + + " \"@timestamp\": {\"type\": \"alias\", \"path\": \"created_at\"},\n" |
| 58 | + + " \"value\": {\"type\": \"integer\"},\n" |
| 59 | + + " \"value_alias\": {\"type\": \"alias\", \"path\": \"value\"}\n" |
| 60 | + + " }\n" |
| 61 | + + " }\n" |
| 62 | + + "}"); |
| 63 | + client().performRequest(createIndex); |
| 64 | + |
| 65 | + // Insert test documents |
| 66 | + Request bulkRequest = new Request("POST", "/" + TEST_INDEX_ALIAS + "/_bulk?refresh=true"); |
| 67 | + bulkRequest.setJsonEntity( |
| 68 | + "{\"index\":{}}\n" |
| 69 | + + "{\"created_at\": \"2024-01-01T10:00:00Z\", \"value\": 100}\n" |
| 70 | + + "{\"index\":{}}\n" |
| 71 | + + "{\"created_at\": \"2024-01-02T10:00:00Z\", \"value\": 200}\n" |
| 72 | + + "{\"index\":{}}\n" |
| 73 | + + "{\"created_at\": \"2024-01-03T10:00:00Z\", \"value\": 300}\n"); |
| 74 | + client().performRequest(bulkRequest); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testMinWithDateAliasField() throws IOException { |
| 79 | + JSONObject actual = |
| 80 | + executeQuery(String.format("source=%s | stats MIN(@timestamp)", TEST_INDEX_ALIAS)); |
| 81 | + verifySchema(actual, schema("MIN(@timestamp)", "timestamp")); |
| 82 | + verifyDataRows(actual, rows("2024-01-01 10:00:00")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testMaxWithDateAliasField() throws IOException { |
| 87 | + JSONObject actual = |
| 88 | + executeQuery(String.format("source=%s | stats MAX(@timestamp)", TEST_INDEX_ALIAS)); |
| 89 | + verifySchema(actual, schema("MAX(@timestamp)", "timestamp")); |
| 90 | + verifyDataRows(actual, rows("2024-01-03 10:00:00")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testMinMaxWithNumericAliasField() throws IOException { |
| 95 | + JSONObject actual = |
| 96 | + executeQuery( |
| 97 | + String.format( |
| 98 | + "source=%s | stats MIN(value_alias), MAX(value_alias)", TEST_INDEX_ALIAS)); |
| 99 | + verifySchemaInOrder( |
| 100 | + actual, schema("MIN(value_alias)", "int"), schema("MAX(value_alias)", "int")); |
| 101 | + verifyDataRows(actual, rows(100, 300)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testFirstWithAliasField() throws IOException { |
| 106 | + JSONObject actual = |
| 107 | + executeQuery( |
| 108 | + String.format( |
| 109 | + "source=%s | sort @timestamp | stats FIRST(@timestamp)", TEST_INDEX_ALIAS)); |
| 110 | + verifySchema(actual, schema("FIRST(@timestamp)", "timestamp")); |
| 111 | + verifyDataRows(actual, rows("2024-01-01 10:00:00")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testLastWithAliasField() throws IOException { |
| 116 | + JSONObject actual = |
| 117 | + executeQuery( |
| 118 | + String.format( |
| 119 | + "source=%s | sort @timestamp | stats LAST(@timestamp)", TEST_INDEX_ALIAS)); |
| 120 | + verifySchema(actual, schema("LAST(@timestamp)", "timestamp")); |
| 121 | + verifyDataRows(actual, rows("2024-01-03 10:00:00")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testTakeWithAliasField() throws IOException { |
| 126 | + JSONObject actual = |
| 127 | + executeQuery( |
| 128 | + String.format( |
| 129 | + "source=%s | sort @timestamp | stats TAKE(@timestamp, 2)", TEST_INDEX_ALIAS)); |
| 130 | + verifySchema(actual, schema("TAKE(@timestamp, 2)", "array")); |
| 131 | + verifyDataRows(actual, rows(List.of("2024-01-01T10:00:00.000Z", "2024-01-02T10:00:00.000Z"))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testAggregationsWithOriginalFieldsStillWork() throws IOException { |
| 136 | + JSONObject actual = |
| 137 | + executeQuery( |
| 138 | + String.format("source=%s | stats MIN(created_at), MAX(value)", TEST_INDEX_ALIAS)); |
| 139 | + verifySchemaInOrder( |
| 140 | + actual, schema("MIN(created_at)", "timestamp"), schema("MAX(value)", "int")); |
| 141 | + verifyDataRows(actual, rows("2024-01-01 10:00:00", 300)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testUnaffectedAggregationsWithAliasFields() throws IOException { |
| 146 | + JSONObject actual = |
| 147 | + executeQuery( |
| 148 | + String.format( |
| 149 | + "source=%s | stats SUM(value_alias), AVG(value_alias), COUNT(value_alias)", |
| 150 | + TEST_INDEX_ALIAS)); |
| 151 | + verifySchemaInOrder( |
| 152 | + actual, |
| 153 | + schema("SUM(value_alias)", "bigint"), |
| 154 | + schema("AVG(value_alias)", "double"), |
| 155 | + schema("COUNT(value_alias)", "bigint")); |
| 156 | + verifyDataRows(actual, rows(600, 200.0, 3)); |
| 157 | + } |
| 158 | +} |
0 commit comments