Skip to content

Commit d94d246

Browse files
evalengine: disable the static IN hash table for JSON operands
Signed-off-by: Graham Campbell <hello@gjcampbell.co.uk>
1 parent 54afb57 commit d94d246

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

go/vt/vtgate/evalengine/compiler_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,29 @@ func TestJSONComparisonDomains(t *testing.T) {
918918
{expression: `'[]' IN (JSON_ARRAY(), '0')`, result: `INT64(0)`},
919919
{expression: `'0' IN (CAST('[]' AS JSON), '0')`, result: `INT64(1)`},
920920

921+
// JSON arrays and objects hash by kind and cardinality only: IN
922+
// must compare them for real instead of trusting a hash hit.
923+
{
924+
expression: `column0 IN (JSON_ARRAY(2))`,
925+
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`[1]`))},
926+
result: `INT64(0)`,
927+
},
928+
{
929+
expression: `column0 IN (JSON_OBJECT('b', 2))`,
930+
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`{"a": 1}`))},
931+
result: `INT64(0)`,
932+
},
933+
{
934+
expression: `column0 IN (JSON_ARRAY(1))`,
935+
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`[1]`))},
936+
result: `INT64(1)`,
937+
},
938+
{
939+
expression: `column0 IN (JSON_OBJECT('a', 1))`,
940+
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`{"a": 1}`))},
941+
result: `INT64(1)`,
942+
},
943+
921944
// ENUM and SET values convert to JSON string scalars of their
922945
// textual value when a JSON value participates in the IN domain.
923946
{

go/vt/vtgate/evalengine/expr_compare.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,13 @@ func (i *InExpr) eval(env *ExpressionEnv) (eval, error) {
727727
}
728728

729729
func (i *InExpr) compileTable(lhs ctype, rhs TupleExpr) map[vthash.Hash]struct{} {
730+
// JSON hashes fingerprint arrays and objects by kind and cardinality
731+
// only: a hash hit is not equality, so JSON stays on the comparing
732+
// slow path.
733+
if lhs.Type == sqltypes.TypeJSON {
734+
return nil
735+
}
736+
730737
var (
731738
table = make(map[vthash.Hash]struct{})
732739
hasher = vthash.New()

0 commit comments

Comments
 (0)