Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Feb 11, 2024
1 parent 9838c7e commit 1defcce
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 4 deletions.
13 changes: 13 additions & 0 deletions velox/exec/tests/utils/QueryAssertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ variant variantAt<TypeKind::TIMESTAMP>(
dataChunk->GetValue(column, row).GetValue<::duckdb::timestamp_t>()));
}

template <>
variant variantAt<TypeKind::UNKNOWN>(
::duckdb::DataChunk* dataChunk,
int32_t row,
int32_t column) {
return variant::null(TypeKind::UNKNOWN);
}

template <TypeKind kind>
variant variantAt(const ::duckdb::Value& value) {
if (value.type() == ::duckdb::LogicalType::INTERVAL) {
Expand All @@ -288,6 +296,11 @@ variant variantAt(const ::duckdb::Value& value) {
}
}

template <>
variant variantAt<TypeKind::UNKNOWN>(const ::duckdb::Value& value) {
return variant::null(TypeKind::UNKNOWN);
}

template <>
velox::variant variantAt<TypeKind::HUGEINT>(const ::duckdb::Value& value) {
auto hugeInt = ::duckdb::HugeIntValue::Get(value);
Expand Down
13 changes: 13 additions & 0 deletions velox/expression/CastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ VectorPtr CastExpr::applyArray(
return result;
}

// Cast from unknown type to other types.
VectorPtr CastExpr::applyUnknown(
const SelectivityVector& rows,
const BaseVector& /*input*/,
exec::EvalCtx& context,
const TypePtr& /*fromType*/,
const TypePtr& toType) {
return BaseVector::createNullConstant(toType, rows.end(), context.pool());
}

VectorPtr CastExpr::applyRow(
const SelectivityVector& rows,
const RowVector* input,
Expand Down Expand Up @@ -724,6 +734,9 @@ void CastExpr::applyPeeled(
fromType->asRow(),
toType);
break;
case TypeKind::UNKNOWN:
result = applyUnknown(rows, input, context, fromType, toType);
break;
default: {
// Handle primitive type conversions.
VELOX_DYNAMIC_SCALAR_TYPE_DISPATCH(
Expand Down
7 changes: 7 additions & 0 deletions velox/expression/CastExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class CastExpr : public SpecialForm {
const TypePtr& toType,
VectorPtr& result);

VectorPtr applyUnknown(
const SelectivityVector& rows,
const BaseVector& /*input*/,
exec::EvalCtx& context,
const TypePtr& /*fromType*/,
const TypePtr& toType);

VectorPtr applyMap(
const SelectivityVector& rows,
const MapVector* input,
Expand Down
13 changes: 13 additions & 0 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ TEST_F(CastExprTest, basics) {
{"1.888", "2.5", "3.6", "100.44", "-100.101", "1", "-2"});
}

TEST_F(CastExprTest, fromUnknownType) {
testCast<UnknownValue, int>(
"int", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, float>(
"float", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, double>(
"double", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, std::string>(
"string", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, bool>(
"boolean", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
}

TEST_F(CastExprTest, realAndDoubleToString) {
setLegacyCast(false);
testCast<double, std::string>(
Expand Down
7 changes: 7 additions & 0 deletions velox/functions/prestosql/aggregates/PrestoHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ FOLLY_ALWAYS_INLINE void PrestoHasher::hash(
hashIntegral<T>(*vector_.get(), rows, hashes);
}

template <>
FOLLY_ALWAYS_INLINE void PrestoHasher::hash<TypeKind::UNKNOWN>(
const SelectivityVector& rows,
BufferPtr& hashes) {
applyHashFunction(rows, *vector_.get(), hashes, [&](auto row) { return 0; });
}

template <>
FOLLY_ALWAYS_INLINE void PrestoHasher::hash<TypeKind::BOOLEAN>(
const SelectivityVector& rows,
Expand Down
9 changes: 8 additions & 1 deletion velox/functions/prestosql/types/JsonType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,21 @@ simdjson::error_code appendMapKey(
const std::string_view& value,
exec::GenericWriter& writer) {
using T = typename TypeTraits<kind>::NativeType;
if constexpr (std::is_same_v<T, void>) {
if constexpr (std::is_same_v<T, void> || std::is_same_v<T, UnknownValue>) {
return simdjson::INCORRECT_TYPE;
} else {
SIMDJSON_ASSIGN_OR_RAISE(writer.castTo<T>(), fromString<T>(value));
return simdjson::SUCCESS;
}
}

template <>
simdjson::error_code appendMapKey<TypeKind::UNKNOWN>(
const std::string_view& value,
exec::GenericWriter& writer) {
VELOX_NYI("UNKNOWN type is not supported!");
}

template <>
simdjson::error_code appendMapKey<TypeKind::VARCHAR>(
const std::string_view& value,
Expand Down
29 changes: 27 additions & 2 deletions velox/type/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ struct UnknownValue {
bool operator>=(const UnknownValue& /* b */) const {
return true;
}

operator std::string() const {
return "NULL";
}
};

template <typename T>
Expand Down Expand Up @@ -1420,6 +1424,10 @@ std::shared_ptr<const OpaqueType> OPAQUE() {
return TEMPLATE_FUNC<::facebook::velox::TypeKind::TIMESTAMP>( \
__VA_ARGS__); \
} \
case ::facebook::velox::TypeKind::UNKNOWN: { \
return TEMPLATE_FUNC<::facebook::velox::TypeKind::UNKNOWN>( \
__VA_ARGS__); \
} \
default: \
VELOX_FAIL( \
"not a scalar type! kind: {}", mapTypeKindToName(typeKind)); \
Expand Down Expand Up @@ -1552,8 +1560,15 @@ std::shared_ptr<const OpaqueType> OPAQUE() {
} \
}()

#define VELOX_DYNAMIC_TYPE_DISPATCH(TEMPLATE_FUNC, typeKind, ...) \
VELOX_DYNAMIC_TYPE_DISPATCH_IMPL(TEMPLATE_FUNC, , typeKind, __VA_ARGS__)
#define VELOX_DYNAMIC_TYPE_DISPATCH(TEMPLATE_FUNC, typeKind, ...) \
[&]() { \
if ((typeKind) == ::facebook::velox::TypeKind::UNKNOWN) { \
return TEMPLATE_FUNC<::facebook::velox::TypeKind::UNKNOWN>(__VA_ARGS__); \
} else { \
return VELOX_DYNAMIC_TYPE_DISPATCH_IMPL( \
TEMPLATE_FUNC, , typeKind, __VA_ARGS__); \
} \
}()

#define VELOX_DYNAMIC_TYPE_DISPATCH_ALL(TEMPLATE_FUNC, typeKind, ...) \
[&]() { \
Expand Down Expand Up @@ -2390,6 +2405,16 @@ struct IsRowType<Row<Ts...>> {

} // namespace facebook::velox

namespace std {
template <>
struct hash<::facebook::velox::UnknownValue> {
size_t operator()(const ::facebook::velox::UnknownValue& /* value */) const {
return 0;
}
};

} // namespace std

namespace folly {
template <>
struct hasher<::facebook::velox::UnknownValue> {
Expand Down
5 changes: 5 additions & 0 deletions velox/vector/arrow/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ const char* exportArrowFormatStr(
return "+m"; // map
case TypeKind::ROW:
return "+s"; // struct
case TypeKind::UNKNOWN:
return "n";

default:
VELOX_NYI("Unable to map type '{}' to ArrowSchema.", type->kind());
Expand Down Expand Up @@ -598,6 +600,7 @@ void exportFlat(
case TypeKind::REAL:
case TypeKind::DOUBLE:
case TypeKind::TIMESTAMP:
case TypeKind::UNKNOWN:
exportValues(vec, rows, out, pool, holder);
break;
case TypeKind::VARCHAR:
Expand Down Expand Up @@ -940,6 +943,8 @@ TypePtr importFromArrowImpl(
return REAL();
case 'g':
return DOUBLE();
case 'n':
return UNKNOWN();

// Map both utf-8 and large utf-8 string to varchar.
case 'u':
Expand Down
2 changes: 1 addition & 1 deletion velox/vector/arrow/tests/ArrowBridgeSchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ TEST_F(ArrowBridgeSchemaExportTest, constant) {
}

TEST_F(ArrowBridgeSchemaExportTest, unsupported) {
GTEST_SKIP() << "Skipping it, cause unknown type supported";
// Try some combination of unsupported types to ensure there's no crash or
// memory leak in failure scenarios.
EXPECT_THROW(testScalarType(UNKNOWN(), ""), VeloxException);
Expand Down Expand Up @@ -395,7 +396,6 @@ TEST_F(ArrowBridgeSchemaImportTest, complexTypes) {
}

TEST_F(ArrowBridgeSchemaImportTest, unsupported) {
EXPECT_THROW(testSchemaImport("n"), VeloxUserError);
EXPECT_THROW(testSchemaImport("C"), VeloxUserError);
EXPECT_THROW(testSchemaImport("S"), VeloxUserError);
EXPECT_THROW(testSchemaImport("I"), VeloxUserError);
Expand Down

0 comments on commit 1defcce

Please sign in to comment.