Skip to content

refactor(query): Enhance OBJECT_INSERT and Rename JSON Functions #18199

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

Merged
merged 2 commits into from
Jun 20, 2025

Conversation

b41sh
Copy link
Member

@b41sh b41sh commented Jun 19, 2025

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

This PR introduces two key changes to enhance Databend's compatibility with Snowflake, specifically focusing on JSON/OBJECT manipulation functions:

1. OBJECT_INSERT Enhancement:

  • Behavior Modification: The OBJECT_INSERT function is modified to align with Snowflake's behavior. When the value argument is NULL and the update_flag argument is set to TRUE, the function will now delete the corresponding key from the object, rather than keep the orignal key. The new key can support any types as well as string type, and the key will be auto converted to string.

    • Example:

      -- Before (current behavior):  Would ignore the key with a NULL value and keep the original key and value.
      SELECT OBJECT_INSERT(parse_json('{"a": 1, "b": 2}'), 'b', NULL, TRUE);
      -- Expected result:  {"a": 1, "b": 2}
      
      -- After (new behavior):  Deletes the key 'b'
      SELECT OBJECT_INSERT(parse_json('{"a": 1, "b": 2}'), 'b', NULL, TRUE);
      -- Expected result:  {"a": 1}
      
      -- Before (current behavior): Report an error if the new key is not string.
      SELECT JSON_OBJECT_INSERT(parse_json('{"a": 1, "b": 2}'), 100, 'test', TRUE);
      -- Report an error: no function matches signature `json_object_insert(Variant, UInt8, String, Boolean)`, you might need to add explicit type casts.
      
      -- After (new behavior): Allow new key auto cast from int to string.
      SELECT OBJECT_INSERT(parse_json('{"a": 1, "b": 2}'), 100, 'test', TRUE);
      -- Expected result: {"100":"test","a":1,"b":2}

2. Function Renaming for Snowflake Compatibility:

  • Function Renames: The following JSON-related functions have been renamed to align with Snowflake's naming conventions:

    Original Databend Name New Name (Snowflake Compatible)
    json_array array_construct
    json_array_distinct array_distinct
    json_array_except array_except
    json_array_insert array_insert
    json_array_intersection array_intersection
    json_array_overlap array_overlap
    json_object_insert object_insert
    json_object_delete object_delete
    json_object_pick object_pick
    json_object object_construct
    try_json_object try_object_construct
    json_object_keep_null object_construct_keep_null
    try_json_object_keep_null try_object_construct_keep_null
  • Aliases: To ensure backward compatibility, the original function names are retained as aliases. Users can continue to use the old names without any code changes. The new names are preferred for new development.

    • Rationale: This renaming improves Databend's compatibility with Snowflake, making it easier to migrate SQL code and reducing the learning curve for users familiar with Snowflake. The aliases provide a smooth transition for existing Databend users.
  • fixes: #[Link the issue here]

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@b41sh b41sh requested review from sundy-li and BohuTANG June 19, 2025 14:31
@b41sh b41sh changed the title Feature: Enhance OBJECT_INSERT and Rename JSON Functions refactor(query): Enhance OBJECT_INSERT and Rename JSON Functions Jun 19, 2025
@github-actions github-actions bot added the pr-refactor this PR changes the code base without new features or bugfix label Jun 19, 2025
@b41sh b41sh force-pushed the fix-object-insert branch from 8eed740 to 31ddecc Compare June 19, 2025 15:09
@b41sh b41sh force-pushed the fix-object-insert branch from 38f13b8 to a7af92e Compare June 20, 2025 02:42
@b41sh b41sh merged commit 874e869 into databendlabs:main Jun 20, 2025
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-refactor this PR changes the code base without new features or bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants