|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite; |
| 7 | + |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.ArgumentMatchers.argThat; |
| 10 | +import static org.mockito.ArgumentMatchers.eq; |
| 11 | +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
| 12 | +import static org.mockito.Mockito.lenient; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.never; |
| 15 | +import static org.mockito.Mockito.verify; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | +import static org.opensearch.sql.ast.dsl.AstDSL.argument; |
| 18 | +import static org.opensearch.sql.ast.dsl.AstDSL.booleanLiteral; |
| 19 | +import static org.opensearch.sql.ast.dsl.AstDSL.field; |
| 20 | +import static org.opensearch.sql.ast.dsl.AstDSL.fillNull; |
| 21 | +import static org.opensearch.sql.ast.dsl.AstDSL.filter; |
| 22 | +import static org.opensearch.sql.ast.dsl.AstDSL.map; |
| 23 | +import static org.opensearch.sql.ast.dsl.AstDSL.mvcombine; |
| 24 | +import static org.opensearch.sql.ast.dsl.AstDSL.project; |
| 25 | +import static org.opensearch.sql.ast.dsl.AstDSL.projectWithArg; |
| 26 | +import static org.opensearch.sql.ast.dsl.AstDSL.relation; |
| 27 | +import static org.opensearch.sql.ast.dsl.AstDSL.rename; |
| 28 | +import static org.opensearch.sql.ast.dsl.AstDSL.stringLiteral; |
| 29 | + |
| 30 | +import java.sql.Connection; |
| 31 | +import java.util.LinkedHashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.stream.Stream; |
| 36 | +import org.apache.calcite.rel.RelNode; |
| 37 | +import org.apache.calcite.rex.RexBuilder; |
| 38 | +import org.apache.calcite.rex.RexNode; |
| 39 | +import org.apache.calcite.sql.SqlKind; |
| 40 | +import org.apache.calcite.tools.FrameworkConfig; |
| 41 | +import org.apache.commons.lang3.tuple.Pair; |
| 42 | +import org.junit.jupiter.api.BeforeEach; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.MockedStatic; |
| 47 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 48 | +import org.opensearch.sql.ast.expression.Field; |
| 49 | +import org.opensearch.sql.ast.expression.UnresolvedExpression; |
| 50 | +import org.opensearch.sql.ast.tree.AddTotals; |
| 51 | +import org.opensearch.sql.ast.tree.Replace; |
| 52 | +import org.opensearch.sql.ast.tree.ReplacePair; |
| 53 | +import org.opensearch.sql.ast.tree.UnresolvedPlan; |
| 54 | +import org.opensearch.sql.calcite.utils.CalciteToolsHelper; |
| 55 | +import org.opensearch.sql.calcite.utils.CalciteToolsHelper.OpenSearchRelBuilder; |
| 56 | +import org.opensearch.sql.calcite.utils.OpenSearchTypeFactory; |
| 57 | +import org.opensearch.sql.executor.QueryType; |
| 58 | + |
| 59 | +@ExtendWith(MockitoExtension.class) |
| 60 | +public class MapPathPreMaterializerTest { |
| 61 | + |
| 62 | + @Mock private CalciteRexNodeVisitor rexVisitor; |
| 63 | + @Mock private OpenSearchRelBuilder relBuilder; |
| 64 | + |
| 65 | + /** Intercepts static factory methods in {@link CalciteToolsHelper} during context creation. */ |
| 66 | + @Mock private MockedStatic<CalciteToolsHelper> mockedToolsHelper; |
| 67 | + |
| 68 | + /** Placeholder child plan node required by commands like rename and fillnull. */ |
| 69 | + private static final UnresolvedPlan DUMMY_CHILD = relation("t"); |
| 70 | + |
| 71 | + private CalcitePlanContext context; |
| 72 | + private MapPathPreMaterializer materializer; |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + void setUp() { |
| 76 | + materializer = new MapPathPreMaterializer(rexVisitor); |
| 77 | + mockedToolsHelper |
| 78 | + .when(() -> CalciteToolsHelper.connect(any(), any())) |
| 79 | + .thenReturn(mock(Connection.class)); |
| 80 | + mockedToolsHelper |
| 81 | + .when(() -> CalciteToolsHelper.create(any(), any(), any())) |
| 82 | + .thenReturn(relBuilder); |
| 83 | + when(relBuilder.getRexBuilder()).thenReturn(new RexBuilder(OpenSearchTypeFactory.TYPE_FACTORY)); |
| 84 | + context = |
| 85 | + CalcitePlanContext.create(mock(FrameworkConfig.class), SysLimit.DEFAULT, QueryType.PPL); |
| 86 | + lenient().when(relBuilder.size()).thenReturn(1); |
| 87 | + lenient().when(relBuilder.peek()).thenReturn(mock(RelNode.class, RETURNS_DEEP_STUBS)); |
| 88 | + } |
| 89 | + |
| 90 | + // ---- Symbol-based commands ---- |
| 91 | + |
| 92 | + @Test |
| 93 | + void testRename() { |
| 94 | + givenMapPaths("doc.user.name") |
| 95 | + .whenCommand(rename(DUMMY_CHILD, map("doc.user.name", "username"))) |
| 96 | + .shouldProject("doc.user.name"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testFillnull() { |
| 101 | + givenMapPaths("doc.user.name") |
| 102 | + .whenCommand( |
| 103 | + fillNull(DUMMY_CHILD, List.of(Pair.of(field("doc.user.name"), stringLiteral("N/A"))))) |
| 104 | + .shouldProject("doc.user.name"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testReplace() { |
| 109 | + givenMapPaths("doc.user.name") |
| 110 | + .whenCommand( |
| 111 | + new Replace( |
| 112 | + List.of(new ReplacePair(stringLiteral("a"), stringLiteral("b"))), |
| 113 | + Set.of(field("doc.user.name")))) |
| 114 | + .shouldProject("doc.user.name"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testFieldsExclusion() { |
| 119 | + givenMapPaths("doc.user.name") |
| 120 | + .whenCommand( |
| 121 | + projectWithArg( |
| 122 | + DUMMY_CHILD, |
| 123 | + List.of(argument("exclude", booleanLiteral(true))), |
| 124 | + field("doc.user.name"))) |
| 125 | + .shouldProject("doc.user.name"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void testAddtotals() { |
| 130 | + givenMapPaths("doc.user.name") |
| 131 | + .whenCommand(new AddTotals(List.of(field("doc.user.name")), Map.of())) |
| 132 | + .shouldProject("doc.user.name"); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testMvcombine() { |
| 137 | + givenMapPaths("doc.user.name") |
| 138 | + .whenCommand(mvcombine(field("doc.user.name"))) |
| 139 | + .shouldProject("doc.user.name"); |
| 140 | + } |
| 141 | + |
| 142 | + // ---- Multiple fields cases ---- |
| 143 | + |
| 144 | + @Test |
| 145 | + void testMultipleMapPaths() { |
| 146 | + givenMapPaths("doc.user.name", "doc.user.age") |
| 147 | + .whenCommand( |
| 148 | + fillNull( |
| 149 | + DUMMY_CHILD, |
| 150 | + List.of( |
| 151 | + Pair.of(field("doc.user.name"), stringLiteral("N/A")), |
| 152 | + Pair.of(field("doc.user.age"), stringLiteral("0"))))) |
| 153 | + .shouldProject("doc.user.name", "doc.user.age"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testMixedMapAndNonMapFields() { |
| 158 | + givenMapPaths("doc.user.name") |
| 159 | + .givenNonMapPaths("regular_field") |
| 160 | + .whenCommand( |
| 161 | + fillNull( |
| 162 | + DUMMY_CHILD, |
| 163 | + List.of( |
| 164 | + Pair.of(field("doc.user.name"), stringLiteral("N/A")), |
| 165 | + Pair.of(field("regular_field"), stringLiteral("0"))))) |
| 166 | + .shouldProject("doc.user.name"); |
| 167 | + } |
| 168 | + |
| 169 | + // ---- No-op cases ---- |
| 170 | + |
| 171 | + @Test |
| 172 | + void testNoOpForFilter() { |
| 173 | + givenMapPaths("doc.user.name") |
| 174 | + .whenCommand(filter(DUMMY_CHILD, field("doc.user.name"))) |
| 175 | + .shouldNotProject(); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + void testNoOpForNonExcludedProject() { |
| 180 | + givenMapPaths("doc.user.name") |
| 181 | + .whenCommand(project(DUMMY_CHILD, field("doc.user.name"))) |
| 182 | + .shouldNotProject(); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void testNoOpWhenRelBuilderStackEmpty() { |
| 187 | + lenient().when(relBuilder.size()).thenReturn(0); |
| 188 | + givenMapPaths("doc.user.name") |
| 189 | + .whenCommand(rename(DUMMY_CHILD, map("doc.user.name", "u"))) |
| 190 | + .shouldNotProject(); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void testNoOpWhenFieldResolvesToNonItemAccess() { |
| 195 | + givenMapPaths() |
| 196 | + .givenNonMapPaths("regular_field") |
| 197 | + .whenCommand(rename(DUMMY_CHILD, map("regular_field", "alias"))) |
| 198 | + .shouldNotProject(); |
| 199 | + } |
| 200 | + |
| 201 | + // ---- Fluent test helper ---- |
| 202 | + |
| 203 | + private MapPathAssertion givenMapPaths(String... fieldNames) { |
| 204 | + return new MapPathAssertion(fieldNames); |
| 205 | + } |
| 206 | + |
| 207 | + private class MapPathAssertion { |
| 208 | + private final Map<String, RexNode> aliasedNodes = new LinkedHashMap<>(); |
| 209 | + |
| 210 | + MapPathAssertion(String... mapFieldNames) { |
| 211 | + for (String name : mapFieldNames) { |
| 212 | + RexNode item = mock(RexNode.class, "item:" + name); |
| 213 | + RexNode aliased = mock(RexNode.class, "aliased:" + name); |
| 214 | + lenient().when(item.getKind()).thenReturn(SqlKind.ITEM); |
| 215 | + lenient().when(rexVisitor.analyze(fieldMatching(name), eq(context))).thenReturn(item); |
| 216 | + lenient().when(relBuilder.alias(item, name)).thenReturn(aliased); |
| 217 | + aliasedNodes.put(name, aliased); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + MapPathAssertion givenNonMapPaths(String... fieldNames) { |
| 222 | + for (String name : fieldNames) { |
| 223 | + RexNode ref = mock(RexNode.class, "ref:" + name); |
| 224 | + when(ref.getKind()).thenReturn(SqlKind.INPUT_REF); |
| 225 | + lenient().when(rexVisitor.analyze(fieldMatching(name), eq(context))).thenReturn(ref); |
| 226 | + } |
| 227 | + return this; |
| 228 | + } |
| 229 | + |
| 230 | + MapPathAssertion whenCommand(UnresolvedPlan command) { |
| 231 | + materializer.materializePaths(command, context); |
| 232 | + return this; |
| 233 | + } |
| 234 | + |
| 235 | + void shouldProject(String... expectedFields) { |
| 236 | + List<RexNode> expected = Stream.of(expectedFields).map(aliasedNodes::get).toList(); |
| 237 | + verify(relBuilder).projectPlus(expected); |
| 238 | + } |
| 239 | + |
| 240 | + void shouldNotProject() { |
| 241 | + verify(relBuilder, never()).projectPlus(any(List.class)); |
| 242 | + } |
| 243 | + |
| 244 | + private static UnresolvedExpression fieldMatching(String name) { |
| 245 | + return argThat(expr -> expr instanceof Field f && f.getField().toString().equals(name)); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments