Skip to content

[8.19] [ESQL] Ensure date/date_nanos implicit casting rule behind snapshot (#130026) #130481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions docs/changelog/127797.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
import static java.util.Collections.singletonList;
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
import static org.elasticsearch.xpack.core.enrich.EnrichPolicy.GEO_MATCH_TYPE;
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.IMPLICIT_CASTING_DATE_AND_DATE_NANOS;
import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
Expand Down Expand Up @@ -168,7 +169,7 @@ public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerCon
new ResolveInference(),
new ResolveLookupTables(),
new ResolveFunctions(),
new DateMillisToNanosInEsRelation()
new DateMillisToNanosInEsRelation(IMPLICIT_CASTING_DATE_AND_DATE_NANOS.isEnabled())
),
new Batch<>(
"Resolution",
Expand Down Expand Up @@ -1692,23 +1693,42 @@ private static LogicalPlan planWithoutSyntheticAttributes(LogicalPlan plan) {
* Cast the union typed fields in EsRelation to date_nanos if they are mixed date and date_nanos types.
*/
private static class DateMillisToNanosInEsRelation extends Rule<LogicalPlan, LogicalPlan> {

private final boolean isSnapshot;

DateMillisToNanosInEsRelation(boolean isSnapshot) {
this.isSnapshot = isSnapshot;
}

@Override
public LogicalPlan apply(LogicalPlan plan) {
return plan.transformUp(EsRelation.class, relation -> {
if (relation.indexMode() == IndexMode.LOOKUP) {
return relation;
}
return relation.transformExpressionsUp(FieldAttribute.class, f -> {
if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) {
HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>();
var convert = new ToDateNanos(f.source(), f);
imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions));
var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions);
return new FieldAttribute(f.source(), f.parentName(), f.name(), resolvedField, f.nullable(), f.id(), f.synthetic());
if (isSnapshot) {
return plan.transformUp(EsRelation.class, relation -> {
if (relation.indexMode() == IndexMode.LOOKUP) {
return relation;
}
return f;
return relation.transformExpressionsUp(FieldAttribute.class, f -> {
if (f.field() instanceof InvalidMappedField imf && imf.types().stream().allMatch(DataType::isDate)) {
HashMap<ResolveUnionTypes.TypeResolutionKey, Expression> typeResolutions = new HashMap<>();
var convert = new ToDateNanos(f.source(), f);
imf.types().forEach(type -> typeResolutions(f, convert, type, imf, typeResolutions));
var resolvedField = ResolveUnionTypes.resolvedMultiTypeEsField(f, typeResolutions);
return new FieldAttribute(
f.source(),
f.parentName(),
f.name(),
resolvedField,
f.nullable(),
f.id(),
f.synthetic()
);
}
return f;
});
});
});
} else {
return plan;
}
}
}

Expand Down