Skip to content

Commit e32af46

Browse files
[ESQL] Ensure date/date_nanos implicit casting rule behind snapshot (#130026) (#130482)
* put date date_nanos implicit casting rule behind snapshot
1 parent ae450be commit e32af46

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

docs/changelog/127797.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Analyzer.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import static java.util.Collections.emptyList;
135135
import static java.util.Collections.singletonList;
136136
import static org.elasticsearch.xpack.core.enrich.EnrichPolicy.GEO_MATCH_TYPE;
137+
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.IMPLICIT_CASTING_DATE_AND_DATE_NANOS;
137138
import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
138139
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
139140
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
@@ -175,7 +176,7 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
175176
new ResolveInference(),
176177
new ResolveLookupTables(),
177178
new ResolveFunctions(),
178-
new DateMillisToNanosInEsRelation()
179+
new DateMillisToNanosInEsRelation(IMPLICIT_CASTING_DATE_AND_DATE_NANOS.isEnabled())
179180
),
180181
new Batch<>(
181182
"Resolution",
@@ -1819,10 +1820,6 @@ private static boolean canConvertOriginalTypes(MultiTypeEsField multiTypeEsField
18191820

18201821
private static Expression typeSpecificConvert(ConvertFunction convert, Source source, DataType type, InvalidMappedField mtf) {
18211822
EsField field = new EsField(mtf.getName(), type, mtf.getProperties(), mtf.isAggregatable());
1822-
return typeSpecificConvert(convert, source, field);
1823-
}
1824-
1825-
private static Expression typeSpecificConvert(ConvertFunction convert, Source source, EsField field) {
18261823
FieldAttribute originalFieldAttr = (FieldAttribute) convert.field();
18271824
FieldAttribute resolvedAttr = new FieldAttribute(
18281825
source,
@@ -1902,23 +1899,42 @@ private static LogicalPlan planWithoutSyntheticAttributes(LogicalPlan plan) {
19021899
* Cast the union typed fields in EsRelation to date_nanos if they are mixed date and date_nanos types.
19031900
*/
19041901
private static class DateMillisToNanosInEsRelation extends Rule<LogicalPlan, LogicalPlan> {
1902+
1903+
private final boolean isSnapshot;
1904+
1905+
DateMillisToNanosInEsRelation(boolean isSnapshot) {
1906+
this.isSnapshot = isSnapshot;
1907+
}
1908+
19051909
@Override
19061910
public LogicalPlan apply(LogicalPlan plan) {
1907-
return plan.transformUp(EsRelation.class, relation -> {
1908-
if (relation.indexMode() == IndexMode.LOOKUP) {
1909-
return relation;
1910-
}
1911-
return relation.transformExpressionsUp(FieldAttribute.class, f -> {
1912-
if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) {
1913-
HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>();
1914-
var convert = new ToDateNanos(f.source(), f);
1915-
imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions));
1916-
var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions);
1917-
return new FieldAttribute(f.source(), f.parentName(), f.name(), resolvedField, f.nullable(), f.id(), f.synthetic());
1911+
if (isSnapshot) {
1912+
return plan.transformUp(EsRelation.class, relation -> {
1913+
if (relation.indexMode() == IndexMode.LOOKUP) {
1914+
return relation;
19181915
}
1919-
return f;
1916+
return relation.transformExpressionsUp(FieldAttribute.class, f -> {
1917+
if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) {
1918+
HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>();
1919+
var convert = new ToDateNanos(f.source(), f);
1920+
imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions));
1921+
var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions);
1922+
return new FieldAttribute(
1923+
f.source(),
1924+
f.parentName(),
1925+
f.name(),
1926+
resolvedField,
1927+
f.nullable(),
1928+
f.id(),
1929+
f.synthetic()
1930+
);
1931+
}
1932+
return f;
1933+
});
19201934
});
1921-
});
1935+
} else {
1936+
return plan;
1937+
}
19221938
}
19231939
}
19241940

0 commit comments

Comments
 (0)