Search in sources :

Example 1 with Root_meta_relational_metamodel_join_Join_Impl

use of org.finos.legend.pure.generated.Root_meta_relational_metamodel_join_Join_Impl in project legend-engine by finos.

the class HelperRelationalBuilder method processDatabaseJoin.

public static org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join processDatabaseJoin(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Join srcJoin, CompileContext context, Database database) {
    MutableMap<String, TableAlias> aliasMap = Maps.adapt(new LinkedHashMap());
    MutableList<org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn> selfJoinTargets = Lists.mutable.empty();
    Operation op = (Operation) processRelationalOperationElement(srcJoin.operation, context, aliasMap, selfJoinTargets);
    MutableList<TableAlias> aliases = aliasMap.valuesView().toList();
    Join join = new Root_meta_relational_metamodel_join_Join_Impl(srcJoin.name)._name(srcJoin.name);
    if (aliases.size() == 2) {
        join._target(aliases.select(new Predicates<TableAlias>() {

            @Override
            public boolean accept(TableAlias tableAlias) {
                return tableAlias._name().equals(srcJoin.target);
            }
        }).getLast());
    }
    if (aliases.isEmpty()) {
        throw new EngineException("A join must refer to at least one table", srcJoin.sourceInformation, EngineErrorType.COMPILATION);
    }
    if (aliases.size() > 2) {
        throw new EngineException("A join can only contain 2 tables. Please use Join chains (using '>') in your mapping in order to compose many of them.", srcJoin.sourceInformation, EngineErrorType.COMPILATION);
    }
    if (aliases.size() == 1) {
        // Self Join
        if (selfJoinTargets.isEmpty()) {
            throw new EngineException("The system can only find one table in the join. Please use the '{target}' notation in order to define a directed self join.", srcJoin.sourceInformation, EngineErrorType.COMPILATION);
        }
        TableAlias existingAlias = aliases.get(0);
        String existingAliasName = existingAlias._name();
        RelationalOperationElement existingRelationalElement = existingAlias._relationalElement();
        TableAlias tableAlias = new Root_meta_relational_metamodel_TableAlias_Impl("");
        tableAlias._name("t_" + existingAliasName);
        tableAlias._relationalElement(existingRelationalElement);
        aliases.add(tableAlias);
        join._target(tableAlias);
        for (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn selfJoinTarget : selfJoinTargets) {
            selfJoinTarget._alias(tableAlias);
            final String columnName = selfJoinTarget._columnName();
            Column col = null;
            if (existingRelationalElement instanceof Relation) {
                col = (Column) ((Relation) existingRelationalElement)._columns().selectWith(COLUMN_NAME_PREDICATE, columnName).toList().getFirst();
            }
            if (col == null) {
                throw new EngineException("The column '" + columnName + "' can't be found in the table '" + ((NamedRelation) existingRelationalElement)._name() + "'");
            }
            selfJoinTarget._column(col);
        }
    }
    join._aliases(Lists.fixedSize.of(new Root_meta_pure_functions_collection_Pair_Impl<TableAlias, TableAlias>("")._first(aliases.get(0))._second(aliases.get(1)), new Root_meta_pure_functions_collection_Pair_Impl<TableAlias, TableAlias>("")._first(aliases.get(1))._second(aliases.get(0))))._database(database)._operation(op);
    return join;
}
Also used : RelationalOperationElement(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) Operation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) Relation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) TableAliasColumn(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn) Column(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Column) Root_meta_relational_metamodel_join_Join_Impl(org.finos.legend.pure.generated.Root_meta_relational_metamodel_join_Join_Impl) Join(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join) RelationalOperationElementWithJoin(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElementWithJoin) Root_meta_pure_functions_collection_Pair_Impl(org.finos.legend.pure.generated.Root_meta_pure_functions_collection_Pair_Impl) org.finos.legend.pure.generated.platform_pure_corefunctions_meta(org.finos.legend.pure.generated.platform_pure_corefunctions_meta) Root_meta_relational_metamodel_TableAlias_Impl(org.finos.legend.pure.generated.Root_meta_relational_metamodel_TableAlias_Impl) TableAliasColumn(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn) TableAlias(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias) TableAliasColumn(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)1 Root_meta_pure_functions_collection_Pair_Impl (org.finos.legend.pure.generated.Root_meta_pure_functions_collection_Pair_Impl)1 Root_meta_relational_metamodel_TableAlias_Impl (org.finos.legend.pure.generated.Root_meta_relational_metamodel_TableAlias_Impl)1 Root_meta_relational_metamodel_join_Join_Impl (org.finos.legend.pure.generated.Root_meta_relational_metamodel_join_Join_Impl)1 org.finos.legend.pure.generated.platform_pure_corefunctions_meta (org.finos.legend.pure.generated.platform_pure_corefunctions_meta)1 Column (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Column)1 RelationalOperationElement (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement)1 RelationalOperationElementWithJoin (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElementWithJoin)1 TableAlias (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias)1 TableAliasColumn (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn)1 Join (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join)1 Operation (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.operation.Operation)1 NamedRelation (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation)1 Relation (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation)1