-
-
Notifications
You must be signed in to change notification settings - Fork 238
Support OCT() function and fix CONV() mishandling of negative floats and empty string N #3020
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
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f96a527
add oct.go impl
elianddb 6ea7cd5
add impl docs
elianddb c7c84e8
add oct_test.go
elianddb 639f18c
fix nval empty string out of index err and negative floating points t…
elianddb a873c87
add empty string tests
elianddb e68d5e3
add oct to registry
elianddb 4ee9999
[ga-format-pr] Run ./format_repo.sh to fix formatting
elianddb af645db
update switch sql/expression/function/conv.go
elianddb 9f7b38e
Merge branch 'main' of github.com:dolthub/go-mysql-server into eliand…
elianddb 3675fc8
fix switch syntax
elianddb 96c2e97
add query tests
elianddb 9cf0f6d
add table queries using oct()
elianddb dba0294
Merge branch 'main' of github.com:dolthub/go-mysql-server into eliand…
elianddb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2025 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package function | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| ) | ||
|
|
||
| // Oct function provides a string representation for the octal value of N, where N is a decimal (base 10) number. | ||
| type Oct struct { | ||
| n sql.Expression | ||
| } | ||
|
|
||
| var _ sql.FunctionExpression = (*Oct)(nil) | ||
| var _ sql.CollationCoercible = (*Oct)(nil) | ||
|
|
||
| // NewOct returns a new Oct expression. | ||
| func NewOct(n sql.Expression) sql.Expression { return &Oct{n} } | ||
|
|
||
| // FunctionName implements sql.FunctionExpression. | ||
| func (o *Oct) FunctionName() string { | ||
| return "oct" | ||
| } | ||
|
|
||
| // Description implements sql.FunctionExpression. | ||
| func (o *Oct) Description() string { | ||
| return "returns a string representation for octal value of N, where N is a decimal (base 10) number." | ||
| } | ||
|
|
||
| // Type implements the Expression interface. | ||
| func (o *Oct) Type() sql.Type { | ||
| return types.LongText | ||
| } | ||
|
|
||
| // IsNullable implements the Expression interface. | ||
| func (o *Oct) IsNullable() bool { | ||
| return o.n.IsNullable() | ||
| } | ||
|
|
||
| // Eval implements the Expression interface. | ||
| func (o *Oct) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
| // Convert a decimal (base 10) number to octal (base 8) | ||
| return NewConv( | ||
| o.n, | ||
| expression.NewLiteral(10, types.Int64), | ||
| expression.NewLiteral(8, types.Int64), | ||
| ).Eval(ctx, row) | ||
| } | ||
|
|
||
| // Resolved implements the Expression interface. | ||
| func (o *Oct) Resolved() bool { | ||
| return o.n.Resolved() | ||
| } | ||
|
|
||
| // Children implements the Expression interface. | ||
| func (o *Oct) Children() []sql.Expression { | ||
| return []sql.Expression{o.n} | ||
| } | ||
|
|
||
| // WithChildren implements the Expression interface. | ||
| func (o *Oct) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
| if len(children) != 1 { | ||
| return nil, sql.ErrInvalidChildrenNumber.New(o, len(children), 1) | ||
| } | ||
| return NewOct(children[0]), nil | ||
| } | ||
|
|
||
| func (o *Oct) String() string { | ||
| return fmt.Sprintf("%s(%s)", o.FunctionName(), o.n) | ||
| } | ||
|
|
||
| // CollationCoercibility implements the interface sql.CollationCoercible. | ||
| func (*Oct) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { | ||
| return ctx.GetCollation(), 4 // strings with collations | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2025 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package function | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| ) | ||
|
|
||
| type test struct { | ||
| name string | ||
| nType sql.Type | ||
| row sql.Row | ||
| expected interface{} | ||
| } | ||
|
|
||
| func TestOct(t *testing.T) { | ||
| tests := []test{ | ||
| // NULL input | ||
| {"n is nil", types.Int32, sql.NewRow(nil), nil}, | ||
|
|
||
| // Positive numbers | ||
| {"positive small", types.Int32, sql.NewRow(8), "10"}, | ||
| {"positive medium", types.Int32, sql.NewRow(64), "100"}, | ||
| {"positive large", types.Int32, sql.NewRow(4095), "7777"}, | ||
| {"positive huge", types.Int64, sql.NewRow(123456789), "726746425"}, | ||
|
|
||
| // Negative numbers | ||
| {"negative small", types.Int32, sql.NewRow(-8), "1777777777777777777770"}, | ||
| {"negative medium", types.Int32, sql.NewRow(-64), "1777777777777777777700"}, | ||
| {"negative large", types.Int32, sql.NewRow(-4095), "1777777777777777770001"}, | ||
|
|
||
| // Zero | ||
| {"zero", types.Int32, sql.NewRow(0), "0"}, | ||
|
|
||
| // String inputs | ||
| {"string number", types.LongText, sql.NewRow("15"), "17"}, | ||
| {"alpha string", types.LongText, sql.NewRow("abc"), "0"}, | ||
| {"mixed string", types.LongText, sql.NewRow("123abc"), "173"}, | ||
|
|
||
| // Edge cases | ||
| {"max int32", types.Int32, sql.NewRow(math.MaxInt32), "17777777777"}, | ||
| {"min int32", types.Int32, sql.NewRow(math.MinInt32), "1777777777760000000000"}, | ||
| {"max int64", types.Int64, sql.NewRow(math.MaxInt64), "777777777777777777777"}, | ||
| {"min int64", types.Int64, sql.NewRow(math.MinInt64), "1000000000000000000000"}, | ||
|
|
||
| // Decimal numbers | ||
| {"decimal", types.Float64, sql.NewRow(15.5), "17"}, | ||
| {"negative decimal", types.Float64, sql.NewRow(-15.5), "1777777777777777777761"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| f := NewOct(expression.NewGetField(0, tt.nType, "n", true)) | ||
| result, err := f.Eval(sql.NewEmptyContext(), tt.row) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if result != tt.expected { | ||
| t.Errorf("got %v; expected %v", result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.