2626import org .elasticsearch .xpack .esql .plan .logical .Aggregate ;
2727import org .elasticsearch .xpack .esql .plan .logical .Drop ;
2828import org .elasticsearch .xpack .esql .plan .logical .Enrich ;
29+ import org .elasticsearch .xpack .esql .plan .logical .EsRelation ;
2930import org .elasticsearch .xpack .esql .plan .logical .Eval ;
3031import org .elasticsearch .xpack .esql .plan .logical .Filter ;
3132import org .elasticsearch .xpack .esql .plan .logical .Fork ;
@@ -71,10 +72,19 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
7172 inlinestatsAggs .add (((InlineStats ) i ).aggregate ());
7273 }
7374
75+ boolean shouldCollectAllDimensions = false ;
76+ // Detect if we are in TS mode
77+ List <LogicalPlan > relations = parsed .collect (UnresolvedRelation .class ::isInstance );
78+ for (LogicalPlan i : relations ) {
79+ if (((UnresolvedRelation ) i ).isTimeSeriesMode ()) {
80+ shouldCollectAllDimensions = true ;
81+ }
82+ }
83+
7484 if (false == parsed .anyMatch (p -> shouldCollectReferencedFields (p , inlinestatsAggs ))) {
7585 // no explicit columns selection, for example "from employees"
7686 // also, inlinestats only adds columns to the existent output, its Aggregate shouldn't interfere with potentially using "*"
77- return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), false );
87+ return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), shouldCollectAllDimensions );
7888 }
7989
8090 Holder <Boolean > projectAll = new Holder <>(false );
@@ -86,7 +96,7 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
8696 });
8797
8898 if (projectAll .get ()) {
89- return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), false );
99+ return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), shouldCollectAllDimensions );
90100 }
91101
92102 var referencesBuilder = new Holder <>(AttributeSet .builder ());
@@ -221,7 +231,7 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
221231 parsed .forEachDownMayReturnEarly (forEachDownProcessor .get ());
222232
223233 if (projectAll .get ()) {
224- return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), false );
234+ return new PreAnalysisResult (enrichResolution , IndexResolver .ALL_FIELDS , Set .of (), shouldCollectAllDimensions );
225235 }
226236
227237 // Add JOIN ON column references afterward to avoid Alias removal
@@ -235,7 +245,12 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
235245
236246 if (fieldNames .isEmpty () && enrichPolicyMatchFields .isEmpty ()) {
237247 // there cannot be an empty list of fields, we'll ask the simplest and lightest one instead: _index
238- return new PreAnalysisResult (enrichResolution , IndexResolver .INDEX_METADATA_FIELD , wildcardJoinIndices , false );
248+ return new PreAnalysisResult (
249+ enrichResolution ,
250+ IndexResolver .INDEX_METADATA_FIELD ,
251+ wildcardJoinIndices ,
252+ shouldCollectAllDimensions
253+ );
239254 } else {
240255 fieldNames .addAll (subfields (fieldNames ));
241256 fieldNames .addAll (enrichPolicyMatchFields );
0 commit comments