Skip to content

Commit

Permalink
make sure evalengine expressions are not simplified
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Dec 20, 2024
1 parent 48aea8b commit 87cda2d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func TestSetAndGetLastInsertID(t *testing.T) {
"update t1 set id2 = 88 where id1 = last_insert_id(%d)",
"delete from t1 where id1 = last_insert_id(%d)",
"select id2, last_insert_id(count(*)) from t1 where %d group by id2",
"set @x = last_insert_id(%d)",
}

for _, workload := range []string{"olap", "oltp"} {
Expand All @@ -187,6 +188,39 @@ func TestSetAndGetLastInsertID(t *testing.T) {
}
}

func TestSetAndGetLastInsertIDWithInsert(t *testing.T) {
mcmp, closer := start(t)
defer closer()

mcmp.Exec("insert into t1(id1, id2) values (last_insert_id(12),0)")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("insert into t1(id1, id2) values (13,last_insert_id(0))")
mcmp.Exec("select last_insert_id()")

mcmp.Exec("begin")
mcmp.Exec("insert into t1(id1, id2) values (last_insert_id(14),0)")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("insert into t1(id1, id2) values (15,last_insert_id(0))")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("commit")

_, err := mcmp.VtConn.ExecuteFetch("set workload = olap", 1, false)
require.NoError(t, err)

mcmp.Exec("insert into t1(id1, id2) values (last_insert_id(16),0)")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("insert into t1(id1, id2) values (17,last_insert_id(0))")
mcmp.Exec("select last_insert_id()")

mcmp.Exec("begin")
mcmp.Exec("insert into t1(id1, id2) values (last_insert_id(18),0)")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("insert into t1(id1, id2) values (19,last_insert_id(0))")
mcmp.Exec("select last_insert_id()")
mcmp.Exec("commit")

}

// TestVindexHints tests that vindex hints work as intended.
func TestVindexHints(t *testing.T) {
mcmp, closer := start(t)
Expand Down
4 changes: 4 additions & 0 deletions go/vt/vtgate/evalengine/fn_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func (call *builtinLastInsertID) compile(c *compiler) (ctype, error) {
return ctype{Type: sqltypes.Uint64, Flag: arg.Flag & flagNullable, Col: collationNumeric}, nil
}

func (call *builtinLastInsertID) constant() bool {
return false // we don't want this function to be simplified away
}

func printIPv6AsIPv4(addr netip.Addr) (netip.Addr, bool) {
b := addr.AsSlice()
if len(b) != 16 {
Expand Down
19 changes: 19 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/set_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -605,5 +605,24 @@
]
}
}
},
{
"QueryType": "SET",
"Original": "set @foo = last_insert_id(1)",
"Instructions": {
"OperatorType": "Set",
"Ops": [
{
"Type": "UserDefinedVariable",
"Name": "foo",
"Expr": "last_insert_id(1)"
}
],
"Inputs": [
{
"OperatorType": "SingleRow"
}
]
}
}
]

0 comments on commit 87cda2d

Please sign in to comment.