use of org.finos.legend.pure.generated.Root_meta_relational_metamodel_TableAlias_Impl in project legend-engine by finos.
the class HelperRelationalBuilder method processRelationalOperationElement.
public static RelationalOperationElement processRelationalOperationElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.RelationalOperationElement operationElement, CompileContext context, MutableMap<String, org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias> aliasMap, MutableList<org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn> selfJoinTargets) {
if (operationElement instanceof org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.TableAliasColumn) {
org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.TableAliasColumn tableAliasColumn = (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.TableAliasColumn) operationElement;
// Self join
if (tableAliasColumn.table.table.equals(SELF_JOIN_TABLE_NAME) && tableAliasColumn.tableAlias.equals(SELF_JOIN_TABLE_NAME)) {
org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn selfJoin = new Root_meta_relational_metamodel_TableAliasColumn_Impl("")._columnName(tableAliasColumn.column);
selfJoinTargets.add(selfJoin);
return selfJoin;
}
Relation relation = getRelation((Database) context.resolveStore(tableAliasColumn.table.database, tableAliasColumn.table.sourceInformation), tableAliasColumn.table.schema, tableAliasColumn.table.table, tableAliasColumn.table.sourceInformation);
Column col = getColumn(relation, tableAliasColumn.column, tableAliasColumn.sourceInformation);
TableAlias alias = aliasMap.getIfAbsentPut(tableAliasColumn.table.schema + "." + tableAliasColumn.tableAlias, (Function0<TableAlias>) () -> new Root_meta_relational_metamodel_TableAlias_Impl("")._name(tableAliasColumn.tableAlias)._relationalElement(col._owner())._database(HelperRelationalBuilder.resolveDatabase(tableAliasColumn.table.getDb(), tableAliasColumn.table.sourceInformation, context)));
return new Root_meta_relational_metamodel_TableAliasColumn_Impl("")._columnName(col._name())._column(col)._alias(alias);
} else if (operationElement instanceof ElementWithJoins) {
ElementWithJoins elementWithJoins = (ElementWithJoins) operationElement;
RelationalOperationElementWithJoin res = new Root_meta_relational_metamodel_RelationalOperationElementWithJoin_Impl("")._joinTreeNode(buildElementWithJoinsJoinTreeNode(elementWithJoins.joins, context));
return elementWithJoins.relationalElement == null ? res : res._relationalOperationElement(processRelationalOperationElement(elementWithJoins.relationalElement, context, UnifiedMap.newMap(), selfJoinTargets));
} else if (operationElement instanceof DynaFunc) {
DynaFunc dynaFunc = (DynaFunc) operationElement;
MutableList<RelationalOperationElement> ps = ListIterate.collect(dynaFunc.parameters, relationalOperationElement -> processRelationalOperationElement(relationalOperationElement, context, aliasMap, selfJoinTargets));
return new Root_meta_relational_metamodel_DynaFunction_Impl("")._name(dynaFunc.funcName)._parameters(ps);
} else if (operationElement instanceof org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.Literal) {
org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.Literal literal = (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.Literal) operationElement;
return new Root_meta_relational_metamodel_Literal_Impl("")._value(convertLiteral(convertLiteral(literal.value)));
} else if (operationElement instanceof org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.LiteralList) {
org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.LiteralList literalList = (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.operation.LiteralList) operationElement;
return new Root_meta_relational_metamodel_LiteralList_Impl("")._values(ListIterate.collect(literalList.values, l -> new Root_meta_relational_metamodel_Literal_Impl("")._value(convertLiteral(l.value))));
}
throw new UnsupportedOperationException();
}
use of org.finos.legend.pure.generated.Root_meta_relational_metamodel_TableAlias_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