Skip to content
Merged
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
27 changes: 16 additions & 11 deletions src/common/types/value/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ Value Value::createDefaultValue(const LogicalType& dataType) {
return Value(dataType.copy(), std::move(children));
}
case LogicalTypeID::MAP:
case LogicalTypeID::LIST: {
return Value(dataType.copy(), std::vector<std::unique_ptr<Value>>{});
}
case LogicalTypeID::LIST:
case LogicalTypeID::UNION: {
std::vector<std::unique_ptr<Value>> children;
children.push_back(std::make_unique<Value>(createNullValue()));
return Value(dataType.copy(), std::move(children));
// We can't create a default value for the union since the
// selected variant is runtime information. Default value
// is initialized when copying (see Value::copyFromUnion).
return Value(dataType.copy(), std::vector<std::unique_ptr<Value>>{});
}
case LogicalTypeID::NODE:
case LogicalTypeID::REL:
Expand Down Expand Up @@ -732,19 +731,25 @@ void Value::copyFromUnion(const uint8_t* kuUnion) {
// For union dataType, only one member can be active at a time. So we don't need to copy all
// union fields into value.
auto activeFieldIdx = UnionType::getInternalFieldIdx(*(union_field_idx_t*)unionValues);
auto childValue = children[0].get();
childValue->dataType = childrenTypes[activeFieldIdx]->copy();
// Create default value now that we know the active field
auto childValue = Value::createDefaultValue(*childrenTypes[activeFieldIdx]);
auto curMemberIdx = 0u;
// Seek to the current active member value.
while (curMemberIdx < activeFieldIdx) {
unionValues += storage::StorageUtils::getDataTypeSize(*childrenTypes[curMemberIdx]);
curMemberIdx++;
}
if (NullBuffer::isNull(unionNullValues, activeFieldIdx)) {
childValue->setNull(true);
childValue.setNull(true);
} else {
childValue.setNull(false);
childValue.copyFromRowLayout(unionValues);
}
if (children.empty()) {
children.push_back(std::make_unique<Value>(std::move(childValue)));
childrenSize = 1;
} else {
childValue->setNull(false);
childValue->copyFromRowLayout(unionValues);
children[0] = std::make_unique<Value>(std::move(childValue));
Copy link
Contributor

Choose a reason for hiding this comment

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

I am a bit confused there?
When will the children by empty? Since you always push a value to the createDefaultValue function:

ase LogicalTypeID::UNION: {
        // We can't create a default value for the union since the
        // selected variant is runtime information. Default value
        // is initialized when copying (see Value::copyFromUnion).
        return Value(dataType.copy(), std::vector<std::unique_ptr<Value>>{});

}
}

Expand Down
8 changes: 8 additions & 0 deletions test/test_files/function/union.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
---- 1
36

-LOG UnionValueOnNestedType
-STATEMENT RETURN union_value(a := struct_pack(b := 123))
---- 1
{b: 123}
-STATEMENT RETURN union_value(a := [12, 34])
---- 1
[12,34]

-LOG UnionValueOnExpr
-STATEMENT MATCH (p:person)-[:knows]->(p1:person) return union_value(age := p.age) AS u1, union_value(age := p1.age) AS u2
---- 14
Expand Down