Skip to content
Closed
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
34 changes: 23 additions & 11 deletions velox/exec/Expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Expand::Expand(
const auto numRows = expandNode->projections().size();
fieldProjections_.reserve(numRows);
constantProjections_.reserve(numRows);
constantOutputs_.reserve(numRows);
const auto numColumns = expandNode->names().size();
for (const auto& rowProjections : expandNode->projections()) {
std::vector<column_index_t> rowProjection;
Expand Down Expand Up @@ -58,6 +59,25 @@ Expand::Expand(
}
}

void Expand::initialize() {
if (constantProjections_.empty()) {
return;
}
const auto numColumns = constantProjections_[0].size();
for (const auto& projections : constantProjections_) {
std::vector<VectorPtr> constantOutput;
constantOutput.reserve(numColumns);
for (const auto& constant : projections) {
if (constant) {
constantOutput.push_back(constant->toConstantVector(pool()));
} else {
constantOutput.push_back(nullptr);
}
}
constantOutputs_.emplace_back(std::move(constantOutput));
}
}

bool Expand::needsInput() const {
return !noMoreInput_ && input_ == nullptr;
}
Expand All @@ -81,21 +101,13 @@ RowVectorPtr Expand::getOutput() {
std::vector<VectorPtr> outputColumns(outputType_->size());

const auto& rowProjection = fieldProjections_[rowIndex_];
const auto& constantProjection = constantProjections_[rowIndex_];
const auto& constantProjection = constantOutputs_[rowIndex_];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

constantOutputs_ contains vectors of size 1, but here we need to produce a vector of size numInput

I just realized that we cannot reuse the vector and may need to call BaseVector::wrapInConstant(numInput, 0, constantOutputs_[rowIndex_]).

const auto numColumns = rowProjection.size();

for (auto i = 0; i < numColumns; ++i) {
if (rowProjection[i] == kConstantChannel) {
const auto& constantExpr = constantProjection[i];
if (constantExpr->value().isNull()) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This check is not correct

// Add null column.
outputColumns[i] = BaseVector::createNullConstant(
outputType_->childAt(i), numInput, pool());
} else {
// Add constant column.
outputColumns[i] = BaseVector::createConstant(
constantExpr->type(), constantExpr->value(), numInput, pool());
}
outputColumns[i] =
BaseVector::wrapInConstant(numInput, 0, constantProjection[i]);
} else {
outputColumns[i] = input_->childAt(rowProjection[i]);
}
Expand Down
4 changes: 4 additions & 0 deletions velox/exec/Expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ class Expand : public Operator {
}

private:
void initialize() override;

std::vector<std::vector<column_index_t>> fieldProjections_;

std::vector<std::vector<std::shared_ptr<const core::ConstantTypedExpr>>>
constantProjections_;

std::vector<std::vector<VectorPtr>> constantOutputs_;

// Used to indicate the index of fieldProjections_.
int32_t rowIndex_{0};
};
Expand Down
28 changes: 26 additions & 2 deletions velox/exec/tests/ExpandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ using namespace facebook::velox;
using namespace facebook::velox::exec::test;

namespace facebook::velox::exec {

namespace {
class ExpandTest : public OperatorTestBase {
public:
Expand All @@ -37,7 +36,31 @@ class ExpandTest : public OperatorTestBase {
});
}
};
} // anonymous namespace

TEST_F(ExpandTest, complexConstant) {
Comment thread
mbasmanova marked this conversation as resolved.
auto data = makeRowVectorData(3);
auto children = data->children();
auto arrayVector =
makeArrayVector<int32_t>({{1, 2, 3}, {1, 2, 3}, {1, 2, 3}});
children.push_back(arrayVector);
children.push_back(makeAllNullArrayVector(3, INTEGER()));
children.push_back(makeNullConstant(TypeKind::INTEGER, 3));
auto expected = makeRowVector(children);

auto plan = PlanBuilder(pool())
.values({data})
.expand(
{{"k1",
"k2",
"a",
"b",
"ARRAY[1, 2, 3] as c",
"null::integer[] as d",
"null::integer as e"}})
.planNode();

assertQuery(plan, expected);
}

TEST_F(ExpandTest, groupingSets) {
auto data = makeRowVectorData(1'000);
Expand Down Expand Up @@ -151,4 +174,5 @@ TEST_F(ExpandTest, invalidUseCases) {
"projections must not be empty.");
}

} // namespace
} // namespace facebook::velox::exec
Loading