|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.api; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertNotNull; |
| 9 | +import static org.junit.Assert.assertThrows; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import org.apache.calcite.schema.Schema; |
| 13 | +import org.apache.calcite.schema.impl.AbstractSchema; |
| 14 | +import org.junit.Test; |
| 15 | +import org.opensearch.sql.executor.QueryType; |
| 16 | + |
| 17 | +public class UnifiedQueryPlannerSqlTest extends UnifiedQueryTestBase { |
| 18 | + |
| 19 | + private final AbstractSchema testDeepSchema = |
| 20 | + new AbstractSchema() { |
| 21 | + @Override |
| 22 | + protected Map<String, Schema> getSubSchemaMap() { |
| 23 | + return Map.of("opensearch", testSchema); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + @Override |
| 28 | + protected QueryType queryType() { |
| 29 | + return QueryType.SQL; |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testSqlQueryPlanning() { |
| 34 | + givenQuery( |
| 35 | + """ |
| 36 | + SELECT * |
| 37 | + FROM catalog.employees\ |
| 38 | + """) |
| 39 | + .assertPlan( |
| 40 | + """ |
| 41 | + LogicalProject(id=[$0], name=[$1], age=[$2], department=[$3]) |
| 42 | + LogicalTableScan(table=[[catalog, employees]]) |
| 43 | + """); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testSqlSelectSpecificColumns() { |
| 48 | + givenQuery( |
| 49 | + """ |
| 50 | + SELECT id, name |
| 51 | + FROM catalog.employees\ |
| 52 | + """) |
| 53 | + .assertPlan( |
| 54 | + """ |
| 55 | + LogicalProject(id=[$0], name=[$1]) |
| 56 | + LogicalTableScan(table=[[catalog, employees]]) |
| 57 | + """); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testSqlFilterQueryPlanning() { |
| 62 | + givenQuery( |
| 63 | + """ |
| 64 | + SELECT name |
| 65 | + FROM catalog.employees |
| 66 | + WHERE age > 30\ |
| 67 | + """) |
| 68 | + .assertPlan( |
| 69 | + """ |
| 70 | + LogicalProject(name=[$1]) |
| 71 | + LogicalFilter(condition=[>($2, 30)]) |
| 72 | + LogicalTableScan(table=[[catalog, employees]]) |
| 73 | + """); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSqlAggregateQueryPlanning() { |
| 78 | + givenQuery( |
| 79 | + """ |
| 80 | + SELECT department, count(*) AS cnt |
| 81 | + FROM catalog.employees |
| 82 | + GROUP BY department\ |
| 83 | + """) |
| 84 | + .assertPlan( |
| 85 | + """ |
| 86 | + LogicalAggregate(group=[{0}], cnt=[COUNT()]) |
| 87 | + LogicalProject(department=[$3]) |
| 88 | + LogicalTableScan(table=[[catalog, employees]]) |
| 89 | + """); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testSqlJoinQueryPlanning() { |
| 94 | + givenQuery( |
| 95 | + """ |
| 96 | + SELECT a.id, b.name |
| 97 | + FROM catalog.employees a |
| 98 | + JOIN catalog.employees b ON a.id = b.age\ |
| 99 | + """) |
| 100 | + .assertPlan( |
| 101 | + """ |
| 102 | + LogicalProject(id=[$0], name=[$5]) |
| 103 | + LogicalJoin(condition=[=($0, $6)], joinType=[inner]) |
| 104 | + LogicalTableScan(table=[[catalog, employees]]) |
| 105 | + LogicalTableScan(table=[[catalog, employees]]) |
| 106 | + """); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSqlOrderByQueryPlanning() { |
| 111 | + givenQuery( |
| 112 | + """ |
| 113 | + SELECT name |
| 114 | + FROM catalog.employees |
| 115 | + ORDER BY age DESC\ |
| 116 | + """) |
| 117 | + .assertPlan( |
| 118 | + """ |
| 119 | + LogicalProject(name=[$0]) |
| 120 | + LogicalSort(sort0=[$1], dir0=[DESC]) |
| 121 | + LogicalProject(name=[$1], age=[$2]) |
| 122 | + LogicalTableScan(table=[[catalog, employees]]) |
| 123 | + """); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testSqlSubqueryPlanning() { |
| 128 | + // Calcite represents scalar subqueries as $SCALAR_QUERY{...} with embedded plan text whose |
| 129 | + // formatting (whitespace, line breaks) may vary across versions. Assert output fields only. |
| 130 | + givenQuery( |
| 131 | + """ |
| 132 | + SELECT name |
| 133 | + FROM catalog.employees |
| 134 | + WHERE age > (SELECT avg(age) FROM catalog.employees)\ |
| 135 | + """) |
| 136 | + .assertFields("name"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testSqlCteQueryPlanning() { |
| 141 | + // CTE is inlined by Calcite — same plan as a direct filter query |
| 142 | + givenQuery( |
| 143 | + """ |
| 144 | + WITH seniors AS ( |
| 145 | + SELECT name, age FROM catalog.employees WHERE age > 30 |
| 146 | + ) |
| 147 | + SELECT name |
| 148 | + FROM seniors\ |
| 149 | + """) |
| 150 | + .assertPlan( |
| 151 | + """ |
| 152 | + LogicalProject(name=[$1]) |
| 153 | + LogicalFilter(condition=[>($2, 30)]) |
| 154 | + LogicalTableScan(table=[[catalog, employees]]) |
| 155 | + """); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testSqlQueryPlanningWithDefaultNamespace() { |
| 160 | + UnifiedQueryContext sqlContext = |
| 161 | + UnifiedQueryContext.builder() |
| 162 | + .language(QueryType.SQL) |
| 163 | + .catalog("opensearch", testSchema) |
| 164 | + .defaultNamespace("opensearch") |
| 165 | + .build(); |
| 166 | + UnifiedQueryPlanner sqlPlanner = new UnifiedQueryPlanner(sqlContext); |
| 167 | + |
| 168 | + assertNotNull("Plan should be created", sqlPlanner.plan("SELECT * FROM opensearch.employees")); |
| 169 | + assertNotNull("Plan should be created", sqlPlanner.plan("SELECT * FROM employees")); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testSqlQueryPlanningWithDefaultNamespaceMultiLevel() { |
| 174 | + UnifiedQueryContext sqlContext = |
| 175 | + UnifiedQueryContext.builder() |
| 176 | + .language(QueryType.SQL) |
| 177 | + .catalog("catalog", testDeepSchema) |
| 178 | + .defaultNamespace("catalog.opensearch") |
| 179 | + .build(); |
| 180 | + UnifiedQueryPlanner sqlPlanner = new UnifiedQueryPlanner(sqlContext); |
| 181 | + |
| 182 | + assertNotNull( |
| 183 | + "Plan should be created", sqlPlanner.plan("SELECT * FROM catalog.opensearch.employees")); |
| 184 | + assertNotNull("Plan should be created", sqlPlanner.plan("SELECT * FROM employees")); |
| 185 | + |
| 186 | + assertThrows( |
| 187 | + IllegalStateException.class, () -> sqlPlanner.plan("SELECT * FROM opensearch.employees")); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testSqlQueryPlanningWithMultipleCatalogs() { |
| 192 | + UnifiedQueryContext sqlContext = |
| 193 | + UnifiedQueryContext.builder() |
| 194 | + .language(QueryType.SQL) |
| 195 | + .catalog("catalog1", testSchema) |
| 196 | + .catalog("catalog2", testSchema) |
| 197 | + .build(); |
| 198 | + UnifiedQueryPlanner sqlPlanner = new UnifiedQueryPlanner(sqlContext); |
| 199 | + |
| 200 | + assertNotNull( |
| 201 | + "Plan should be created", |
| 202 | + sqlPlanner.plan( |
| 203 | + """ |
| 204 | + SELECT a.id |
| 205 | + FROM catalog1.employees a |
| 206 | + JOIN catalog2.employees b ON a.id = b.id\ |
| 207 | + """)); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testInvalidSqlThrowsException() { |
| 212 | + assertThrows(IllegalStateException.class, () -> planner.plan("SELECT FROM")); |
| 213 | + } |
| 214 | +} |
0 commit comments