Skip to content
Open
Show file tree
Hide file tree
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
66 changes: 66 additions & 0 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,69 @@ func TestCompilerNonConstant(t *testing.T) {
})
}
}

// TestJSONInStaticTable checks that IN over folded JSON literals does not
// compile the static hash table: JSON arrays and objects hash by kind and
// cardinality only, so a hash hit is not equality. Compilation fails until
// the compiler learns to push JSON literals; the follow-up literal change
// flips this test to compiled evaluation.
func TestJSONInStaticTable(t *testing.T) {
testCases := []struct {
expression string
values []sqltypes.Value
result string
}{
{
expression: `column0 IN (JSON_ARRAY(2))`,
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`[1]`))},
result: `INT64(0)`,
},
{
expression: `column0 IN (JSON_OBJECT('b', 2))`,
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`{"a": 1}`))},
result: `INT64(0)`,
},
{
expression: `column0 IN (JSON_ARRAY(1))`,
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`[1]`))},
result: `INT64(1)`,
},
{
expression: `column0 IN (JSON_OBJECT('a', 1))`,
values: []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`{"a": 1}`))},
result: `INT64(1)`,
},
}

venv := vtenv.NewTestEnv()
for _, tc := range testCases {
t.Run(tc.expression, func(t *testing.T) {
expr, err := venv.Parser().ParseExpr(tc.expression)
require.NoError(t, err)

fields := evalengine.FieldResolver(makeFields(tc.values))
cfg := &evalengine.Config{
ResolveColumn: fields.Column,
Collation: collations.CollationUtf8mb4ID,
Environment: venv,
}

translated, err := evalengine.Translate(expr, cfg)
require.NoError(t, err)

untyped, ok := translated.(*evalengine.UntypedExpr)
require.True(t, ok)

env := evalengine.EmptyExpressionEnv(venv)
env.Row = tc.values
env.Fields = makeFields(tc.values)

res, err := env.EvaluateAST(untyped)
require.NoError(t, err)
assert.Equal(t, tc.result, res.String())

_, err = untyped.Compile(env)
require.ErrorContains(t, err, "unsupported literal kind")
})
}
}
7 changes: 7 additions & 0 deletions go/vt/vtgate/evalengine/expr_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ func (i *InExpr) eval(env *ExpressionEnv) (eval, error) {
}

func (i *InExpr) compileTable(lhs ctype, rhs TupleExpr) map[vthash.Hash]struct{} {
// JSON hashes fingerprint arrays and objects by kind and cardinality
// only: a hash hit is not equality, so JSON stays on the comparing
// slow path.
if lhs.Type == sqltypes.TypeJSON {
return nil
}
Comment thread
GrahamCampbell marked this conversation as resolved.

var (
table = make(map[vthash.Hash]struct{})
hasher = vthash.New()
Expand Down
Loading