WL#11935: Add folding of constants when compared to fields

Affects: Server-8.0   —   Status: Complete

We want to fold cases during optimize time where we currently handle type skews
at execution time. In this WL, we handle comparisons[1] between a field and a
constant value where the constant is of another type (or out of range) with
respect to the type of the field.

For comparison operators, we can let MySQL convert the type of the constant
to match the other expression, or perform constant folding if a conversion is
not possible.

For cases when both operands are non-constant, we need to inject CASTs to allow
the comparison operators to work on operands of the same type. This work is
to be covered by separate work.

In the special case that we are comparing a field to a constant, the work
described in this WL applies.

This WL will facilitate speed up at execution at the cost of a little more
analysis at optimize[2] time.

For example, given this table:

   CREATE TABLE t (ti TINYINT UNSIGNED NOT NULL);

the condition in this query:

   SELECT * FROM t WHERE ti < 256;

contains the integral constant 256 which is out range of TINYINT
[-128,127]. MySQL would earlier compare using full 64 bits lenght on both
operand, but that is overkill in this case: we can fold the WHERE expression to

   SELECT * FROM t WHERE TRUE 

which allows the optimizer to remove the WHERE expression.  If the field is
nullable, the expression is folded to

   SELECT * FROM t WHERE ti IS NOT NULL

to preserve SQL semantics.

[1] The comparison operands presently covered are >, >=, <, <=, <>, = and <=>.
    We do not fold constants used with BETWEEN and IN as part of this WL.

[2] For prepared statements, the constant's value is not known at prepare time,
so the conversion/constant folding of this WL is performed at optimize time.