Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Nov 11, 2023
1 parent ec0abed commit eaa0b75
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
13 changes: 13 additions & 0 deletions velox/expression/CastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ VectorPtr CastExpr::applyArray(
return result;
}

// Cast from unkown 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 @@ -608,6 +618,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 @@ -106,6 +106,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
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 @@ -2346,6 +2361,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 @@ -257,6 +257,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 @@ -596,6 +598,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 @@ -1024,6 +1027,8 @@ TypePtr importFromArrow(const ArrowSchema& arrowSchema) {
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
3 changes: 2 additions & 1 deletion velox/vector/arrow/tests/ArrowBridgeSchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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 @@ -349,7 +350,7 @@ TEST_F(ArrowBridgeSchemaImportTest, complexTypes) {
}

TEST_F(ArrowBridgeSchemaImportTest, unsupported) {
EXPECT_THROW(testSchemaImport("n"), VeloxUserError);
// 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 eaa0b75

Please sign in to comment.