Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Fix integer literals parsing #840

Merged
erizocosmico merged 16 commits into src-d:master from agarciamontoro:fix.parsing
Oct 15, 2019
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
8754654
Modify convertInt so it returns the smallest representation allowed
agarciamontoro Sep 27, 2019
09e535d
Add a rule to convert integer literals in INSERT
agarciamontoro Oct 9, 2019
25ce62f
Test the new rule to convert integers in INSERT nodes
agarciamontoro Oct 11, 2019
be23624
Modify getInt64Literal to try to convert smaller representations
agarciamontoro Oct 15, 2019
506c515
Convert literals to specific int64 values in GROUP BY parsing
agarciamontoro Oct 9, 2019
463c5e4
Modify ROUND and YEARWEEK functions to manage all integer types
agarciamontoro Oct 11, 2019
244439c
Adapt all tests to new integer parsing
agarciamontoro Oct 9, 2019
5616382
Add missing cases to pilosa decodeGob and compare functions
agarciamontoro Oct 10, 2019
95124bb
Add smaller integer types to the conversion to sqltypes.Value
agarciamontoro Oct 10, 2019
2395078
Fix formatting of convertInt. HT @erizocosmico
agarciamontoro Oct 11, 2019
daf82c5
Run gofmt over the whole codebase
agarciamontoro Oct 11, 2019
f38b019
Move integer conversion from ad-hoc rule to Insert.Execute
agarciamontoro Oct 11, 2019
dcf2612
Test missing cases in ROUND function
agarciamontoro Oct 14, 2019
2233b4d
Refactor type tests to cover all missing cases
agarciamontoro Oct 14, 2019
d436a96
Add missing cases to pilosa decodeGob test
agarciamontoro Oct 14, 2019
c0899fb
Test parsing of integer literals
agarciamontoro Oct 14, 2019
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
Next Next commit
Modify convertInt so it returns the smallest representation allowed
Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
  • Loading branch information
agarciamontoro committed Oct 14, 2019
commit 87546547b55b58dd7b31d66e5c985e8061882fd1
57 changes: 41 additions & 16 deletions sql/parse/parse.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -950,22 +950,51 @@ func isAggregateFunc(v *sqlparser.FuncExpr) bool {
return v.IsAggregate()
}

// Convert an integer, represented by the specified string in the specified
// base, to its smallest representation possible, out of:
// int8, uint8, int16, uint16, int32, uint32, int64 and uint64
func convertInt(value string, base int) (sql.Expression, error) {
i8, err := strconv.ParseInt(value, base, 8)
if err != nil {
ui8, err := strconv.ParseUint(value, base, 8)
if err != nil {
i16, err := strconv.ParseInt(value, base, 16)
if err != nil {
ui16, err := strconv.ParseUint(value, base, 16)
if err != nil {
i32, err := strconv.ParseInt(value, base, 32)
if err != nil {
ui32, err := strconv.ParseUint(value, base, 32)
if err != nil {
i64, err := strconv.ParseInt(value, base, 64)
if err != nil {
ui64, err := strconv.ParseUint(value, base, 64)
if err != nil {
return nil, err
}
return expression.NewLiteral(uint64(ui64), sql.Uint64), nil
}
return expression.NewLiteral(int64(i64), sql.Int64), nil
}
return expression.NewLiteral(uint32(ui32), sql.Uint32), nil
}
return expression.NewLiteral(int32(i32), sql.Int32), nil
}
return expression.NewLiteral(uint16(ui16), sql.Uint16), nil
}
return expression.NewLiteral(int16(i16), sql.Int16), nil
}
return expression.NewLiteral(uint8(ui8), sql.Uint16), nil
}
return expression.NewLiteral(int8(i8), sql.Int8), nil
}

func convertVal(v *sqlparser.SQLVal) (sql.Expression, error) {
switch v.Type {
case sqlparser.StrVal:
return expression.NewLiteral(string(v.Val), sql.Text), nil
case sqlparser.IntVal:
//TODO: Use smallest integer representation and widen later.
val, err := strconv.ParseInt(string(v.Val), 10, 64)
if err != nil {
// Might be a uint64 value that is greater than int64 max
val, checkErr := strconv.ParseUint(string(v.Val), 10, 64)
if checkErr != nil {
return nil, err
}
return expression.NewLiteral(val, sql.Uint64), nil
}
return expression.NewLiteral(val, sql.Int64), nil
return convertInt(string(v.Val), 10)
case sqlparser.FloatVal:
val, err := strconv.ParseFloat(string(v.Val), 64)
if err != nil {
Expand All @@ -980,11 +1009,7 @@ func convertVal(v *sqlparser.SQLVal) (sql.Expression, error) {
v = strings.Trim(v[1:], "'")
}

val, err := strconv.ParseInt(v, 16, 64)
if err != nil {
return nil, err
}
return expression.NewLiteral(val, sql.Int64), nil
return convertInt(v, 16)
case sqlparser.HexVal:
val, err := v.HexDecode()
if err != nil {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /