Skip to content

Commit

Permalink
Attempt 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsaka committed Oct 11, 2024
1 parent fa7aaa5 commit 6a2354a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions velox/functions/prestosql/types/IPPrefixType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <folly/IPAddress.h>
#include <folly/small_vector.h>

#include "velox/expression/CastExpr.h"
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/IPPrefixType.h"
Expand Down Expand Up @@ -134,12 +135,18 @@ 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>>();

VectorPtr ip = BaseVector::create(HUGEINT(), input.size(), context.pool());
VectorPtr prefix = BaseVector::create(TINYINT(), input.size(), context.pool());
RowVectorPtr rowVector = std::make_shared<RowVector>(context.pool(), IPPREFIX(), nullptr, input.size(), {ip, prefix});
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});

context.applyToSelectedNoThrow(rows, [&](auto row) {
auto ipAddressString = ipAddressStrings->valueAt(row);
Expand Down Expand Up @@ -251,9 +258,15 @@ class IPPrefixCastOperator : public exec::CastOperator {
.mask(net.second)
.toByteArray();
}

std::reverse(addrBytes.begin(), addrBytes.end());
memcpy(&intAddr, &addrBytes, kIPAddressBytes);
ip->set(rowIndex, intAddr);
prefix->set(rowIndex, net.second);
rowIndex++;
});

context.moveOrCopyResult(localResult, rows, result);
result = rowResultVector;
}
};

Expand Down

0 comments on commit 6a2354a

Please sign in to comment.