Skip to content

ESQL: ToString/ToDatetime/ToDateNanos converters timezone support#138985

Merged
ivancea merged 37 commits intoelastic:mainfrom
ivancea:esql-tostring-timezone
Jan 16, 2026
Merged

ESQL: ToString/ToDatetime/ToDateNanos converters timezone support#138985
ivancea merged 37 commits intoelastic:mainfrom
ivancea:esql-tostring-timezone

Conversation

@ivancea
Copy link
Copy Markdown
Contributor

@ivancea ivancea commented Dec 3, 2025

Add timezone support to TO_STRING, TO_DATETIME and TO_DATENANOS.

As they all are conversion functions, the change was also applied to the implicit casting rule, and some cases in the parser where a TO_STRING is needed.

@ivancea ivancea added >feature Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) :Analytics/ES|QL AKA ESQL v9.3.0 labels Dec 3, 2025
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @ivancea, I've created a changelog YAML for you.

@@ -7,9 +7,7 @@

package org.elasticsearch.xpack.esql.core.type;

import org.elasticsearch.common.io.stream.NamedWriteable;

public interface Converter extends NamedWriteable {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never written/read or even registered

/**
* Converters that don't require a Configuration.
*/
private static final Map<DataType, BiFunction<Source, Expression, AbstractConvertFunction>> TYPE_TO_CONVERTER_FUNCTION = Map.ofEntries(
Copy link
Copy Markdown
Contributor Author

@ivancea ivancea Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I separated the conversion functions in 2 blocks:

  1. Simple converters that need no configuration
  2. Converters for functions that need the config. In the parser there's no config available, so we pass ConfiguratioNAware.CONFIGURATION_MARKER

@@ -241,56 +265,62 @@ public enum INTERVALS {
public static final String INVALID_INTERVAL_ERROR =
"Invalid interval value in [{}], expected integer followed by one of {} but got [{}]";

public static Converter converterFor(DataType from, DataType to) {
public static Converter converterFor(DataType from, DataType to, Configuration configuration) {
Copy link
Copy Markdown
Contributor Author

@ivancea ivancea Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I directly moved the EsqlConverter enum lambdas here, as to reduce the extra indirection, and removed the enum. Current callers now directly use the functions, which are correctly typed (No cast needed).

I considered removing this, and delegating conversion to the conversion functions directly, with folding, either manually here or through the planner. But that looked like a bigger change, and I'm not sure yet

# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToString.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringTests.java
@ivancea ivancea marked this pull request as draft December 15, 2025 12:22
@alex-spies alex-spies removed their request for review December 16, 2025 11:14
# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToDateNanos.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToString.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/type/EsqlDataTypeConverter.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringTests.java
# Conflicts:
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java
@ivancea ivancea marked this pull request as ready for review January 15, 2026 13:43
Copy link
Copy Markdown
Member

@luigidellaquila luigidellaquila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ivancea, LGTM

I just left a couple of minor comments

*/

// Evaluators dynamically created in #factories()
Map.entry(KEYWORD, (source, fieldEval) -> null),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly, but I guess it does the job (for supportedTypes())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I didn't find a simpler way, without refactoring the factories() method requiring a map like that.
I could remove those from the map and just add them dynamically, but it would make harder to know what the function actually supports

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as it is. This list was not intended to be dynamic, and having a all the keys defined ahead of time is a reasonable advantage. The comment is clear enough IMHO

@ivancea ivancea merged commit 56a4bb5 into elastic:main Jan 16, 2026
35 checks passed
@ivancea ivancea deleted the esql-tostring-timezone branch January 16, 2026 13:06
ivancea added a commit that referenced this pull request Jan 16, 2026
Fix release tests on ToStringTests for DateRange introduced on #138985

DateRange is snapshot-only for now, so those tests fail on release mode.
spinscale pushed a commit to spinscale/elasticsearch that referenced this pull request Jan 21, 2026
…astic#138985)

Add timezone support to TO_STRING, TO_DATETIME and TO_DATENANOS.

As they all are conversion functions, the change was also applied to the implicit casting rule, and some cases in the parser where a TO_STRING is needed.
spinscale pushed a commit to spinscale/elasticsearch that referenced this pull request Jan 21, 2026
…40837)

Fix release tests on ToStringTests for DateRange introduced on elastic#138985

DateRange is snapshot-only for now, so those tests fail on release mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >feature Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants