Example #1
0
        /// <summary>
        /// Creates a measure expression
        /// </summary>
        /// <param name="node">method call node</param>
        /// <returns>a measure expression corresponding to the node</returns>
        internal Expression CreateMeasureExpression(MethodCallExpression node)
        {
            _measureCounter++;
            Expression c = _binder.Visit(node.Arguments[0]);
            Type       t = node.Method.ReturnType;

            if (c is GroupingProjection)
            {
                GroupingProjection grp = ((GroupingProjection)c);
                c = grp.SubQueryProjectionExpression.Projector;
                t = grp.SubQueryProjectionExpression.Projector.Type;
            }
            if (c is ProjectionExpression)
            {
                c = ((ProjectionExpression)c).Projector;
            }

            var col = c as ColumnExpression;

            if (col != null)
            {
                var table = col.TableName;

                // TODO: Add a level of indirection here as well see XAggregations
                string dbname = node.Method.Name.ToUpper() + "(" + col.DbName + ")";

                string name = string.Format("[{0}Of{1}{2}]", node.Method.Name, col.Name, _measureCounter);
                if (node.Arguments.Count() == 1)
                {
                    return(new MeasureExpression(
                               t, //node.Method.ReturnType,
                               name,
                               dbname,
                               name,
                               table));
                }

                Expression filter;
                switch (node.Arguments[1].NodeType)
                {
                case ExpressionType.Parameter:
                    filter = TableFactory.GetTableExpression(node.Arguments[1]);
                    break;

                default:
                    filter = _binder.Visit(node.Arguments[1]);
                    break;
                }

                return(new MeasureExpression(t, (ExpressionType)DaxExpressionType.Measure, name, dbname, name, table,
                                             filter));
            }

            throw new TabularException("could not create measure: the column reference was not found");
        }
        private Expression VisitGroupingProjection(GroupingProjection groupingProjection)
        {
            var key      = (ProjectionExpression)Visit(groupingProjection.KeyProjection);
            var subQuery = (ProjectionExpression)Visit(groupingProjection.SubQueryProjectionExpression);

            if (key != groupingProjection.KeyProjection || subQuery != groupingProjection.SubQueryProjectionExpression)
            {
                return(new GroupingProjection(groupingProjection.Type, key, subQuery));
            }

            return(groupingProjection);
        }