Esempio n. 1
0
        public IJoinSelectBuilder AddJoinExpr(IJoinExpression joinExpr)
        {
            this.joinExpr   = joinExpr;
            this.ifThenElse = this.joinExpr.Operands.ContainsKey("apply") && this.joinExpr.Operands["apply"].OperatorSymbol == "if";

            return(this);
        }
        public void Constructor_JoinExpr_JoinExpr()
        {
            IExpression           expr    = TestExprFactory.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol);
            IExpression           refExpr = ModelResolvers.ExprResolver();
            ITransformationSchema schema  = ModelResolvers.SchemaResolver();

            refExpr.AddOperand("ds", expr);
            expr.ContainingSchema    = schema;
            expr.ReferenceExpression = refExpr;

            IJoinExpression joinExpr2 = ModelResolvers.JoinExprResolver(expr);

            joinExpr2.BasicStructure = ModelResolvers.DsResolver();

            JoinExpression joinExpr = new JoinExpression(joinExpr2);

            Assert.Equal(expr.ParentExpression, joinExpr.ParentExpression);
            Assert.Equal(expr.ContainingSchema, joinExpr.ContainingSchema);
            Assert.Equal(expr.ExpressionText, joinExpr.ExpressionText);
            Assert.Equal(expr.LineNumber, joinExpr.LineNumber);
            Assert.Equal(expr.OperandsCollection, joinExpr.OperandsCollection);
            Assert.Equal(expr.OperatorDefinition, joinExpr.OperatorDefinition);
            Assert.Equal(expr.ParamSignature, joinExpr.ParamSignature);
            Assert.Equal(expr.ReferenceExpression, joinExpr.ReferenceExpression);
            Assert.Equal(expr.ResultName, joinExpr.ResultName);
            Assert.Equal(expr.Structure, joinExpr.Structure);
            Assert.Equal(joinExpr2.BasicStructure, joinExpr.BasicStructure);
        }
        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));
                }
            }
        }
        public IDataStructure GetOutputStructure(IExpression expression)
        {
            IJoinExpression joinExpr        = (IJoinExpression)expression;
            IDataStructure  mergedStructure = this._dsResolver();

            if (!joinExpr.Operands["ds"].Operands.All(ds => !ds.Value.Structure.IsSingleComponent))
            {
                throw new VtlOperatorError(joinExpr, this.Name, "Scalar joinExprs cannot be joined.");
            }

            this.ProcessDsBranch(joinExpr, mergedStructure);
            if (joinExpr.Operands.ContainsKey("apply"))
            {
                mergedStructure = this.ProcessApplyBranch(joinExpr, mergedStructure);
            }

            joinExpr.BasicStructure = this.ProcessDsBranch(joinExpr, this._dsResolver());

            if (joinExpr.Operands.ContainsKey("aggr"))
            {
                IExpression aggrExpr = joinExpr.Operands["aggr"];

                joinExpr.AddOperand("calc", aggrExpr.Operands["calc"]);
                joinExpr.AddOperand("group", aggrExpr.Operands["group"]);
                if (aggrExpr.Operands.ContainsKey("having"))
                {
                    joinExpr.AddOperand("having", aggrExpr.Operands["having"]);
                }
                joinExpr.Operands.Remove("aggr");

                joinExpr.Operands["calc"].OperatorDefinition.Keyword = "Aggr";
            }

            if (joinExpr.Operands.ContainsKey("keep") ||
                joinExpr.Operands.ContainsKey("drop"))
            {
                mergedStructure = this.ProcessKeepDropBranch(joinExpr, mergedStructure);
            }
            if (joinExpr.Operands.ContainsKey("calc"))
            {
                mergedStructure = this.ProcessCalcBranch(joinExpr, mergedStructure);
            }
            if (joinExpr.Operands.ContainsKey("rename"))
            {
                mergedStructure = this.ProcessRenameBranch(joinExpr, mergedStructure);
            }
            if (joinExpr.Operands.ContainsKey("filter") &&
                (!joinExpr.Operands["filter"].Structure.IsSingleComponent ||
                 joinExpr.Operands["filter"].Structure.Components[0].ValueDomain.DataType != BasicDataType.Boolean))
            {
                throw new VtlOperatorError(joinExpr, this.Name, "Expected boolean single component expression as filter branch.");
            }

            if (joinExpr.Operands.ContainsKey("subspace"))
            {
                (mergedStructure.Identifiers as List <StructureComponent>).RemoveAll(id => id.ComponentName.In(joinExpr.Operands["subspace"].Structure.Identifiers.Select(id => id.ComponentName).ToArray()));
            }

            return(mergedStructure);
        }
        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));
                }
            }
        }
        public void GetOutputStructure_Group2ScalarsExpr_DataStructure(params TestExprType[] types)
        {
            IExpression compCreateExpr = this._exprFac.GetExpression("group", ExpressionFactoryNameTarget.OperatorSymbol);
            IExpression idExpr1        = ModelResolvers.ExprResolver();
            IExpression idExpr2        = ModelResolvers.ExprResolver();

            idExpr1.ExpressionText = "Id1";
            idExpr2.ExpressionText = "Id2";
            idExpr1.Structure      = ModelResolvers.DsResolver("Id1", ComponentType.Identifier, (BasicDataType)types[0]);
            idExpr2.Structure      = ModelResolvers.DsResolver("Id2", ComponentType.Identifier, (BasicDataType)types[1]);
            compCreateExpr.AddOperand("ds_1", idExpr1);
            compCreateExpr.AddOperand("ds_2", idExpr2);

            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(this._exprFac.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            joinExpr.AddOperand("ds_1", compCreateExpr);

            IDataStructure expectedStructure = compCreateExpr.Operands["ds_1"].Structure.GetCopy();

            expectedStructure.Identifiers.Add(compCreateExpr.Operands["ds_2"].Structure.GetCopy().Identifiers[0]);

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

            Assert.True(expectedStructure.EqualsObj(dataStructure));
        }
Esempio n. 7
0
 /// <summary>
 /// Performs a joint of two tables.
 /// </summary>
 /// <returns></returns>
 public static QueryResults <KeyValuePair <Selectable1, Selectable2> > JoinAsync <Selectable1, T1, Selectable2, T2, Key>(
     Node <object> table1, IJoinExpression <Selectable1, T1, Key> expression1,
     Node <object> table2, IJoinExpression <Selectable2, T2, Key> expression2)
     where Key : IComparable <Key>
 {
     return(null);
 }
Esempio n. 8
0
        public void CurrentJoinExpr_JoinExprAncestor_CorrectExpression()
        {
            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(TestExprFactory.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));
            Expression      expr     = new Expression(joinExpr);

            Assert.Equal(joinExpr, expr.CurrentJoinExpr);
        }
Esempio n. 9
0
 /// <summary>
 /// Performs a joint of two tables.
 /// </summary>
 /// <returns></returns>
 public static List <KeyValuePair <Selectable1, Selectable2> > Join <Selectable1, T1, Selectable2, T2, Key>(
     Node <object> table1, IJoinExpression <Selectable1, T1, Key> expression1,
     Node <object> table2, IJoinExpression <Selectable2, T2, Key> expression2)
     where Key : IComparable <Key>
 {
     return(new List <KeyValuePair <Selectable1, Selectable2> >());;
 }
Esempio n. 10
0
        public static ISelectStatement Join(this ISelectStatement select, IJoinExpression join)
        {
            var joins = select?.Joins?.ToList() ?? new List <IJoinExpression>();

            joins.Add(join);
            select.Joins = joins.ToArray();
            return(select);
        }
Esempio n. 11
0
 /// <summary>
 /// Performs a joint of two tables.
 /// </summary>
 /// <returns></returns>
 public static QueryResults <Joined> JoinAsync <Joined, Selectable1, T1, Selectable2, T2, Key>(
     Node <object> table1, IJoinExpression <Selectable1, T1, Key> expression1,
     Node <object> table2, IJoinExpression <Selectable2, T2, Key> expression2,
     JoinDelegate <Joined, Selectable1, Selectable2> customJoin)
     where Key : IComparable <Key>
 {
     return(null);
 }
        /// <summary>
        /// Gets an expression with a given id name from an aliases collection from a "ds" branch.
        /// </summary>
        /// <param name="joinExpr">The "join" operator expression.</param>
        /// <param name="idName">The name of the identifier.</param>
        /// <returns>Operand containing a given id name.</returns>
        public static IExpression GetAliasExprWithId(this IJoinExpression joinExpr, string idName)
        {
            List <IExpression> aliases = joinExpr.Operands["ds"].OperandsCollection.Where(d => d.Structure.Identifiers.FirstOrDefault(id =>
                                                                                                                                      id.ComponentName.GetNameWithoutAlias() == idName.GetNameWithoutAlias()) != null).ToList();

            if (aliases.Count > 1 && idName.Split('#').Length > 1)
            {
                return(aliases.First(alias => alias.ParamSignature == idName.Split('#')[0]));
            }
            return(aliases.FirstOrDefault());
        }
        public void Build_Expr_Expr()
        {
            UsingBranch usingBranch = new UsingBranch(this._exprFac);

            IExpression     expr     = TestExprFactory.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol);
            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(expr);

            IExpression ds1Expr = TestExprFactory.GetExpression("get", ExpressionFactoryNameTarget.OperatorSymbol);

            ds1Expr.Structure = ModelResolvers.DsResolver();
            ds1Expr.Structure.Identifiers.Add(new StructureComponent(BasicDataType.Integer, "Id1"));
            ds1Expr.Structure.Identifiers.Add(new StructureComponent(BasicDataType.Boolean, "Id2"));

            IExpression ds2Expr = TestExprFactory.GetExpression("get", ExpressionFactoryNameTarget.OperatorSymbol);

            ds2Expr.Structure = ModelResolvers.DsResolver();
            ds2Expr.Structure.Identifiers.Add(new StructureComponent(BasicDataType.Integer, "Id1"));
            ds2Expr.Structure.Identifiers.Add(new StructureComponent(BasicDataType.Boolean, "Id2"));
            ds2Expr.Structure.Identifiers.Add(new StructureComponent(BasicDataType.String, "Id3"));

            IExpression dsBranch = ModelResolvers.ExprResolver();

            dsBranch.AddOperand("ds_1", ds1Expr);
            dsBranch.AddOperand("ds_2", ds2Expr);

            joinExpr.AddOperand("ds", dsBranch);

            IExpression expected = TestExprFactory.GetExpression("Using", ExpressionFactoryNameTarget.ResultName);

            expected.ParamSignature = "using";
            expected.ExpressionText = "using Id1, Id2";

            IExpression expected_1 = ModelResolvers.ExprResolver();
            IExpression expected_2 = ModelResolvers.ExprResolver();

            expected_1.ExpressionText = "Id1";
            expected_2.ExpressionText = "Id2";

            expected.AddOperand("Id1", expected_1);
            expected.AddOperand("Id2", expected_2);

            IExpression result = usingBranch.Build(joinExpr);

            Assert.Equal(expected.OperandsCollection.Count, result.OperandsCollection.Count);
            Assert.Equal(expected.Operands["Id1"].ExpressionText, result.Operands["Id1"].ExpressionText);
            Assert.Equal(expected.Operands["Id2"].ExpressionText, result.Operands["Id2"].ExpressionText);
            Assert.Equal(expected.ExpressionText, result.ExpressionText);
        }
        /// <summary>
        /// Gets an expression with a given viral attribute from an aliases collection from a "ds" branch.
        /// </summary>
        /// <param name="joinExpr">the "join" operator expression.</param>
        /// <param name="attributeName">The name of the viral attribute.</param>
        /// <returns>Operand containing a given viral attribute.</returns>
        public static IExpression GettAliasExprWithViralAttribute(this IJoinExpression joinExpr, string attributeName)
        {
            List <IExpression> aliases = joinExpr.Operands["ds"].OperandsCollection.Where(d => d.Structure.ViralAttributes.FirstOrDefault(at =>
                                                                                                                                          at.ComponentName.GetNameWithoutAlias() == attributeName.GetNameWithoutAlias()) != null).ToList();

            if (aliases.Count > 1)
            {
                if (attributeName.Split('#').Length > 1)
                {
                    return(aliases.First(alias => alias.ParamSignature == attributeName.Split('#')[0]));
                }
                return(null);
            }

            return(aliases.FirstOrDefault());
        }
        /// <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);
        }
        public void GetOutputStructure_2ScalarsExpr_DataStructure(string opSymbol, params TestExprType[] types)
        {
            IExpression compCreateExpr = TestExprFactory.GetExpression(types);

            compCreateExpr.OperatorDefinition = this._exprFac.OperatorResolver(opSymbol);
            compCreateExpr.Operands["ds_2"].Structure.Measures[0].ComponentName += "2";

            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(this._exprFac.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            joinExpr.AddOperand("ds_1", compCreateExpr);

            IDataStructure expectedStructure = compCreateExpr.Operands["ds_1"].Structure.GetCopy();

            expectedStructure.Measures.Add(compCreateExpr.Operands["ds_2"].Structure.GetCopy().Measures[0]);

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

            Assert.True(expectedStructure.EqualsObj(dataStructure));
        }
Esempio n. 17
0
        public new IDataStructure GetOutputStructure(IExpression expression)
        {
            IDataStructure  structure = base.GetOutputStructure(expression);
            IJoinExpression joinExpr  = expression.CurrentJoinExpr;

            if (joinExpr?.Operands.ContainsKey("calc") == true)
            {
                foreach (StructureComponent renameComponent in structure.Components)
                {
                    if (joinExpr.Operands["calc"].Structure.Components.FirstOrDefault(calcComponent => calcComponent.ComponentName == renameComponent.ComponentName) != null &&
                        joinExpr.GetSubsetAliasStructure().Components.FirstOrDefault(dsComponent => dsComponent.ComponentName == renameComponent.ComponentName) == null)
                    {
                        throw new VtlOperatorError(expression, this.Name, $"Rename branch of \"join\" operator expression cannot use component name {renameComponent.ComponentName} because it's in use in \"calc\" branch.");
                    }
                }
            }

            return(structure);
        }
        /// <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);
        }
        /// <summary>
        /// Gets an expression with a given measure name from an aliases collection from a "ds" branch.
        /// </summary>
        /// <param name="joinExpr">The "join" operator expression.</param>
        /// <param name="measureName">The name of the measure.</param>
        /// <returns>Operand containing a given measure name.</returns>
        public static IExpression GetAliasExprWithMeasure(this IJoinExpression joinExpr, string measureName)
        {
            List <IExpression> aliases = joinExpr.Operands["ds"].OperandsCollection.Where(d => d.Structure.Measures.FirstOrDefault(me =>
                                                                                                                                   me.ComponentName.GetNameWithoutAlias() == measureName.GetNameWithoutAlias()) != null).ToList();

            if (aliases.Count == 0 &&
                joinExpr.Operands.ContainsKey("apply") &&
                joinExpr.Operands["apply"].OperatorSymbol.In("#", "period_indicator"))
            {
                aliases = joinExpr.Operands["ds"].OperandsCollection.Where(d => d.Structure.Components.FirstOrDefault(me =>
                                                                                                                      me.ComponentName.GetNameWithoutAlias() == measureName.GetNameWithoutAlias()) != null).ToList();
            }
            if (aliases.Count > 1 &&
                measureName.Split('#').Length > 1)
            {
                return(aliases.First(alias => alias.ParamSignature == measureName.Split('#')[0]));
            }

            return(aliases.FirstOrDefault());
        }
        public void GetOutputStructure_GroupNoId_ThrowsException()
        {
            TestExprType[][] wrongCombs = Enum.GetValues(typeof(TestExprType)).Cast <TestExprType>().Where(type => (int)type <= 8).GetCombinations(2);

            IJoinExpression joinExpr = ModelResolvers.JoinExprResolver(this._exprFac.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            foreach (TestExprType[] wrongComb in wrongCombs)
            {
                IExpression compCreateExpr = this._exprFac.GetExpression("group", ExpressionFactoryNameTarget.OperatorSymbol);
                IExpression idExpr1        = ModelResolvers.ExprResolver();
                IExpression idExpr2        = ModelResolvers.ExprResolver();

                idExpr1.ExpressionText = "Id1";
                idExpr2.ExpressionText = "Id2";
                idExpr1.Structure      = ModelResolvers.DsResolver("Id1", ComponentType.Identifier, (BasicDataType)wrongComb[0]);
                idExpr2.Structure      = ModelResolvers.DsResolver("Id2", ComponentType.Measure, (BasicDataType)wrongComb[1]);
                compCreateExpr.AddOperand("ds_1", idExpr1);
                compCreateExpr.AddOperand("ds_2", idExpr2);
                joinExpr.AddOperand("ds_1", compCreateExpr);

                // Debug condition example: wrongComb[0] == TestExprType.Integer && wrongComb[1] == TestExprType.Integer
                Assert.ThrowsAny <VtlOperatorError>(() => { compCreateExpr.OperatorDefinition.GetOutputStructure(compCreateExpr); });
            }
        }
        public void Build_CorrectBranches_JoinExpr(params string[] branches)
        {
            JoinBuilder joinBuilder = new JoinBuilder(ModelResolvers.JoinExprResolver, this.joinBranches);

            joinBuilder.AddMainExpr(TestExprFactory.GetExpression("join", ExpressionFactoryNameTarget.OperatorSymbol));

            Assert.Empty(joinBuilder.Branches["main"].OperandsCollection);

            foreach (string branch in branches)
            {
                joinBuilder.AddBranch(branch, TestExprFactory.GetExpression("opt", ExpressionFactoryNameTarget.OperatorSymbol));
            }

            IJoinExpression result = joinBuilder.Build();

            Assert.Equal(branches.Length, joinBuilder.Branches["main"].OperandsCollection.Count);
            Assert.Equal(branches.Length, result.OperandsCollection.Count);

            foreach (string branch in branches)
            {
                Assert.True(joinBuilder.Branches["main"].Operands.ContainsKey(branch));
                Assert.True(result.Operands.ContainsKey(branch));
            }
        }
        /// <summary>
        /// Transforms a dataset expression to a "join" operator expression.
        /// </summary>
        /// <param name="expression">The expression to modify.</param>
        /// <returns>The modified expression.</returns>
        private IJoinExpression TransformToJoin(IExpression expression)
        {
            IExpression additionalAliasExpr = null;

            if (expression.OperatorSymbol == "datasetClause")
            {
                if (expression.Operands["ds_1"].OperatorSymbol == "join")
                {
                    additionalAliasExpr = expression.Operands["ds_1"];
                }

                string resultName = expression.Operands["ds_2"].ResultName;

                if (!resultName.In(DatasetClauseOperator.ClauseNames))
                {
                    throw new ArgumentException($"Unknown dataset clause symbol: {resultName}", "expression");
                }

                if (resultName == "Aggregation")
                {
                    IExpression aggrExpr = expression.Operands["ds_2"];

                    this._builder.AddBranch("calc", aggrExpr.Operands["calc"]);
                    this._builder.AddBranch("group", aggrExpr.Operands["group"]);
                    if (aggrExpr.Operands.ContainsKey("having"))
                    {
                        this._builder.AddBranch("having", aggrExpr.Operands["having"]);
                    }

                    this._builder.Branches["calc"].OperatorDefinition.Keyword = "Aggr";
                }
                else
                {
                    this._builder.AddBranch(resultName.ToLower(), expression.Operands["ds_2"]);
                    if (resultName == "Filter")
                    {
                        if (!this._builder.Branches["filter"].Structure.IsSingleComponent || this._builder.Branches["filter"].Structure.Components[0].ValueDomain.DataType != BasicDataType.Boolean)
                        {
                            throw new ArgumentException("Expected boolean single component expression as filter branch.", "expression");
                        }
                    }
                }
            }
            else if (expression.OperatorSymbol.In(AnalyticFunctionOperator.Symbols))
            {
                this._builder.AddBranch("over", expression.Operands["over"]);
            }
            else if (expression.OperatorSymbol.In(AggrFunctionOperator.Symbols))
            {
                if (expression.Operands.ContainsKey("group"))
                {
                    this._builder.AddBranch("group", expression.Operands["group"]);
                    if (expression.Operands.ContainsKey("having"))
                    {
                        this._builder.AddBranch("having", expression.Operands["having"]);
                    }
                    if (expression.OperatorSymbol == "count")
                    {
                        this._builder.BuildBranch("calc", expression);
                    }
                }
                else
                {
                    this._builder.AddBranch("over", expression.Operands["over"]);
                }
            }
            else if (expression.OperatorSymbol.In(JoinOperators.ComparisonOperators) &&
                     (expression.Operands["ds_1"].Structure.Measures[0].ComponentName != "bool_var" ||
                      expression.Operands["ds_2"].Structure.Measures[0].ComponentName != "bool_var"))
            {
                this._builder.BuildBranch("rename", expression);
            }

            if (additionalAliasExpr == null)
            {
                this._builder.BuildBranch("ds", expression);
            }
            if (expression.OperatorSymbol != "datasetClause" && !this._builder.Branches.ContainsKey("calc"))
            {
                this._builder.BuildBranch("apply", expression);
            }

            expression.OperatorDefinition         = this._exprFactory.OperatorResolver("join");
            expression.OperatorDefinition.Keyword = "inner";
            expression.Operands.Clear();

            if (additionalAliasExpr != null)
            {
                IExpression dsBranch = this._exprFactory.GetExpression("Alias", ExpressionFactoryNameTarget.ResultName);
                dsBranch.ExpressionText = $"{additionalAliasExpr.ExpressionText} as ds1";
                dsBranch.AddOperand("ds1", additionalAliasExpr);

                additionalAliasExpr.OperatorDefinition         = this._exprFactory.OperatorResolver("join");
                additionalAliasExpr.OperatorDefinition.Keyword = "inner";
                additionalAliasExpr.ResultName = "Join";

                this._builder.Branches.Add("ds", dsBranch);
            }

            expression.AddOperand("ds", this._builder.Branches["ds"]);
            expression = this._joinExprResolver(expression);

            this._builder.BuildBranch("using", expression);
            this._builder.AddMainExpr(expression);

            IJoinExpression joinExpr = this._builder.Build();

            this._builder.Clear();

            joinExpr.Structure = joinExpr.OperatorDefinition.GetOutputStructure(joinExpr);
            return(joinExpr);
        }
 /// <summary>
 /// Precesses a "join" expression's applying operations at a given data structure.
 /// </summary>
 /// <param name="expression">The "join" expression.</param>
 /// <param name="mergedStructure">The data structure.</param>
 private IDataStructure ProcessApplyBranch(IJoinExpression expression, IDataStructure mergedStructure)
 {
     mergedStructure.Measures = expression.Operands["apply"].Structure.GetCopy().Measures;
     return(mergedStructure);
 }
        /// <summary>
        /// Precesses a "join" expression's keeping/dropping operations at a given data structure.
        /// </summary>
        /// <param name="expression">The "join" expression.</param>
        /// <param name="mergedStructure">The data structure.</param>
        private IDataStructure ProcessKeepDropBranch(IJoinExpression expression, IDataStructure mergedStructure)
        {
            bool           isKeep           = expression.Operands.ContainsKey("keep");
            IExpression    removerExpr      = isKeep ? expression.Operands["keep"] : expression.Operands["drop"];
            string         symbol           = removerExpr.OperatorSymbol;
            IDataStructure removerStructure = removerExpr.Structure.GetCopy();

            if (symbol == "drop")
            {
                List <string> dropped = new List <string>();
                while (removerStructure.Components.Count > 0)
                {
                    StructureComponent removerComp = removerStructure.Components[0];
                    StructureComponent component   = mergedStructure.Components.FirstOrDefault(comp => comp.BaseComponentName.GetNameWithoutAlias() == removerComp.BaseComponentName.GetNameWithoutAlias());

                    if (component == null)
                    {
                        throw new VtlOperatorError(expression, this.Name, $"Could not {symbol} component {removerComp.ComponentName}");
                    }

                    component.BaseComponentName = removerComp.BaseComponentName;
                    component.ComponentName     = removerComp.ComponentName;

                    dropped.Add(component.ComponentName);

                    string[] freeAliases = expression.GetAliasesSignatures(component.BaseComponentName.GetNameWithoutAlias()).Where(
                        signature => dropped.FirstOrDefault(d => d.Contains(component.BaseComponentName.GetNameWithoutAlias()) &&
                                                            (d.Contains($"{signature}#") || !d.Contains('#'))) == null).ToArray();
                    string[] newNames = null;
                    if (freeAliases != null)
                    {
                        newNames = new string[freeAliases.Length];
                        for (int i = 0; i < freeAliases.Length; i++)
                        {
                            if (component.BaseComponentName.Split('#').Length == 1)
                            {
                                newNames[i] = $"{freeAliases[i]}#{component.BaseComponentName}";
                            }
                            else
                            {
                                newNames[i] = $"{freeAliases[i]}#{component.BaseComponentName.Split('#')[1]}";
                            }
                        }
                    }

                    switch (component.ComponentType)
                    {
                    case ComponentType.Identifier: throw new VtlOperatorError(expression, this.Name, $"Unexpected Identifier component.");

                    case ComponentType.Measure:
                        mergedStructure.Measures.Remove(component);
                        if (newNames != null)
                        {
                            foreach (string newName in newNames)
                            {
                                mergedStructure.Measures.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                            }
                        }

                        break;

                    case ComponentType.ViralAttribute:
                        mergedStructure.ViralAttributes.Remove(component);
                        if (newNames != null)
                        {
                            foreach (string newName in newNames)
                            {
                                mergedStructure.ViralAttributes.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                            }
                        }

                        break;

                    case ComponentType.NonViralAttribute:
                        mergedStructure.NonViralAttributes.Remove(component);
                        if (newNames != null)
                        {
                            foreach (string newName in newNames)
                            {
                                mergedStructure.NonViralAttributes.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                            }
                        }

                        break;

                    default: throw new VtlOperatorError(expression, this.Name, $"Unknown component type: {component.ComponentType}.");
                    }

                    switch (removerComp.ComponentType)
                    {
                    case ComponentType.Identifier: throw new VtlOperatorError(expression, this.Name, $"Unexpected identifier component.");

                    case ComponentType.Measure: removerStructure.Measures.Remove(removerComp); break;

                    case ComponentType.ViralAttribute: removerStructure.ViralAttributes.Remove(removerComp); break;

                    case ComponentType.NonViralAttribute: removerStructure.NonViralAttributes.Remove(removerComp); break;

                    default: throw new VtlOperatorError(expression, this.Name, $"Unknown component type: {removerComp.ComponentType}.");
                    }
                }
            }
            else
            {
                mergedStructure.Measures           = removerStructure.Measures;
                mergedStructure.ViralAttributes    = removerStructure.ViralAttributes;
                mergedStructure.NonViralAttributes = removerStructure.NonViralAttributes;
            }

            return(mergedStructure);
        }
Esempio n. 25
0
 public static ISelectStatement J(this ISelectStatement select, IJoinExpression join)
 {
     return(Join(select, join));
 }
Esempio n. 26
0
 /// <summary>
 /// Constructs a criteria with one expression.
 /// </summary>
 /// <param name="firstExpr">The first expression to add.</param>
 /// <param name="howToAddExpressions">How expressions will be added together.  Determines
 ///                                   if we do exp1 AND exp2 AND exp3, or if we do
 ///                                   exp1 OR exp2 OR exp3.</param>
 /// <param name="typeOfJoin">Is this an inner join, left join, etc.</param>
 public DaoJoinCriteria(JoinType typeOfJoin, IJoinExpression firstExpr,
     BooleanOperator howToAddExpressions)
 {
     TypeOfJoin = typeOfJoin;
     BoolType = howToAddExpressions;
     if (firstExpr != null)
     {
         Expressions.Add(firstExpr);
     }
 }
Esempio n. 27
0
 /// <summary>
 /// Constructs a criteria with one expression.  May be handy for cases
 /// where you only need one expression.
 /// </summary>
 /// <param name="firstExpr">The first expression to add.</param>
 /// <param name="typeOfJoin">Is this an inner join, left join, etc.</param>
 public DaoJoinCriteria(JoinType typeOfJoin, IJoinExpression firstExpr)
     : this(typeOfJoin, firstExpr, BooleanOperator.And)
 {
 }
Esempio n. 28
0
 /// <summary>
 /// Constructs an inner join criteria with one expression.
 /// </summary>
 /// <param name="firstExpr">The first expression to add.</param>
 /// <param name="howToAddExpressions">How expressions will be added together.  Determines
 ///                                   if we do exp1 AND exp2 AND exp3, or if we do
 ///                                   exp1 OR exp2 OR exp3.</param>
 public DaoJoinCriteria(IJoinExpression firstExpr,
     BooleanOperator howToAddExpressions)
     : this(JoinType.Inner, firstExpr, howToAddExpressions)
 {
 }
Esempio n. 29
0
 /// <summary>
 /// Constructs an inner join criteria with one expression.  May be handy for cases
 /// where you only need one expression.
 /// </summary>
 /// <param name="firstExpr">The first expression to add.</param>
 public DaoJoinCriteria(IJoinExpression firstExpr)
     : this(JoinType.Inner, firstExpr, BooleanOperator.And)
 {
 }
        /// <summary>
        /// Precesses a "join" expression's renaming operations at a given data structure.
        /// </summary>
        /// <param name="expression">The "join" expression.</param>
        /// <param name="mergedStructure">The data structure.</param>
        private IDataStructure ProcessRenameBranch(IJoinExpression expression, IDataStructure mergedStructure)
        {
            IDataStructure renameStructure = expression.Operands["rename"].Structure.GetCopy();

            while (renameStructure.Components.Count > 0)
            {
                bool renamed = false;
                foreach (StructureComponent component in mergedStructure.Components)
                {
                    bool               divideComp = false;
                    string             baseName   = component.BaseComponentName;
                    StructureComponent renameComp = renameStructure.Components.FirstOrDefault(comp => comp.BaseComponentName == baseName);

                    if (renameComp == null)
                    {
                        divideComp = true;
                        baseName   = baseName.GetNameWithoutAlias();
                        string[] aliases = expression.GetAliasesSignatures(baseName);

                        foreach (string alias in aliases)
                        {
                            string name = $"{alias}#{baseName}";
                            renameComp = renameStructure.Components.FirstOrDefault(comp => comp.BaseComponentName == name);
                            if (renameComp != null)
                            {
                                baseName = name;
                                break;
                            }
                        }
                    }

                    if (renameComp != null)
                    {
                        string[] freeAliases = null;
                        string[] newNames    = null;

                        if (divideComp)
                        {
                            freeAliases = expression.GetAliasesSignatures(baseName.GetNameWithoutAlias())
                                          .Where(alias => alias != baseName.Split('#')[0]).ToArray();
                            if (freeAliases != null)
                            {
                                newNames = new string[freeAliases.Length];
                                for (int i = 0; i < freeAliases.Length; i++)
                                {
                                    newNames[i] = $"{freeAliases[i]}#{component.BaseComponentName.GetNameWithoutAlias()}";
                                }
                            }
                        }

                        switch (renameComp.ComponentType)
                        {
                        case ComponentType.Identifier:
                            mergedStructure.Identifiers.Remove(mergedStructure.Identifiers.FirstOrDefault(id => id.BaseComponentName == renameComp.BaseComponentName));
                            renameStructure.Identifiers.Remove(renameComp);
                            if (newNames != null)
                            {
                                foreach (string newName in newNames)
                                {
                                    mergedStructure.Identifiers.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                                }
                            }

                            break;

                        case ComponentType.Measure:
                            mergedStructure.Measures.Remove(mergedStructure.Measures.FirstOrDefault(me => me.BaseComponentName == renameComp.BaseComponentName));
                            renameStructure.Measures.Remove(renameComp);
                            if (newNames != null)
                            {
                                foreach (string newName in newNames)
                                {
                                    mergedStructure.Measures.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                                }
                            }

                            break;

                        case ComponentType.ViralAttribute:
                            mergedStructure.ViralAttributes.Remove(mergedStructure.ViralAttributes.FirstOrDefault(at => at.BaseComponentName == renameComp.BaseComponentName));
                            renameStructure.ViralAttributes.Remove(renameComp);
                            if (newNames != null)
                            {
                                foreach (string newName in newNames)
                                {
                                    mergedStructure.ViralAttributes.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                                }
                            }

                            break;

                        case ComponentType.NonViralAttribute:
                            mergedStructure.NonViralAttributes.Remove(mergedStructure.NonViralAttributes.FirstOrDefault(at => at.BaseComponentName == renameComp.BaseComponentName));
                            renameStructure.NonViralAttributes.Remove(renameComp);
                            if (newNames != null)
                            {
                                foreach (string newName in newNames)
                                {
                                    mergedStructure.NonViralAttributes.Add(new StructureComponent(component.ValueDomain.DataType, newName));
                                }
                            }

                            break;

                        default: throw new VtlOperatorError(expression, this.Name, $"Unknown component type: {component.ComponentType}.");
                        }

                        component.BaseComponentName = renameComp.BaseComponentName;
                        component.ComponentName     = renameComp.ComponentName;

                        renamed = true;
                        break;
                    }
                }

                if (!renamed)
                {
                    throw new VtlOperatorError(expression, this.Name, $"Could not rename one of given components. Have any of them been dropped?");
                }
            }

            return(mergedStructure);
        }