Esempio n. 1
0
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IDataStructure structure = this._dsResolver();

            if (expression.OperandsCollection.Count != 2)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected two components.");
            }

            IExpression identiefierExpr = expression.OperandsCollection.First();
            IExpression measureExpr     = expression.OperandsCollection.Last();

            if (!identiefierExpr.IsScalar || !measureExpr.IsScalar)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected scalar expression.");
            }

            if (identiefierExpr.Structure.Identifiers.Count != 1)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected identifier.");
            }
            if (measureExpr.Structure.Measures.Count != 1)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected measure.");
            }

            structure.AddStructure(measureExpr.Structure.GetCopy());
            structure.Measures.First().ComponentName = $"pivot_{identiefierExpr.Structure.Identifiers.First().ComponentName}";

            structure.DatasetType = DatasetType.Pivoted;

            return(structure);
        }
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IExpression expr1 = expression.OperandsCollection.ToArray()[0];
            IExpression expr2 = expression.OperandsCollection.ToArray()[1];

            IDataStructure structure = expr1.Structure.GetCopy();

            if (expr2.ResultName.In("Calc", "Aggregation"))
            {
                structure.AddStructure(expr2.Structure);
            }
            else if (expr2.ResultName.In("Keep", "Drop"))
            {
                this.ProcessKeepDropClause(structure, expr2);
            }
            else if (expr2.ResultName == "Rename")
            {
                this.ProcessRenameClause(structure, expr2);
            }
            else if (expr2.ResultName == "Pivot")
            {
                this.ProcessPivotClause(structure, expr2);
            }
            else if (expr2.ResultName == "Unpivot")
            {
                this.ProcessUnpivotClause(structure, expr2);
            }
            else if (expr2.ResultName == "Subspace")
            {
                this.ProcessSubspaceClause(structure, expr2);
            }

            return(structure);
        }
        public void GetOutputStructure_GroupNScalarsExpr_DataStructure()
        {
            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(this._exprFac.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            for (int i = 3; i <= 5; i++) // very expensive
            {
                TestExprType[][] combinations = Enum.GetValues(typeof(TestExprType)).Cast <TestExprType>().Where(type => (int)type <= 8).GetCombinations(i);

                foreach (TestExprType[] combination in combinations)
                {
                    IExpression compCreateExpr = this._exprFac.GetExpression("group", ExpressionFactoryNameTarget.OperatorSymbol);
                    joinExpr.AddOperand("ds_1", compCreateExpr);

                    IDataStructure expectedStructure = ModelResolvers.DsResolver();
                    for (int k = 2; k <= i; k++)
                    {
                        IExpression idExpr = ModelResolvers.ExprResolver();
                        idExpr.ExpressionText = $"Id{k}";
                        idExpr.Structure      = ModelResolvers.DsResolver($"Id{k}", ComponentType.Identifier, (BasicDataType)combination[k - 1]);
                        compCreateExpr.AddOperand($"ds_{k}", idExpr);

                        expectedStructure.AddStructure(compCreateExpr.Operands[$"ds_{k}"].Structure);
                    }

                    IDataStructure dataStructure = compCreateExpr.OperatorDefinition.GetOutputStructure(compCreateExpr);

                    Assert.True(expectedStructure.EqualsObj(dataStructure));
                }
            }
        }
 /// <summary>
 /// Precesses unpivoting operations at a given data structure.
 /// </summary>
 /// <param name="structure">The data structure.</param>
 /// <param name="unpivotExpr">The unpivot expression.</param>
 private void ProcessUnpivotClause(IDataStructure structure, IExpression unpivotExpr)
 {
     (structure.Measures as List <StructureComponent>).Clear();
     (structure.NonViralAttributes as List <StructureComponent>).Clear();
     (structure.ViralAttributes as List <StructureComponent>).Clear();
     structure.AddStructure(unpivotExpr.Structure);
 }
        public void GetOutputStructure_NScalarsExpr_DataStructure(string opSymbol)
        {
            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(this._exprFac.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            for (int i = 3; i <= 5; i++) // very expensive
            {
                TestExprType[][] combinations = Enum.GetValues(typeof(TestExprType)).Cast <TestExprType>().Where(type => (int)type <= 8).GetCombinations(i);

                foreach (TestExprType[] combination in combinations)
                {
                    IExpression compCreateExpr = TestExprFactory.GetExpression(combination);
                    compCreateExpr.OperatorDefinition = this._exprFac.OperatorResolver(opSymbol);
                    joinExpr.AddOperand("ds_1", compCreateExpr);

                    IDataStructure expectedStructure = compCreateExpr.Operands["ds_1"].Structure.GetCopy();
                    for (int k = 2; k <= i; k++)
                    {
                        expectedStructure.AddStructure(compCreateExpr.Operands[$"ds_{k}"].Structure);
                    }

                    IDataStructure dataStructure = compCreateExpr.OperatorDefinition.GetOutputStructure(compCreateExpr);

                    Assert.True(expectedStructure.EqualsObj(dataStructure));
                }
            }
        }
Esempio n. 6
0
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IDataStructure structure = this._dsResolver();

            foreach (IExpression expr in expression.OperandsCollection)
            {
                if (!expr.IsScalar)
                {
                    throw new VtlOperatorError(expression, this.Name, "Expected scalar expression.");
                }
                if (expr.OperatorSymbol == "#")
                {
                    string compName = $"{expr.OperandsCollection.ToArray()[0].ExpressionText}#{expr.Operands["ds_2"].ExpressionText}";
                    expr.Structure.Components[0].BaseComponentName = compName;
                    expr.Structure.Components[0].ComponentName     = compName;
                }

                structure.AddStructure(expr.Structure);
            }

            if (structure.Identifiers.Count != 0)
            {
                throw new VtlOperatorError(expression, this.Name, $"Identifiers cannot be included to {this.Symbol.ToLower()} branch of the \"join\" operator expression.");
            }

            return(structure);
        }
        /// <summary>
        /// Precesses pivoting operations at a given data structure.
        /// </summary>
        /// <param name="structure">The data structure.</param>
        /// <param name="pivotExpr">The pivot expression.</param>
        private void ProcessPivotClause(IDataStructure structure, IExpression pivotExpr)
        {
            IExpression compToRemove = pivotExpr.OperandsCollection.First();

            (structure.Identifiers as List <StructureComponent>).RemoveAll(r => r.ComponentName == compToRemove.Structure.Components.First().ComponentName);
            (structure.Measures as List <StructureComponent>).Clear();
            (structure.NonViralAttributes as List <StructureComponent>).Clear();
            (structure.ViralAttributes as List <StructureComponent>).Clear();
            structure.AddStructure(pivotExpr.Structure);
            structure.DatasetType = DatasetType.Pivoted;
        }
Esempio n. 8
0
        public void GetOutputStructure_Correct2Operands()
        {
            IExpression expr = ModelResolvers.ExprResolver();

            expr.OperatorDefinition = ModelResolvers.OperatorResolver("sub");

            IExpression operand1 = TestExprFactory.GetDatasetExpr(new KeyValuePair <ComponentType, BasicDataType>(ComponentType.Identifier, BasicDataType.Integer));
            IExpression operand2 = TestExprFactory.GetDatasetExpr(new KeyValuePair <ComponentType, BasicDataType>(ComponentType.Identifier, BasicDataType.String));

            expr.AddOperand("ds_1", operand1);
            expr.AddOperand("ds_2", operand2);

            IDataStructure operandCollection = ModelResolvers.DsResolver();

            operandCollection.AddStructure(operand1.Structure);
            operandCollection.AddStructure(operand2.Structure);

            IDataStructure dataStructure = expr.OperatorDefinition.GetOutputStructure(expr);

            Assert.True(operandCollection.EqualsObj(dataStructure));
        }
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IDataStructure structure = this._dsResolver();

            if (expression.OperandsCollection.Count != 2)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected two components.");
            }

            IExpression identiefierExpr = expression.OperandsCollection.First();
            IExpression measureExpr     = expression.OperandsCollection.Last();

            if (!identiefierExpr.IsScalar || !measureExpr.IsScalar)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected scalar expression.");
            }

            if (identiefierExpr.Structure.Identifiers.Count != 1)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected identifier.");
            }
            if (measureExpr.Structure.Measures.Count != 1)
            {
                throw new VtlOperatorError(expression, this.Name, "Expected measure.");
            }

            if (expression.ParentExpression?.Operands["ds_1"]?.Structure.Measures.GroupBy(g => g.ValueDomain.DataType).Count() != 1)
            {
                throw new VtlOperatorError(expression, this.Name, "All types of Measure must be that same");
            }


            structure.AddStructure(identiefierExpr.Structure.GetCopy());
            structure.AddStructure(measureExpr.Structure.GetCopy());

            return(structure);
        }
        /// <summary>
        /// Gets the structure of the resulting operator parameter for specified operands.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>A dynamically defined structure of the output parameter for the given input parameters.</returns>
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IDataStructure dataStructure = this._dsResolver();

            foreach (IExpression expr in expression.OperandsCollection)
            {
                if (!expr.IsScalar)
                {
                    throw new VtlOperatorError(expression, this._name, "Expected scalar expression.");
                }
                dataStructure.AddStructure(expr.Structure);
            }

            return(dataStructure);
        }
        /// <summary>
        /// Precesses a "join" expression's datasets structures merging into a given data structure.
        /// </summary>
        /// <param name="expression">The "join" expression.</param>
        /// <param name="mergedStructure">The data structure.</param>
        private IDataStructure ProcessDsBranch(IJoinExpression expression, IDataStructure mergedStructure)
        {
            IExpression[] aliases = expression.Operands["ds"].OperandsCollection.ToArray();
            for (int i = 0; i < aliases.Length; i++)
            {
                if (i + 1 != aliases.Length &&
                    aliases[i].Structure.IsSupersetOf(aliases[i + 1].Structure) &&
                    !aliases[i + 1].Structure.IsSupersetOf(aliases[i].Structure))
                {
                    throw new VtlOperatorError(expression, this.Symbol, "Datasets doesn't fit");
                }

                mergedStructure.AddStructure(aliases[i].Structure.GetCopy());
            }

            return(mergedStructure);
        }
        /// <summary>
        /// Precesses a "join" expression's component calculating operations at a given data structure.
        /// </summary>
        /// <param name="expression">The "join" expression.</param>
        /// <param name="mergedStructure">The data structure.</param>
        private IDataStructure ProcessCalcBranch(IJoinExpression expression, IDataStructure mergedStructure)
        {
            IExpression calcBranch = expression.Operands["calc"];

            if (calcBranch.OperatorDefinition.Keyword == "Aggr")
            {
                foreach (StructureComponent component in calcBranch.Structure.Components.Where(comp => comp.ComponentType != ComponentType.Identifier))
                {
                    List <StructureComponent> comps = mergedStructure.Components as List <StructureComponent>;
                    comps.RemoveAll(comp => comp.ComponentName != component.BaseComponentName && comp.ComponentType == component.ComponentType);
                    mergedStructure.Measures           = comps.Where(comp => comp.ComponentType == ComponentType.Measure).ToList();
                    mergedStructure.NonViralAttributes = comps.Where(comp => comp.ComponentType == ComponentType.NonViralAttribute).ToList();
                    mergedStructure.ViralAttributes    = comps.Where(comp => comp.ComponentType == ComponentType.ViralAttribute).ToList();
                }
            }

            mergedStructure.AddStructure(calcBranch.Structure.GetCopy());

            return(mergedStructure);
        }
        public IDataStructure GetMeasuresStructure(IExpression expression)
        {
            IDataStructure             measuresStructure = this._dsResolver();
            List <List <IExpression> > operands          = new List <List <IExpression> >();

            foreach (IExpression expr in expression.OperandsCollection)
            {
                operands.Add(new List <IExpression>());

                if (expr.IsApplyComponent)
                {
                    IDataStructure structure = expr.Structure;
                    if (structure == null && expr.OperatorSymbol != null)
                    {
                        structure = expr.Operands["ds_1"].Structure;
                    }

                    if (structure != null)
                    {
                        for (int i = 0; i < structure.Measures.Count; i++)
                        {
                            StructureComponent measure = structure.Measures[i];
                            IExpression        operand = this._exprFac.GetExpression("Alias", ExpressionFactoryNameTarget.ResultName);

                            operand.Structure = this._dsResolver(measure.ComponentName, measure.ComponentType, measure.ValueDomain.DataType);
                            operands.Last().Add(operand);
                        }
                    }
                }
                else
                {
                    operands.Last().Add(expr);
                }
            }

            List <IExpression> measuresExprList = operands.Last(op => op.FirstOrDefault(o => o.ResultName == "Alias") != null); // Last because first in if-then-else is condition

            for (int i = 0; i < measuresExprList.Count; i++)
            {
                IExpression operatorExpr = this._exprFac.GetExpression(expression.OperatorSymbol, ExpressionFactoryNameTarget.OperatorSymbol);
                for (int j = 0; j < operands.Count; j++)
                {
                    IExpression operand = operands[j][0];
                    if (operands[j].Count > 1 && (j != 0 || j == 0 && expression.OperatorSymbol != "if"))
                    {
                        if (!this.AreExprListsEqual(operands[j], measuresExprList))
                        {
                            throw new VtlOperatorError(expression, expression.OperatorSymbol, "Aliased measures collections don't match each other.");
                        }

                        operand = operands[j][i];
                    }

                    operatorExpr.AddOperand($"ds_{j + 1}", operand);
                }

                if (operatorExpr.OperatorSymbol == "if")
                {
                    IExpression[] ifThenElseExprs = operatorExpr.OperandsCollection.ToArray();
                    operatorExpr.Operands.Clear();
                    operatorExpr.AddOperand("if", ifThenElseExprs[0]);
                    operatorExpr.AddOperand("then", ifThenElseExprs[1]);
                    operatorExpr.AddOperand("else", ifThenElseExprs[2]);
                    if (operatorExpr.Operands["if"].ResultName != "If")
                    {
                        operatorExpr.Operands["if"].AddOperand("ds_1", ifThenElseExprs[0]);
                    }
                    if (operatorExpr.Operands["then"].ResultName != "Then")
                    {
                        operatorExpr.Operands["then"].AddOperand("ds_1", ifThenElseExprs[1]);
                    }
                    if (operatorExpr.Operands["else"].ResultName != "Else")
                    {
                        operatorExpr.Operands["else"].AddOperand("ds_1", ifThenElseExprs[2]);
                    }
                }

                IDataStructure operatorStructure = operatorExpr.OperatorDefinition.GetOutputStructure(operatorExpr);
                operatorStructure.Measures[0].BaseComponentName = measuresExprList[i].Structure.Measures[0].BaseComponentName;
                operatorStructure.Measures[0].ComponentName     = measuresExprList[i].Structure.Measures[0].ComponentName;
                measuresStructure.AddStructure(operatorStructure);
            }

            return(measuresStructure);
        }