Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for casting UNKNOWN type to any other type #6996

Closed
wants to merge 5 commits into from

Conversation

PHILO-HE
Copy link
Contributor

@PHILO-HE PHILO-HE commented Oct 11, 2023

No description provided.

@netlify
Copy link

netlify bot commented Oct 11, 2023

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 596a342
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/65dd3872dec35c000891cfd1

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 11, 2023
@PHILO-HE PHILO-HE marked this pull request as ready for review January 15, 2024 01:47
@PHILO-HE PHILO-HE changed the title Support casting unknow type to other types Support casting unknown type to other types Feb 11, 2024
@PHILO-HE PHILO-HE changed the title Support casting unknown type to other types Support casting UNKNOWN type to other types Feb 11, 2024
@PHILO-HE
Copy link
Contributor Author

@mbasmanova, could you spare some time to review this patch? Thanks!

Copy link
Contributor

@mbasmanova mbasmanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PHILO-HE Curious, what's the motivation for this PR?

@@ -274,6 +274,14 @@ variant variantAt<TypeKind::TIMESTAMP>(
dataChunk->GetValue(column, row).GetValue<::duckdb::timestamp_t>()));
}

template <>
variant variantAt<TypeKind::UNKNOWN>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this be called? I believe there is a check for null before this call. If that's the case, then, perhaps, a safer implementation would be to throw VELOX_UNREACHABLE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removed it and will cover it in another PR.

@@ -724,6 +734,9 @@ void CastExpr::applyPeeled(
fromType->asRow(),
toType);
break;
case TypeKind::UNKNOWN:
result = applyUnknown(rows, input, context, fromType, toType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just inline

result = BaseVector::createNullConstant(toType, rows.end(), context.pool());

@@ -113,6 +113,13 @@ class CastExpr : public SpecialForm {
const TypePtr& toType,
VectorPtr& result);

VectorPtr applyUnknown(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not using any member variables, hence, can be a free function in .cpp file. Also, the implementation is a one-liner and there is only one caller, hence, this function is not needed at all.

@@ -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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int -> int32_t

Can we test all types?

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add a test case for this change?

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this ever be called? Isn't there a null check before this call? If so, VELOX_UNREACHABLE would be more appropriate.

@@ -129,6 +129,10 @@ struct UnknownValue {
bool operator>=(const UnknownValue& /* b */) const {
return true;
}

operator std::string() const {
return "NULL";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change for? Would it be possible to add a test?

@@ -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>( \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this change? It will require a lot of updates to existing code. Would it be possible to avoid it?

@PHILO-HE PHILO-HE force-pushed the fix-unknown-type branch 3 times, most recently from ff45670 to d4576d4 Compare February 16, 2024 16:03
@PHILO-HE
Copy link
Contributor Author

@PHILO-HE Curious, what's the motivation for this PR?

Hi @mbasmanova, thanks for your review! The motivation is we are trying to fix issues when enabling Spark NullType (UNKNOWN type of Velox).
I just divided this PR and removed the code irrelevant to the topic of this pr. Could you review again?

@@ -2390,6 +2390,16 @@ struct IsRowType<Row<Ts...>> {

} // namespace facebook::velox

namespace std {
template <>
struct hash<::facebook::velox::UnknownValue> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash for UNKNOWN type is required by VectorMakerStats. If lacked, compiling will fail.
See https://github.com/facebookincubator/velox/blob/main/velox/vector/tests/utils/VectorMakerStats.h#L86.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VectorMakerStats is deprecated. It would be great to not use it (and eventually delete it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just removed this and introduced some code in VectorMakerStats.h to make the compiling pass. Maybe, better to delete deprecated code in a separate PR.

Copy link
Contributor

@mbasmanova mbasmanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good % let's update documentation.

@mbasmanova mbasmanova changed the title Support casting UNKNOWN type to other types Add support to cast UNKNOWN type to any other type Feb 16, 2024
@PHILO-HE PHILO-HE changed the title Add support to cast UNKNOWN type to any other type Add tests for casting UNKNOWN type to any other type Feb 17, 2024
@PHILO-HE
Copy link
Contributor Author

Hi @mbasmanova, I found the added tests can pass without any change for CastExpr.cpp. So I just removed the unused change and changed the PR title.

Copy link
Contributor

@mbasmanova mbasmanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PHILO-HE Looks good % one nit.

@@ -2,6 +2,8 @@
Conversion Functions
====================

Casting from UNKNOWN type to all other scalar types are supported, e.g., cast(NULL as int).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "Casting.. are supported" -> "Casting... is supported"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbasmanova, just fixed it and rebased the code. Thanks!

@facebook-github-bot
Copy link
Contributor

@mbasmanova has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mbasmanova has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mbasmanova has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mbasmanova merged this pull request in 62b8cfd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants