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

[trim] trim all whitespace by default, not just spaces #47

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions internal/function_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,15 +1148,10 @@ func bindLtrim(args ...Value) (Value, error) {
if existsNull(args) {
return nil, nil
}
cutset := " "
if len(args) == 2 {
v, err := args[1].ToString()
if err != nil {
return nil, err
}
cutset = v
return LTRIM(args[0], args[1])
}
return LTRIM(args[0], cutset)
return LTRIM(args[0], nil)
}

func bindNormalize(args ...Value) (Value, error) {
Expand Down Expand Up @@ -1348,18 +1343,16 @@ func bindRpad(args ...Value) (Value, error) {
}

func bindRtrim(args ...Value) (Value, error) {
if len(args) != 1 && len(args) != 2 {
return nil, fmt.Errorf("RTRIM: invalid argument num %d", len(args))
}
if existsNull(args) {
return nil, nil
}
var cutset string = " "
if len(args) > 1 {
v, err := args[1].ToString()
if err != nil {
return nil, err
}
cutset = v
if len(args) == 2 {
return RTRIM(args[0], args[1])
}
return RTRIM(args[0], cutset)
return RTRIM(args[0], nil)
}

func bindSafeConvertBytesToString(args ...Value) (Value, error) {
Expand Down
68 changes: 53 additions & 15 deletions internal/function_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,20 +454,36 @@ func LOWER(v Value) (Value, error) {
return nil, fmt.Errorf("LOWER: value type is must be STRING or BYTES type")
}

func LTRIM(v Value, cutset string) (Value, error) {
func LTRIM(v Value, cutsetV Value) (Value, error) {
var cutset string
if cutsetV != nil {
b, err := cutsetV.ToBytes()
if err != nil {
return nil, err
}
cutset = string(b)
}
switch v.(type) {
case StringValue:
s, err := v.ToString()
if err != nil {
return nil, err
}
return StringValue(strings.TrimLeft(s, cutset)), nil
if cutsetV == nil {
return StringValue(strings.TrimLeftFunc(s, unicode.IsSpace)), nil
} else {
return StringValue(strings.TrimLeft(s, cutset)), nil
}
case BytesValue:
b, err := v.ToBytes()
if err != nil {
return nil, err
}
return BytesValue(bytes.TrimLeft(b, cutset)), nil
if cutsetV == nil {
return nil, fmt.Errorf("LTRIM: must set_of_characters_to_remove when trimming bytes")
} else {
return BytesValue(bytes.TrimLeft(b, cutset)), nil
}
}
return nil, fmt.Errorf("LTRIM: value type is must be STRING or BYTES type")
}
Expand Down Expand Up @@ -887,20 +903,36 @@ func RPAD(originalValue Value, returnLength int64, pattern Value) (Value, error)
return nil, fmt.Errorf("RPAD: originalValue must be STRING or BYTES")
}

func RTRIM(value Value, cutset string) (Value, error) {
switch value.(type) {
func RTRIM(v Value, cutsetV Value) (Value, error) {
var cutset string
if cutsetV != nil {
b, err := cutsetV.ToBytes()
if err != nil {
return nil, err
}
cutset = string(b)
}
switch v.(type) {
case StringValue:
v, err := value.ToString()
s, err := v.ToString()
if err != nil {
return nil, err
}
return StringValue(strings.TrimRight(v, cutset)), nil
if cutsetV == nil {
return StringValue(strings.TrimRightFunc(s, unicode.IsSpace)), nil
} else {
return StringValue(strings.TrimRight(s, cutset)), nil
}
case BytesValue:
v, err := value.ToBytes()
b, err := v.ToBytes()
if err != nil {
return nil, err
}
return BytesValue(bytes.TrimRight(v, cutset)), nil
if cutsetV == nil {
return nil, fmt.Errorf("RTRIM: must set_of_characters_to_remove when trimming bytes")
} else {
return BytesValue(bytes.TrimRight(b, cutset)), nil
}
}
return nil, fmt.Errorf("RTRIM: value1 must be STRING or BYTES")
}
Expand Down Expand Up @@ -1234,11 +1266,9 @@ func TRANSLATE(expr, source, target Value) (Value, error) {
return nil, fmt.Errorf("TRANSLATE: expression type is must be STRING or BYTES type")
}

func TRIM(v, cutsetV Value) (Value, error) {
func TRIM(v Value, cutsetV Value) (Value, error) {
var cutset string
if cutsetV == nil {
cutset = " "
} else {
if cutsetV != nil {
b, err := cutsetV.ToBytes()
if err != nil {
return nil, err
Expand All @@ -1251,13 +1281,21 @@ func TRIM(v, cutsetV Value) (Value, error) {
if err != nil {
return nil, err
}
return StringValue(strings.Trim(s, cutset)), nil
if cutsetV == nil {
return StringValue(strings.TrimSpace((s))), nil
} else {
return StringValue(strings.Trim(s, cutset)), nil
}
case BytesValue:
b, err := v.ToBytes()
if err != nil {
return nil, err
}
return BytesValue(bytes.Trim(b, cutset)), nil
if cutsetV == nil {
return nil, fmt.Errorf("TRIM: must set_of_characters_to_remove when trimming bytes")
} else {
return BytesValue(bytes.Trim(b, cutset)), nil
}
}
return nil, fmt.Errorf("TRIM: expression type is must be STRING or BYTES type")
}
Expand Down
26 changes: 21 additions & 5 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3576,8 +3576,8 @@ WITH example AS
},
{
name: "ltrim",
query: `SELECT LTRIM(' apple '), LTRIM('***apple***', '*'), LTRIM(NULL), LTRIM(' . ', NULL)`,
expectedRows: [][]interface{}{{"apple ", "apple***", nil, nil}},
query: `SELECT LTRIM(' apple '), LTRIM(' apple '), LTRIM('***apple***', '*'), LTRIM(NULL), LTRIM(' . ', NULL)`,
expectedRows: [][]interface{}{{"apple ", "apple ", "apple***", nil, nil}},
},
{
name: "normalize",
Expand Down Expand Up @@ -4004,6 +4004,22 @@ WITH items AS (
{"pear"},
},
},
{
name: "rtrim3",
query: `
WITH items AS (
SELECT 'apple ' as item UNION ALL
SELECT 'banana ' as item UNION ALL
SELECT ' orange ' as item UNION ALL
SELECT 'pear' as item
) SELECT RTRIM(item) FROM items`,
expectedRows: [][]interface{}{
{"apple"},
{"banana"},
{" orange"},
{"pear"},
},
},
{
name: "safe_convert_bytes_to_string",
query: `SELECT SAFE_CONVERT_BYTES_TO_STRING(b'\xc2'), SAFE_CONVERT_BYTES_TO_STRING(NULL)`,
Expand Down Expand Up @@ -4135,8 +4151,8 @@ WITH example AS (
},
{
name: "trim",
query: `SELECT TRIM(' apple '), TRIM('***apple***', '*'), TRIM(NULL), TRIM('abc', NULL)`,
expectedRows: [][]interface{}{{"apple", "apple", nil, nil}},
query: `SELECT TRIM(' apple '), TRIM(' apple '), TRIM('***apple***', '*'), TRIM(NULL), TRIM('abc', NULL)`,
expectedRows: [][]interface{}{{"apple", "apple", "apple", nil, nil}},
},
{
name: "unicode",
Expand Down Expand Up @@ -4219,7 +4235,7 @@ WITH example AS (
`,
expectedRows: [][]interface{}{{"2014-12-29T00:00:00", int64(2015)}},
},
{
{
name: "PIVOT",
query: `
WITH produce AS (
Expand Down
Loading