|
8 | 8 | import static org.junit.Assert.assertFalse; |
9 | 9 | import static org.junit.Assert.assertTrue; |
10 | 10 | import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.when; |
11 | 12 |
|
12 | 13 | import org.apache.calcite.rel.RelNode; |
13 | 14 | import org.junit.Before; |
14 | 15 | import org.junit.Test; |
15 | 16 | import org.opensearch.analytics.exec.QueryPlanExecutor; |
| 17 | +import org.opensearch.cluster.ClusterState; |
| 18 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 19 | +import org.opensearch.cluster.metadata.Metadata; |
16 | 20 | import org.opensearch.cluster.service.ClusterService; |
17 | | -import org.opensearch.sql.common.setting.Settings; |
| 21 | +import org.opensearch.common.settings.Settings; |
| 22 | +import org.opensearch.index.IndexSettings; |
18 | 23 | import org.opensearch.sql.executor.QueryType; |
19 | 24 | import org.opensearch.transport.client.node.NodeClient; |
20 | 25 |
|
21 | 26 | /** |
22 | | - * Tests for analytics index routing in RestUnifiedQueryAction. Uses context parser for AST-based |
23 | | - * index name extraction. |
| 27 | + * Tests for analytics index routing in RestUnifiedQueryAction. Routing requires both {@code |
| 28 | + * index.pluggable.dataformat.enabled=true} and {@code index.pluggable.dataformat=parquet}. |
24 | 29 | */ |
25 | 30 | public class RestUnifiedQueryActionTest { |
26 | 31 |
|
| 32 | + private ClusterService clusterService; |
| 33 | + private Metadata metadata; |
27 | 34 | private RestUnifiedQueryAction action; |
28 | 35 |
|
29 | 36 | @Before |
30 | 37 | public void setUp() { |
| 38 | + clusterService = mock(ClusterService.class); |
| 39 | + ClusterState clusterState = mock(ClusterState.class); |
| 40 | + metadata = mock(Metadata.class); |
| 41 | + when(clusterService.state()).thenReturn(clusterState); |
| 42 | + when(clusterState.metadata()).thenReturn(metadata); |
| 43 | + |
31 | 44 | @SuppressWarnings("unchecked") |
32 | 45 | QueryPlanExecutor<RelNode, Iterable<Object[]>> executor = mock(QueryPlanExecutor.class); |
33 | 46 | action = |
34 | 47 | new RestUnifiedQueryAction( |
35 | | - mock(NodeClient.class), mock(ClusterService.class), executor, mock(Settings.class)); |
| 48 | + mock(NodeClient.class), |
| 49 | + clusterService, |
| 50 | + executor, |
| 51 | + mock(org.opensearch.sql.common.setting.Settings.class)); |
36 | 52 | } |
37 | 53 |
|
38 | 54 | @Test |
39 | | - public void parquetIndexRoutesToAnalytics() { |
| 55 | + public void pluggableDataformatIndexRoutesToAnalytics() { |
| 56 | + registerIndex( |
| 57 | + "parquet_logs", |
| 58 | + Settings.builder() |
| 59 | + .put(IndexSettings.PLUGGABLE_DATAFORMAT_ENABLED_SETTING.getKey(), true) |
| 60 | + .put(IndexSettings.PLUGGABLE_DATAFORMAT_VALUE_SETTING.getKey(), "parquet") |
| 61 | + .build()); |
| 62 | + |
40 | 63 | assertTrue(action.isAnalyticsIndex("source = parquet_logs | fields ts", QueryType.PPL)); |
41 | 64 | assertTrue( |
42 | 65 | action.isAnalyticsIndex("source = opensearch.parquet_logs | fields ts", QueryType.PPL)); |
43 | 66 | } |
44 | 67 |
|
45 | 68 | @Test |
46 | | - public void nonParquetIndexRoutesToLucene() { |
47 | | - assertFalse(action.isAnalyticsIndex("source = my_logs | fields ts", QueryType.PPL)); |
| 69 | + public void pluggableEnabledButLuceneFormatRoutesToLucene() { |
| 70 | + registerIndex( |
| 71 | + "lucene_logs", |
| 72 | + Settings.builder() |
| 73 | + .put(IndexSettings.PLUGGABLE_DATAFORMAT_ENABLED_SETTING.getKey(), true) |
| 74 | + .put(IndexSettings.PLUGGABLE_DATAFORMAT_VALUE_SETTING.getKey(), "lucene") |
| 75 | + .build()); |
| 76 | + |
| 77 | + assertFalse(action.isAnalyticsIndex("source = lucene_logs | fields ts", QueryType.PPL)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void indexWithoutSettingRoutesToLucene() { |
| 82 | + registerIndex("plain_logs", Settings.EMPTY); |
| 83 | + |
| 84 | + assertFalse(action.isAnalyticsIndex("source = plain_logs | fields ts", QueryType.PPL)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void missingIndexRoutesToLucene() { |
| 89 | + assertFalse(action.isAnalyticsIndex("source = does_not_exist | fields ts", QueryType.PPL)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void nullAndEmptyQueriesRouteToLucene() { |
48 | 94 | assertFalse(action.isAnalyticsIndex(null, QueryType.PPL)); |
49 | 95 | assertFalse(action.isAnalyticsIndex("", QueryType.PPL)); |
50 | 96 | } |
| 97 | + |
| 98 | + private void registerIndex(String name, Settings settings) { |
| 99 | + IndexMetadata indexMetadata = mock(IndexMetadata.class); |
| 100 | + when(indexMetadata.getSettings()).thenReturn(settings); |
| 101 | + when(metadata.index(name)).thenReturn(indexMetadata); |
| 102 | + } |
51 | 103 | } |
0 commit comments