1#include "duckdb/function/cast/default_casts.hpp"
2#include "duckdb/function/cast/vector_cast_helpers.hpp"
3#include "duckdb/common/operator/string_cast.hpp"
4namespace duckdb {
5
6BoundCastInfo DefaultCasts::DateCastSwitch(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
7 // now switch on the result type
8 switch (target.id()) {
9 case LogicalTypeId::VARCHAR:
10 // date to varchar
11 return BoundCastInfo(&VectorCastHelpers::StringCast<date_t, duckdb::StringCast>);
12 case LogicalTypeId::TIMESTAMP:
13 case LogicalTypeId::TIMESTAMP_TZ:
14 // date to timestamp
15 return BoundCastInfo(&VectorCastHelpers::TryCastLoop<date_t, timestamp_t, duckdb::TryCast>);
16 case LogicalTypeId::TIMESTAMP_NS:
17 return BoundCastInfo(&VectorCastHelpers::TryCastLoop<date_t, timestamp_t, duckdb::TryCastToTimestampNS>);
18 case LogicalTypeId::TIMESTAMP_SEC:
19 return BoundCastInfo(&VectorCastHelpers::TryCastLoop<date_t, timestamp_t, duckdb::TryCastToTimestampSec>);
20 case LogicalTypeId::TIMESTAMP_MS:
21 return BoundCastInfo(&VectorCastHelpers::TryCastLoop<date_t, timestamp_t, duckdb::TryCastToTimestampMS>);
22 default:
23 return TryVectorNullCast;
24 }
25}
26
27BoundCastInfo DefaultCasts::TimeCastSwitch(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
28 // now switch on the result type
29 switch (target.id()) {
30 case LogicalTypeId::VARCHAR:
31 // time to varchar
32 return BoundCastInfo(&VectorCastHelpers::StringCast<dtime_t, duckdb::StringCast>);
33 case LogicalTypeId::TIME_TZ:
34 // time to time with time zone
35 return ReinterpretCast;
36 default:
37 return TryVectorNullCast;
38 }
39}
40
41BoundCastInfo DefaultCasts::TimeTzCastSwitch(BindCastInput &input, const LogicalType &source,
42 const LogicalType &target) {
43 // now switch on the result type
44 switch (target.id()) {
45 case LogicalTypeId::VARCHAR:
46 // time with time zone to varchar
47 return BoundCastInfo(&VectorCastHelpers::StringCast<dtime_t, duckdb::StringCastTZ>);
48 case LogicalTypeId::TIME:
49 // time with time zone to time
50 return ReinterpretCast;
51 default:
52 return TryVectorNullCast;
53 }
54}
55
56BoundCastInfo DefaultCasts::TimestampCastSwitch(BindCastInput &input, const LogicalType &source,
57 const LogicalType &target) {
58 // now switch on the result type
59 switch (target.id()) {
60 case LogicalTypeId::VARCHAR:
61 // timestamp to varchar
62 return BoundCastInfo(&VectorCastHelpers::StringCast<timestamp_t, duckdb::StringCast>);
63 case LogicalTypeId::DATE:
64 // timestamp to date
65 return BoundCastInfo(&VectorCastHelpers::TemplatedCastLoop<timestamp_t, date_t, duckdb::Cast>);
66 case LogicalTypeId::TIME:
67 case LogicalTypeId::TIME_TZ:
68 // timestamp to time
69 return BoundCastInfo(&VectorCastHelpers::TemplatedCastLoop<timestamp_t, dtime_t, duckdb::Cast>);
70 case LogicalTypeId::TIMESTAMP_TZ:
71 // timestamp (us) to timestamp with time zone
72 return ReinterpretCast;
73 case LogicalTypeId::TIMESTAMP_NS:
74 // timestamp (us) to timestamp (ns)
75 return BoundCastInfo(
76 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampUsToNs>);
77 case LogicalTypeId::TIMESTAMP_MS:
78 // timestamp (us) to timestamp (ms)
79 return BoundCastInfo(
80 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampUsToMs>);
81 case LogicalTypeId::TIMESTAMP_SEC:
82 // timestamp (us) to timestamp (s)
83 return BoundCastInfo(
84 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampUsToSec>);
85 default:
86 return TryVectorNullCast;
87 }
88}
89
90BoundCastInfo DefaultCasts::TimestampTzCastSwitch(BindCastInput &input, const LogicalType &source,
91 const LogicalType &target) {
92 // now switch on the result type
93 switch (target.id()) {
94 case LogicalTypeId::VARCHAR:
95 // timestamp with time zone to varchar
96 return BoundCastInfo(&VectorCastHelpers::StringCast<timestamp_t, duckdb::StringCastTZ>);
97 case LogicalTypeId::TIME_TZ:
98 // timestamp with time zone to time with time zone.
99 // TODO: set the offset to +00
100 return BoundCastInfo(&VectorCastHelpers::TemplatedCastLoop<timestamp_t, dtime_t, duckdb::Cast>);
101 case LogicalTypeId::TIMESTAMP:
102 // timestamp with time zone to timestamp (us)
103 return ReinterpretCast;
104 default:
105 return TryVectorNullCast;
106 }
107}
108
109BoundCastInfo DefaultCasts::TimestampNsCastSwitch(BindCastInput &input, const LogicalType &source,
110 const LogicalType &target) {
111 // now switch on the result type
112 switch (target.id()) {
113 case LogicalTypeId::VARCHAR:
114 // timestamp (ns) to varchar
115 return BoundCastInfo(&VectorCastHelpers::StringCast<timestamp_t, duckdb::CastFromTimestampNS>);
116 case LogicalTypeId::TIMESTAMP:
117 // timestamp (ns) to timestamp (us)
118 return BoundCastInfo(
119 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampNsToUs>);
120 default:
121 return TryVectorNullCast;
122 }
123}
124
125BoundCastInfo DefaultCasts::TimestampMsCastSwitch(BindCastInput &input, const LogicalType &source,
126 const LogicalType &target) {
127 // now switch on the result type
128 switch (target.id()) {
129 case LogicalTypeId::VARCHAR:
130 // timestamp (ms) to varchar
131 return BoundCastInfo(&VectorCastHelpers::StringCast<timestamp_t, duckdb::CastFromTimestampMS>);
132 case LogicalTypeId::TIMESTAMP:
133 // timestamp (ms) to timestamp (us)
134 return BoundCastInfo(
135 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampMsToUs>);
136 default:
137 return TryVectorNullCast;
138 }
139}
140
141BoundCastInfo DefaultCasts::TimestampSecCastSwitch(BindCastInput &input, const LogicalType &source,
142 const LogicalType &target) {
143 // now switch on the result type
144 switch (target.id()) {
145 case LogicalTypeId::VARCHAR:
146 // timestamp (sec) to varchar
147 return BoundCastInfo(&VectorCastHelpers::StringCast<timestamp_t, duckdb::CastFromTimestampSec>);
148 case LogicalTypeId::TIMESTAMP:
149 // timestamp (s) to timestamp (us)
150 return BoundCastInfo(
151 &VectorCastHelpers::TemplatedCastLoop<timestamp_t, timestamp_t, duckdb::CastTimestampSecToUs>);
152 default:
153 return TryVectorNullCast;
154 }
155}
156BoundCastInfo DefaultCasts::IntervalCastSwitch(BindCastInput &input, const LogicalType &source,
157 const LogicalType &target) {
158 // now switch on the result type
159 switch (target.id()) {
160 case LogicalTypeId::VARCHAR:
161 // time to varchar
162 return BoundCastInfo(&VectorCastHelpers::StringCast<interval_t, duckdb::StringCast>);
163 default:
164 return TryVectorNullCast;
165 }
166}
167
168} // namespace duckdb
169