7272import org .elasticsearch .xpack .esql .index .EsIndex ;
7373import org .elasticsearch .xpack .esql .index .IndexResolution ;
7474import org .elasticsearch .xpack .esql .inference .ResolvedInference ;
75+ import org .elasticsearch .xpack .esql .optimizer .rules .logical .SubstituteSurrogateExpressions ;
7576import org .elasticsearch .xpack .esql .parser .ParsingException ;
7677import org .elasticsearch .xpack .esql .plan .IndexPattern ;
7778import org .elasticsearch .xpack .esql .plan .logical .Aggregate ;
@@ -1617,6 +1618,7 @@ private LogicalPlan doRule(LogicalPlan plan) {
16171618 }
16181619
16191620 private Expression resolveConvertFunction (ConvertFunction convert , List <FieldAttribute > unionFieldAttributes ) {
1621+ Expression convertExpression = (Expression ) convert ;
16201622 if (convert .field () instanceof FieldAttribute fa && fa .field () instanceof InvalidMappedField imf ) {
16211623 HashMap <TypeResolutionKey , Expression > typeResolutions = new HashMap <>();
16221624 Set <DataType > supportedTypes = convert .supportedTypes ();
@@ -1643,9 +1645,11 @@ private Expression resolveConvertFunction(ConvertFunction convert, List<FieldAtt
16431645 return createIfDoesNotAlreadyExist (fa , resolvedField , unionFieldAttributes );
16441646 }
16451647 } else if (convert .field () instanceof AbstractConvertFunction subConvert ) {
1646- return convert .replaceChildren (Collections .singletonList (resolveConvertFunction (subConvert , unionFieldAttributes )));
1648+ return convertExpression .replaceChildren (
1649+ Collections .singletonList (resolveConvertFunction (subConvert , unionFieldAttributes ))
1650+ );
16471651 }
1648- return ( Expression ) convert ;
1652+ return convertExpression ;
16491653 }
16501654
16511655 private Expression createIfDoesNotAlreadyExist (
@@ -1693,19 +1697,13 @@ private Expression typeSpecificConvert(ConvertFunction convert, Source source, D
16931697 originalFieldAttr .id (),
16941698 true
16951699 );
1696- Expression e = convert .replaceChildren (Collections .singletonList (resolvedAttr ));
1700+ Expression e = (( Expression ) convert ) .replaceChildren (Collections .singletonList (resolvedAttr ));
16971701 /*
16981702 * Resolve surrogates immediately because these type specific conversions are serialized
16991703 * and SurrogateExpressions are expected to be resolved on the coordinating node. At least,
17001704 * TO_IP is expected to be resolved there.
17011705 */
1702- if (e instanceof SurrogateExpression s ) {
1703- Expression surrogate = s .surrogate ();
1704- if (surrogate != null ) {
1705- return surrogate ;
1706- }
1707- }
1708- return e ;
1706+ return SubstituteSurrogateExpressions .rule (e );
17091707 }
17101708 }
17111709
0 commit comments