15
15
import org .elasticsearch .xpack .esql .analysis .Analyzer ;
16
16
import org .elasticsearch .xpack .esql .analysis .AnalyzerContext ;
17
17
import org .elasticsearch .xpack .esql .analysis .AnalyzerTestUtils ;
18
+ import org .elasticsearch .xpack .esql .analysis .EnrichResolution ;
18
19
import org .elasticsearch .xpack .esql .evaluator .predicate .operator .comparison .Equals ;
19
20
import org .elasticsearch .xpack .esql .evaluator .predicate .operator .comparison .GreaterThan ;
20
21
import org .elasticsearch .xpack .esql .evaluator .predicate .operator .comparison .GreaterThanOrEqual ;
85
86
86
87
import java .util .List ;
87
88
import java .util .Map ;
89
+ import java .util .Set ;
88
90
89
91
import static java .util .Collections .emptyList ;
90
92
import static java .util .Collections .emptyMap ;
93
+ import static java .util .Collections .emptySet ;
91
94
import static java .util .Collections .singletonList ;
92
95
import static org .elasticsearch .xpack .esql .EsqlTestUtils .L ;
93
96
import static org .elasticsearch .xpack .esql .EsqlTestUtils .TEST_VERIFIER ;
96
99
import static org .elasticsearch .xpack .esql .EsqlTestUtils .loadMapping ;
97
100
import static org .elasticsearch .xpack .esql .EsqlTestUtils .localSource ;
98
101
import static org .elasticsearch .xpack .esql .EsqlTestUtils .withDefaultLimitWarning ;
102
+ import static org .elasticsearch .xpack .esql .analysis .Analyzer .NO_FIELDS ;
99
103
import static org .elasticsearch .xpack .esql .type .EsqlDataTypes .GEO_POINT ;
100
104
import static org .elasticsearch .xpack .ql .TestUtils .relation ;
101
105
import static org .elasticsearch .xpack .ql .tree .Source .EMPTY ;
@@ -124,21 +128,17 @@ public class LogicalPlanOptimizerTests extends ESTestCase {
124
128
private static Map <String , EsField > mapping ;
125
129
private static Map <String , EsField > mappingAirports ;
126
130
private static Analyzer analyzerAirports ;
131
+ private static EnrichResolution enrichResolution ;
127
132
128
133
@ BeforeClass
129
134
public static void init () {
130
135
parser = new EsqlParser ();
131
136
logicalOptimizer = new LogicalPlanOptimizer (new LogicalOptimizerContext (EsqlTestUtils .TEST_CFG ));
132
- var enrichResolution = AnalyzerTestUtils .loadEnrichPolicyResolution (
133
- "languages_idx" ,
134
- "id" ,
135
- "languages_idx" ,
136
- "mapping-languages.json"
137
- );
137
+ enrichResolution = AnalyzerTestUtils .loadEnrichPolicyResolution ("languages_idx" , "id" , "languages_idx" , "mapping-languages.json" );
138
138
139
139
// Most tests used data from the test index, so we load it here, and use it in the plan() function.
140
140
mapping = loadMapping ("mapping-basic.json" );
141
- EsIndex test = new EsIndex ("test" , mapping );
141
+ EsIndex test = new EsIndex ("test" , mapping , Set . of ( "test" ) );
142
142
IndexResolution getIndexResult = IndexResolution .valid (test );
143
143
analyzer = new Analyzer (
144
144
new AnalyzerContext (EsqlTestUtils .TEST_CFG , new EsqlFunctionRegistry (), getIndexResult , enrichResolution ),
@@ -147,7 +147,7 @@ public static void init() {
147
147
148
148
// Some tests use data from the airports index, so we load it here, and use it in the plan_airports() function.
149
149
mappingAirports = loadMapping ("mapping-airports.json" );
150
- EsIndex airports = new EsIndex ("airports" , mappingAirports );
150
+ EsIndex airports = new EsIndex ("airports" , mappingAirports , Set . of ( "airports" ) );
151
151
IndexResolution getIndexResultAirports = IndexResolution .valid (airports );
152
152
analyzerAirports = new Analyzer (
153
153
new AnalyzerContext (EsqlTestUtils .TEST_CFG , new EsqlFunctionRegistry (), getIndexResultAirports , enrichResolution ),
@@ -3182,6 +3182,33 @@ public void testStatsWithCanonicalAggregate() throws Exception {
3182
3182
assertThat (Expressions .attribute (fields .get (1 )), is (Expressions .attribute (sum_argument )));
3183
3183
}
3184
3184
3185
+ public void testEmptyMappingIndex () {
3186
+ EsIndex empty = new EsIndex ("empty_test" , emptyMap (), emptySet ());
3187
+ IndexResolution getIndexResultAirports = IndexResolution .valid (empty );
3188
+ var analyzer = new Analyzer (
3189
+ new AnalyzerContext (EsqlTestUtils .TEST_CFG , new EsqlFunctionRegistry (), getIndexResultAirports , enrichResolution ),
3190
+ TEST_VERIFIER
3191
+ );
3192
+
3193
+ var plan = logicalOptimizer .optimize (analyzer .analyze (parser .createStatement ("from empty_test" )));
3194
+ as (plan , LocalRelation .class );
3195
+ assertThat (plan .output (), equalTo (NO_FIELDS ));
3196
+
3197
+ plan = logicalOptimizer .optimize (analyzer .analyze (parser .createStatement ("from empty_test [metadata _id] | eval x = 1" )));
3198
+ as (plan , LocalRelation .class );
3199
+ assertThat (Expressions .names (plan .output ()), contains ("_id" , "x" ));
3200
+
3201
+ plan = logicalOptimizer .optimize (analyzer .analyze (parser .createStatement ("from empty_test [metadata _id, _version] | limit 5" )));
3202
+ as (plan , LocalRelation .class );
3203
+ assertThat (Expressions .names (plan .output ()), contains ("_id" , "_version" ));
3204
+
3205
+ plan = logicalOptimizer .optimize (
3206
+ analyzer .analyze (parser .createStatement ("from empty_test | eval x = \" abc\" | enrich languages_idx on x" ))
3207
+ );
3208
+ LocalRelation local = as (plan , LocalRelation .class );
3209
+ assertThat (Expressions .names (local .output ()), contains (NO_FIELDS .get (0 ).name (), "x" , "language_code" , "language_name" ));
3210
+ }
3211
+
3185
3212
private LogicalPlan optimizedPlan (String query ) {
3186
3213
return plan (query );
3187
3214
}
0 commit comments