Skip to content

[Feature Request]: Implement UNHEX() to replace DECODE("xxx", "hex") #15261

Closed

Description

@arjunsk

Is there an existing issue for the same feature request?

  • I have checked the existing issues.

Is your feature request related to a problem?

We had previously implemented the HEX() function #4773. However, we did not implement the UNHEX() function. When we are refactoring #15027 to its MYSQL API, we need to replace the existing decode("xx", "hex") with UNHEX("xx") for Vector Binary Insert.

Describe the feature you'd like

Implement UNHEX function from this spec: https://www.w3resource.com/mysql/string-functions/mysql-unhex-function.php

Describe the implementation you've considered

No response

Documentation, Adoption, Use Case, Migration Strategy

No response

Additional information

No response

Activity

  1. self-assigned this
    on Apr 1, 2024
  2. added
    priority/p1
    good first issue
    on Apr 1, 2024
  3. xxxuuu commented on Apr 8, 2024

    @xxxuuu
    Contributor

    Hi @arjunsk, have you already been working on this issue? I'd like to give it a try.

  4. assigned and unassigned on Apr 8, 2024
  5. arjunsk commented on Apr 8, 2024

    @arjunsk
    ContributorAuthor

    Thank you @xxxuuu .

    The logic is mostly implemented here

    case "HEX":
    buf := make([]byte, hex.DecodedLen(len(functionUtil.QuickBytesToStr(data))))
    _, err = hex.Decode(buf, data)
    if err != nil {
    return nil, err
    }

    You just need to define a new function_id for the UNHEX and port this logic there.

    The return type is BLOB (because this function will be used for inserting VECTOR values as BLOB)

    	// function `decode`
    	{
    		functionId: DECODE,
    		class:      plan.Function_STRICT,
    		layout:     STANDARD_FUNCTION,
    		checkFn:    fixedTypeMatch,
    
    		Overloads: []overload{
    			{
    				overloadId: 0,
    				args:       []types.T{types.T_varchar, types.T_varchar},
    				retType: func(parameters []types.Type) types.Type {
    					return types.T_blob.ToType()
    				},
    				newOp: func() executeLogicOfOverload {
    					return Decode
    				},
    			},
    		},
    	},
  6. xxxuuu commented on Apr 9, 2024

    @xxxuuu
    Contributor

    Thank you @xxxuuu .

    The logic is mostly implemented here

    case "HEX":
    buf := make([]byte, hex.DecodedLen(len(functionUtil.QuickBytesToStr(data))))
    _, err = hex.Decode(buf, data)
    if err != nil {
    return nil, err
    }

    You just need to define a new function_id for the UNHEX and port this logic there.

    The return type is BLOB (because this function will be used for inserting VECTOR values as BLOB)

    	// function `decode`
    	{
    		functionId: DECODE,
    		class:      plan.Function_STRICT,
    		layout:     STANDARD_FUNCTION,
    		checkFn:    fixedTypeMatch,
    
    		Overloads: []overload{
    			{
    				overloadId: 0,
    				args:       []types.T{types.T_varchar, types.T_varchar},
    				retType: func(parameters []types.Type) types.Type {
    					return types.T_blob.ToType()
    				},
    				newOp: func() executeLogicOfOverload {
    					return Decode
    				},
    			},
    		},
    	},

    Hi @arjunsk, I have basically implemented UNHEX, but there is still one thing that is unclear to me: how do I return NULL? Just like in this example from the documentation:

    mysql> SELECT UNHEX('www');
    +--------------+
    | UNHEX('www') |
    +--------------+
    | NULL         |
    +--------------+
    1 row in set (0.00 sec)
    

    I return nil in opUnaryBytesToBytes, but in the console, I only get an empty result:

    mysql> SELECT UNHEX('www');
    +------------+
    | unhex(www) |
    +------------+
    |            |
    +------------+
    1 row in set (0.00 sec)
    
  7. arjunsk commented on Apr 9, 2024

    @arjunsk
    ContributorAuthor

    Hi @xxxuuu , this is a quick example of how NULL is returned (without using opUnaryBytesToBytes)

    if err := to.Append(result, true); err != nil {
    return err

    image

  8. added
    attention/doc-influence
    on Apr 11, 2024
  9. assigned and unassigned on Apr 11, 2024
  10. arjunsk commented on Apr 11, 2024

    @arjunsk
    ContributorAuthor

    Thank you for your contribution @xxxuuu

  11. arjunsk commented on Apr 11, 2024

    @arjunsk
    ContributorAuthor

    Pending changes

    1. Update BVT to use UNHEX instead of DECODE("...", "HEX") -- done
    2. Update Decode("...", "hex") to use UNHEX in the VECTOR documentation -- done
    3. Run benchmark using UNHEX (330 seconds to insert SIFT128) -- done
    4. Add UNHEX documentation -- done
  12. arjunsk commented on Apr 12, 2024

    @arjunsk
    ContributorAuthor

    This issue is ready for testing.

  13. added this to the 1.2.1 milestone on May 27, 2024
  14. Ariznawlll commented on Jun 6, 2024

    @Ariznawlll
    Collaborator

    hex() and unhex() test done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

attention/doc-influencegood first issuekind/featurepriority/p1

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions