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

[native] Fix varchar cast for json #24396

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ std::optional<TypedExprPtr> convertCastToVarcharWithMaxLength(
VELOX_DCHECK(end == returnType.data() + returnType.size() - 1);

VELOX_DCHECK_EQ(args.size(), 1);
const auto arg = args[0];

auto arg = args[0];
// If the argument is of JSON type, convert it to VARCHAR before applying
// substr.
if (velox::isJsonType(arg->type())) {
arg = std::make_shared<CastTypedExpr>(velox::VARCHAR(), arg, false);
}
return std::make_shared<CallTypedExpr>(
arg->type(),
std::vector<TypedExprPtr>{
Expand Down Expand Up @@ -256,8 +261,8 @@ std::optional<TypedExprPtr> tryConvertCast(
}

// When the return type is varchar with max length, truncate if only the
// argument type is varchar (or varchar with max length). Non-varchar argument
// types are not truncated.
// argument type is varchar, or varchar with max length or json. Non-varchar
// argument types are not truncated.
if (returnType.find(kVarchar) == 0 &&
args[0]->type()->kind() == TypeKind::VARCHAR &&
returnType.size() > strlen(kVarchar)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "presto_cpp/presto_protocol/core/presto_protocol_core.h"
#include "velox/core/Expressions.h"
#include "velox/type/Type.h"
#include "velox/functions/prestosql/types/JsonType.h"

using namespace facebook::presto;
using namespace facebook::velox;
Expand All @@ -30,6 +31,7 @@ class RowExpressionTest : public ::testing::Test {
}

void SetUp() override {
registerJsonType();
pool_ = memory::MemoryManager::getInstance()->addLeafPool();
converter_ =
std::make_unique<VeloxExprConverter>(pool_.get(), &typeParser_);
Expand Down Expand Up @@ -626,6 +628,29 @@ TEST_F(RowExpressionTest, castToVarchar) {
ASSERT_TRUE(returnExpr->nullOnFailure());
ASSERT_EQ(returnExpr->type()->toString(), "VARCHAR");
}
// CAST(json AS varchar(3))
{
std::shared_ptr<protocol::CallExpression> p =
json::parse(makeCastToVarchar(false, "json", "varchar(3)"));
auto expr = converter_->toVeloxExpr(p);
auto returnExpr = std::dynamic_pointer_cast<const CallTypedExpr>(expr);

ASSERT_NE(returnExpr, nullptr);
ASSERT_EQ(returnExpr->name(), "presto.default.substr");

auto returnArg1 = std::dynamic_pointer_cast<const CastTypedExpr>(
returnExpr->inputs()[0]);
auto returnArg2 = std::dynamic_pointer_cast<const ConstantTypedExpr>(
returnExpr->inputs()[1]);
auto returnArg3 = std::dynamic_pointer_cast<const ConstantTypedExpr>(
returnExpr->inputs()[2]);

ASSERT_EQ(returnArg1->type()->toString(), "VARCHAR");
ASSERT_EQ(returnArg2->type()->toString(), "BIGINT");
ASSERT_EQ(returnArg2->value().toJson(returnArg2->type()), "1");
ASSERT_EQ(returnArg3->type()->toString(), "BIGINT");
ASSERT_EQ(returnArg3->value().toJson(returnArg3->type()), "3");
}
}

TEST_F(RowExpressionTest, special) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ public void testCast()
// Round-trip tests of casts for Json.
assertQuery("SELECT cast(cast(name as JSON) as VARCHAR), cast(cast(size as JSON) as INTEGER), cast(cast(size + 0.01 as JSON) as DOUBLE), cast(cast(size > 5 as JSON) as BOOLEAN) FROM part");
assertQuery("SELECT cast(cast(array[suppkey, nationkey] as JSON) as ARRAY(INTEGER)), cast(cast(map(array[name, address, phone], array[1.1, 2.2, 3.3]) as JSON) as MAP(VARCHAR(40), DOUBLE)), cast(cast(map(array[name], array[phone]) as JSON) as MAP(VARCHAR(25), JSON)), cast(cast(array[array[suppkey], array[nationkey]] as JSON) as ARRAY(JSON)) from supplier");
assertQuery("SELECT cast(json_extract(x, '$.a') AS varchar(255)) AS extracted_value FROM (VALUES ('{\"a\": \"Some long string\"}')) AS t(x)");
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be better to add one more test case of assertQueryFails(). But I don't want to block the merging of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea. I merged this already, will follow up with an appropriate failure test case


// Cast from date to timestamp
assertQuery("SELECT CAST(date(shipdate) AS timestamp) FROM lineitem");
Expand Down
Loading