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

use decimal type for arithmetic functions #1244

Merged
merged 22 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
14 changes: 14 additions & 0 deletions enginetest/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-errors.v1"
Expand Down Expand Up @@ -434,6 +435,19 @@ func checkResults(
}
}

// The result from SELECT or WITH queries can be decimal.Decimal type.
// The exact expected value cannot be defined in enginetests, so convert the result to string format,
// which is the value we get on sql shell.
if strings.HasPrefix(upperQuery, "SELECT ") || strings.HasPrefix(upperQuery, "WITH ") {
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
for _, widenedRow := range widenedRows {
for i, val := range widenedRow {
if d, ok := val.(decimal.Decimal); ok {
widenedRow[i] = d.StringFixed(d.Exponent() * -1)
}
}
}
}

// .Equal gives better error messages than .ElementsMatch, so use it when possible
if orderBy || len(expected) <= 1 {
require.Equal(widenedExpected, widenedRows, "Unexpected result for query %s", q)
Expand Down
8 changes: 4 additions & 4 deletions enginetest/queries/column_alias_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var ColumnAliasQueries = []QueryTest{
},
{
Name: "COL2",
Type: sql.Float64,
Type: sql.Int64,
},
},
// TODO: SUM should be integer typed for integers
Expand All @@ -98,7 +98,7 @@ var ColumnAliasQueries = []QueryTest{
},
{
Name: "COL2",
Type: sql.Float64,
Type: sql.Int64,
},
},
Expected: []sql.Row{
Expand All @@ -116,7 +116,7 @@ var ColumnAliasQueries = []QueryTest{
},
{
Name: "coL2",
Type: sql.Float64,
Type: sql.Int64,
},
},
Expected: []sql.Row{
Expand All @@ -134,7 +134,7 @@ var ColumnAliasQueries = []QueryTest{
},
{
Name: "TimeStamp",
Type: sql.Float64,
Type: sql.Int64,
},
},
Expected: []sql.Row{
Expand Down
40 changes: 23 additions & 17 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,15 @@ var QueryTests = []QueryTest{
{
Query: "SELECT pk DIV 2, SUM(c3) + sum(c3) as sum FROM one_pk GROUP BY 1 ORDER BY 1",
Expected: []sql.Row{
{int64(0), float64(28)},
{int64(1), float64(108)},
{int64(0), int64(28)},
{int64(1), int64(108)},
},
},
{
Query: "SELECT pk DIV 2, SUM(c3) + min(c3) as sum_and_min FROM one_pk GROUP BY 1 ORDER BY 1",
Expected: []sql.Row{
{int64(0), float64(16)},
{int64(1), float64(76)},
{int64(0), int64(16)},
{int64(1), int64(76)},
},
ExpectedColumns: sql.Schema{
{
Expand All @@ -507,15 +507,15 @@ var QueryTests = []QueryTest{
},
{
Name: "sum_and_min",
Type: sql.Float64,
Type: sql.Int64,
},
},
},
{
Query: "SELECT pk DIV 2, SUM(`c3`) + min( c3 ) FROM one_pk GROUP BY 1 ORDER BY 1",
Expected: []sql.Row{
{int64(0), float64(16)},
{int64(1), float64(76)},
{int64(0), int64(16)},
{int64(1), int64(76)},
},
ExpectedColumns: sql.Schema{
{
Expand All @@ -524,7 +524,7 @@ var QueryTests = []QueryTest{
},
{
Name: "SUM(`c3`) + min( c3 )",
Type: sql.Float64,
Type: sql.Int64,
},
},
},
Expand Down Expand Up @@ -1426,9 +1426,9 @@ var QueryTests = []QueryTest{
},
},
{
Query: "with recursive t (n) as (select sum(1) from dual union all select (2) from dual) select sum(n) from t;",
Query: "with recursive t (n) as (select sum(1) from dual union all select (2.00) from dual) select sum(n) from t;",
Expected: []sql.Row{
{float64(3)},
{"3.00"},
},
},
{
Expand Down Expand Up @@ -3775,9 +3775,9 @@ var QueryTests = []QueryTest{
{
Query: "SELECT SUM(i) + 1, i FROM mytable GROUP BY i ORDER BY i",
Expected: []sql.Row{
{float64(2), int64(1)},
{float64(3), int64(2)},
{float64(4), int64(3)},
{int64(2), int64(1)},
{int64(3), int64(2)},
{int64(4), int64(3)},
},
},
{
Expand Down Expand Up @@ -3921,7 +3921,13 @@ var QueryTests = []QueryTest{
{
Query: `SELECT AVG(23.222000)`,
Expected: []sql.Row{
{float64(23.222)},
{"23.2220000000"},
},
},
{
Query: `SELECT AVG("23.222000")`,
Expected: []sql.Row{
{23.222},
},
},
{
Expand Down Expand Up @@ -6079,9 +6085,9 @@ var QueryTests = []QueryTest{
{
Query: "select sum(x.i) + y.i from mytable as x, mytable as y where x.i = y.i GROUP BY x.i",
Expected: []sql.Row{
{float64(2)},
{float64(4)},
{float64(6)},
{int64(2)},
{int64(4)},
{int64(6)},
},
},
{
Expand Down
44 changes: 43 additions & 1 deletion enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ var ScriptTests = []ScriptTest{
},
{
Query: "SELECT SUM( DISTINCT + col1 ) * - 22 - - ( - COUNT( * ) ) col0 FROM tab1 AS cor0",
Expected: []sql.Row{{float64(-1455)}},
Expected: []sql.Row{{int64(-1455)}},
},
{
Query: "SELECT MIN (DISTINCT col1) from tab1 GROUP BY col0 ORDER BY col0",
Expand Down Expand Up @@ -2154,6 +2154,48 @@ var ScriptTests = []ScriptTest{
},
},
},
{
Name: "sum() and avg() on DECIMAL type column returns the DECIMAL type result",
SetUpScript: []string{
"create table decimal_table (id int, val decimal(18,16));",
"insert into decimal_table values (1,-2.5633000000000384);",
"insert into decimal_table values (2,2.5633000000000370);",
"insert into decimal_table values (3,0.0000000000000004);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT val FROM decimal_table;",
Expected: []sql.Row{{"-2.5633000000000384"}, {"2.5633000000000370"}, {"0.0000000000000004"}},
},
{
Query: "SELECT sum(val) FROM decimal_table;",
Expected: []sql.Row{{"-0.0000000000000010"}},
},
{
Query: "SELECT avg(val) FROM decimal_table;",
Expected: []sql.Row{{"-0.00000000000000033333"}},
},
},
},
{
Name: "sum() and avg() on non-DECIMAL type column returns the DOUBLE type result",
SetUpScript: []string{
"create table decimal_table (id int, val1 double, val2 float);",
"insert into decimal_table values (1,-2.5633000000000384, 2.3);",
"insert into decimal_table values (2,2.5633000000000370, 2.4);",
"insert into decimal_table values (3,0.0000000000000004, 5.3);",
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT sum(id), sum(val1), sum(val2) FROM decimal_table;",
Expected: []sql.Row{{float64(6), -9.322676295501879e-16, 10.000000238418579}},
},
{
Query: "SELECT avg(id), avg(val1), avg(val2) FROM decimal_table;",
Expected: []sql.Row{{float64(2), -3.107558765167293e-16, 3.333333412806193}},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
3 changes: 1 addition & 2 deletions enginetest/queries/variable_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ var VariableQueries = []ScriptTest{
Assertions: []ScriptTestAssertion{
{
Query: "select @myvar, @@autocommit, @myvar2, @myvar3",
// TODO: unclear why the last var is getting a float type, should be an int
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
Expected: []sql.Row{
{1, 1, 0, 0.0},
{1, 1, 0, 0},
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions sql/analyzer/aggregations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestFlattenAggregationExprs(t *testing.T) {
expected: plan.NewProject(
[]sql.Expression{
expression.NewArithmetic(
expression.NewGetField(0, sql.Float64, "SUM(foo.a)", false),
expression.NewGetField(0, sql.Int64, "SUM(foo.a)", false),
expression.NewLiteral(int64(1), sql.Int64),
"+",
),
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestFlattenAggregationExprs(t *testing.T) {
[]sql.Expression{
expression.NewAlias("x",
expression.NewArithmetic(
expression.NewGetField(0, sql.Float64, "SUM(foo.a)", false),
expression.NewGetField(0, sql.Int64, "SUM(foo.a)", false),
expression.NewLiteral(int64(1), sql.Int64),
"+",
)),
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestFlattenAggregationExprs(t *testing.T) {
expected: plan.NewProject(
[]sql.Expression{
expression.NewArithmetic(
expression.NewGetField(0, sql.Float64, "SUM(foo.a)", false),
expression.NewGetField(0, sql.Int64, "SUM(foo.a)", false),
expression.NewGetField(1, sql.Int64, "COUNT(foo.a)", false),
"/",
),
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestFlattenAggregationExprs(t *testing.T) {
expected: plan.NewProject(
[]sql.Expression{
expression.NewArithmetic(
expression.NewGetField(0, sql.Float64, "SUM(foo.a)", false),
expression.NewGetField(0, sql.Int64, "SUM(foo.a)", false),
expression.NewGetFieldWithTable(1, sql.Int64, "bar", "a", false),
"+",
),
Expand Down
6 changes: 3 additions & 3 deletions sql/analyzer/resolve_having_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestResolveHaving(t *testing.T) {
),
expected: plan.NewHaving(
expression.NewGreaterThan(
expression.NewGetField(0, sql.Float64, "x", true),
expression.NewGetField(0, sql.Int64, "x", true),
expression.NewLiteral(int64(5), sql.Int64),
),
plan.NewGroupBy(
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestResolveHaving(t *testing.T) {
),
expected: plan.NewHaving(
expression.NewGreaterThan(
expression.NewGetField(0, sql.Float64, "x", true),
expression.NewGetField(0, sql.Int64, "x", true),
expression.NewLiteral(int64(5), sql.Int64),
),
plan.NewGroupBy(
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestResolveHaving(t *testing.T) {
),
expected: plan.NewProject(
[]sql.Expression{
expression.NewGetField(0, sql.Float64, "x", true),
expression.NewGetField(0, sql.Int64, "x", true),
expression.NewGetFieldWithTable(1, sql.Int64, "t", "foo", false),
},
plan.NewHaving(
Expand Down
Loading