Function sql::plan::typeconv::guess_best_common_type [−][src]
pub fn guess_best_common_type(
types: &[Option<ScalarType>],
type_hint: Option<&ScalarType>
) -> Option<ScalarType>
Expand description
Guesses the most-common type among a set of ScalarType
s that all members
can be cast to. Returns None
if a common type cannot be deduced.
The returned type is not guaranteed to be accurate because we ignore type
categories, e.g. on input [ScalarType::Date, ScalarType::Int32]
, will
guess that Date
is the common type.
However, if there is a common type among the input, it will correctly determine it, i.e. returns false positives but never false negatives.
The types
parameter is meant to represent the types inferred from a
Vec<CoercibleScalarExpr>
. If no known types are present in the types
parameter, it will try to use a provided type hint, instead.
Note that this function implements the type-determination components of
Postgres’ “UNION
, CASE
, and Related Constructs” type
conversion.
Type hints
Some types contain embedded values, e.g. ScalarType::Numeric
contains
the values’ scales. When choosing a common type, and the type_hint
is of
the type chosen, you should guess it because the given type hint can
contain a distinct embedded value necessary to retain type invariants.