You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Queries with date literals fail if date format does not includeepoch_millis #1847
What is the bug?
Timestamp literals are always converted to epoch_millis in OpenSearch DSL. This results in OpenSearch exception if the field does not support epoch_millis
How can one reproduce the bug?
Create an index with this mapping:
POST /date-test/_doc
{
"dateField": "2000-03-04"
}
POST /date-test/_doc
{
"dateField": "1999-01-02"
}
Execute SQL query:
POST /_plugins/sql
{
"query": "select * from date-test where dateField = date \"2002-03-04\""
}
Results in OpenSearch error:
{
"error": {
"details": "Shard[0]: [date-test/Yd43agSsSlqDvbN9XM13tw] QueryShardException[failed to create query: failed to parse date field [1015200000000] with format [strict_date_optional_time]: [failed to parse date field [1015200000000] with format [strict_date_optional_time]]]; nested: OpenSearchParseException[failed to parse date field [1015200000000] with format [strict_date_optional_time]: [failed to parse date field [1015200000000] with format [strict_date_optional_time]]]; nested: IllegalArgumentException[failed to parse date field [1015200000000] with format [strict_date_optional_time]]; nested: DateTimeParseException[Text '1015200000000' could not be parsed at index 0];\n\nFor more details, please send request for Json format to see the raw response from OpenSearch engine.",
"reason": "Error occurred in OpenSearch engine: all shards failed",
"type": "SearchPhaseExecutionException"
},
"status": 400
}
What is the expected behavior?
Query work to return one row.
Additional context
The query executes successfully if format for dateField includes epoch_millis.
AWS currently offers only 2.5 OpenSearch version. SQL plugin there interpret all date fields in TIMESTAMP SQL type. WHERE clause has a function which compares a TIMESTAMP with DATE provided by DATE literal, so cast_to_timestamp function is added to the right side of expression to provide values of the same type for comparison.
The filter expression was like
Hereby cast_to_timestamp function provides an ExprTimestampValue backed by Instant which is converted for epoch millis for query DSL. Epoch millis (a number) isn't comparable with any string, since query failed.
2.8
2.8 behaves the same way as 2.5 in that case. It has improvements for named formats, but custom formats are still not supported.
2.9
#1821 brings support of custom formats, so date field with format "yyyy-mm-dd" is interpreted as TIMESTAMPbecause there is a typo. mm (lowercase) stands for minutes, so this format refers date and time parts both. SQL plugin still adds cast_to_timestamp to the filter. Once format fixed to "yyyy-mm-dd" (MM in uppercase stands for months), SQL plugin detects that this field is in DATE format and nothing added to DSL (explain response):
While my example was problematic, users can still run into this bug if the field has a custom format that can include time. For example, with format = "strict_date_optional_time" users will get the same error. I'll update the ticket text.
strict_date_optional_time is also a format for TIMESTAMP values, so cast_to_timestamp is added. OpenSearchDateType (:opensearch module) has format information, but then it is simplified to ExprCoreType (:core module). Analyzer (:core module) builds TypeEnvironment with simplified data types only, because they are used in DSL. ExprCoreType doesn't store type specific information like format.
Later, TermQuery (:opensearch module) builds a DSL query to OpenSearch using given formats. Unfortunately, it has no access to extended formats retrieved in the same module.
if (literal.type().equals(ExprCoreType.TIMESTAMP)) {
returnliteral.timestampValue().toEpochMilli();
Workaround:
Add epoch_millis format to datetime fields.
Use date formats for date fields and time for time.
Fix:
Update ExpType interface and make all complex types (all OpenSearchDataType, including OpenSearchDateType) are passed through :core module. Changes in FunctionDSL are also needed to use simplified types. Full types would become available in :opensearch when it is needed to build a DSL query.
Activity
Yury-Fridlyand commentedon Jul 11, 2023
2.5
AWS currently offers only 2.5 OpenSearch version. SQL plugin there interpret all
datefields inTIMESTAMPSQL type.WHEREclause has a function which compares aTIMESTAMPwithDATEprovided byDATEliteral, socast_to_timestampfunction is added to the right side of expression to provide values of the same type for comparison.The
filterexpression was likeHereby
cast_to_timestampfunction provides anExprTimestampValuebacked byInstantwhich is converted for epoch millis for query DSL. Epoch millis (a number) isn't comparable with any string, since query failed.2.8
2.8 behaves the same way as 2.5 in that case. It has improvements for named formats, but custom formats are still not supported.
2.9
#1821 brings support of custom formats, so
datefield with format"yyyy-mm-dd"is interpreted asTIMESTAMPbecause there is a typo.mm(lowercase) stands for minutes, so this format refers date and time parts both. SQL plugin still addscast_to_timestampto the filter. Once format fixed to"yyyy-mm-dd"(MMin uppercase stands for months), SQL plugin detects that this field is inDATEformat and nothing added to DSL (explain response):{ "from": 0, "size": 200, "timeout": "1m", "query": { "term": { "dateField": { "value": "2002-03-04", "boost": 1 } } }, "_source": { "includes": [ "dateField" ], "excludes": [] }, "sort": [ { "_doc": { "order": "asc" } } ] }MaxKsyunz commentedon Jul 11, 2023
D'oh! Thank you for looking into this @Yury-Fridlyand.
While my example was problematic, users can still run into this bug if the field has a custom format that can include time. For example, with
format = "strict_date_optional_time"users will get the same error. I'll update the ticket text.Yury-Fridlyand commentedon Jul 11, 2023
strict_date_optional_timeis also a format forTIMESTAMPvalues, socast_to_timestampis added.OpenSearchDateType(:opensearchmodule) has format information, but then it is simplified toExprCoreType(:coremodule).Analyzer(:coremodule) buildsTypeEnvironmentwith simplified data types only, because they are used inDSL.ExprCoreTypedoesn't store type specific information likeformat.Later,
TermQuery(:opensearchmodule) builds a DSL query to OpenSearch using given formats. Unfortunately, it has no access to extended formats retrieved in the same module.sql/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/TermQuery.java
Lines 28 to 29 in f5031a7
Workaround:
epoch_millisformat to datetime fields.Fix:
Update
ExpTypeinterface and make all complex types (allOpenSearchDataType, includingOpenSearchDateType) are passed through:coremodule. Changes inFunctionDSLare also needed to use simplified types. Full types would become available in:opensearchwhen it is needed to build a DSL query.