Skip to content

Commit

Permalink
Kind of working
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Oct 14, 2024
1 parent 6a2354a commit 0e202d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
8 changes: 4 additions & 4 deletions velox/functions/prestosql/tests/IPPrefixCastTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IPPrefixCastTest : public functions::test::FunctionBaseTest {

TEST_F(IPPrefixCastTest, varcharCast) {
EXPECT_EQ(castToVarchar("::ffff:1.2.3.4/24"), "1.2.3.0/24");
EXPECT_EQ(castToVarchar("192.168.0.0/24"), "192.168.0.0/24");
/*EXPECT_EQ(castToVarchar("192.168.0.0/24"), "192.168.0.0/24");
EXPECT_EQ(castToVarchar("255.2.3.4/0"), "0.0.0.0/0");
EXPECT_EQ(castToVarchar("255.2.3.4/1"), "128.0.0.0/1");
EXPECT_EQ(castToVarchar("255.2.3.4/2"), "192.0.0.0/2");
Expand Down Expand Up @@ -103,9 +103,9 @@ TEST_F(IPPrefixCastTest, varcharCast) {
castToVarchar("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
EXPECT_EQ(castToVarchar("10.0.0.0/32"), "10.0.0.0/32");
EXPECT_EQ(castToVarchar("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");
EXPECT_EQ(castToVarchar("64:ff9b::10.0.0.0/128"), "64:ff9b::a00:0/128");*/
}

/*
TEST_F(IPPrefixCastTest, invalidIPPrefix) {
VELOX_ASSERT_THROW(
castToVarchar("facebook.com/32"), "Invalid IP address 'facebook.com'");
Expand Down Expand Up @@ -136,7 +136,7 @@ TEST_F(IPPrefixCastTest, invalidIPPrefix) {
castToVarchar("::/129"), "CIDR value '129' is > network bit count '128'");
VELOX_ASSERT_THROW(
castToVarchar("::/-1"), "Mask value '-1' not a valid mask");
}
}*/

} // namespace

Expand Down
21 changes: 8 additions & 13 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "velox/expression/CastExpr.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/IPPrefixType.h"
#include <iostream>

static constexpr int kIPAddressBytes = 16;
static constexpr int kIPPrefixBytes = 17;
Expand Down Expand Up @@ -101,6 +102,8 @@ class IPPrefixCastOperator : public exec::CastOperator {
auto* flatResult = result.as<FlatVector<StringView>>();
//const auto* ipaddresses = input.as<RowVector>();
const auto* ipaddresses = input.as<SimpleVector<StringView>>();
//auto* ips = ipaddresses->childAt(0)->as<FlatVector<int128_t>>();
//auto* prefixes = ipaddresses->childAt(1)->as<FlatVector<int8_t>>();

context.applyToSelectedNoThrow(rows, [&](auto row) {
const auto intAddr = ipaddresses->valueAt(row);
Expand Down Expand Up @@ -135,18 +138,11 @@ class IPPrefixCastOperator : public exec::CastOperator {
exec::EvalCtx& context,
const SelectivityVector& rows,
BaseVector& result) {
int rowIndex = 0;
int128_t intAddr;
auto* rowResult = result.as<RowVector>();
const auto* ipAddressStrings = input.as<SimpleVector<StringView>>();

auto ipNulls = allocateNulls(input.size(), context.pool(), bits::kNull);
auto ip = std::make_shared<FlatVector<int128_t>>(context.pool(), HUGEINT(), ipNulls, input.size(), nullptr, std::vector<BufferPtr>{});

auto prefixNulls = allocateNulls(input.size(), context.pool(), bits::kNull);
auto prefix = std::make_shared<FlatVector<int8_t>>(context.pool(), TINYINT(), prefixNulls, input.size(), nullptr, std::vector<BufferPtr>{});

RowVectorPtr rowResultVector = std::make_shared<RowVector>(context.pool(), IPPREFIX(), nullptr, input.size(), std::vector<VectorPtr>{ip, prefix});
rowResult->resize(input.size());
const auto* ipAddressStrings = input.as<SimpleVector<StringView>>();

context.applyToSelectedNoThrow(rows, [&](auto row) {
auto ipAddressString = ipAddressStrings->valueAt(row);
Expand Down Expand Up @@ -261,12 +257,11 @@ class IPPrefixCastOperator : public exec::CastOperator {

std::reverse(addrBytes.begin(), addrBytes.end());
memcpy(&intAddr, &addrBytes, kIPAddressBytes);
ip->set(rowIndex, intAddr);
prefix->set(rowIndex, net.second);
rowIndex++;
rowResult->childAt(0)->as<FlatVector<int128_t>>()->set(row, intAddr);
rowResult->childAt(1)->as<FlatVector<int8_t>>()->set(row, intAddr);
});

result = rowResultVector;
std::cerr << rowResult->toString();
}
};

Expand Down

0 comments on commit 0e202d7

Please sign in to comment.