|
45 | 45 | #include <DataTypes/DataTypesNumber.h> |
46 | 46 | #include <DataTypes/ObjectUtils.h> |
47 | 47 | #include <DataTypes/Serializations/SerializationDecimal.h> |
| 48 | +#include <DataTypes/getLeastSupertype.h> |
48 | 49 | #include <Formats/FormatSettings.h> |
49 | 50 | #include <Formats/FormatFactory.h> |
50 | 51 | #include <Functions/CastOverloadResolver.h> |
@@ -1573,6 +1574,55 @@ struct ConvertImpl |
1573 | 1574 | arguments, result_type, input_rows_count, additions); |
1574 | 1575 | } |
1575 | 1576 | } |
| 1577 | + else if constexpr (std::is_same_v<FromDataType, DataTypeInterval> && std::is_same_v<ToDataType, DataTypeInterval>) |
| 1578 | + { |
| 1579 | + IntervalKind to = typeid_cast<const DataTypeInterval *>(result_type.get())->getKind(); |
| 1580 | + IntervalKind from = typeid_cast<const DataTypeInterval *>(arguments[0].type.get())->getKind(); |
| 1581 | + |
| 1582 | + if (from == to) |
| 1583 | + return arguments[0].column; |
| 1584 | + |
| 1585 | + const auto &map = getGranularityMap(); |
| 1586 | + Int64 conversion_factor = 1; |
| 1587 | + Int64 result_value; |
| 1588 | + |
| 1589 | + int from_position = map.at(from).first; |
| 1590 | + int to_position = map.at(to).first; // Positions of each interval according to granurality map |
| 1591 | + |
| 1592 | + if (from_position < to_position) |
| 1593 | + { |
| 1594 | + for (int i = from_position - 1; i <= to_position; ++i) |
| 1595 | + { |
| 1596 | + // Find the kind that matches this position |
| 1597 | + for (const auto &entry : map) |
| 1598 | + { |
| 1599 | + if (entry.second.first == i) |
| 1600 | + { |
| 1601 | + conversion_factor *= entry.second.second; |
| 1602 | + break; |
| 1603 | + } |
| 1604 | + } |
| 1605 | + } |
| 1606 | + result_value = arguments[0].column->getInt(0) / conversion_factor; |
| 1607 | + } |
| 1608 | + else |
| 1609 | + { |
| 1610 | + for (int i = from_position - 1; i >= to_position; --i) |
| 1611 | + { |
| 1612 | + for (const auto &entry : map) |
| 1613 | + { |
| 1614 | + if (entry.second.first == i) |
| 1615 | + { |
| 1616 | + conversion_factor *= entry.second.second; |
| 1617 | + break; |
| 1618 | + } |
| 1619 | + } |
| 1620 | + } |
| 1621 | + result_value = arguments[0].column->getInt(0) * conversion_factor; |
| 1622 | + } |
| 1623 | + |
| 1624 | + return ColumnConst::create(ColumnInt64::create(1, result_value), input_rows_count); |
| 1625 | + } |
1576 | 1626 | else |
1577 | 1627 | { |
1578 | 1628 | using FromFieldType = typename FromDataType::FieldType; |
@@ -2181,7 +2231,7 @@ class FunctionConvert : public IFunction |
2181 | 2231 | const DataTypePtr from_type = removeNullable(arguments[0].type); |
2182 | 2232 | ColumnPtr result_column; |
2183 | 2233 |
|
2184 | | - [[maybe_unused]] FormatSettings::DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior; |
| 2234 | + FormatSettings::DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior; |
2185 | 2235 |
|
2186 | 2236 | if (context) |
2187 | 2237 | date_time_overflow_behavior = context->getSettingsRef().date_time_overflow_behavior.value; |
@@ -2277,7 +2327,7 @@ class FunctionConvert : public IFunction |
2277 | 2327 | } |
2278 | 2328 | } |
2279 | 2329 | else |
2280 | | - result_column = ConvertImpl<LeftDataType, RightDataType, Name>::execute(arguments, result_type, input_rows_count, from_string_tag); |
| 2330 | + result_column = ConvertImpl<LeftDataType, RightDataType, Name>::execute(arguments, result_type, input_rows_count, from_string_tag); |
2281 | 2331 |
|
2282 | 2332 | return true; |
2283 | 2333 | }; |
@@ -2334,6 +2384,11 @@ class FunctionConvert : public IFunction |
2334 | 2384 | else |
2335 | 2385 | done = callOnIndexAndDataType<ToDataType>(from_type->getTypeId(), call, BehaviourOnErrorFromString::ConvertDefaultBehaviorTag); |
2336 | 2386 | } |
| 2387 | + |
| 2388 | + if constexpr (std::is_same_v<ToDataType, DataTypeInterval>) |
| 2389 | + { |
| 2390 | + done = callOnIndexAndDataType<ToDataType>(from_type->getTypeId(), call, BehaviourOnErrorFromString::ConvertDefaultBehaviorTag); |
| 2391 | + } |
2337 | 2392 | } |
2338 | 2393 |
|
2339 | 2394 | if (!done) |
@@ -5224,7 +5279,7 @@ REGISTER_FUNCTION(Conversion) |
5224 | 5279 | /// MySQL compatibility alias. Cannot be registered as alias, |
5225 | 5280 | /// because we don't want it to be normalized to toDate in queries, |
5226 | 5281 | /// otherwise CREATE DICTIONARY query breaks. |
5227 | | - factory.registerFunction("DATE", &FunctionToDate::create, {}, FunctionFactory::Case::Insensitive); |
| 5282 | + factory.registerFunction("DATE", &FunctionToDate::create, {}, FunctionFactory::CaseInsensitive); |
5228 | 5283 |
|
5229 | 5284 | factory.registerFunction<FunctionToDate32>(); |
5230 | 5285 | factory.registerFunction<FunctionToDateTime>(); |
|
0 commit comments