MySQL 9.0.0
Source Code Documentation
common_subexpression_elimination.cc File Reference
#include <assert.h>
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/sql_executor.h"
#include "sql/sql_list.h"
#include "template_utils.h"

Namespaces

namespace  anonymous_namespace{common_subexpression_elimination.cc}
 

Functions

Itemanonymous_namespace{common_subexpression_elimination.cc}::OrGroupWithSomeRemoved (Item_cond_or *or_item, const List< Item > &items_to_remove)
 For an OR disjunction, return a new disjunction with elements from “items_to_remove” logically set to TRUE (ie., removed). More...
 
bool anonymous_namespace{common_subexpression_elimination.cc}::IsOr (const Item *item)
 
bool anonymous_namespace{common_subexpression_elimination.cc}::AlwaysPresent (Item *expr, const Item *item)
 Check if “item” is necessary to make the expression true. More...
 
bool anonymous_namespace{common_subexpression_elimination.cc}::MatchesAny (Item *item, const List< Item > &items)
 Check if “item” matches any item in “items”. More...
 
void anonymous_namespace{common_subexpression_elimination.cc}::ExtractItemsExceptSome (Item_cond_and *and_item, const List< Item > &items_to_remove, List< Item > *output)
 For all items in an AND conjunction, add those (possibly none) that are not in “items_to_remove”. More...
 
ItemCommonSubexpressionElimination (Item *cond)
 Do simple CSE (common subexpression elimination) on “item”, and return the answer. More...
 

Function Documentation

◆ CommonSubexpressionElimination()

Item * CommonSubexpressionElimination ( Item cond)

Do simple CSE (common subexpression elimination) on “item”, and return the answer.

The CSE done is exclusively moving common expressions out of conjunctions-of-disjunctions, ie. it rewrites

(a AND b) OR (a AND c)

into

a AND (b OR c)

The primary motivation is that such split-out items are more versatile; they can be pushed independently, be made into hash join conditions etc. However, an added bonus is that the expressions will simply execute faster.

This function does not descend into subexpressions that are not AND/OR conjunctions, so e.g. an expression like

1 + ((a AND b) OR (a AND c))

will be left as-is.