Skip to content

Commit

Permalink
Support date type in SubstraitToVeloxPlan and hash function (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE authored Dec 14, 2022
1 parent 7a34ae7 commit 238e35f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions velox/functions/sparksql/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ uint32_t hashDouble(double input, uint32_t seed) {
input == -0. ? 0 : *reinterpret_cast<uint64_t*>(&input), seed);
}

uint32_t hashDate(Date input, uint32_t seed) {
return hashInt32(input.days(), seed);
}

class HashFunction final : public exec::VectorFunction {
bool isDefaultNullBehavior() const final {
return false;
Expand Down Expand Up @@ -154,6 +158,7 @@ class HashFunction final : public exec::VectorFunction {
CASE(VARBINARY, hashBytes, StringView);
CASE(REAL, hashFloat, float);
CASE(DOUBLE, hashDouble, double);
CASE(DATE, hashDate, int32_t);
#undef CASE
default:
VELOX_NYI("Unsupported type for HASH(): {}", arg->type()->toString());
Expand Down
11 changes: 11 additions & 0 deletions velox/substrait/SubstraitToVeloxPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,13 @@ void SubstraitVeloxPlanConverter::setFilterMap(
setColInfoMap<std::string>(
functionName, colIdxVal, val, reverse, colInfoMap);
break;
case TypeKind::DATE:
if (substraitLit) {
val = variant(Date(substraitLit.value().date()));
}
setColInfoMap<int>(
functionName, colIdxVal, val, reverse, colInfoMap);
break;
default:
VELOX_NYI(
"Subfield filters creation not supported for input type '{}'",
Expand Down Expand Up @@ -1892,6 +1899,10 @@ connector::hive::SubfieldFilters SubstraitVeloxPlanConverter::mapToFilters(
constructSubfieldFilters<TypeKind::VARCHAR, common::Filter>(
colIdx, inputNameList[colIdx], colInfoMap[colIdx], filters);
break;
case TypeKind::DATE:
constructSubfieldFilters<TypeKind::DATE, common::BigintRange>(
colIdx, inputNameList[colIdx], colInfoMap[colIdx], filters);
break;
default:
VELOX_NYI(
"Subfield filters creation not supported for input type '{}'",
Expand Down
5 changes: 0 additions & 5 deletions velox/substrait/SubstraitToVeloxPlanValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,8 @@ bool SubstraitToVeloxPlanValidator::validate(
return false;
}

// In velox, the supported hash type in projectNode is
// BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, VARCHAR, VARBINARY
// REAL, DOUBLE. Hash.cpp (L148 - L156)
for (auto i = 0; i < types.size(); i++) {
switch (types[i]->kind()) {
case TypeKind::DATE:
return false;
case TypeKind::ARRAY:
return false;
default:;
Expand Down
7 changes: 7 additions & 0 deletions velox/substrait/TypeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ struct RangeTraits<TypeKind::VARCHAR> {
using NativeType = std::string;
};

template <>
struct RangeTraits<TypeKind::DATE> {
using RangeType = common::BigintRange;
using MultiRangeType = common::BigintMultiRange;
using NativeType = int32_t;
};

#endif /* RANGETRAITS_H */

} // namespace facebook::velox::substrait

0 comments on commit 238e35f

Please sign in to comment.