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

enginetest: Update JSON Scripts tests to add primary keys and ordered results #942

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Changes from all 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
110 changes: 55 additions & 55 deletions enginetest/json_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on one column",
SetUpScript: []string{
"create table t (o_id int)",
"create table t (o_id int primary key)",
"INSERT INTO t VALUES (1),(2)",
},
Assertions: []ScriptTestAssertion{
Expand All @@ -37,15 +37,15 @@ var JsonScripts = []ScriptTest{
{
Name: "Simple JSON_ARRAYAGG on two columns",
SetUpScript: []string{
"create table t (o_id int, attribute longtext)",
"INSERT INTO t VALUES (2, 'color'), (2, 'fabric')",
"create table t (o_id int primary key, attribute longtext)",
"INSERT INTO t VALUES (1, 'color'), (2, 'fabric')",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT JSON_ARRAYAGG(o_id), JSON_ARRAYAGG(`attribute`) FROM t",
Query: "SELECT JSON_ARRAYAGG(o_id), JSON_ARRAYAGG(`attribute`) FROM (SELECT * FROM t ORDER BY o_id) as sub;",
Expected: []sql.Row{
{
sql.MustJSON(`[2,2]`),
sql.MustJSON(`[1,2]`),
sql.MustJSON(`["color","fabric"]`),
},
},
Expand All @@ -55,12 +55,12 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on column with string values w/ groupby",
SetUpScript: []string{
"create table t (o_id int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')",
"create table t (o_id int primary key, c0 int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (1, 2, 'color', 'red'), (2, 2, 'fabric', 'silk')",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT o_id, JSON_ARRAYAGG(`attribute`) FROM t GROUP BY o_id",
Query: "SELECT c0, JSON_ARRAYAGG(`attribute`) FROM (SELECT * FROM t ORDER BY o_id) as sub GROUP BY c0",
Expected: []sql.Row{
{
2,
Expand All @@ -69,7 +69,7 @@ var JsonScripts = []ScriptTest{
},
},
{
Query: "SELECT o_id, JSON_ARRAYAGG(value) FROM t GROUP BY o_id",
Query: "SELECT c0, JSON_ARRAYAGG(value) FROM (SELECT * FROM t ORDER BY o_id) as sub GROUP BY c0",
Expected: []sql.Row{
{
2,
Expand All @@ -82,12 +82,12 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on column with int values w/ groupby",
SetUpScript: []string{
"create table t2 (o_id int, val int)",
"INSERT INTO t2 VALUES (1,1), (1,2), (1,3)",
"create table t2 (o_id int primary key, val int)",
"INSERT INTO t2 VALUES (1,1), (2,1), (3,1)",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT o_id, JSON_ARRAYAGG(val) FROM t2 GROUP BY o_id",
Query: "SELECT val, JSON_ARRAYAGG(o_id) FROM (SELECT * FROM t2 ORDER BY o_id) AS sub GROUP BY val",
Expected: []sql.Row{
{
1,
Expand All @@ -100,8 +100,8 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on unknown column throws error",
SetUpScript: []string{
"create table t2 (o_id int, val int)",
"INSERT INTO t2 VALUES (1,1), (1,2), (1,3)",
"create table t2 (o_id int primary key, val int)",
"INSERT INTO t2 VALUES (1,1), (2,2), (3,3)",
},
Assertions: []ScriptTestAssertion{
{
Expand All @@ -113,7 +113,7 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on column with no rows returns NULL",
SetUpScript: []string{
"create table t2 (o_id int)",
"create table t2 (o_id int primary key)",
},
Assertions: []ScriptTestAssertion{
{
Expand All @@ -129,7 +129,7 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG on row with 1 value, 1 null is fine",
SetUpScript: []string{
"create table x(pk int, c1 int)",
"create table x(pk int primary key, c1 int)",
"INSERT INTO x VALUES (1,NULL)",
},
Assertions: []ScriptTestAssertion{
Expand All @@ -155,25 +155,25 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAYAGG and group by use the same field.",
SetUpScript: []string{
"create table x(pk int, c1 int)",
"create table x(pk int primary key, c1 int)",
"INSERT INTO x VALUES (1, 1)",
"INSERT INTO x VALUES (1, 2)",
"INSERT INTO x VALUES (2, 3)",
"INSERT INTO x VALUES (2, 3)",
"INSERT INTO x VALUES (3, 5)",
"INSERT INTO x VALUES (2, 1)",
"INSERT INTO x VALUES (3, 3)",
"INSERT INTO x VALUES (4, 3)",
"INSERT INTO x VALUES (5, 5)",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT JSON_ARRAYAGG(pk) FROM x GROUP BY pk",
Query: "SELECT JSON_ARRAYAGG(pk) FROM x GROUP BY c1",
Expected: []sql.Row{
{
sql.MustJSON(`[1,1]`),
sql.MustJSON(`[1,2]`),
},
{
sql.MustJSON(`[2,2]`),
sql.MustJSON(`[3,4]`),
},
{
sql.MustJSON(`[3]`),
sql.MustJSON(`[5]`),
},
},
},
Expand All @@ -182,11 +182,11 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_ARRAGG with simple and nested json objects.",
SetUpScript: []string{
"create table j(field JSON)",
`INSERT INTO j VALUES('{"key1": {"key": "value"}}')`,
`INSERT INTO j VALUES('{"key1": "value1", "key2": "value2"}')`,
`INSERT INTO j VALUES('{"key1": {"key": [2,3]}}')`,
`INSERT INTO j VALUES('["a", 1]')`,
"create table j(pk int primary key, field JSON)",
`INSERT INTO j VALUES(1, '{"key1": {"key": "value"}}')`,
`INSERT INTO j VALUES(2, '{"key1": "value1", "key2": "value2"}')`,
`INSERT INTO j VALUES(3, '{"key1": {"key": [2,3]}}')`,
`INSERT INTO j VALUES(4, '["a", 1]')`,
},
Assertions: []ScriptTestAssertion{
{
Expand All @@ -211,12 +211,12 @@ var JsonScripts = []ScriptTest{
{
Name: "Simple JSON_OBJECTAGG with GROUP BY",
SetUpScript: []string{
"create table t2 (o_id int, val int)",
"INSERT INTO t2 VALUES (1,1), (1,2), (1,3)",
"create table t2 (o_id int primary key, val int)",
"INSERT INTO t2 VALUES (1,1), (2,1), (3,1)",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT JSON_OBJECTAGG(o_id, val) FROM t2 GROUP BY o_id",
Query: "SELECT JSON_OBJECTAGG(val, o_id) FROM (SELECT * FROM t2 ORDER BY o_id) as sub GROUP BY val",
Expected: []sql.Row{
{
sql.MustJSON(`{"1": 3}`),
Expand All @@ -228,13 +228,13 @@ var JsonScripts = []ScriptTest{
{
Name: "More complex JSON_OBJECTAGG WITH GROUP BY",
SetUpScript: []string{
"create table t (o_id int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')",
"INSERT INTO t VALUES (3, 'color', 'green'), (3, 'shape', 'square')",
"create table t (o_id int primary key, c0 int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (1, 2, 'color', 'red'), (2, 2, 'fabric', 'silk')",
"INSERT INTO t VALUES (3, 3, 'color', 'green'), (4, 3, 'shape', 'square')",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT o_id, JSON_OBJECTAGG(`attribute`, value) FROM t GROUP BY o_id",
Query: "SELECT c0, JSON_OBJECTAGG(`attribute`, value) FROM (SELECT * FROM t ORDER BY o_id) as sub GROUP BY c0",
Expected: []sql.Row{
{
2, sql.MustJSON(`{"color": "red", "fabric": "silk"}`),
Expand All @@ -245,7 +245,7 @@ var JsonScripts = []ScriptTest{
},
},
{
Query: `SELECT o_id, JSON_OBJECTAGG(o_id, value) FROM t GROUP BY o_id`,
Query: `SELECT c0, JSON_OBJECTAGG(c0, value) FROM (SELECT * FROM t ORDER BY o_id) as sub GROUP BY c0`,
Expected: []sql.Row{
{
2, sql.MustJSON(`{"2": "silk"}`),
Expand All @@ -260,13 +260,13 @@ var JsonScripts = []ScriptTest{
{
Name: "3 column table that uses JSON_OBJECTAGG without groupby",
SetUpScript: []string{
"create table t (o_id int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')",
"INSERT INTO t VALUES (3, 'color', 'green'), (3, 'shape', 'square')",
"create table t (o_id int primary key, c0 int, attribute longtext, value longtext)",
"INSERT INTO t VALUES (1, 2, 'color', 'red'), (2, 2, 'fabric', 'silk')",
"INSERT INTO t VALUES (3, 3, 'color', 'green'), (4, 3, 'shape', 'square')",
},
Assertions: []ScriptTestAssertion{
{
Query: `select JSON_OBJECTAGG(o_id, value) from t`,
Query: `select JSON_OBJECTAGG(c0, value) from (SELECT * FROM t ORDER BY o_id) as sub`,
Expected: []sql.Row{
{
sql.MustJSON(`{"2": "silk", "3": "square"}`),
Expand Down Expand Up @@ -303,14 +303,14 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_OBJECTAGG and nested json values",
SetUpScript: []string{
"create table j(pk int, val JSON)",
`INSERT INTO j VALUES(1, '{"key1": "value1", "key2": "value2"}')`,
`INSERT INTO j VALUES(1, '{"key1": {"key": [2,3]}}')`,
`INSERT INTO j VALUES(2, '["a", 1]')`,
"create table j(pk int primary key, c0 int, val JSON)",
`INSERT INTO j VALUES(1, 1, '{"key1": "value1", "key2": "value2"}')`,
`INSERT INTO j VALUES(2, 1, '{"key1": {"key": [2,3]}}')`,
`INSERT INTO j VALUES(3, 2, '["a", 1]')`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT JSON_OBJECTAGG(pk, val) from j`,
Query: `SELECT JSON_OBJECTAGG(c0, val) from j`,
Expected: []sql.Row{
{
sql.MustJSON(`{"1": {"key1": {"key": [2, 3]}}, "2": ["a", 1]}`),
Expand Down Expand Up @@ -338,33 +338,33 @@ var JsonScripts = []ScriptTest{
{
Name: "JSON_OBJECTAGG handles errors appropriately",
SetUpScript: []string{
`create table test (pk int, val longtext)`,
`insert into test values (1, NULL)`,
`insert into test values (NULL, 1)`, // NULL keys are not allowed in JSON_OBJECTAGG
`create table test (pk int primary key, c0 int, val longtext)`,
`insert into test values (1, 1, NULL)`,
`insert into test values (2, NULL, 1)`, // NULL keys are not allowed in JSON_OBJECTAGG
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT JSON_OBJECTAGG(pk, notval) from test`,
Query: `SELECT JSON_OBJECTAGG(c0, notval) from test`,
ExpectedErr: sql.ErrColumnNotFound,
},
{
Query: `SELECT JSON_OBJECTAGG(notpk, val) from test`,
ExpectedErr: sql.ErrColumnNotFound,
},
{
Query: `SELECT JSON_OBJECTAGG(pk, val) from nottest`,
Query: `SELECT JSON_OBJECTAGG(c0, val) from nottest`,
ExpectedErr: sql.ErrTableNotFound,
},
{
Query: `SELECT JSON_OBJECTAGG(pk, val, badarg) from test`,
Query: `SELECT JSON_OBJECTAGG(c0, val, badarg) from test`,
ExpectedErr: sql.ErrInvalidArgumentNumber,
},
{
Query: `SELECT JSON_OBJECTAGG(pk) from test`,
Query: `SELECT JSON_OBJECTAGG(c0) from test`,
ExpectedErr: sql.ErrInvalidArgumentNumber,
},
{
Query: `SELECT JSON_OBJECTAGG(pk, val) from test`,
Query: `SELECT JSON_OBJECTAGG(c0, val) from test`,
ExpectedErr: sql.ErrJSONObjectAggNullKey,
},
},
Expand Down