Skip to content

Commit

Permalink
Update for array input
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Nov 20, 2024
1 parent b17f009 commit 02988dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
40 changes: 12 additions & 28 deletions velox/functions/sparksql/ConcatWs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ class ConcatWs : public exec::VectorFunction {
const exec::LocalDecodedVector& decodedSeparator) const {
auto arrayArgNum = decodedArrays.size();
std::vector<const ArrayVector*> arrayVectors;
std::vector<DecodedVector*> elementsDecodedVectors;
for (auto i = 0; i < arrayArgNum; ++i) {
auto arrayVector = decodedArrays[i].get()->base()->as<ArrayVector>();
arrayVectors.push_back(arrayVector);
auto elements = arrayVector->elements();
exec::LocalSelectivityVector nestedRows(context, elements->size());
nestedRows.get()->setAll();
exec::LocalDecodedVector elementsHolder(
context, *elements, *nestedRows.get());
elementsDecodedVectors.push_back(elementsHolder.get());
}

size_t totalResultBytes = 0;
Expand All @@ -61,16 +54,15 @@ class ConcatWs : public exec::VectorFunction {
// Calculate size for array columns data.
for (int i = 0; i < arrayArgNum; i++) {
auto arrayVector = arrayVectors[i];
auto rawSizes = arrayVector->rawSizes();
auto rawOffsets = arrayVector->rawOffsets();
auto indices = decodedArrays[i].get()->indices();
auto elementsDecoded = elementsDecodedVectors[i];
SelectivityVector nestedRows(arrayVector->elements()->size());
DecodedVector elementsDecoded(*arrayVector->elements(), nestedRows);
auto size = arrayVector->sizeAt(indices[row]);
auto offset = arrayVector->offsetAt(indices[row]);

auto size = rawSizes[indices[row]];
auto offset = rawOffsets[indices[row]];
for (int j = 0; j < size; ++j) {
if (!elementsDecoded->isNullAt(offset + j)) {
auto element = elementsDecoded->valueAt<StringView>(offset + j);
if (!elementsDecoded.isNullAt(offset + j)) {
auto element = elementsDecoded.valueAt<StringView>(offset + j);
// No matter empty string or not.
++allElements;
totalResultBytes += element.size();
Expand Down Expand Up @@ -209,16 +201,9 @@ class ConcatWs : public exec::VectorFunction {
decodedSeparator);

std::vector<const ArrayVector*> arrayVectors;
std::vector<DecodedVector*> elementsDecodedVectors;
for (auto i = 0; i < decodedArrays.size(); ++i) {
auto arrayVector = decodedArrays[i].get()->base()->as<ArrayVector>();
arrayVectors.push_back(arrayVector);
auto elements = arrayVector->elements();
exec::LocalSelectivityVector nestedRows(context, elements->size());
nestedRows.get()->setAll();
exec::LocalDecodedVector elementsHolder(
context, *elements, *nestedRows.get());
elementsDecodedVectors.push_back(elementsHolder.get());
}
// Allocate a string buffer.
auto rawBuffer =
Expand Down Expand Up @@ -256,16 +241,15 @@ class ConcatWs : public exec::VectorFunction {
for (auto itArgs = args.begin() + 1; itArgs != args.end(); ++itArgs) {
if ((*itArgs)->typeKind() == TypeKind::ARRAY) {
auto arrayVector = arrayVectors[i];
auto rawSizes = arrayVector->rawSizes();
auto rawOffsets = arrayVector->rawOffsets();
auto indices = decodedArrays[i].get()->indices();
auto elementsDecoded = elementsDecodedVectors[i];
SelectivityVector nestedRows(arrayVector->elements()->size());
DecodedVector elementsDecoded(*arrayVector->elements(), nestedRows);
auto size = arrayVector->sizeAt(indices[row]);
auto offset = arrayVector->offsetAt(indices[row]);

auto size = rawSizes[indices[row]];
auto offset = rawOffsets[indices[row]];
for (int k = 0; k < size; ++k) {
if (!elementsDecoded->isNullAt(offset + k)) {
auto element = elementsDecoded->valueAt<StringView>(offset + k);
if (!elementsDecoded.isNullAt(offset + k)) {
auto element = elementsDecoded.valueAt<StringView>(offset + k);
copyToBuffer(
element,
isConstantSeparator()
Expand Down
9 changes: 8 additions & 1 deletion velox/functions/sparksql/tests/ConcatWsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ TEST_F(ConcatWsTest, stringArgsWithNulls) {

auto result = evaluate<SimpleVector<StringView>>(
"concat_ws('~','',c0,'x',NULL::VARCHAR)", makeRowVector({input}));
auto expected = makeNullableFlatVector<StringView>({
auto expected = makeFlatVector<StringView>({
"~~x",
"~x",
"~a~x",
Expand Down Expand Up @@ -219,6 +219,13 @@ TEST_F(ConcatWsTest, arrayArgs) {
"red--purple--green--red--purple--green",
});
velox::test::assertEqualVectors(expected, result);

// Constant arrays.
auto dummyInput = makeRowVector(makeRowType({VARCHAR()}), 1);
result = evaluate<SimpleVector<StringView>>(
"concat_ws('--', array['a','b','c'], array['d'])", dummyInput);
expected = makeFlatVector<StringView>({"a--b--c--d"});
velox::test::assertEqualVectors(expected, result);
}

TEST_F(ConcatWsTest, mixedStringAndArrayArgs) {
Expand Down

0 comments on commit 02988dd

Please sign in to comment.