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

feat: Add json_array_length Spark function #11946

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

WangGuangxin
Copy link

json_array_length in Spark is similar as Presto but has difference in return type, Presto is int64_t, Spark is int32_t.

Examples:

SELECT json_array_length('[1,2,3,4]');  -> 4

Spark doc: https://spark.apache.org/docs/latest/api/sql/#json_array_length

@WangGuangxin WangGuangxin force-pushed the feat_json_array_length branch from a971121 to a5c0d3b Compare December 24, 2024 01:52
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 24, 2024
Copy link

netlify bot commented Dec 24, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit a971121
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/676a13f34f6f9b00089907c6

Copy link

netlify bot commented Dec 24, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit d787729
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/676b6f6fa0082800082e80dc

@WangGuangxin
Copy link
Author

@rui-mo @JkSelf @PHILO-HE Can you please help review this?

struct JsonArrayLengthFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE bool call(int32_t& len, const arg_type<Json>& json) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
Change const arg_type<Json>& to const arg_type<Varchar>& and remove the above corresponding header.

I think JsonType is just for presto.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

Copy link
Contributor

@PHILO-HE PHILO-HE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@@ -24,6 +25,8 @@ void registerJsonFunctions(const std::string& prefix) {
{prefix + "get_json_object"});
registerFunction<JsonObjectKeysFunction, Array<Varchar>, Varchar>(
{prefix + "json_object_keys"});
registerFunction<JsonArrayLengthFunction, int32_t, Varchar>(
{prefix + "json_array_length"});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid duplicating code, we would recommend extracting the implementation to the 'velox/functions/lib/' and registering it with corresponding return types for Presto and Spark if the semantics are the same (corner cases may need to be verified).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rui-mo The input type is a bit different, one is Json, another is Varchar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rui-mo removed duplicated code. Please take a look again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants