#21211·vitess

Bug Report: evalengine LOWER/UPPER over a JSON value keep the JSON type and can panic

Author: GrahamCampbellCreated Sep 24, 2026Updated Sep 30, 2026
LabelsType: BugComponent: Query Serving

Overview of the Issue

LOWER, LCASE, UPPER and UCASE over a JSON value behave differently in the evalengine's interpreter and its compiler, and neither matches MySQL. MySQL returns a character string built from the document's text: for UPPER(CAST('{"a":"b"}' AS JSON)) it returns {"A": "B"} with a LONG_BLOB utf8mb4 result column, and COLLATION() of the result is utf8mb4_bin.

Both paths are in builtinChangeCase on main at 38d6627b56. The interpreter's eval case-maps *evalBytes and converts numerics, and returns every other value unchanged, so a *json.Value comes back untouched and still typed JSON: UPPER(j) does not uppercase anything.

https://github.com/vitessio/vitess/blob/38d6627b56f7dc263b58c2084908f2bd6602d57d/go/vt/vtgate/evalengine/fn_string.go#L590-L620

The compiler's compile emits Convert_xc to VARCHAR for any non-textual argument and then Fn_LUCASE, so at run time the stack holds a case-mapped *evalBytes, but it returns the argument's own ctype, which for a JSON argument is TypeJSON.

https://github.com/vitessio/vitess/blob/38d6627b56f7dc263b58c2084908f2bd6602d57d/go/vt/vtgate/evalengine/fn_string.go#L622-L639

Any consumer that trusts that static type then mishandles the run-time value. A comparison such as LOWER(j) = '{"a": "b"}' sees a JSON operand, takes the compareAsJSON branch and emits CmpJSON:

https://github.com/vitessio/vitess/blob/38d6627b56f7dc263b58c2084908f2bd6602d57d/go/vt/vtgate/evalengine/expr_compare.go#L424-L427

https://github.com/vitessio/vitess/blob/38d6627b56f7dc263b58c2084908f2bd6602d57d/go/vt/vtgate/evalengine/compiler.go#L687-L699

CmpJSON asserts *evalJSON on both stack slots and panics on the *evalBytes that Fn_LUCASE left there:

https://github.com/vitessio/vitess/blob/38d6627b56f7dc263b58c2084908f2bd6602d57d/go/vt/vtgate/evalengine/compiler_asm.go#L729-L738

interface conversion: evalengine.eval is *evalengine.evalBytes, not *json.Value

The interpreter does not panic on the same expression but returns the wrong answer: it compares the untouched JSON object against the string as a JSON comparison and returns 0, where MySQL returns 1.

The fix is for compile to return a VARCHAR ctype (with the collation Convert_xc applied, or the argument's own collation when it was already textual) after Fn_LUCASE, and for eval to convert a *json.Value to its text before case-mapping it, so both paths return text the way MySQL does.

Reproduction Steps

Against MySQL 8.4.11:

sql
SELECT UPPER(CAST('{"a":"b"}' AS JSON)) AS upper_json, JSON_TYPE(CAST('{"a":"b"}' AS JSON)) AS json_type;
SELECT LOWER(CAST('{"a":"b"}' AS JSON)) = '{"a": "b"}' AS lower_eq_text;
SELECT COLLATION(UPPER(CAST('{"a":"b"}' AS JSON))) AS upper_collation;
+------------+-----------+
| upper_json | json_type |
+------------+-----------+
| {"A": "B"} | OBJECT    |
+------------+-----------+
+---------------+
| lower_eq_text |
+---------------+
|             1 |
+---------------+
+-----------------+
| upper_collation |
+-----------------+
| utf8mb4_bin     |
+-----------------+

With mysql --column-type-info, the upper_json column is reported as Type: LONG_BLOB, Collation: utf8mb4_0900_ai_ci (255), Flags: BINARY, while the JSON argument itself is Type: JSON. A JSON column in a table behaves the same as the cast: SELECT UPPER(j), LOWER(j) = '{"a": "b"}' FROM t returns {"A": "B"} and 1.

The same expressions evaluated by the evalengine on main at 38d6627b56, through the evalengine.Translate harness used by TestCompilerSingle (NoConstantFolding set, column0 resolved as a TypeJSON field holding {"a": "b"}, then EvaluateAST for the interpreter and Evaluate for the compiled program). This is the engine VTGate runs whenever it evaluates an expression itself rather than pushing it down to MySQL; I have not run these through a full cluster.

Expression MySQL 8.4.11 interpreted compiled
upper(column0) {"A": "B"} (text) JSON("{\"a\": \"b\"}") VARCHAR("{\"A\": \"B\"}")
lower(column0) {"a": "b"} (text) JSON("{\"a\": \"b\"}") VARCHAR("{\"a\": \"b\"}")
upper(column0) = '{"A": "B"}' 1 INT64(0) panic
lower(column0) = '{"a": "b"}' 1 INT64(0) panic
upper(json_object('a', 'b')) {"A": "B"} (text) JSON("{\"a\": \"b\"}") VARCHAR("{\"A\": \"B\"}")
lower(json_object('a', 'b')) = '{"a": "b"}' 1 INT64(0) panic

The harness, dropped into go/vt/vtgate/evalengine as an external test next to compiler_test.go (which provides makeFields):

go
func issueEval(t *testing.T, expression string, values []sqltypes.Value, compiled bool) (out string) {
	t.Helper()
	venv := vtenv.NewTestEnv()
	expr, err := venv.Parser().ParseExpr(expression)
	if err != nil {
		t.Fatal(err)
	}
	fields := evalengine.FieldResolver(makeFields(values))
	cfg := &evalengine.Config{
		ResolveColumn:     fields.Column,
		ResolveType:       fields.Type,
		Collation:         collations.CollationUtf8mb4ID,
		Environment:       venv,
		NoConstantFolding: true,
		NoCompilation:     !compiled,
	}
	converted, err := evalengine.Translate(expr, cfg)
	if err != nil {
		return "translate error: " + err.Error()
	}
	env := evalengine.NewExpressionEnv(t.Context(), nil, evalengine.NewEmptyVCursor(venv, nil))
	env.Row = values
	defer func() {
		if r := recover(); r != nil {
			out = fmt.Sprintf("PANIC: %v", r)
		}
	}()
	var res evalengine.EvalResult
	if compiled {
		res, err = env.Evaluate(converted)
	} else {
		res, err = env.EvaluateAST(converted)
	}
	if err != nil {
		return "error: " + err.Error()
	}
	return res.String()
}

func TestIssueChangeCaseJSON(t *testing.T) {
	j := []sqltypes.Value{sqltypes.MakeTrusted(sqltypes.TypeJSON, []byte(`{"a": "b"}`))}
	for _, expression := range []string{
		`upper(column0)`,
		`lower(column0)`,
		`upper(column0) = '{"A": "B"}'`,
		`lower(column0) = '{"a": "b"}'`,
		`upper(json_object('a', 'b'))`,
		`lower(json_object('a', 'b')) = '{"a": "b"}'`,
	} {
		t.Logf("%-40s interpreted: %-32s compiled: %s", expression, issueEval(t, expression, j, false), issueEval(t, expression, j, true))
	}
}

Binary Version

bash
main at 38d6627b56 (vitessio/vitess), MySQL 8.4.11

Operating System and Environment details

bash
macOS (Darwin 25.6.0), arm64, go1.27.1; MySQL 8.4.11 in Docker (mysql:8.4)

Log Fragments

bash
=== RUN   TestIssueChangeCaseJSON
    zz_issue_test.go:74: upper(column0)                           interpreted: JSON("{\"a\": \"b\"}")           compiled: VARCHAR("{\"A\": \"B\"}")
    zz_issue_test.go:74: lower(column0)                           interpreted: JSON("{\"a\": \"b\"}")           compiled: VARCHAR("{\"a\": \"b\"}")
    zz_issue_test.go:74: upper(column0) = '{"A": "B"}'            interpreted: INT64(0)                         compiled: PANIC: interface conversion: evalengine.eval is *evalengine.evalBytes, not *json.Value
    zz_issue_test.go:74: lower(column0) = '{"a": "b"}'            interpreted: INT64(0)                         compiled: PANIC: interface conversion: evalengine.eval is *evalengine.evalBytes, not *json.Value
    zz_issue_test.go:74: upper(json_object('a', 'b'))             interpreted: JSON("{\"a\": \"b\"}")           compiled: VARCHAR("{\"A\": \"B\"}")
    zz_issue_test.go:74: lower(json_object('a', 'b')) = '{"a": "b"}' interpreted: INT64(0)                         compiled: PANIC: interface conversion: evalengine.eval is *evalengine.evalBytes, not *json.Value
--- PASS: TestIssueChangeCaseJSON (0.00s)

panic: interface conversion: evalengine.eval is *evalengine.evalBytes, not *json.Value
vitess.io/vitess/go/vt/vtgate/evalengine.(*assembler).CmpJSON.func1(...)
	go/vt/vtgate/evalengine/compiler_asm.go:732
vitess.io/vitess/go/vt/vtgate/evalengine.(*ExpressionEnv).EvaluateVM(...)
	go/vt/vtgate/evalengine/vm.go:85
vitess.io/vitess/go/vt/vtgate/evalengine.(*ExpressionEnv).Evaluate(...)