Skip to content

Commit

Permalink
add copyright and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
hg2990656 committed Feb 4, 2020
1 parent 5ef975a commit 8a119e4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
15 changes: 13 additions & 2 deletions executor/aggfuncs/func_json_objectagg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package aggfuncs

import (
Expand Down Expand Up @@ -49,8 +62,6 @@ func (e *jsonObjectAgg) AppendFinalResult2Chunk(sctx sessionctx.Context, pr Part
return errors.Trace(err)
}
p.entries[key] = strVal
default:
p.entries[key] = val
}
}

Expand Down
13 changes: 13 additions & 0 deletions executor/aggfuncs/func_json_objectagg_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package aggfuncs_test

import (
Expand Down
45 changes: 45 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3590,6 +3590,51 @@ func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) {
tk.MustQuery("select * from d").Check(testkit.Rows("hello,h"))
}

func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec("drop table if exists t;")
tk.MustExec(`CREATE TABLE t (
a int(11),
b varchar(100),
c decimal(3,2),
d json,
e date,
f time,
g datetime DEFAULT '2012-01-01',
h timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
i char(36),
j text(50));`)

tk.MustExec(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '0000-00-00 00:00:00', 'first', 'json_objectagg_test');`)
tk.MustExec(`insert into t values(2, 'cd', 2.5, '{"id": 2}', '2020-01-11', '12:12:13', '2020-02-11', '2020-01-02 00:00:00', 'second', 'json_objectagg_test');`);

result := tk.MustQuery("select json_objectagg(a, b) from t group by a order by a;")
result.Check(testkit.Rows(`{"1": "ab"}`, `{"2": "cd"}`))
result = tk.MustQuery("select json_objectagg(b, c) from t group by b order by b;")
result.Check(testkit.Rows(`{"ab": 5.5}`, `{"cd": 2.5}`))
result = tk.MustQuery("select json_objectagg(c, d) from t group by c order by c;")
result.Check(testkit.Rows(`{"2.50": {"id": 2}}`, `{"5.50": {"id": 1}}`))
result = tk.MustQuery("select json_objectagg(d, e) from t group by d order by d;")
result.Check(testkit.Rows(`{"{\"id\": 1}": "2020-01-10"}`, `{"{\"id\": 2}": "2020-01-11"}`))
result = tk.MustQuery("select json_objectagg(e, f) from t group by e order by e;")
result.Check(testkit.Rows(`{"2020-01-10": "11:12:13"}`, `{"2020-01-11": "12:12:13"}`))
result = tk.MustQuery("select json_objectagg(f, g) from t group by f order by f;")
result.Check(testkit.Rows(`{"11:12:13": "2020-01-11 00:00:00"}`, `{"12:12:13": "2020-02-11 00:00:00"}`))
result = tk.MustQuery("select json_objectagg(g, h) from t group by g order by g;")
result.Check(testkit.Rows(`{"2020-01-11 00:00:00": "0000-00-00 00:00:00"}`, `{"2020-02-11 00:00:00": "2020-01-02 00:00:00"}`))
result = tk.MustQuery("select json_objectagg(h, i) from t group by h order by h;")
result.Check(testkit.Rows(`{"0000-00-00 00:00:00": "first"}`, `{"2020-01-02 00:00:00": "second"}`))
result = tk.MustQuery("select json_objectagg(i, j) from t group by i order by i;")
result.Check(testkit.Rows(`{"first": "json_objectagg_test"}`, `{"second": "json_objectagg_test"}`))
result = tk.MustQuery("select json_objectagg(a, null) from t group by a order by a;")
result.Check(testkit.Rows(`{"1": null}`, `{"2": null}`))

tk.MustGetErrCode("select json_objectagg(null, a) from t group by a order by a;", mysql.ErrJSONDocumentNULLKey)
}

func (s *testIntegrationSuite2) TestOtherBuiltin(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit 8a119e4

Please sign in to comment.