Skip to content

Commit 4b6348d

Browse files
Sergey Pershinfacebook-github-bot
authored andcommitted
feat: Add MAP() signature for MapFunction.
Summary: Some queries can fail with the following error: Scalar function presto.default.map not registered with arguments: () MAP() is supported in Presto and works by creating an emtpy map of the output type. Add the same capability to Velox. Differential Revision: D68359107
1 parent dc6c6e3 commit 4b6348d

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

velox/functions/prestosql/Map.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class MapFunction : public exec::VectorFunction {
3232
const TypePtr& outputType,
3333
exec::EvalCtx& context,
3434
VectorPtr& result) const override {
35+
// No arguments case (empty map).
36+
if (args.empty()) {
37+
auto emptyMapVector =
38+
std::make_shared<MapVector>(context.pool(), outputType, rows);
39+
context.moveOrCopyResult(emptyMapVector, rows, result);
40+
return;
41+
}
42+
3543
VELOX_CHECK_EQ(args.size(), 2);
3644

3745
auto keys = args[0];
@@ -277,13 +285,19 @@ class MapFunction : public exec::VectorFunction {
277285

278286
static std::vector<std::shared_ptr<exec::FunctionSignature>> signatures() {
279287
// array(K), array(V) -> map(K,V)
280-
return {exec::FunctionSignatureBuilder()
281-
.typeVariable("K")
282-
.typeVariable("V")
283-
.returnType("map(K,V)")
284-
.argumentType("array(K)")
285-
.argumentType("array(V)")
286-
.build()};
288+
// () -> map()
289+
return {
290+
exec::FunctionSignatureBuilder()
291+
.typeVariable("K")
292+
.typeVariable("V")
293+
.returnType("map(K,V)")
294+
.argumentType("array(K)")
295+
.argumentType("array(V)")
296+
.build(),
297+
exec::FunctionSignatureBuilder()
298+
.returnType("map(unknown,unknown)")
299+
.build(),
300+
};
287301
}
288302

289303
private:

velox/functions/prestosql/tests/MapTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ TEST_F(MapTest, noNulls) {
9797

9898
auto result = evaluate("map(c0, c1)", makeRowVector({keys, values}));
9999
assertEqualVectors(expectedMap, result);
100+
101+
// Empty map constructor. We use unknown types here because no way to specify
102+
// the output type for the map() call.
103+
const TypePtr& emptyMapType =
104+
MAP(CppToType<UnknownValue>::create(), CppToType<UnknownValue>::create());
105+
SelectivityVector rows(size);
106+
auto emptyMapVector =
107+
std::make_shared<MapVector>(pool_.get(), emptyMapType, rows);
108+
109+
result = evaluate("map()", makeRowVector({keys, values}));
110+
assertEqualVectors(emptyMapVector, result);
100111
}
101112

102113
TEST_F(MapTest, someNulls) {

velox/vector/ComplexVector.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,25 @@ void ArrayVector::copyRanges(
12011201
copyRangesImpl(source, ranges, &elements_, nullptr);
12021202
}
12031203

1204+
MapVector::MapVector(
1205+
velox::memory::MemoryPool* pool,
1206+
std::shared_ptr<const Type> type,
1207+
const SelectivityVector& rows)
1208+
: ArrayVectorBase(
1209+
pool,
1210+
type,
1211+
VectorEncoding::Simple::MAP,
1212+
nullptr, // nulls
1213+
rows.end(),
1214+
0, // nullCount
1215+
allocateOffsets(rows.end(), pool),
1216+
allocateSizes(rows.end(), pool)),
1217+
keys_(BaseVector::create(type->childAt(0), 0, pool)),
1218+
values_(BaseVector::create(type->childAt(1), 0, pool)),
1219+
sortedKeys_{false} {
1220+
VELOX_CHECK_EQ(type->kind(), TypeKind::MAP);
1221+
}
1222+
12041223
bool MapVector::containsNullAt(vector_size_t idx) const {
12051224
if (BaseVector::isNullAt(idx)) {
12061225
return true;

velox/vector/ComplexVector.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ class MapVector : public ArrayVectorBase {
591591
type->childAt(1)->toString());
592592
}
593593

594+
/// Constructor for creating an empty MapVector for the given rows.
595+
MapVector(
596+
velox::memory::MemoryPool* pool,
597+
std::shared_ptr<const Type> type,
598+
const SelectivityVector& rows);
599+
594600
virtual ~MapVector() override {}
595601

596602
bool containsNullAt(vector_size_t idx) const override;

0 commit comments

Comments
 (0)