Skip to content

Commit 0cfd5ce

Browse files
[ESQL] Ensure date/date_nanos implicit casting rule behind snapshot (#130026) (#130481)
* put date date_nanos implicit casting rule behind snapshot
1 parent 74eb065 commit 0cfd5ce

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import static java.util.Collections.singletonList;
131131
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
132132
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;
133134
import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
134135
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
135136
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
@@ -168,7 +169,7 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
168169
new ResolveInference(),
169170
new ResolveLookupTables(),
170171
new ResolveFunctions(),
171-
new DateMillisToNanosInEsRelation()
172+
new DateMillisToNanosInEsRelation(IMPLICIT_CASTING_DATE_AND_DATE_NANOS.isEnabled())
172173
),
173174
new Batch<>(
174175
"Resolution",
@@ -1692,23 +1693,42 @@ private static LogicalPlan planWithoutSyntheticAttributes(LogicalPlan plan) {
16921693
* Cast the union typed fields in EsRelation to date_nanos if they are mixed date and date_nanos types.
16931694
*/
16941695
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+
16951703
@Override
16961704
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;
17081709
}
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+
});
17101728
});
1711-
});
1729+
} else {
1730+
return plan;
1731+
}
17121732
}
17131733
}
17141734

0 commit comments

Comments
 (0)