Skip to content

Commit

Permalink
added nil check in Addition expression (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp authored Mar 2, 2024
1 parent 857701a commit 031d033
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/expression/addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func (a *Addition) Resolved() bool {

// String implements the sql.Expression interface.
func (a *Addition) String() string {
if a.left == nil || a.right == nil {
return "? + ?"
}
return a.left.String() + " + " + a.right.String()
}

Expand Down
2 changes: 2 additions & 0 deletions server/expression/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func (c *Cast) Eval(ctx *sql.Context, row sql.Row) (any, error) {
if err != nil {
return nil, err
}
case query.Type_NULL_TYPE:
fromType = pgtypes.Null
default:
return nil, fmt.Errorf("encountered a GMS type that cannot be handled")
}
Expand Down
73 changes: 73 additions & 0 deletions testing/go/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,78 @@ func TestRegressions(t *testing.T) {
},
},
},
{
Skip: true,
Name: "casting null as integer",
SetUpScript: []string{
`CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);`,
`INSERT INTO tab0 VALUES (0,698,169.42,'apdbu',431,316.15,'sqvis'), (1,538,676.36,'fuqeu',514,685.97,'bgwrq');`,
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT ALL + 58 FROM tab0 WHERE NULL NOT BETWEEN + 71 * CAST ( NULL AS INTEGER ) AND col4",
Expected: []sql.Row{},
},
},
},
{
Name: "addition expression in prepared statement",
SetUpScript: []string{
`CREATE TABLE t1(x INTEGER);`,
`CREATE TABLE t2(y INTEGER PRIMARY KEY);`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT 1 IN (SELECT x+y FROM t1, t2);`,
Expected: []sql.Row{{0}},
},
},
},
{
Skip: true,
Name: "casting from float64 to int64 and float32",
SetUpScript: []string{
`CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);`,
`INSERT INTO tab0 VALUES (0,698,169.42,'apdbu',431,316.15,'sqvis'), (1,538,676.36,'fuqeu',514,685.97,'bgwrq'), (2,90,205.26,'yrrzx',123,836.88,'kpuhc'),
(3,620,864.8,'myrdv',877,820.98,'oxkuv'), (4,754,677.3,'iofrg',67,665.49,'bzqba'), (5,107,710.19,'lhfro',286,504.28,'kwwsg'), (6,904,193.16,'eozui',48,698.55,'ejyzs'),
(7,606,650.64,'ovmce',417,962.43,'dvkbh'), (8,535,18.11,'ijika',630,489.63,'hpnyu'), (9,501,776.40,'cvygg',725,75.5,'etlyv');`,
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT ALL * FROM tab0 cor0 WHERE ( + CAST ( - col4 AS INTEGER ) ) IN ( - col1, + col3, col3 );",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM tab0 WHERE - - col0 * + - col4 >= ( + CAST ( col1 AS REAL ) );",
Expected: []sql.Row{},
},
},
},
{
Skip: true,
Name: "typecheck fails to detect doltgres types in GMS ",
SetUpScript: []string{
`CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER);`,
`INSERT INTO tab0 VALUES (97,1,99), (15,81,47), (87,21,10);`,
`CREATE TABLE tab1(col0 INTEGER, col1 INTEGER, col2 INTEGER);`,
`INSERT INTO tab1 VALUES (51,14,96), (85,5,59), (91,47,68);`,
`CREATE TABLE tab2(col0 INTEGER, col1 INTEGER, col2 INTEGER);`,
`INSERT INTO tab2 VALUES(64,77,40), (75,67,58), (46,51,23);`,
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT ALL + col2 * + ( ( 43 ) ) - 47 + + col1 * CAST ( - ( + 63 ) / col0 AS INTEGER ) AS col0 FROM tab1 AS cor0;",
Expected: []sql.Row{{4067}, {2490}, {2877}},
},
{
Query: "SELECT - COUNT ( * ) - 26 * + 96 AS col2 FROM tab0 WHERE + 2 * col0 NOT BETWEEN 33 * CAST ( + 7 / 91 AS REAL ) AND 52;",
Expected: []sql.Row{{-2498}},
},
{
Query: "SELECT CAST ( + CAST ( 73 AS REAL ) AS INTEGER ) * 62 FROM tab2 WHERE NULL IS NULL;",
Expected: []sql.Row{{4526}, {4526}, {4526}},
},
},
},
})
}

0 comments on commit 031d033

Please sign in to comment.