|
130 | 130 | import static java.util.Collections.singletonList; |
131 | 131 | import static org.elasticsearch.common.logging.LoggerMessageFormat.format; |
132 | 132 | import static org.elasticsearch.xpack.core.enrich.EnrichPolicy.GEO_MATCH_TYPE; |
| 133 | +import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.IMPLICIT_CASTING_DATE_AND_DATE_NANOS; |
133 | 134 | import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN; |
134 | 135 | import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME; |
135 | 136 | import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS; |
@@ -168,7 +169,7 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon |
168 | 169 | new ResolveInference(), |
169 | 170 | new ResolveLookupTables(), |
170 | 171 | new ResolveFunctions(), |
171 | | - new DateMillisToNanosInEsRelation() |
| 172 | + new DateMillisToNanosInEsRelation(IMPLICIT_CASTING_DATE_AND_DATE_NANOS.isEnabled()) |
172 | 173 | ), |
173 | 174 | new Batch<>( |
174 | 175 | "Resolution", |
@@ -1692,23 +1693,42 @@ private static LogicalPlan planWithoutSyntheticAttributes(LogicalPlan plan) { |
1692 | 1693 | * Cast the union typed fields in EsRelation to date_nanos if they are mixed date and date_nanos types. |
1693 | 1694 | */ |
1694 | 1695 | private static class DateMillisToNanosInEsRelation extends Rule<LogicalPlan, LogicalPlan> { |
| 1696 | + |
| 1697 | + private final boolean isSnapshot; |
| 1698 | + |
| 1699 | + DateMillisToNanosInEsRelation(boolean isSnapshot) { |
| 1700 | + this.isSnapshot = isSnapshot; |
| 1701 | + } |
| 1702 | + |
1695 | 1703 | @Override |
1696 | 1704 | public LogicalPlan apply(LogicalPlan plan) { |
1697 | | - return plan.transformUp(EsRelation.class, relation -> { |
1698 | | - if (relation.indexMode() == IndexMode.LOOKUP) { |
1699 | | - return relation; |
1700 | | - } |
1701 | | - return relation.transformExpressionsUp(FieldAttribute.class, f -> { |
1702 | | - if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) { |
1703 | | - HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>(); |
1704 | | - var convert = new ToDateNanos(f.source(), f); |
1705 | | - imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions)); |
1706 | | - var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions); |
1707 | | - return new FieldAttribute(f.source(), f.parentName(), f.name(), resolvedField, f.nullable(), f.id(), f.synthetic()); |
| 1705 | + if (isSnapshot) { |
| 1706 | + return plan.transformUp(EsRelation.class, relation -> { |
| 1707 | + if (relation.indexMode() == IndexMode.LOOKUP) { |
| 1708 | + return relation; |
1708 | 1709 | } |
1709 | | - return f; |
| 1710 | + return relation.transformExpressionsUp(FieldAttribute.class, f -> { |
| 1711 | + if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) { |
| 1712 | + HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>(); |
| 1713 | + var convert = new ToDateNanos(f.source(), f); |
| 1714 | + imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions)); |
| 1715 | + var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions); |
| 1716 | + return new FieldAttribute( |
| 1717 | + f.source(), |
| 1718 | + f.parentName(), |
| 1719 | + f.name(), |
| 1720 | + resolvedField, |
| 1721 | + f.nullable(), |
| 1722 | + f.id(), |
| 1723 | + f.synthetic() |
| 1724 | + ); |
| 1725 | + } |
| 1726 | + return f; |
| 1727 | + }); |
1710 | 1728 | }); |
1711 | | - }); |
| 1729 | + } else { |
| 1730 | + return plan; |
| 1731 | + } |
1712 | 1732 | } |
1713 | 1733 | } |
1714 | 1734 |
|
|
0 commit comments