Skip to content
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

[BUG] when use "epoch_millis" format for date, got error when sorting in descending order #10253

Closed
erzhuerzhu opened this issue Sep 27, 2023 · 7 comments · Fixed by #12676
Closed
Assignees
Labels

Comments

@erzhuerzhu
Copy link

erzhuerzhu commented Sep 27, 2023

Describe the bug
get error:
{"type":"response_handler_failure_transport_exception","reason":"java.time.DateTimeException: Field EpochMillis cannot be printed as the value -9223372036854775808 cannot be negative according to the SignStyle","caused_by":{"type":"date_time_exception","reason":"Field EpochMillis cannot be printed as the value -9223372036854775808 cannot be negative according to the SignStyle"}}}]},"status":500}

To Reproduce

  1. create local and single opensearch server
docker pull opensearchproject/opensearch:2.7.0
docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" opensearchproject/opensearch:2.7.0
  1. create index with below setting
curl -ku 'admin:admin' -XPUT  'https://localhost:9200/test' -d '{"mappings":{"dynamic":"strict","properties":{"date1":{"type":"date","format":"epoch_millis"},"date2":{"type":"date","format":"yyyy-MM-dd"},"id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":255}}}}}}' -H 'Content-Type: application/json'

{
    "date1": {
        "type": "date",
        "format": "epoch_millis"
    },
    "date2": {
        "type": "date",
        "format": "yyyy-MM-dd"
    },
    "id": {
        "type": "text",
        "fields": {
            "keyword": {
                "type": "keyword",
                "ignore_above": 255
            }
        }
    }
}
  1. ingest data:
around 2000 documents, only 10 of date1 have value. all the other value of date1 are null
{
    'id': 2000,
    'date1': 1578712393000,
    'date2': '1979-01-11'
}

{
    'id': 2000,
    'date1': null,
    'date2': '1979-01-11'
}
  1. use below api to query
{
    "query": {
        "bool": {
            "must": {
                "exists": {
                    "field": "id"
                }
            }
        }
    },
    "highlight": {
        "fields": {}
    },
    "size": 100,
    "from": 0,
    "sort": [{
        "date1": {
            "missing": "_last",
            "order": "asc"
        }
    }, {
        "id.keyword": "desc"
    }]
}

no issue

when change the order into desc, get below error:


{
    "query": {
        "bool": {
            "must": {
                "exists": {
                    "field": "id"
                }
            }
        }
    },
    "highlight": {
        "fields": {}
    },
    "size": 100,
    "from": 0,
    "sort": [{
        "date1": {
            "missing": "_last",
            "order": "desc"
        }
    }, {
        "id.keyword": "desc"
    }]
}

{
    "error": {
        "root_cause": [{
            "type": "response_handler_failure_transport_exception",
            "reason": "java.time.DateTimeException: Field EpochMillis cannot be printed as the value -9223372036854775808 cannot be negative according to the SignStyle"
        }],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [{
            "shard": 0,
            "index": "sort1",
            "node": "YUWWdMlhSX2RvXLCyLRWuw",
            "reason": {
                "type": "response_handler_failure_transport_exception",
                "reason": "java.time.DateTimeException: Field EpochMillis cannot be printed as the value -9223372036854775808 cannot be negative according to the SignStyle",
                "caused_by": {
                    "type": "date_time_exception",
                    "reason": "Field EpochMillis cannot be printed as the value -9223372036854775808 cannot be negative according to the SignStyle"
                }
            }
        }]
    },
    "status": 500
}

Expected behavior

Plugins
no

Screenshots

Host/Environment (please complete the following information):

  • OS: docker on mac
  • Version :2.7.0

Additional context
releated issue :elastic/elasticsearch#52396 (comment)
if use yyyy-MM-dd format for field date1, I will get long overflow error for asc order for date1, so I changed it into epoch_millis format

tested in 2.10.0, still get same error

@msfroh
Copy link
Collaborator

msfroh commented Dec 6, 2023

This sounds like it's "just" a problem with the formatter. Instead of trying to format the epoch timestamp, we should detect the sentinel missing value (in this case, Long.MIN_VALUE), and format it differently -- either by excluding the field (since it's missing) or maybe we output it as "missing", or something.

@msfroh msfroh added good first issue Good for newcomers and removed untriaged labels Dec 6, 2023
@afn7081
Copy link

afn7081 commented Jan 8, 2024

can i take this up if not already taken?

@dblock
Copy link
Member

dblock commented Jan 9, 2024

@afn7081 please do!

@msfroh
Copy link
Collaborator

msfroh commented Feb 14, 2024

@afn7081 -- Are you still planning to work on this? Thanks!

@Slawutich
Copy link

Hello @afn7081

I also have this problem, is there any solution?

@msfroh
Copy link
Collaborator

msfroh commented Mar 14, 2024

I've been digging into this a bit. I believe the underlying problem is an overflow (underflow?) in this expression:

long instantSecondsInMillis = temporal.getLong(ChronoField.INSTANT_SECONDS) * 1_000;

@msfroh
Copy link
Collaborator

msfroh commented Mar 14, 2024

I managed to fix it by replacing that line with:

            long instantSeconds = temporal.getLong(ChronoField.INSTANT_SECONDS);
            if (instantSeconds < Long.MIN_VALUE / 1000L || instantSeconds > Long.MAX_VALUE / 1000L) {
                // Multiplying would yield integer overflow
                return Long.MAX_VALUE;
            }
            long instantSecondsInMillis = instantSeconds * 1_000;

msfroh added a commit to msfroh/OpenSearch that referenced this issue Mar 14, 2024
msfroh added a commit to msfroh/OpenSearch that referenced this issue Mar 14, 2024
andrross pushed a commit that referenced this issue Mar 15, 2024
…ld (#12676)

Fixes #10253

Signed-off-by: Michael Froh <froh@amazon.com>
opensearch-trigger-bot bot pushed a commit that referenced this issue Mar 15, 2024
…ld (#12676)

Fixes #10253

Signed-off-by: Michael Froh <froh@amazon.com>
(cherry picked from commit fcecd00)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
msfroh pushed a commit that referenced this issue Mar 15, 2024
…ld (#12676) (#12681)

Fixes #10253


(cherry picked from commit fcecd00)

Signed-off-by: Michael Froh <froh@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
rayshrey pushed a commit to rayshrey/OpenSearch that referenced this issue Mar 18, 2024
ruai0511 pushed a commit to ruai0511/OpenSearch that referenced this issue Mar 19, 2024
shiv0408 pushed a commit to Gaurav614/OpenSearch that referenced this issue Apr 25, 2024
…ld (opensearch-project#12676)

Fixes opensearch-project#10253

Signed-off-by: Michael Froh <froh@amazon.com>
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

6 participants