On Mon, Oct 16, 2023 at 10:44 PM Anton A. Melnikov <a.melni...@postgrespro.ru> wrote: > On 16.10.2023 15:49, jian he wrote: > > add the following code after ExecEvalJsonExprCoercion if > > (!InputFunctionCallSafe(...) works, but seems like a hack. > > > > if (!val_string) > > { > > *op->resnull = true; > > *op->resvalue = (Datum) 0; > > } > > It seems the constraint should work here: > > After > > CREATE TABLE test ( > js text, > i int, > x jsonb DEFAULT JSON_QUERY(jsonb '[1,2]', '$[*]' WITH WRAPPER) > CONSTRAINT test_constraint > CHECK (JSON_QUERY(js::jsonb, '$.a' RETURNING char(5) OMIT > QUOTES EMPTY ARRAY ON EMPTY) > 'a') > ); > > INSERT INTO test_jsonb_constraints VALUES ('[]'); > > one expected to see an error like that: > > ERROR: new row for relation "test" violates check constraint > "test_constraint" > DETAIL: Failing row contains ([], null, [1, 2]). > > not "INSERT 0 1"
Yes, the correct thing here is for the constraint to fail. One thing jian he missed during the debugging is that ExecEvalJsonExprCoersion() receives the EMPTY ARRAY value via *op->resvalue/resnull, set by ExecEvalJsonExprBehavior(), because that's the ON EMPTY behavior specified in the constraint. The bug was that the code in ExecEvalJsonExprCoercion() failed to set val_string to that value ("[]") before passing to InputFunctionCallSafe(), so the latter would assume the input is NULL. -- Thanks, Amit Langote EDB: http://www.enterprisedb.com