Skip to content

Conversation

y2kwak
Copy link
Contributor

@y2kwak y2kwak commented Aug 25, 2025

Description

Currently, when using optimizer trace, the expanded_query field can produce invalid JSON when queries contain special characters like quotes or backslashes. This makes the JSON output invalid and difficult to parse programmatically.

This PR modifies the expanded_query output to properly escape special characters using json_escape(), ensuring valid JSON output while maintaining readability.

It also includes MTR tests to verify the fix.

How can this PR be tested?

Enable optimizer trace and run queries with special characters:

SET optimizer_trace='enabled=on';
SELECT LENGTH("a");
SELECT json_valid(trace), trace FROM information_schema.optimizer_trace;

Before change:

{
            "expanded_query": "select octet_length('a') AS `LENGTH("a")`"
}

After change:

{
            "expanded_query": "select octet_length('a') AS `LENGTH(\"a\")`"
}

Additional test cases:

  • Nested quotes: SELECT LENGTH("a\"b");
{
    "expanded_query": "select octet_length('a\"b') AS `LENGTH(\"a\\\"b\")`"
}
  • Special characters with quotes: SELECT LENGTH("a\n\"b\t\"c");
{
    "expanded_query": "select octet_length('a\\n\"b\\t\"c') AS `LENGTH(\"a\\n\\\"b\\t\\\"c\")`"
}
  • New line:
SELECT COLUMN_JSON(COLUMN_CREATE("foo", "New
line" AS CHAR));
{
    "expanded_query": "select column_json(column_create('foo','New\\nline' AS char charset latin1 collate latin1_swedish_ci )) AS `COLUMN_JSON(COLUMN_CREATE(\"foo\", \"New\nline\" AS CHAR))`"}

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.

Copyright

All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.

@CLAassistant
Copy link

CLAassistant commented Aug 25, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@y2kwak y2kwak changed the base branch from main to 10.11 August 25, 2025 17:40
@y2kwak y2kwak force-pushed the MDEV-30354 branch 2 times, most recently from 2523483 to 50d1ce0 Compare August 25, 2025 17:45
@y2kwak y2kwak changed the base branch from 10.11 to main August 25, 2025 17:45
@svoj svoj added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Aug 27, 2025
Copy link
Contributor

@svoj svoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@y2kwak thanks for your contribution! A few minor issues outlined.

The expanded_query field in optimizer trace could produce invalid JSON when
queries contained special characters. Use json_escape() to properly handle
quotes, backslashes and other special characters in the output.
If json_escape() fails, emit an error message in the expanded_query.

Example:
Before: "expanded_query": "select length('a') AS `LENGTH("a")`"
After:  "expanded_query": "select length('a') AS `LENGTH(\"a\")`"

The output is now properly escaped and valid JSON while maintaining
readability.

Added test cases to mysql-test/main/opt_trace.test to verify the fix.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.
Development

Successfully merging this pull request may close these issues.

3 participants