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

Working with Histrogram from C API #2876

Closed
sdmcallister opened this issue Jan 6, 2022 · 9 comments
Closed

Working with Histrogram from C API #2876

sdmcallister opened this issue Jan 6, 2022 · 9 comments
Labels

Comments

@sdmcallister
Copy link

So far I would do: "SELECT histogram(i)::VARCHAR FROM integers". Then I have to parse the resulting string separately. The JSON library I use though expects a quoted key like : {"42": 42} so I need to parse it using other means. Is there a better way I'm missing? Would be nice if the histogram function also returned as regular table of two columns.

@Alex-Monahan
Copy link
Contributor

Hello! Have a look at using the Unnest function instead of casting to varchar. That should create a table like you are looking for!
Unnest Docs

@sdmcallister
Copy link
Author

Hm, would you be able to provide an example?

create table integers(i integer)
INSERT INTO integers VALUES (1),(1),(3),(20),(20),(20)
select histogram(i) FROM integers; 
select unnest(histogram(i)) from integers # expects a list but hist returns map. Not sure how to fully unpack without knowing the keys in advance.

@wangfenjin
Copy link
Contributor

The problem is Map data type is not supported in C API

@taniabogatsch
Copy link
Contributor

Hey, maybe I am misunderstanding something here, but you need the first column as the key and the second column as the count?

Would be nice if the histogram function also returned as regular table of two columns.

This would be possible without using the histogram function at all.

CREATE TABLE integers (i integer);
INSERT INTO integers VALUES (1), (1), (3), (20), (20), (20);
SELECT i, COUNT(i) FROM integers GROUP BY I;

┌────┬──────────┐
│ i  │ count(i) │
├────┼──────────┤
│ 12        │
│ 31        │
│ 203        │
└────┴──────────┘

If you need the results ordered as in the histogram function, you can add an ORDER BY i behind the GROUP BY i.

Then, you can maybe use the JSON extension to get a JSON. I don't know how that works with the C API though.

SELECT json_group_object(k, v) FROM example;
-- {"duck":42,"goose":7}

Maybe this helps. :)

@github-actions
Copy link

github-actions bot commented Aug 1, 2023

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the stale label Aug 1, 2023
@StLeoX
Copy link

StLeoX commented Aug 3, 2023

D select unnest(histogram(i)) FROM integers; 
Error: Binder Error: UNNEST() can only be applied to lists, structs and NULL

@github-actions github-actions bot removed the stale label Aug 4, 2023
@taniabogatsch
Copy link
Contributor

Hi @StLeoX. We do not support histogram inside UNNEST, so this is the intended behavior.

Copy link

github-actions bot commented Nov 3, 2023

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

Copy link

github-actions bot commented Dec 3, 2023

This issue was closed because it has been stale for 30 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants