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

topn() fails on large number of NULLs #767

Open
SimonMTS opened this issue Aug 23, 2023 · 1 comment
Open

topn() fails on large number of NULLs #767

SimonMTS opened this issue Aug 23, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@SimonMTS
Copy link

Relevant system information:

  • OS: 6.1.41-1-MANJARO
  • PostgreSQL version: 15.4
  • TimescaleDB Toolkit version: 1.17.0
  • Installation method: docker (also happens on Timescale Cloud)

Describe the bug
Using topn() on a large number of NULL rows causes an Option::unwrap() error.
On a smaller number of rows it works as expected.

To Reproduce
Steps to reproduce the behavior:

  1. Start the image timescale/timescaledb-ha:pg15-all.
    (This is how I reproduced it, but it probably happens on all recent versions)
  2. Run the following SQL to setup the database:
    CREATE TABLE public.example (time TIMESTAMPTZ NOT NULL, string_value TEXT NULL);
    
    INSERT INTO public.example (time, string_value)
        SELECT time, NULL AS string_value
        FROM generate_series(now() - interval '5 day', now(), interval '1 second') AS g1(time);
    
  3. Run the following query to trigger the bug:
    SELECT topn(mcv_agg(1, string_value)) FROM public.example;
  4. See the error ERROR: called 'Option::unwrap()' on a 'None' value

Expected behavior
On smaller (NULL) datasets topn() results in:

 topn
------
(0 rows)

I expect this behaviour not to change depending on dataset size.

Actual behavior
On larger (NULL) datasets topn() results in:

ERROR:  called `Option::unwrap()` on a `None` value

Additional context
The bug can be reproduced on Timescale Cloud and locally in docker.

This bug was particularly pernicious because it only occurs on larger datasets.
So we didn't encounter it during testing.

Workaround
A Timescale Support agent quickly provided us with a workaround.
Replacing string_value with COALESCE(string_value,''::text)),
e.g. SELECT topn(mcv_agg(1, COALESCE(string_value,''::text))) FROM public.example;

Which changes the behaviour to return an empty string instead of no results.
But did solve our production issue.

@SimonMTS SimonMTS added the bug Something isn't working label Aug 23, 2023
@supergoudvis116

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants