Skip to content

Commit

Permalink
planner, json: restore cast flag after substitute column in cast (#39997
Browse files Browse the repository at this point in the history
)

close #39806
  • Loading branch information
YangKeao authored Dec 16, 2022
1 parent d2393e3 commit 3970cfc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,22 @@ func TestAggregationBuiltinJSONObjectAgg(t *testing.T) {
result.Check(testkit.Rows(`{"first": "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}`))

// For issue: https://github.com/pingcap/tidb/issues/39806
// Optimization shouldn't rewrite the flag of `castStringAsJson`.
tk.MustQuery(`
select a from (
select JSON_OBJECT('number', number, 'name', name) 'a' from
(
select 1 as number, 'name-1' as name union
(select 2, 'name-2' ) union
(select 3, 'name-3' ) union
(select 4, 'name-4' ) union
(select 5, 'name-5' ) union
(select 6, 'name-2' )
) temp1
) temp
where a ->> '$.number' = 1`).Check(testkit.Rows(`{"name": "name-1", "number": 1}`))
}

func TestOtherBuiltin(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,10 @@ func ColumnSubstituteImpl(expr Expression, schema *Schema, newExprs []Expression
return substituted, hasFail, v
}
if substituted {
flag := v.RetType.GetFlag()
e := BuildCastFunction(v.GetCtx(), newArg, v.RetType)
e.SetCoercibility(v.Coercibility())
e.GetType().SetFlag(flag)
return true, false, e
}
return false, false, v
Expand Down

0 comments on commit 3970cfc

Please sign in to comment.