Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::CastRules Class Reference

Contains a list of rules for casting. More...

Static Public Member Functions

static int64_t ImplicitCast (const LogicalType &from, const LogicalType &to)
 Returns the cost of performing an implicit cost from "from" to "to", or -1 if an implicit cast is not possible.

Detailed Description

Contains a list of rules for casting.

Member Function Documentation

◆ ImplicitCast()

int64_t duckdb::CastRules::ImplicitCast ( const LogicalType & from,
const LogicalType & to )
static

Returns the cost of performing an implicit cost from "from" to "to", or -1 if an implicit cast is not possible.

219858 {
219859 if (from.id() == LogicalTypeId::SQLNULL && to.id() == LogicalTypeId::TEMPLATE) {
219860 // Prefer the TEMPLATE type for NULL casts, as it is the most generic
219861 return 5;
219862 }
219863 if (from.id() == LogicalTypeId::SQLNULL || to.id() == LogicalTypeId::ANY || to.id() == LogicalTypeId::TEMPLATE) {
219864 // NULL expression can be cast to anything
219865 return TargetTypeCost(to);
219866 }
219867 if (from.id() == LogicalTypeId::ANY && to.IsTemplated()) {
219868 // This can happen when changing a function from using ANY to using TEMPLATE.
219869 return TargetTypeCost(to);
219870 }
219871 if (from.id() == LogicalTypeId::UNKNOWN) {
219872 // parameter expression can be cast to anything for no cost
219873 return 0;
219874 }
219875 if (from.id() == LogicalTypeId::STRING_LITERAL) {
219876 // string literals can be cast to any type for low cost as long as the type is valid
219877 // i.e. we cannot cast to LIST(ANY) as we don't know what "ANY" should be
219878 // we cannot cast to DECIMAL without precision/width specified
219879 if (!LogicalTypeIsValid(to)) {
219880 return -1;
219881 }
219882 if (to.id() == LogicalTypeId::VARCHAR && to.GetAlias().empty()) {
219883 return 1;
219884 }
219885 return 20;
219886 }
219887 if (from.id() == LogicalTypeId::INTEGER_LITERAL) {
219888 // the integer literal has an underlying type - this type always matches
219889 if (IntegerLiteral::GetType(from).id() == to.id()) {
219890 return 0;
219891 }
219892 // integer literals can be cast to any other integer type for a low cost, but only if the literal fits
219893 if (IntegerLiteral::FitsInType(from, to)) {
219894 // to avoid ties we prefer BIGINT, INT, ...
219895 auto target_cost = TargetTypeCost(to);
219896 if (target_cost < 100) {
219897 throw InternalException("Integer literal implicit cast - TargetTypeCost should be >= 100");
219898 }
219899 return target_cost - 90;
219900 }
219901 // in any other case we use the casting rules of the preferred type of the literal
219903 }
219904 if (from.GetAlias() != to.GetAlias()) {
219905 // if aliases are different, an implicit cast is not possible
219906 return -1;
219907 }
219908 if (from.id() == LogicalTypeId::LIST && to.id() == LogicalTypeId::LIST) {
219909 // Lists can be cast if their child types can be cast
219910 auto child_cost = ImplicitCast(ListType::GetChildType(from), ListType::GetChildType(to));
219911 if (child_cost >= 1) {
219912 // subtract one from the cost because we prefer LIST[X] -> LIST[VARCHAR] over LIST[X] -> VARCHAR
219913 child_cost--;
219914 }
219915 return child_cost;
219916 }
219917 if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::ARRAY) {
219918 // Arrays can be cast if their child types can be cast and the source and target has the same size
219919 // or the target type has a unknown (any) size.
219920 auto from_size = ArrayType::GetSize(from);
219921 auto to_size = ArrayType::GetSize(to);
219922 auto to_is_any_size = ArrayType::IsAnySize(to);
219923 if (from_size == to_size || to_is_any_size) {
219924 auto child_cost = ImplicitCast(ArrayType::GetChildType(from), ArrayType::GetChildType(to));
219925 if (child_cost >= 100) {
219926 // subtract one from the cost because we prefer ARRAY[X] -> ARRAY[VARCHAR] over ARRAY[X] -> VARCHAR
219927 child_cost--;
219928 }
219929 return child_cost;
219930 }
219931 return -1; // Not possible if the sizes are different
219932 }
219933 if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::LIST) {
219934 // Arrays can be cast to lists for the cost of casting the child type
219935 auto child_cost = ImplicitCast(ArrayType::GetChildType(from), ListType::GetChildType(to));
219936 if (child_cost < 0) {
219937 return -1;
219938 }
219939 // add 1 because we prefer ARRAY->ARRAY casts over ARRAY->LIST casts
219940 return child_cost + 1;
219941 }
219942 if (from.id() == LogicalTypeId::LIST && (to.id() == LogicalTypeId::ARRAY && !ArrayType::IsAnySize(to))) {
219943 // Lists can be cast to arrays for the cost of casting the child type, if the target size is known
219944 // there is no way for us to resolve the size at bind-time without inspecting the list values.
219945 // TODO: if we can access the expression we could resolve the size if the list is constant.
219946 return ImplicitCast(ListType::GetChildType(from), ArrayType::GetChildType(to));
219947 }
219948 if (from.id() == LogicalTypeId::UNION && to.id() == LogicalTypeId::UNION) {
219949 // Check that the target union type is fully resolved.
219950 if (to.AuxInfo() == nullptr) {
219951 // If not, try anyway and let the actual cast logic handle it.
219952 // This is to allow passing unions into functions that take a generic union type (without specifying member
219953 // types) as an argument.
219954 return 0;
219955 }
219956 // Unions can be cast if the source tags are a subset of the target tags
219957 // in which case the most expensive cost is used
219958 int64_t cost = -1;
219959 for (idx_t from_member_idx = 0; from_member_idx < UnionType::GetMemberCount(from); from_member_idx++) {
219960 auto &from_member_name = UnionType::GetMemberName(from, from_member_idx);
219961
219962 bool found = false;
219963 for (idx_t to_member_idx = 0; to_member_idx < UnionType::GetMemberCount(to); to_member_idx++) {
219964 auto &to_member_name = UnionType::GetMemberName(to, to_member_idx);
219965
219966 if (StringUtil::CIEquals(from_member_name, to_member_name)) {
219967 auto &from_member_type = UnionType::GetMemberType(from, from_member_idx);
219968 auto &to_member_type = UnionType::GetMemberType(to, to_member_idx);
219969
219970 auto child_cost = ImplicitCast(from_member_type, to_member_type);
219971 cost = MaxValue(cost, child_cost);
219972 found = true;
219973 break;
219974 }
219975 }
219976 if (!found) {
219977 return -1;
219978 }
219979 }
219980 return cost;
219981 }
219982 if (from.id() == LogicalTypeId::STRUCT && to.id() == LogicalTypeId::STRUCT) {
219983 if (to.AuxInfo() == nullptr) {
219984 // If this struct is not fully resolved, we'll leave it to the actual cast logic to handle it.
219985 return 0;
219986 }
219987
219988 auto &source_children = StructType::GetChildTypes(from);
219989 auto &target_children = StructType::GetChildTypes(to);
219990
219991 if (source_children.size() != target_children.size()) {
219992 // different number of children: not possible
219993 return -1;
219994 }
219995
219996 auto target_is_unnamed = StructType::IsUnnamed(to);
219997 auto source_is_unnamed = StructType::IsUnnamed(from);
219998 auto named_struct_cast = !source_is_unnamed && !target_is_unnamed;
219999
220000 int64_t cost = -1;
220001 if (named_struct_cast) {
220002 // Collect the target members in a map for easy lookup
220003 case_insensitive_map_t<idx_t> target_members;
220004 for (idx_t target_idx = 0; target_idx < target_children.size(); target_idx++) {
220005 auto &target_name = target_children[target_idx].first;
220006 if (target_members.find(target_name) != target_members.end()) {
220007 // duplicate name in target struct
220008 return -1;
220009 }
220010 target_members[target_name] = target_idx;
220011 }
220012 // Match the source members to the target members by name
220013 for (idx_t source_idx = 0; source_idx < source_children.size(); source_idx++) {
220014 auto &source_child = source_children[source_idx];
220015 auto entry = target_members.find(source_child.first);
220016 if (entry == target_members.end()) {
220017 // element in source struct was not found in target struct
220018 return -1;
220019 }
220020 auto target_idx = entry->second;
220021 target_members.erase(entry);
220022 auto child_cost = ImplicitCast(source_child.second, target_children[target_idx].second);
220023 if (child_cost == -1) {
220024 return -1;
220025 }
220026 cost = MaxValue(cost, child_cost);
220027 }
220028 } else {
220029 // Match the source members to the target members by position
220030 for (idx_t i = 0; i < source_children.size(); i++) {
220031 auto &source_child = source_children[i];
220032 auto &target_child = target_children[i];
220033 auto child_cost = ImplicitCast(source_child.second, target_child.second);
220034 if (child_cost == -1) {
220035 return -1;
220036 }
220037 cost = MaxValue(cost, child_cost);
220038 }
220039 }
220040 return cost;
220041 }
220042
220043 if (from.id() == to.id()) {
220044 // arguments match: do nothing
220045 return 0;
220046 }
220047
220048 // Special case: Anything can be cast to a union if the source type is a member of the union
220049 if (to.id() == LogicalTypeId::UNION) {
220050 // check that the union type is fully resolved.
220051 if (to.AuxInfo() == nullptr) {
220052 return -1;
220053 }
220054 // check if the union contains something castable from the source type
220055 // in which case the least expensive (most specific) cast should be used
220056 bool found = false;
220057 auto cost = NumericLimits<int64_t>::Maximum();
220058 for (idx_t i = 0; i < UnionType::GetMemberCount(to); i++) {
220059 auto target_member = UnionType::GetMemberType(to, i);
220060 auto target_cost = ImplicitCast(from, target_member);
220061 if (target_cost != -1) {
220062 found = true;
220063 cost = MinValue(cost, target_cost);
220064 }
220065 }
220066 return found ? cost : -1;
220067 }
220068
220069 switch (from.id()) {
220070 case LogicalTypeId::TINYINT:
220071 return ImplicitCastTinyint(to);
220072 case LogicalTypeId::SMALLINT:
220073 return ImplicitCastSmallint(to);
220074 case LogicalTypeId::INTEGER:
220075 return ImplicitCastInteger(to);
220076 case LogicalTypeId::BIGINT:
220077 return ImplicitCastBigint(to);
220078 case LogicalTypeId::UTINYINT:
220079 return ImplicitCastUTinyint(to);
220080 case LogicalTypeId::USMALLINT:
220081 return ImplicitCastUSmallint(to);
220082 case LogicalTypeId::UINTEGER:
220083 return ImplicitCastUInteger(to);
220084 case LogicalTypeId::UBIGINT:
220085 return ImplicitCastUBigint(to);
220086 case LogicalTypeId::HUGEINT:
220087 return ImplicitCastHugeint(to);
220088 case LogicalTypeId::UHUGEINT:
220089 return ImplicitCastUhugeint(to);
220090 case LogicalTypeId::FLOAT:
220091 return ImplicitCastFloat(to);
220092 case LogicalTypeId::DOUBLE:
220093 return ImplicitCastDouble(to);
220094 case LogicalTypeId::DATE:
220095 return ImplicitCastDate(to);
220096 case LogicalTypeId::DECIMAL:
220097 return ImplicitCastDecimal(to);
220098 case LogicalTypeId::ENUM:
220099 return ImplicitCastEnum(to);
220100 case LogicalTypeId::TIMESTAMP_SEC:
220101 return ImplicitCastTimestampSec(to);
220102 case LogicalTypeId::TIMESTAMP_MS:
220103 return ImplicitCastTimestampMS(to);
220105 return ImplicitCastTimestampNS(to);
220106 case LogicalTypeId::TIMESTAMP:
220107 return ImplicitCastTimestamp(to);
220108 case LogicalTypeId::BIGNUM:
220109 return ImplicitCastBignum(to);
220110 case LogicalTypeId::VARIANT:
220111 return ImplicitCastVariant(to);
220112 default:
220113 return -1;
220114 }
220115}
static int64_t ImplicitCast(const LogicalType &from, const LogicalType &to)
Returns the cost of performing an implicit cost from "from" to "to", or -1 if an implicit cast is not...
Definition duckdb.cpp:219858
static DUCKDB_API bool CIEquals(const string &l1, const string &l2)
Case insensitive equals.
Definition duckdb.cpp:109696
::int64_t int64_t
@ TIMESTAMP_NS
us
Definition duckdb.hpp:2886
static int64_t TargetTypeCost(const LogicalType &type)
The target type determines the preferred implicit casts.
Definition duckdb.cpp:219510
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static DUCKDB_API bool FitsInType(const LogicalType &type, const LogicalType &target)
Whether or not the integer literal fits into the target numeric type.
Definition duckdb.cpp:140905
static DUCKDB_API LogicalType GetType(const LogicalType &type)
Returns the type that this integer literal "prefers".
Definition duckdb.cpp:140898
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: