| 1 | #include "duckdb/common/limits.hpp" |
|---|---|
| 2 | |
| 3 | #include "duckdb/common/exception.hpp" |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | namespace duckdb { |
| 8 | |
| 9 | // we offset the minimum value by 1 to account for the NULL value in the |
| 10 | // hashtables |
| 11 | int64_t MinimumValue(TypeId type) { |
| 12 | switch (type) { |
| 13 | case TypeId::INT8: |
| 14 | return MinimumValue<int8_t>(); |
| 15 | case TypeId::INT16: |
| 16 | return MinimumValue<int16_t>(); |
| 17 | case TypeId::INT32: |
| 18 | return MinimumValue<int32_t>(); |
| 19 | case TypeId::INT64: |
| 20 | return MinimumValue<int64_t>(); |
| 21 | case TypeId::HASH: |
| 22 | return MinimumValue<uint64_t>(); |
| 23 | case TypeId::POINTER: |
| 24 | return MinimumValue<uintptr_t>(); |
| 25 | default: |
| 26 | throw InvalidTypeException(type, "MinimumValue requires integral type"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | uint64_t MaximumValue(TypeId type) { |
| 31 | switch (type) { |
| 32 | case TypeId::INT8: |
| 33 | return MaximumValue<int8_t>(); |
| 34 | case TypeId::INT16: |
| 35 | return MaximumValue<int16_t>(); |
| 36 | case TypeId::INT32: |
| 37 | return MaximumValue<int32_t>(); |
| 38 | case TypeId::INT64: |
| 39 | return MaximumValue<int64_t>(); |
| 40 | case TypeId::HASH: |
| 41 | return MaximumValue<uint64_t>(); |
| 42 | case TypeId::POINTER: |
| 43 | return MaximumValue<uintptr_t>(); |
| 44 | default: |
| 45 | throw InvalidTypeException(type, "MaximumValue requires integral type"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TypeId MinimalType(int64_t value) { |
| 50 | if (value >= MinimumValue(TypeId::INT8) && (uint64_t)value <= MaximumValue(TypeId::INT8)) { |
| 51 | return TypeId::INT8; |
| 52 | } |
| 53 | if (value >= MinimumValue(TypeId::INT16) && (uint64_t)value <= MaximumValue(TypeId::INT16)) { |
| 54 | return TypeId::INT16; |
| 55 | } |
| 56 | if (value >= MinimumValue(TypeId::INT32) && (uint64_t)value <= MaximumValue(TypeId::INT32)) { |
| 57 | return TypeId::INT32; |
| 58 | } |
| 59 | return TypeId::INT64; |
| 60 | } |
| 61 | |
| 62 | } // namespace duckdb |
| 63 |