public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var arg             = node.Arguments[1] as NewArrayExpression;
                var argMember       = node.Arguments[1] as MemberExpression;

                string expr2;

                if (arg != null)
                {
                    expr2 = string.Join(", ", arg.Expressions.Select(x => innerTranslator.VisitAndClearBuffer(x)));
                }
                else if (argMember != null)
                {
                    var names = (Array)Expression.Lambda(argMember).Compile().DynamicInvoke();
                    expr2 = string.Join(", ", names.Cast <object>().Select(x => DataTypeFormatter.Format(x)));
                }
                else
                {
                    throw new InvalidOperationException();
                }

                return(string.Format("{0} {1}({2})", expr1, fuctionName, expr2));
            }
Exemple #2
0
        public override string ToString()
        {
            var translator = new BigQueryTranslateVisitor();

            var orderByBuilder = new List <string>();
            FullWindowFunction <TSource, TResult, TPartitionKey> self = this;

            while (self.parent != null)
            {
                orderByBuilder.Add(translator.VisitAndClearBuffer(self.orderKeySelector) + (self.isDescending ? " DESC" : ""));
                self = self.parent;
            }
            orderByBuilder.Add(translator.VisitAndClearBuffer(self.orderKeySelector) + (self.isDescending ? " DESC" : ""));
            orderByBuilder.Reverse();

            if (self.partitionKeySelector != null)
            {
                var expr1 = translator.VisitAndClearBuffer(self.partitionKeySelector);
                var expr2 = string.Join(", ", orderByBuilder);
                var s     = string.Format("{0}({1}) OVER (PARTITION BY {2} ORDER BY {3})", self.methodName, self.argument, expr1, expr2);
                return(s);
            }
            else
            {
                var expr2 = string.Join(", ", orderByBuilder);
                var s     = string.Format("{0}({1}) OVER (ORDER BY {2})", self.methodName, self.argument, expr2);
                return(s);
            }
        }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var expr2           = innerTranslator.VisitAndClearBuffer(node.Arguments[1]);

                return(expr1 + " " + fuctionName + " " + expr2);
            }
Exemple #4
0
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var expr       = node.Arguments[0];
                var translater = new BigQueryTranslateVisitor(depth, indentSize);
                var nodeName   = translater.VisitAndClearBuffer(node.Arguments[1]);
                var str        = translater.VisitAndClearBuffer(expr);

                return(str + " WITHIN " + nodeName);
            }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var expr2           = innerTranslator.VisitAndClearBuffer(node.Arguments[1]);
                var expr3           = innerTranslator.VisitAndClearBuffer(node.Arguments[2]);

                return(string.Format("({0} BETWEEN {1} AND {2})", expr1, expr2, expr3));
            }
Exemple #6
0
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var arg             = node.Arguments[1] as NewArrayExpression;
                var expr2           = string.Join(", ", arg.Expressions.Select(x => innerTranslator.VisitAndClearBuffer(x)));

                return(string.Format("{0} {1}({2})", expr1, fuctionName, expr2));
            }
Exemple #7
0
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();

                var timestamp    = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var interval     = innerTranslator.VisitAndClearBuffer(node.Arguments[1]);
                var intervalUnit = ((IntervalUnit)(node.Arguments[2] as ConstantExpression).Value).ToString().ToUpper();

                return(string.Format("DATE_ADD({0}, {1}, '{2}')", timestamp, interval, intervalUnit));
            }
Exemple #8
0
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innnerTranslator = new BigQueryTranslateVisitor(0, 0);

                var expr1 = innnerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var expr2 = innnerTranslator.VisitAndClearBuffer(node.Arguments[1]);

                var format = string.Format("REGEXP_EXTRACT({0}, r{1})", expr1, expr2);

                return(format);
            }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innnerTranslator = new BigQueryTranslateVisitor(0, 0);

                var expr1 = innnerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var expr2 = innnerTranslator.VisitAndClearBuffer(node.Arguments[1]);

                var format = string.Format("REGEXP_EXTRACT({0}, r{1})", expr1, expr2);

                return format;
            }
Exemple #10
0
        protected override Expression VisitMemberInit(MemberInitExpression node)
        {
            var indent          = BuildIndent();
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            var merge = node.Bindings.Select(expr =>
            {
                var assignment = expr as MemberAssignment;
                var x          = assignment.Member;
                var y          = assignment.Expression;

                var rightValue = innerTranslator.VisitAndClearBuffer(y);

                if (x.Name == rightValue.Trim('[', ']'))
                {
                    return("[" + x.Name + "]");
                }

                return(rightValue + " AS " + "[" + x.Name + "]");
            });

            var command = string.Join("," + Environment.NewLine,
                                      merge.Select(x => indent + x));

            sb.Append(command);

            return(node);
        }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var args            = string.Join(", ", node.Arguments.Select(x => innerTranslator.VisitAndClearBuffer(x)));

                return("COUNT(DISTINCT " + args + ")");
            }
Exemple #12
0
        public override string ToString()
        {
            var translator = new BigQueryTranslateVisitor();
            var expr1      = translator.VisitAndClearBuffer(partitionKeySelector);
            var s          = string.Format("{0}({1}) OVER (PARTITION BY {2})", methodName, argument, expr1);

            return(s);
        }
Exemple #13
0
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();

                var arg  = node.Arguments[0] as NewArrayExpression;
                var expr = string.Join(", ", arg.Expressions.Select(x => innerTranslator.VisitAndClearBuffer(x)));

                return("CONCAT(" + expr + ")");
            }
Exemple #14
0
        public override string ToString()
        {
            var translator = new BigQueryTranslateVisitor();

            if (partitionKeySelector != null)
            {
                var expr1 = translator.VisitAndClearBuffer(partitionKeySelector);
                var expr2 = translator.VisitAndClearBuffer(orderKeySelector);
                var s     = string.Format("{0}({1}) OVER (PARTITION BY {2} ORDER BY {3}{4})", methodName, argument, expr1, expr2, (isDescending) ? " DESC" : "");
                return(s);
            }
            else
            {
                var expr2 = translator.VisitAndClearBuffer(orderKeySelector);
                var s     = string.Format("{0}({1}) OVER (ORDER BY {2}{3})", methodName, argument, expr2, (isDescending) ? " DESC" : "");
                return(s);
            }
        }
Exemple #15
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            // special case, String.Contains
            if (node.Method == StringContains)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Object);
                var expr2           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                sb.Append(expr1 + " CONTAINS " + expr2);
                return(node);
            }

            // window function
            if (node.Method.GetCustomAttributes <WindowFunctionAlertAttribute>().Any())
            {
                throw new InvalidOperationException("WindowFunction must call .Value property");
            }

            var attr = node.Method.GetCustomAttributes <FunctionNameAttribute>().FirstOrDefault();

            if (attr == null)
            {
                throw new InvalidOperationException("Not support method:" + node.Method.DeclaringType.Name + "." + node.Method.Name + " Method can only call BigQuery.Linq.Functions.*");
            }

            if (attr.SpecifiedFormatterType != null)
            {
                var formatter = Activator.CreateInstance(attr.SpecifiedFormatterType, true) as ISpecifiedFormatter;
                sb.Append(formatter.Format(depth, indentSize, attr.Name, node));
            }
            else
            {
                sb.Append(attr.Name + "(");

                var innerTranslator = new BigQueryTranslateVisitor();
                var args            = string.Join(", ", node.Arguments.Select(x => innerTranslator.VisitAndClearBuffer(x)));

                sb.Append(args);

                sb.Append(")");
            }

            return(node);
        }
            public string Format(int depth, int indentSize, string functionName, MethodCallExpression node)
            {
                var type       = node.Method.GetGenericArguments()[0];
                var dataType   = DataTypeUtility.ToDataType(type);
                var identifier = dataType.ToIdentifier();

                var innerTranslator = new BigQueryTranslateVisitor();
                var expr            = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);

                return($"{functionName}({expr} AS {identifier})");
            }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1           = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var l    = Expression.Lambda(node.Arguments[1]);
                var ctor = l.Compile();
                var bq   = ctor.DynamicInvoke(null) as ExecutableBigQueryableBase;

                var indent = new string(' ', indentSize *(depth));
                var sb     = new StringBuilder();

                sb.AppendLine(expr1 + " " + fuctionName);
                sb.Append(indent);
                sb.AppendLine("(");

                sb.AppendLine(bq.ToQueryString(depth + 1));

                sb.Append(indent);
                sb.Append(")");

                return(sb.ToString());
            }
Exemple #18
0
        protected override Expression VisitNew(NewExpression node)
        {
            // specialize for DateTime
            if (FormatIfExprIsDateTime(node))
            {
                return(node);
            }

            // promising type initializer(goto:VisitMemberInit)
            if (!node.Type.IsAnonymousType())
            {
                return(node);
            }

            var indent          = BuildIndent();
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            var merge = node.Members.Zip(node.Arguments, (x, y) =>
            {
                var rightValue = innerTranslator.VisitAndClearBuffer(y);

                if (x.Name == rightValue.Trim('[', ']'))
                {
                    return("[" + x.Name + "]");
                }

                return(rightValue + " AS " + "[" + x.Name + "]");
            });

            var command = string.Join("," + Environment.NewLine,
                                      merge.Select(x => indent + x));

            sb.Append(command);

            return(node);
        }
        protected override Expression VisitMemberInit(MemberInitExpression node)
        {
            var indent = BuildIndent();
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            var merge = node.Bindings.Select(expr =>
            {
                var assignment = expr as MemberAssignment;
                var x = assignment.Member;
                var y = assignment.Expression;

                var rightValue = innerTranslator.VisitAndClearBuffer(y);

                if (x.Name == rightValue.Trim('[', ']')) return "[" + x.Name + "]";

                return rightValue + " AS " + "[" + x.Name + "]";
            });

            var command = string.Join("," + Environment.NewLine,
                merge.Select(x => indent + x));

            sb.Append(command);

            return node;
        }
        protected override Expression VisitNew(NewExpression node)
        {
            // specialize for DateTime
            if (FormatIfExprIsDateTime(node)) return node;

            // promising type initializer(goto:VisitMemberInit)
            if (!node.Type.IsAnonymousType()) return node;

            var indent = BuildIndent();
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            var merge = node.Members.Zip(node.Arguments, (x, y) =>
            {
                var rightValue = innerTranslator.VisitAndClearBuffer(y);

                if (x.Name == rightValue.Trim('[', ']')) return "[" + x.Name + "]";

                return rightValue + " AS " + "[" + x.Name + "]";
            });

            var command = string.Join("," + Environment.NewLine,
                merge.Select(x => indent + x));

            sb.Append(command);

            return node;
        }
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            // case when ... then ... ... else .. end
            if (node.IfFalse is ConditionalExpression)
            {
                /*
                 * CASE
                   __WHEN cond THEN value
                 * __WHEN cond THEN value
                 * __ELSE value
                 * END
                 */
                var whenIndent = new string(' ', indentSize * (depth + 1));

                sb.Append("CASE");
                sb.AppendLine();

                // WHEN
                Expression right = node;
                while (right is ConditionalExpression)
                {
                    var rightNode = right as ConditionalExpression;

                    sb.Append(whenIndent);
                    sb.Append("WHEN ");
                    {
                        sb.Append(innerTranslator.VisitAndClearBuffer(rightNode.Test));
                    }

                    sb.Append(" THEN ");
                    {
                        sb.Append(innerTranslator.VisitAndClearBuffer(rightNode.IfTrue));
                    }

                    right = rightNode.IfFalse;
                    sb.AppendLine();
                }

                sb.Append(whenIndent);
                sb.Append("ELSE ");
                sb.Append(innerTranslator.VisitAndClearBuffer(right));

                // END
                sb.AppendLine();
                sb.Append(BuildIndent());
                sb.Append("END");
            }
            else
            {
                sb.Append("IF(");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.Test));
                }
                sb.Append(", ");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.IfTrue));
                }
                sb.Append(", ");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.IfFalse));
                }
                sb.Append(")");
            }
            return node;
        }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();

                var timestamp = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var interval = innerTranslator.VisitAndClearBuffer(node.Arguments[1]);
                var intervalUnit = ((IntervalUnit)(node.Arguments[2] as ConstantExpression).Value).ToString().ToUpper();

                return string.Format("DATE_ADD({0}, {1}, '{2}')", timestamp, interval, intervalUnit);
            }
Exemple #23
0
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);

            // case when ... then ... ... else .. end
            if (node.IfFalse is ConditionalExpression)
            {
                /*
                 * CASE
                 * __WHEN cond THEN value
                 * __WHEN cond THEN value
                 * __ELSE value
                 * END
                 */
                var whenIndent = new string(' ', indentSize *(depth + 1));

                sb.Append("CASE");
                sb.AppendLine();

                // WHEN
                Expression right = node;
                while (right is ConditionalExpression)
                {
                    var rightNode = right as ConditionalExpression;

                    sb.Append(whenIndent);
                    sb.Append("WHEN ");
                    {
                        sb.Append(innerTranslator.VisitAndClearBuffer(rightNode.Test));
                    }

                    sb.Append(" THEN ");
                    {
                        sb.Append(innerTranslator.VisitAndClearBuffer(rightNode.IfTrue));
                    }

                    right = rightNode.IfFalse;
                    sb.AppendLine();
                }

                sb.Append(whenIndent);
                sb.Append("ELSE ");
                sb.Append(innerTranslator.VisitAndClearBuffer(right));

                // END
                sb.AppendLine();
                sb.Append(BuildIndent());
                sb.Append("END");
            }
            else
            {
                sb.Append("IF(");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.Test));
                }
                sb.Append(", ");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.IfTrue));
                }
                sb.Append(", ");
                {
                    sb.Append(innerTranslator.VisitAndClearBuffer(node.IfFalse));
                }
                sb.Append(")");
            }
            return(node);
        }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            // special case, String.Contains
            if (node.Method == StringContains)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr1 = innerTranslator.VisitAndClearBuffer(node.Object);
                var expr2 = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                sb.Append(expr1 + " CONTAINS " + expr2);
                return node;
            }

            // window function
            if (node.Method.GetCustomAttributes<WindowFunctionAlertAttribute>().Any())
            {
                throw new InvalidOperationException("WindowFunction must call .Value property");
            }

            var attr = node.Method.GetCustomAttributes<FunctionNameAttribute>().FirstOrDefault();
            if (attr == null) throw new InvalidOperationException("Not support method:" + node.Method.DeclaringType.Name + "." + node.Method.Name + " Method can only call BigQuery.Linq.Functions.*");

            if (attr.SpecifiedFormatterType != null)
            {
                var formatter = Activator.CreateInstance(attr.SpecifiedFormatterType, true) as ISpecifiedFormatter;
                sb.Append(formatter.Format(depth, indentSize, attr.Name, node));
            }
            else
            {
                sb.Append(attr.Name + "(");

                var innerTranslator = new BigQueryTranslateVisitor();
                var args = string.Join(", ", node.Arguments.Select(x => innerTranslator.VisitAndClearBuffer(x)));

                sb.Append(args);

                sb.Append(")");
            }

            return node;
        }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();

                var arg = node.Arguments[0] as NewArrayExpression;
                var expr = string.Join(", ", arg.Expressions.Select(x => innerTranslator.VisitAndClearBuffer(x)));
                return "CONCAT(" + expr + ")";
            }
            public string Format(int depth, int indentSize, string fuctionName, MethodCallExpression node)
            {
                var innerTranslator = new BigQueryTranslateVisitor();
                var expr = innerTranslator.VisitAndClearBuffer(node.Arguments[0]);
                var str = innerTranslator.VisitAndClearBuffer(node.Arguments[1]);

                return expr + " CONTAINS " + str;
            }