Skip to content

Commit

Permalink
fix: json type in bind variable
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Aug 20, 2024
1 parent 3c2e8f9 commit 5e16eb6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
22 changes: 22 additions & 0 deletions go/test/endtoend/vtgate/queries/dml/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,25 @@ func TestInsertAlias(t *testing.T) {
// this validates the record.
mcmp.Exec("select id, region_id, name from user_tbl order by id")
}

// TestInsertJson test insert of json data.
func TestInsertJson(t *testing.T) {
mcmp, closer := start(t)
defer closer()

// simple insert
mcmp.Exec(`insert into j_tbl(id, jdoc) values (1, '{}'), (2, '{"a": 1, "b": 2}')`)
mcmp.Exec(`select * from j_tbl order by id`)

// insert select sharded
mcmp.Exec(`insert into j_tbl(id, jdoc) select id * 10, jdoc from j_tbl`)
mcmp.Exec(`select * from j_tbl order by id`)

// insert select dual
mcmp.Exec(`insert into j_tbl(id, jdoc) select 3, json_object("k", "a")`)
mcmp.Exec(`select * from j_tbl order by id`)

// insert unsharded select sharded
utils.Exec(t, mcmp.VtConn, `insert into uks.j_utbl(id, jdoc) select * from sks.j_tbl`)
utils.Exec(t, mcmp.VtConn, `select * from uks.j_utbl order by id`)
}
7 changes: 7 additions & 0 deletions go/test/endtoend/vtgate/queries/dml/sharded_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ create table lkp_mixed_idx
keyspace_id varbinary(20),
primary key (lkp_key)
) Engine = InnoDB;

create table j_tbl
(
id bigint,
jdoc json,
primary key (id)
) Engine = InnoDB;
9 changes: 8 additions & 1 deletion go/test/endtoend/vtgate/queries/dml/unsharded_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ values (0, 1, 1000);
insert into auto_seq(id, next_id, cache)
values (0, 666, 1000);
insert into mixed_seq(id, next_id, cache)
values (0, 1, 1000);
values (0, 1, 1000);

create table j_utbl
(
id bigint,
jdoc json,
primary key (id)
) Engine = InnoDB;
8 changes: 8 additions & 0 deletions go/test/endtoend/vtgate/queries/dml/vschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@
"name": "hash"
}
]
},
"j_tbl": {
"column_vindexes": [
{
"column": "id",
"name": "hash"
}
]
}
}
}
14 changes: 14 additions & 0 deletions go/vt/sqlparser/normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ func TestNormalize(t *testing.T) {
outbv: map[string]*querypb.BindVariable{
"v1": sqltypes.BitNumBindVariable([]byte("0b11")),
},
}, {
// json value in insert
in: "insert into t values ('{\"k\", \"v\"}')",
outstmt: "insert into t values (:bv1 /* VARCHAR */)",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.StringBindVariable("{\"k\", \"v\"}"),
},
}, {
// json function in insert
in: "insert into t values (JSON_OBJECT('_id', 27, 'name', 'carrot'))",
outstmt: "insert into t values (json_object(:bv1 /* VARCHAR */, :bv2 /* INT64 */, :bv3 /* VARCHAR */, :bv4 /* VARCHAR */))",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.StringBindVariable("{\"k\", \"v\"}"),
},
}, {
// ORDER BY column_position
in: "select a, b from t order by 1 asc",
Expand Down
12 changes: 10 additions & 2 deletions go/vt/vtgate/engine/insert_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ func (ins *InsertSelect) getInsertUnshardedQuery(rows []sqltypes.Row, bindVars m
row := sqlparser.ValTuple{}
for c, value := range inputRow {
bvName := insertVarOffset(r, c)
bindVars[bvName] = sqltypes.ValueBindVariable(value)
if value.Type() == querypb.Type_JSON {
bindVars[bvName] = sqltypes.StringBindVariable(value.RawStr())
} else {
bindVars[bvName] = sqltypes.ValueBindVariable(value)
}
row = append(row, sqlparser.NewArgument(bvName))
}
mids = append(mids, row)
Expand Down Expand Up @@ -264,7 +268,11 @@ func (ins *InsertSelect) getInsertShardedQueries(
row := sqlparser.ValTuple{}
for colOffset, value := range rows[index] {
bvName := insertVarOffset(index, colOffset)
bvs[bvName] = sqltypes.ValueBindVariable(value)
if value.Type() == querypb.Type_JSON {
bvs[bvName] = sqltypes.StringBindVariable(value.RawStr())
} else {
bvs[bvName] = sqltypes.ValueBindVariable(value)
}
row = append(row, sqlparser.NewArgument(bvName))
}
mids = append(mids, row)
Expand Down

0 comments on commit 5e16eb6

Please sign in to comment.