示例#1
0
        // TODO: not supported in version 8? see unit test
        public static object Evaluate(NullIfExpression nullIf, IArgument arg, Scope scope)
        {
            var n1 = Evaluate <object>(nullIf.FirstExpression, arg, scope);
            var n2 = Evaluate <object>(nullIf.SecondExpression, arg, scope);

            return(Equality.Equal(n1, n2) ? null : n1);
        }
示例#2
0
        private static WhenClause MatchingClause(CaseExpression expr, IArgument arg, Scope scope)
        {
            switch (expr)
            {
            case SimpleCaseExpression simple:
                var input = Evaluate(simple.InputExpression, arg, scope);
                return(simple.WhenClauses
                       .FirstOrDefault(x => Equality.Equal(input, Evaluate(x.WhenExpression, arg, scope))));

            case SearchedCaseExpression searched:
                return(searched.WhenClauses
                       .FirstOrDefault(x => Evaluate(x.WhenExpression, arg, scope)));

            default:
                throw FeatureNotSupportedException.Subtype(expr);
            }
        }
        public static EngineResult Evaluate(BinaryQueryExpressionType type, bool all, Table left, Table right, Env env)
        {
            // TODO: does offset/fetch/top happen before or after?

            if (left.Columns.Count != right.Columns.Count)
            {
                throw new Exception("tables must have the same number of columns");
            }

            foreach (var i in Enumerable.Range(0, left.Columns.Count))
            {
                var a = left.Columns[i];
                var b = right.Columns[i];

                if (a.Name.Length > 0 && b.Name.Length > 0 && !a.Name.Last().Similar(b.Name.Last()))
                {
                    throw new Exception("columns must have the same names");
                }

                // TODO: identify lowest common type
                if (a.Type != b.Type)
                {
                    throw new Exception("types must match");
                }
            }

            var result = new Table {
                Columns = left.Columns
            };

            bool Contains(Table t, Row r) =>
            t.Rows.Any(s =>
                       Enumerable.Range(0, r.Columns.Count).All(i =>
                                                                Equality.Equal(r.Values[i], s.Values[i])));

            switch (type)
            {
            case BinaryQueryExpressionType.Except:
                foreach (var x in left.Rows)
                {
                    if (!Contains(right, x) && (all || !Contains(result, x)))
                    {
                        result.AddCopy(x, env);
                    }
                }

                break;

            case BinaryQueryExpressionType.Intersect:
                foreach (var x in left.Rows)
                {
                    if (Contains(right, x) && (all || !Contains(result, x)))
                    {
                        result.AddCopy(x, env);
                    }
                }

                if (all)
                {
                    foreach (var x in right.Rows)
                    {
                        if (Contains(left, x))
                        {
                            result.AddCopy(x, env);
                        }
                    }
                }

                break;

            case BinaryQueryExpressionType.Union:
                foreach (var x in left.Rows)
                {
                    if (all || !Contains(result, x))
                    {
                        result.AddCopy(x, env);
                    }
                }

                foreach (var x in right.Rows)
                {
                    if (all || !Contains(result, x))
                    {
                        result.AddCopy(x, env);
                    }
                }

                break;

            default:
                throw FeatureNotSupportedException.Value(type);
            }

            return(new EngineResult(result));
        }