As mentioned above,
THD is currently a required
argument and the runtime context for every function in the server.
Therefore, in order to call the parser and allocate memory in the
statement memory root we perform several save-restore steps with
THD::free_list (the active arena of
In order to parse a statement, we save the currently active arena of
THDand assign its members from the permanent arena of the statement. This is achieved by calling
THD::set_and_backup_active_arena. This way
yyparseoperate on the permanent arena.
We don't want the garbage which is created during statement validation to be left in the permanent arena of the statement. For that, after parse but before validation of the statement, we restore the THD arena saved in (1). In other words, we use the arena of
THDthat was active when
Prepared_statement::preparewas invoked as the runtime arena of the statement when it is validated.
Statement validation is performed in function
check_prepared_statement(). This function will subsequently call
setup_fields()for the main LEX unit, create
JOINsfor every unit, and call
JOIN::preparefor every join (
JOINsin MySQL represents a part of the execution plan). Our prepared statement engine does not save the execution plan in a prepared statement for reuse, and ideally we should not create it at prepare stage. However, currently there is no other way to validate a statement except to call
JOIN::preparefor all its units.
During validation we may perform a transformation of the parsed tree. In a clean implementation this would belong to a separate step, but in our case the majority of the server runtime was not refactored to support reexecution of statements, and a permanent transformation of the parsed tree can happen at any moment during validation. Such transformations absolutely must use the permanent arena of the prepared statement. To make this arena accessible, we save a pointer to it in
Later, whenever we need to perform a permanent transformation, we first call
THD::activate_stmt_arena_if_neededto make the permanent arena active, transform the tree, and restore the runtime arena.
Some parts of the execution do not distinguish between preparation of a prepared statement and its execution and perform destructive optimizations of the parsed tree even during validation. These changes of the parsed tree are recorded in
After the validation is done, we rollback the changes registered in
THD::change_listand free new items and other memory allocated by destructive transformations.