Skip to content

Commit 676c2be

Browse files
committed
Change to use checking typeid
1 parent d734943 commit 676c2be

2 files changed

Lines changed: 56 additions & 27 deletions

File tree

velox/type/Type.h

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,15 @@ constexpr bool is_nested_kind(TypeKind kind) {
377377
template <TypeKind KIND>
378378
struct TypeFactory;
379379

380-
#define VELOX_FLUENT_CAST(NAME, KIND) \
380+
#define VELOX_TYPE_AS(NAME, KIND) \
381381
const typename TypeTraits<TypeKind::KIND>::ImplType& as##NAME() const { \
382382
return this->as<TypeKind::KIND>(); \
383-
} \
384-
virtual bool is##NAME() const { \
385-
return this->kind() == TypeKind::KIND; \
383+
}
384+
385+
#define VELOX_FLUENT_CAST(NAME, KIND) \
386+
VELOX_TYPE_AS(NAME, KIND) \
387+
bool is##NAME() const { \
388+
return this->kind() == TypeKind::KIND; \
386389
}
387390

388391
class Type;
@@ -627,9 +630,14 @@ class Type : public Tree<const TypePtr>, public velox::ISerializable {
627630
VELOX_FLUENT_CAST(Boolean, BOOLEAN)
628631
VELOX_FLUENT_CAST(Tinyint, TINYINT)
629632
VELOX_FLUENT_CAST(Smallint, SMALLINT)
630-
VELOX_FLUENT_CAST(Integer, INTEGER)
631-
VELOX_FLUENT_CAST(Bigint, BIGINT)
632-
VELOX_FLUENT_CAST(Hugeint, HUGEINT)
633+
634+
VELOX_TYPE_AS(Integer, INTEGER)
635+
VELOX_TYPE_AS(Bigint, BIGINT)
636+
VELOX_TYPE_AS(Hugeint, HUGEINT)
637+
bool isInteger() const;
638+
bool isBigint() const;
639+
bool isHugeint() const;
640+
633641
VELOX_FLUENT_CAST(Real, REAL)
634642
VELOX_FLUENT_CAST(Double, DOUBLE)
635643
VELOX_FLUENT_CAST(Varchar, VARCHAR)
@@ -682,6 +690,7 @@ class Type : public Tree<const TypePtr>, public velox::ISerializable {
682690
const bool providesCustomComparison_;
683691
};
684692

693+
#undef VELOX_TYPE_AS
685694
#undef VELOX_FLUENT_CAST
686695

687696
template <TypeKind KIND, typename = void>
@@ -817,6 +826,18 @@ class ScalarType : public CanProvideCustomComparisonType<KIND> {
817826
}
818827
};
819828

829+
FOLLY_ALWAYS_INLINE bool Type::isInteger() const {
830+
return typeid(*this) == typeid(ScalarType<TypeKind::INTEGER>);
831+
}
832+
833+
FOLLY_ALWAYS_INLINE bool Type::isBigint() const {
834+
return typeid(*this) == typeid(ScalarType<TypeKind::BIGINT>);
835+
}
836+
837+
FOLLY_ALWAYS_INLINE bool Type::isHugeint() const {
838+
return typeid(*this) == typeid(ScalarType<TypeKind::HUGEINT>);
839+
}
840+
820841
/// This class represents the fixed-point numbers.
821842
/// The parameter "precision" represents the number of digits the
822843
/// Decimal Type can support and "scale" represents the number of digits to
@@ -828,14 +849,6 @@ class DecimalType : public ScalarType<KIND> {
828849
static constexpr uint8_t kMaxPrecision = KIND == TypeKind::BIGINT ? 18 : 38;
829850
static constexpr uint8_t kMinPrecision = KIND == TypeKind::BIGINT ? 1 : 19;
830851

831-
bool isBigint() const override {
832-
return false;
833-
}
834-
835-
bool isHugeint() const override {
836-
return false;
837-
}
838-
839852
inline bool equivalent(const Type& other) const override {
840853
if (!Type::hasSameTypeId(other)) {
841854
return false;
@@ -1431,10 +1444,6 @@ class IntervalDayTimeType final : public BigintType {
14311444
return this == &other;
14321445
}
14331446

1434-
bool isBigint() const override {
1435-
return false;
1436-
}
1437-
14381447
std::string toString() const override {
14391448
return name();
14401449
}
@@ -1491,10 +1500,6 @@ class IntervalYearMonthType final : public IntegerType {
14911500
return name();
14921501
}
14931502

1494-
bool isInteger() const override {
1495-
return false;
1496-
}
1497-
14981503
/// Returns the interval 'value' (months) formatted as YEARS MONTHS.
14991504
/// For example, 14 months (INTERVAL '1-2' YEAR TO MONTH) would be
15001505
/// represented as 1-2; -14 months would be represents as -1-2.
@@ -1540,10 +1545,6 @@ class DateType final : public IntegerType {
15401545
return this == &other;
15411546
}
15421547

1543-
bool isInteger() const override {
1544-
return false;
1545-
}
1546-
15471548
std::string toString() const override {
15481549
return name();
15491550
}

velox/type/tests/TypeTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,3 +1238,31 @@ TEST(TypeTest, time) {
12381238

12391239
testTypeSerde(timeType);
12401240
}
1241+
1242+
TEST(TypeTest, typeCheck) {
1243+
EXPECT_TRUE(INTEGER()->isInteger());
1244+
EXPECT_TRUE(BIGINT()->isBigint());
1245+
EXPECT_TRUE(HUGEINT()->isHugeint());
1246+
1247+
EXPECT_TRUE(DATE()->isDate());
1248+
EXPECT_FALSE(DATE()->isInteger());
1249+
1250+
// Short decimal.
1251+
const auto shortDecimal = DECIMAL(10, 5);
1252+
EXPECT_TRUE(shortDecimal->isDecimal());
1253+
EXPECT_TRUE(shortDecimal->isShortDecimal());
1254+
EXPECT_FALSE(shortDecimal->isLongDecimal());
1255+
EXPECT_FALSE(shortDecimal->isBigint());
1256+
// Long decimal.
1257+
const auto longDecimal = DECIMAL(30, 5);
1258+
EXPECT_TRUE(longDecimal->isDecimal());
1259+
EXPECT_TRUE(longDecimal->isLongDecimal());
1260+
EXPECT_FALSE(longDecimal->isShortDecimal());
1261+
EXPECT_FALSE(longDecimal->isHugeint());
1262+
1263+
EXPECT_TRUE(INTERVAL_YEAR_MONTH()->isIntervalYearMonth());
1264+
EXPECT_FALSE(INTERVAL_YEAR_MONTH()->isInteger());
1265+
1266+
EXPECT_TRUE(INTERVAL_DAY_TIME()->isIntervalDayTime());
1267+
EXPECT_FALSE(INTERVAL_DAY_TIME()->isBigint());
1268+
}

0 commit comments

Comments
 (0)