-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[#29816]: Add CAST to list of supported ClickHouse functions #45876
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
base: master
Are you sure you want to change the base?
[#29816]: Add CAST to list of supported ClickHouse functions #45876
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 3 comments
|
@sakce or @orian Whenever you've time, can you give me a review. First time doing any work on PostHog, please let me know if their is anything you want different to match posthog style. I did review this before writing the pr- https://posthog.com/handbook/engineering/conventions/backend-coding |
|
Pinginfg @Gilbert09 since I considered doing this in the past and was told this was possibly too dangerous |
I was taking another look through the code, I do see y'all
I guess the risk with |
|
Going to update the code to instead of CAST(x AS JSON), add explicit functions: "castToJSON": HogQLFunctionMeta("CAST", 1, 1, suffix_args=[ast.Constant(value="JSON")]),
"castToString": HogQLFunctionMeta("CAST", 1, 1, suffix_args=[ast.Constant(value="String")]),Then a user would write: castToJSON(properties) instead of CAST(properties AS JSON) |
e40ce21 to
e126c1c
Compare
e126c1c to
ba13a06
Compare
|
@matt-metivier Appreciate the contribution! I don’t have full ClickHouse context here (Tom will have more insight), but language-wise you’ll also need to update the c++ parser. While we keep the parsers in sync, in practice we currently really only use the c++ one. That said, we’re in the middle of transitioning to a new, language-agnostic parser that outputs JSON instead of python objects. Because of that, there’s no need to update the legacy c++ parser, just the new JSON one. |
ba13a06 to
61e88fd
Compare
Didn't realize this, thank you for mentioning it. I updated it and I opened a pr to update the docs in case we decide to go with this path pending @Gilbert09 approval. Flow:
|
Related to PostHog/posthog#45876 which adds the toJSON function for converting values to the ClickHouse JSON type.
Related to PostHog/posthog#45876 which adds the toJSON function for converting values to the ClickHouse JSON type.
- Added toJSON to clickhouse-functions.mdx type conversions list - Added toJSON to data-access.mdx type conversion functions list - Added toJSON example in useful-functions.mdx JSON section Related to PostHog/posthog#45876
Problem
Users cannot use CAST(column AS Type) syntax in HogQL. The immediate need is casting String columns to JSON type for the Query API. We intentionally avoid exposing raw CAST syntax to keep type strings non-user-controlled.
Flow:
Closes #29816
Changes
toJSON(x)HogQL function that printsCAST(x AS JSON)in ClickHousetoJSON()outputsCAST(... AS JSON)How did you test this code?
Added unit tests in:
posthog/hogql/test/_test_parser.py- parser test for CAST rejectionposthog/hogql/printer/test/test_printer.py- printer test fortoJSON()outputPublish to changelog?
Yes
Notes
toJSON(properties)instead ofCAST(properties AS JSON).contents/docs/sql/clickhouse-functions.mdx-https://github.com/PostHog/posthog.com/pull/14549/changes