Skip to content

Commit d2afd5f

Browse files
committed
replace hardcoding with type dispatch
1 parent 985b170 commit d2afd5f

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

velox/type/Type.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ std::string mapTypeKindToName(const TypeKind& typeKind) {
119119
return found->second;
120120
}
121121

122+
namespace {
123+
template <TypeKind Kind>
124+
static int32_t kindSize() {
125+
return sizeof(typename KindToFlatVector<Kind>::HashRowType);
126+
}
127+
128+
static int32_t typeKindSize(TypeKind kind) {
129+
if (kind == TypeKind::UNKNOWN) {
130+
return sizeof(UnknownValue);
131+
}
132+
133+
return VELOX_DYNAMIC_TYPE_DISPATCH(kindSize, kind);
134+
}
135+
} // namespace
136+
137+
uint8_t getDecimalBitWidth(const Type& type) {
138+
// Get the physical type's bit width
139+
return typeKindSize(type.kind()) * 8;
140+
}
141+
122142
std::pair<uint8_t, uint8_t> getDecimalPrecisionScale(const Type& type) {
123143
if (type.isShortDecimal()) {
124144
const auto& decimalType = static_cast<const ShortDecimalType&>(type);

velox/type/Type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,8 @@ FOLLY_ALWAYS_INLINE bool isDecimalName(const std::string& name) {
845845
return (name == "DECIMAL");
846846
}
847847

848+
uint8_t getDecimalBitWidth(const Type& type);
849+
848850
std::pair<uint8_t, uint8_t> getDecimalPrecisionScale(const Type& type);
849851

850852
class UnknownType : public CanProvideCustomComparisonType<TypeKind::UNKNOWN> {

velox/vector/arrow/Bridge.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,6 @@ const char* exportArrowFormatTimestampStr(
259259
return formatBuffer.c_str();
260260
}
261261

262-
uint8_t getDecimalWidth(const Type& type) {
263-
// Get the physical type's bit width
264-
switch (type.kind()) {
265-
case TypeKind::BOOLEAN:
266-
return 1;
267-
case TypeKind::TINYINT:
268-
return 8;
269-
case TypeKind::SMALLINT:
270-
return 16;
271-
case TypeKind::INTEGER:
272-
return 32;
273-
case TypeKind::BIGINT:
274-
return 64;
275-
case TypeKind::REAL:
276-
return 32;
277-
case TypeKind::DOUBLE:
278-
return 64;
279-
case TypeKind::HUGEINT:
280-
return 128;
281-
default:
282-
return 0; // Default for complex types
283-
}
284-
}
285-
286262
// Returns the Arrow C data interface format type for a given Velox type.
287263
const char* exportArrowFormatStr(
288264
const TypePtr& type,
@@ -291,7 +267,7 @@ const char* exportArrowFormatStr(
291267
if (type->isDecimal()) {
292268
// Decimal types encode the precision, scale values.
293269
const auto& [precision, scale] = getDecimalPrecisionScale(*type);
294-
auto const width = getDecimalWidth(*type);
270+
auto const width = getDecimalBitWidth(*type);
295271
formatBuffer = fmt::format("d:{},{},{}", precision, scale, width);
296272
return formatBuffer.c_str();
297273
}

0 commit comments

Comments
 (0)