Search in sources :

Example 1 with GroupByMapping

use of org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping in project legend-pure by finos.

the class TestView method testGroupByIncrementalStoreAndMappingChange.

@Test
public void testGroupByIncrementalStoreAndMappingChange() {
    String viewDynaColName = "orderPnl";
    String groupByStore = GroupByStoreTemplate.format(GroupByStoreTemplate, viewDynaColName);
    String groupByMapping = GroupByMappingTemplate.format(GroupByMappingTemplate, viewDynaColName);
    String viewDynaColNameUpdated = "orderPnlUpdated";
    String groupByStoreUpdated = GroupByStoreTemplate.format(GroupByStoreTemplate, viewDynaColNameUpdated);
    String groupByMappingUpdated = GroupByMappingTemplate.format(GroupByMappingTemplate, viewDynaColNameUpdated);
    RuntimeVerifier.verifyOperationIsStable(new RuntimeTestScriptBuilder().createInMemorySources(Maps.mutable.with("store.pure", groupByStore, "model.pure", GroupByModel, "mapping.pure", groupByMapping)).compile(), new RuntimeTestScriptBuilder().updateSource("store.pure", groupByStoreUpdated).updateSource("mapping.pure", groupByMappingUpdated).compile().updateSource("store.pure", groupByStore).updateSource("mapping.pure", groupByMapping).compile(), this.runtime, this.functionExecution, Lists.fixedSize.<RuntimeVerifier.FunctionExecutionStateVerifier>of());
}
Also used : RuntimeVerifier(org.finos.legend.pure.m3.RuntimeVerifier) RuntimeTestScriptBuilder(org.finos.legend.pure.m3.RuntimeTestScriptBuilder) Test(org.junit.Test)

Example 2 with GroupByMapping

use of org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping in project legend-pure by finos.

the class TestView method testGroupByIncrementalModelChange.

@Test
public void testGroupByIncrementalModelChange() {
    String viewDynaColName = "orderPnl";
    String groupByStore = GroupByStoreTemplate.format(GroupByStoreTemplate, viewDynaColName);
    String groupByMapping = GroupByMappingTemplate.format(GroupByMappingTemplate, viewDynaColName);
    RuntimeVerifier.verifyOperationIsStable(new RuntimeTestScriptBuilder().createInMemorySources(Maps.mutable.with("store.pure", groupByStore, "model.pure", GroupByModel, "mapping.pure", groupByMapping)).compile(), new RuntimeTestScriptBuilder().updateSource("model.pure", GroupByModel + "\n\n\n").compile().updateSource("model.pure", GroupByModel).compile(), this.runtime, this.functionExecution, Lists.fixedSize.<RuntimeVerifier.FunctionExecutionStateVerifier>of());
}
Also used : RuntimeVerifier(org.finos.legend.pure.m3.RuntimeVerifier) RuntimeTestScriptBuilder(org.finos.legend.pure.m3.RuntimeTestScriptBuilder) Test(org.junit.Test)

Example 3 with GroupByMapping

use of org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping in project legend-pure by finos.

the class RelationalMappingSpecificationProcessing method processGroupByMapping.

static Pair<GroupByMapping, MutableSet<TableAlias>> processGroupByMapping(RelationalMappingSpecification implementation, CoreInstance mappingOwner, ProcessorState state, Matcher matcher, ModelRepository repository, ProcessorSupport processorSupport) {
    GroupByMapping groupByMapping = implementation != null ? implementation._groupBy() : null;
    MutableSet<TableAlias> groupByTableAliases = Sets.mutable.empty();
    if (groupByMapping != null) {
        RichIterable<? extends RelationalOperationElement> columns = groupByMapping._columns();
        for (RelationalOperationElement impl : columns) {
            RelationalOperationElementProcessor.processColumnExpr(impl, implementation, mappingOwner, groupByTableAliases, matcher, state, repository, processorSupport);
        }
    }
    return Tuples.pair(groupByMapping, groupByTableAliases);
}
Also used : RelationalOperationElement(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement) GroupByMapping(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping) TableAlias(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias)

Example 4 with GroupByMapping

use of org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping in project legend-pure by finos.

the class RelationalInstanceSetImplementationProcessor method process.

@Override
public void process(RootRelationalInstanceSetImplementation implementation, ProcessorState state, Matcher matcher, final ModelRepository repository, final Context context, final ProcessorSupport processorSupport) {
    // No need to cross reference... ClassMapping is responsible for that
    ModelElement cls = (ModelElement) ImportStub.withImportStubByPass(implementation._classCoreInstance(), processorSupport);
    GenericType instanceGenericType = cls._classifierGenericType() == null ? (GenericType) Type.wrapGenericType(processorSupport.getClassifier(cls), processorSupport) : cls._classifierGenericType();
    CoreInstance propertyReturnGenericType = org.finos.legend.pure.m3.navigation.generictype.GenericType.resolvePropertyReturnType((implementation._classifierGenericType() == null ? (GenericType) Type.wrapGenericType(processorSupport.getClassifier(implementation), processorSupport) : implementation._classifierGenericType()), implementation.getKeyByName(M3Properties._class), processorSupport);
    if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.subTypeOf(instanceGenericType, propertyReturnGenericType, processorSupport)) {
        throw new PureCompilationException(implementation.getSourceInformation(), "Trying to map an unsupported type in Relational: Type Error: '" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(instanceGenericType, processorSupport) + "' not a subtype of '" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(propertyReturnGenericType, processorSupport) + "'");
    }
    RichIterable<? extends PropertyMapping> propertyMappings = implementation._propertyMappings();
    MutableSet<TableAlias> tableAliases = RelationalPropertyMappingProcessor.processRelationalPropertyMappings(propertyMappings, implementation, implementation, implementation._id(), matcher, state, repository, processorSupport);
    if (implementation._id().equals(implementation._superSetImplementationId())) {
        throw new PureCompilationException(implementation.getSourceInformation(), "Extend mapping id cannot reference self \'" + implementation._id() + "\'");
    }
    TableAlias mainTableAlias;
    TableAlias userDefinedMainTable = implementation._mainTableAlias();
    if (userDefinedMainTable == null) {
        MutableSet<RelationalOperationElement> tables = tableAliases.collect(TABLE_ALIAS_TO_RELATIONAL_OPERATION_ELEMENT_FN);
        MutableSet<Database> databases = tableAliases.collectWith(TABLE_ALIAS_TO_DATABASE_FN, processorSupport);
        if (implementation._superSetImplementationId() != null) {
            RootRelationalInstanceSetImplementation superImplementation = getSuperMapping(implementation, processorSupport);
            PostProcessor.processElement(matcher, superImplementation, state, processorSupport);
            collectTableAndDatabaseFromSuperImplementation(superImplementation, implementation, tables, databases, processorSupport);
        }
        if (tables.size() != 1) {
            throw new PureCompilationException(implementation.getSourceInformation(), "Can't find the main table for class '" + cls._name() + "'. Please specify a main table using the ~mainTable directive.");
        }
        if (databases.size() != 1) {
            throw new PureCompilationException(implementation.getSourceInformation(), "Can't find the main table for class '" + cls._name() + "'. Inconsistent database definitions for the mapping");
        }
        mainTableAlias = (TableAlias) processorSupport.newAnonymousCoreInstance(null, M2RelationalPaths.TableAlias);
        mainTableAlias._name(repository.newStringCoreInstance_cached("").getName());
        mainTableAlias._relationalElement(tables.toList().getFirst());
        mainTableAlias._databaseCoreInstance(databases.toList().getFirst());
        implementation._mainTableAlias(mainTableAlias);
    } else {
        if (implementation._superSetImplementationId() == null) {
            mainTableAlias = userDefinedMainTable;
            Database database = (Database) ImportStub.withImportStubByPass(mainTableAlias._databaseCoreInstance(), processorSupport);
            NamedRelation table = (NamedRelation) DatabaseProcessor.findTableForAlias(database, mainTableAlias, processorSupport);
            mainTableAlias._relationalElement(table);
            mainTableAlias._setMappingOwner(implementation);
        } else {
            throw new PureCompilationException(implementation.getSourceInformation(), "Cannot specify main table explicitly for extended mapping [" + implementation._id() + "]");
        }
    }
    RelationalOperationElement mainTable = mainTableAlias._relationalElement();
    for (JoinTreeNode joinTreeNode : RelationalPropertyMappingProcessor.collectJoinTreeNodes(propertyMappings)) {
        RelationalOperationElementProcessor.processAliasForJoinTreeNode(joinTreeNode, mainTable, processorSupport);
    }
    RelationalMappingSpecificationProcessing.processFilterMapping(implementation, implementation, mainTable, matcher, state, repository, processorSupport);
    GroupByMapping groupByMapping = RelationalMappingSpecificationProcessing.processGroupByMapping(implementation, implementation, state, matcher, repository, processorSupport).getOne();
    Boolean distinct = implementation._distinct();
    if (groupByMapping != null) {
        implementation._primaryKey(groupByMapping._columns());
    } else if (distinct) {
        RichIterable<RelationalOperationElement> pks = propertyMappings.flatCollect(PROPERTY_MAPPING_TO_RELATIONAL_OPERATION_ELEMENT_FN);
        RichIterable<RelationalOperationElement> pksWithDistinctColumns = pks.groupBy(TAC_TO_COLUMN).keyMultiValuePairsView().collect(Functions.<RichIterable<RelationalOperationElement>>secondOfPair()).collect(new Function<RichIterable<RelationalOperationElement>, RelationalOperationElement>() {

            @Override
            public RelationalOperationElement valueOf(RichIterable<RelationalOperationElement> roes) {
                return roes.getFirst();
            }
        });
        implementation._primaryKey(pksWithDistinctColumns.select(RELATIONAL_OPERATION_ELEMENT_PK_PREDICATE));
    } else if (implementation._primaryKey().isEmpty()) {
        Relation relation = (Relation) mainTableAlias._relationalElement();
        final TableAlias finalMainTable = mainTableAlias;
        DatabaseProcessor.processTable(relation, matcher, state, processorSupport);
        RichIterable<? extends Column> columns = FastList.newList();
        if (relation instanceof Table) {
            columns = ((Table) relation)._primaryKey();
        } else if (relation instanceof View) {
            columns = ((View) relation)._primaryKey();
        }
        RichIterable<TableAliasColumn> primaryKey = columns.collect(new Function<Column, TableAliasColumn>() {

            @Override
            public TableAliasColumn valueOf(Column column) {
                TableAliasColumn tableAliasColumn = (TableAliasColumn) repository.newEphemeralAnonymousCoreInstance(null, processorSupport.package_getByUserPath(M2RelationalPaths.TableAliasColumn));
                tableAliasColumn._column(column);
                tableAliasColumn._alias(finalMainTable);
                return tableAliasColumn;
            }
        });
        implementation._primaryKey(primaryKey);
    } else // else if (Instance.getValueForMetaPropertyToManyResolved(implementation, M2RelationalProperties.primaryKey, context, processorSupport).isEmpty())
    // {
    // //            throw new PureCompilationException(implementation.getSourceInformation(), "Please provide a primaryKey");
    // }
    {
        processUserDefinedPrimaryKey(implementation, implementation, matcher, state, repository, processorSupport);
    }
    implementation._stores(implementation._propertyMappings().collect(STORE).toSet().with(implementation._mainTableAlias()._database()).without(null));
    MilestoningPropertyMappingProcessor.processMilestoningPropertyMapping(implementation, implementation, processorSupport);
}
Also used : RelationalOperationElement(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement) ModelElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement) Function(org.eclipse.collections.api.block.function.Function) Relation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) Column(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Column) TableAliasColumn(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn) Database(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Database) GroupByMapping(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Table(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Table) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) JoinTreeNode(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.JoinTreeNode) View(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.View) RichIterable(org.eclipse.collections.api.RichIterable) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) TableAlias(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias) TableAliasColumn(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn)

Example 5 with GroupByMapping

use of org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping in project legend-pure by finos.

the class TestSimpleGrammar method testGroupBy.

@Test
public void testGroupBy() {
    Loader.parseM3("Class mapping::groupby::model::domain::IncomeFunction\n" + "{\n" + "   code:Integer[1];\n" + "   name:String[1];\n" + "}\n" + "\n" + "\n" + "\n" + "###Relational\n" + "\n" + "Database mapping::groupby::model::store::myDB\n" + "(\n" + "    Table ACCOUNT_INFO\n" + "    (\n" + "        id INT PRIMARY KEY,\n" + "        ACC_NUM INT,\n" + "        IF_CODE INT,\n" + "        IF_NAME VARCHAR(200)\n" + "    )\n" + "\n" + ")\n" + "\n" + "###Mapping\n" + "import mapping::groupby::model::domain::*;\n" + "import mapping::groupby::model::store::*;\n" + "\n" + "Mapping mapping::testMapping\n" + "(\n" + "    IncomeFunction: Relational\n" + "    {\n" + "       ~groupBy([myDB]ACCOUNT_INFO.IF_CODE, [myDB]ACCOUNT_INFO.IF_NAME)\n" + "       scope([myDB]ACCOUNT_INFO)\n" + "       (\n" + "          code: [myDB]IF_CODE,\n" + "          name : [myDB]IF_NAME\n" + "       )\n" + "       \n" + "    }\n" + ")\n" + "###Pure\n" + "import other::*;\n" + "import meta::relational::metamodel::*;\n" + "import meta::relational::metamodel::relation::*;\n" + "import mapping::groupby::model::domain::*;\n" + "import meta::relational::mapping::*;\n" + "\n" + "function test():Boolean[1]\n" + "{" + "   let groupBy = mapping::testMapping.classMappingByClass(IncomeFunction)->cast(@RootRelationalInstanceSetImplementation).groupBy;\n" + "   print($groupBy, 2);\n" + "   assert(2 == $groupBy.columns->size(), |'');\n" + "}\n" + "\n" + "", this.repository, new ParserLibrary(Lists.immutable.with(new M3AntlrParser(), new MappingParser(), new RelationalParser())), ValidationType.DEEP, VoidM3M4StateListener.VOID_M3_M4_STATE_LISTENER, this.context);
    this.runtime.compile();
    CoreInstance mapping = this.runtime.getCoreInstance("mapping::testMapping");
    CoreInstance classMapping = this.graphWalker.getMany(mapping, "classMappings").getFirst();
    CoreInstance groupBy = this.graphWalker.getOne(classMapping, "groupBy");
    Assert.assertEquals("Anonymous_StripedId instance GroupByMapping\n" + "    columns(Property):\n" + "        Anonymous_StripedId instance TableAliasColumn\n" + "            alias(Property):\n" + "                Anonymous_StripedId instance TableAlias\n" + "                    database(Property):\n" + "                        [~>] myDB instance Database\n" + "                    name(Property):\n" + "                        [>2] ACCOUNT_INFO instance String\n" + "                    relationalElement(Property):\n" + "                        [>2] Anonymous_StripedId instance Table\n" + "            column(Property):\n" + "                Anonymous_StripedId instance Column\n" + "                    name(Property):\n" + "                        [>2] IF_CODE instance String\n" + "                    nullable(Property):\n" + "                        [>2] true instance Boolean\n" + "                    owner(Property):\n" + "                        [>2] Anonymous_StripedId instance Table\n" + "                    type(Property):\n" + "                        [>2] Anonymous_StripedId instance Integer\n" + "            columnName(Property):\n" + "                IF_CODE instance String\n" + "            setMappingOwner(Property):\n" + "                Anonymous_StripedId instance RootRelationalInstanceSetImplementation\n" + "                    class(Property):\n" + "                        [~>] IncomeFunction instance Class\n" + "                    distinct(Property):\n" + "                        [>2] false instance Boolean\n" + "                    groupBy(Property):\n" + "                        [>2] Anonymous_StripedId instance GroupByMapping\n" + "                    id(Property):\n" + "                        [>2] mapping_groupby_model_domain_IncomeFunction instance String\n" + "                    mainTableAlias(Property):\n" + "                        [>2] Anonymous_StripedId instance TableAlias\n" + "                    parent(Property):\n" + "                        [~>] mapping::testMapping instance Mapping\n" + "                    primaryKey(Property):\n" + "                        [>2] Anonymous_StripedId instance TableAliasColumn\n" + "                        [>2] Anonymous_StripedId instance TableAliasColumn\n" + "                    propertyMappings(Property):\n" + "                        [>2] Anonymous_StripedId instance RelationalPropertyMapping\n" + "                        [>2] Anonymous_StripedId instance RelationalPropertyMapping\n" + "                    root(Property):\n" + "                        [>2] true instance Boolean\n" + "                    stores(Property):\n" + "                        [X] myDB instance Database\n" + "                    userDefinedPrimaryKey(Property):\n" + "                        [>2] false instance Boolean\n" + "        Anonymous_StripedId instance TableAliasColumn\n" + "            alias(Property):\n" + "                Anonymous_StripedId instance TableAlias\n" + "                    database(Property):\n" + "                        [~>] myDB instance Database\n" + "                    name(Property):\n" + "                        [>2] ACCOUNT_INFO instance String\n" + "                    relationalElement(Property):\n" + "                        [>2] Anonymous_StripedId instance Table\n" + "            column(Property):\n" + "                Anonymous_StripedId instance Column\n" + "                    name(Property):\n" + "                        [>2] IF_NAME instance String\n" + "                    nullable(Property):\n" + "                        [>2] true instance Boolean\n" + "                    owner(Property):\n" + "                        [>2] Anonymous_StripedId instance Table\n" + "                    type(Property):\n" + "                        [>2] Anonymous_StripedId instance Varchar\n" + "            columnName(Property):\n" + "                IF_NAME instance String\n" + "            setMappingOwner(Property):\n" + "                Anonymous_StripedId instance RootRelationalInstanceSetImplementation\n" + "                    class(Property):\n" + "                        [~>] IncomeFunction instance Class\n" + "                    distinct(Property):\n" + "                        [>2] false instance Boolean\n" + "                    groupBy(Property):\n" + "                        [>2] Anonymous_StripedId instance GroupByMapping\n" + "                    id(Property):\n" + "                        [>2] mapping_groupby_model_domain_IncomeFunction instance String\n" + "                    mainTableAlias(Property):\n" + "                        [>2] Anonymous_StripedId instance TableAlias\n" + "                    parent(Property):\n" + "                        [~>] mapping::testMapping instance Mapping\n" + "                    primaryKey(Property):\n" + "                        [>2] Anonymous_StripedId instance TableAliasColumn\n" + "                        [>2] Anonymous_StripedId instance TableAliasColumn\n" + "                    propertyMappings(Property):\n" + "                        [>2] Anonymous_StripedId instance RelationalPropertyMapping\n" + "                        [>2] Anonymous_StripedId instance RelationalPropertyMapping\n" + "                    root(Property):\n" + "                        [>2] true instance Boolean\n" + "                    stores(Property):\n" + "                        [X] myDB instance Database\n" + "                    userDefinedPrimaryKey(Property):\n" + "                        [>2] false instance Boolean", Printer.print(groupBy, 2));
}
Also used : EnumerationMappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser) MappingParser(org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser) ParserLibrary(org.finos.legend.pure.m3.serialization.grammar.ParserLibrary) M3AntlrParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) RelationalParser(org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 RuntimeTestScriptBuilder (org.finos.legend.pure.m3.RuntimeTestScriptBuilder)3 RuntimeVerifier (org.finos.legend.pure.m3.RuntimeVerifier)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 MappingParser (org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.MappingParser)2 RelationalParser (org.finos.legend.pure.m2.relational.serialization.grammar.v1.RelationalParser)2 GroupByMapping (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.GroupByMapping)2 RelationalOperationElement (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement)2 TableAlias (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias)2 ParserLibrary (org.finos.legend.pure.m3.serialization.grammar.ParserLibrary)2 M3AntlrParser (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3AntlrParser)2 RichIterable (org.eclipse.collections.api.RichIterable)1 Function (org.eclipse.collections.api.block.function.Function)1 EnumerationMappingParser (org.finos.legend.pure.m2.dsl.mapping.serialization.grammar.v1.EnumerationMappingParser)1 ModelElement (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement)1 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)1 RootRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)1 Column (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Column)1 Database (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Database)1 TableAliasColumn (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAliasColumn)1