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;
}
Aggregations