Skip to content

Commit

Permalink
[BugFix]: FROM_BASE64 return NULL if throws error (matrixorigin#15924)
Browse files Browse the repository at this point in the history
Making FROM_BASE64 Mysql complaint.

Approved by: @heni02, @m-schen
  • Loading branch information
arjunsk authored May 9, 2024
1 parent 341c2b6 commit ca3d370
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
21 changes: 16 additions & 5 deletions pkg/sql/plan/function/func_unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,26 @@ func ToBase64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc
})
}

func FromBase64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) {
return opUnaryBytesToBytesWithErrorCheck(ivecs, result, proc, length, func(data []byte) ([]byte, error) {
func FromBase64(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
source := vector.GenerateFunctionStrParameter(parameters[0])
rs := vector.MustFunctionResult[types.Varlena](result)

rowCount := uint64(length)
for i := uint64(0); i < rowCount; i++ {
data, null := source.GetStrValue(i)
if null {
return rs.AppendMustNullForBytesResult()
}

buf := make([]byte, base64.StdEncoding.DecodedLen(len(functionUtil.QuickBytesToStr(data))))
_, err := base64.StdEncoding.Decode(buf, data)
if err != nil {
return nil, err
return rs.AppendMustNullForBytesResult()
}
return buf, nil
})
_ = rs.AppendMustBytesValue(buf)
}

return nil
}

func Length(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
Expand Down
Binary file modified test/distributed/cases/function/func_encode_decode.result
Binary file not shown.
8 changes: 4 additions & 4 deletions test/distributed/cases/function/func_encode_decode.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ select to_base64(-123);
select to_base64(2003-09-06);
select to_base64('2003-09-06');
select to_base64('数据库');
-- @bvt:issue#15904

select from_base64(''),from_base64(NULL);
select from_base64('@#%#$^jfe12');
-- @bvt:issue

select from_base64(123dokgr);
-- @bvt:issue#15904

select from_base64(-123);
-- @bvt:issue

select from_base64(2003-09-06);
select from_base64('5pWw5o2u5bqT');
select from_base64('MjAwMy0wOS0wNg==');
Expand Down

0 comments on commit ca3d370

Please sign in to comment.