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
219861 return 5;
219862 }
219863 if (from.id() == LogicalTypeId::SQLNULL || to.id() == LogicalTypeId::ANY || to.id() == LogicalTypeId::TEMPLATE) {
219864
219866 }
219867 if (from.id() == LogicalTypeId::ANY && to.IsTemplated()) {
219868
219870 }
219871 if (from.id() == LogicalTypeId::UNKNOWN) {
219872
219873 return 0;
219874 }
219875 if (from.id() == LogicalTypeId::STRING_LITERAL) {
219876
219877
219878
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
219890 return 0;
219891 }
219892
219894
219896 if (target_cost < 100) {
219897 throw InternalException("Integer literal implicit cast - TargetTypeCost should be >= 100");
219898 }
219899 return target_cost - 90;
219900 }
219901
219903 }
219904 if (from.GetAlias() != to.GetAlias()) {
219905
219906 return -1;
219907 }
219908 if (from.id() == LogicalTypeId::LIST && to.id() == LogicalTypeId::LIST) {
219909
219910 auto child_cost =
ImplicitCast(ListType::GetChildType(from), ListType::GetChildType(to));
219911 if (child_cost >= 1) {
219912
219913 child_cost--;
219914 }
219915 return child_cost;
219916 }
219917 if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::ARRAY) {
219918
219919
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
219927 child_cost--;
219928 }
219929 return child_cost;
219930 }
219931 return -1;
219932 }
219933 if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::LIST) {
219934
219935 auto child_cost =
ImplicitCast(ArrayType::GetChildType(from), ListType::GetChildType(to));
219936 if (child_cost < 0) {
219937 return -1;
219938 }
219939
219940 return child_cost + 1;
219941 }
219942 if (from.id() == LogicalTypeId::LIST && (to.id() == LogicalTypeId::ARRAY && !ArrayType::IsAnySize(to))) {
219943
219944
219945
219946 return ImplicitCast(ListType::GetChildType(from), ArrayType::GetChildType(to));
219947 }
219948 if (from.id() == LogicalTypeId::UNION && to.id() == LogicalTypeId::UNION) {
219949
219950 if (to.AuxInfo() == nullptr) {
219951
219952
219953
219954 return 0;
219955 }
219956
219957
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
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
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
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
220001 if (named_struct_cast) {
220002
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
220008 return -1;
220009 }
220010 target_members[target_name] = target_idx;
220011 }
220012
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
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
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
220045 return 0;
220046 }
220047
220048
220049 if (to.id() == LogicalTypeId::UNION) {
220050
220051 if (to.AuxInfo() == nullptr) {
220052 return -1;
220053 }
220054
220055
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
@ 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