@@ -1697,15 +1697,18 @@ private Expression resolveConvertFunction(ConvertFunction convert, List<FieldAtt
16971697 && fa .field () instanceof MultiTypeEsField mtf ) {
16981698 // This is an explicit casting of a union typed field that has been converted to MultiTypeEsField in EsRelation by
16991699 // DateMillisToNanosInEsRelation, it is not necessary to cast it again to the same type, replace the implicit casting
1700- // with explicit casting.
1700+ // with explicit casting. However, it is useful to differentiate implicit and explicit casting in some cases, for
1701+ // example, an expression like multiTypeEsField(synthetic=false, date_nanos)::date_nanos::datetime is rewritten to
1702+ // multiTypeEsField(synthetic=true, date_nanos)::datetime, the implicit casting is overwritten by explicit casting and
1703+ // the multiTypeEsField is not casted to datetime directly.
17011704 if (((Expression ) convert ).dataType () == mtf .getDataType ()) {
17021705 return createIfDoesNotAlreadyExist (fa , mtf , unionFieldAttributes );
17031706 }
17041707
17051708 // Data type is different between implicit(date_nanos) and explicit casting, if the conversion is supported, create a
17061709 // new MultiTypeEsField with explicit casting type, and add it to unionFieldAttributes.
17071710 Set <DataType > supportedTypes = convert .supportedTypes ();
1708- if (supportedTypes .contains (fa .dataType ())) {
1711+ if (supportedTypes .contains (fa .dataType ()) && canConvertOriginalTypes ( mtf , supportedTypes ) ) {
17091712 // Build the mapping between index name and conversion expressions
17101713 Map <String , Expression > indexToConversionExpressions = new HashMap <>();
17111714 for (Map .Entry <String , Expression > entry : mtf .getIndexToConversionExpressions ().entrySet ()) {
@@ -1716,7 +1719,7 @@ private Expression resolveConvertFunction(ConvertFunction convert, List<FieldAtt
17161719 indexToConversionExpressions .put (indexName , newConvertFunction );
17171720 }
17181721 MultiTypeEsField multiTypeEsField = new MultiTypeEsField (
1719- fa .name (),
1722+ fa .fieldName (),
17201723 convertExpression .dataType (),
17211724 false ,
17221725 indexToConversionExpressions
@@ -1767,6 +1770,16 @@ private static MultiTypeEsField resolvedMultiTypeEsField(
17671770 return MultiTypeEsField .resolveFrom (imf , typesToConversionExpressions );
17681771 }
17691772
1773+ private static boolean canConvertOriginalTypes (MultiTypeEsField multiTypeEsField , Set <DataType > supportedTypes ) {
1774+ return multiTypeEsField .getIndexToConversionExpressions ()
1775+ .values ()
1776+ .stream ()
1777+ .allMatch (
1778+ e -> e instanceof AbstractConvertFunction convertFunction
1779+ && supportedTypes .contains (convertFunction .field ().dataType ().widenSmallNumeric ())
1780+ );
1781+ }
1782+
17701783 private static Expression typeSpecificConvert (ConvertFunction convert , Source source , DataType type , InvalidMappedField mtf ) {
17711784 EsField field = new EsField (mtf .getName (), type , mtf .getProperties (), mtf .isAggregatable ());
17721785 return typeSpecificConvert (convert , source , field );
0 commit comments