lbug 0.18.0

An in-process property graph database management system built for query speed and scalability
#include "planner/operator/logical_union.h"

#include "planner/operator/factorization/flatten_resolver.h"
#include "planner/operator/factorization/sink_util.h"

namespace lbug {
namespace planner {

f_group_pos_set LogicalUnion::getGroupsPosToFlatten(uint32_t childIdx) {
    f_group_pos_set groupsPos;
    auto childSchema = children[childIdx]->getSchema();
    for (auto i = 0u; i < expressionsToUnion.size(); ++i) {
        if (requireFlatExpression(i)) {
            // Use the non-deduplicated projection list rather than the child schema's
            // expressionsInScope, which may be shorter when a child projects the same
            // expression more than once (e.g. RETURN b.age, b.age).
            DASSERT(childIdx < childProjections.size());
            auto expression = childProjections[childIdx][i];
            groupsPos.insert(childSchema->getGroupPos(*expression));
        }
    }
    return FlattenAll::getGroupsPosToFlatten(groupsPos, *childSchema);
}

void LogicalUnion::computeFactorizedSchema() {
    auto firstChildSchema = children[0]->getSchema();
    createEmptySchema();
    SinkOperatorUtil::recomputeSchema(*firstChildSchema, firstChildSchema->getExpressionsInScope(),
        *schema);
}

void LogicalUnion::computeFlatSchema() {
    createEmptySchema();
    schema->createGroup();
    for (auto& expression : children[0]->getSchema()->getExpressionsInScope()) {
        schema->insertToGroupAndScope(expression, 0);
    }
}

std::unique_ptr<LogicalOperator> LogicalUnion::copy() {
    std::vector<std::shared_ptr<LogicalOperator>> copiedChildren;
    copiedChildren.reserve(getNumChildren());
    for (auto i = 0u; i < getNumChildren(); ++i) {
        copiedChildren.push_back(getChild(i)->copy());
    }
    auto result = make_unique<LogicalUnion>(expressionsToUnion, std::move(copiedChildren));
    result->setChildProjections(childProjections);
    return result;
}

bool LogicalUnion::requireFlatExpression(uint32_t expressionIdx) {
    for (auto childIdx = 0u; childIdx < children.size(); ++childIdx) {
        auto childSchema = children[childIdx]->getSchema();
        // Use the non-deduplicated projection list; indexing by unique name would not
        // work because different arms may have different expressions at the same
        // position (only types are validated to match).
        DASSERT(childIdx < childProjections.size());
        DASSERT(expressionIdx < childProjections[childIdx].size());
        auto expression = childProjections[childIdx][expressionIdx];
        if (childSchema->getGroup(expression)->isFlat()) {
            return true;
        }
    }
    return false;
}

} // namespace planner
} // namespace lbug