Пример #1
0
        public override CosmosElement Visit(
            SqlBetweenScalarExpression scalarExpression,
            CosmosElement document)
        {
            // expression <not> BETWEEN left AND right === <not>(expression >= left && expression <= right);
            SqlBinaryScalarExpression expressionGTELeft = SqlBinaryScalarExpression.Create(
                SqlBinaryScalarOperatorKind.GreaterThanOrEqual,
                scalarExpression.Expression,
                scalarExpression.StartInclusive);

            SqlBinaryScalarExpression expressionLTERight = SqlBinaryScalarExpression.Create(
                SqlBinaryScalarOperatorKind.LessThanOrEqual,
                scalarExpression.Expression,
                scalarExpression.EndInclusive);

            SqlScalarExpression logicalBetween = SqlBinaryScalarExpression.Create(
                SqlBinaryScalarOperatorKind.And,
                expressionGTELeft,
                expressionLTERight);

            if (scalarExpression.Not)
            {
                logicalBetween = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Not, logicalBetween);
            }

            return(logicalBetween.Accept(this, document));
        }
        public void SqlUnaryScalarExpressionTest()
        {
            SqlLiteralScalarExpression five        = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5));
            SqlLiteralScalarExpression trueBoolean = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.True);

            {
                SqlUnaryScalarExpression bitwiseNot = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.BitwiseNot, five);
                AssertEvaluation(CosmosNumber64.Create(~5), bitwiseNot);
            }

            {
                SqlLiteralScalarExpression largeNumber = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(130679749712577953));
                SqlUnaryScalarExpression   bitwiseNot  = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.BitwiseNot, largeNumber);
                AssertEvaluation(CosmosNumber64.Create(-1022657953), bitwiseNot);
            }

            {
                SqlUnaryScalarExpression not = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Not, trueBoolean);
                AssertEvaluation(CosmosBoolean.Create(!true), not);
            }

            {
                SqlUnaryScalarExpression minus = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Minus, five);
                AssertEvaluation(CosmosNumber64.Create(-5), minus);
            }

            {
                SqlUnaryScalarExpression plus = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Plus, five);
                AssertEvaluation(CosmosNumber64.Create(5), plus);
            }
        }
Пример #3
0
        public override CosmosElement Visit(SqlUnaryScalarExpression scalarExpression, CosmosElement document)
        {
            CosmosElement expression = scalarExpression.Expression.Accept(this, document);

            CosmosElement result;

            switch (scalarExpression.OperatorKind)
            {
            case SqlUnaryScalarOperatorKind.BitwiseNot:
                result = PerformUnaryNumberOperation((number) => ~DoubleToInt32Bitwise(number), expression);
                break;

            case SqlUnaryScalarOperatorKind.Not:
                result = PerformUnaryBooleanOperation((boolean) => !boolean, expression);
                break;

            case SqlUnaryScalarOperatorKind.Minus:
                result = PerformUnaryNumberOperation((number) => - number, expression);
                break;

            case SqlUnaryScalarOperatorKind.Plus:
                result = PerformUnaryNumberOperation((number) => + number, expression);
                break;

            default:
                throw new ArgumentException($"Unknown {nameof(SqlUnaryScalarOperatorKind)}: {scalarExpression.OperatorKind}");
            }

            return(result);
        }
 public override void Visit(SqlUnaryScalarExpression sqlUnaryScalarExpression)
 {
     this.writer.Write("(");
     this.writer.Write(SqlObjectTextSerializer.SqlUnaryScalarOperatorKindToString(sqlUnaryScalarExpression.OperatorKind));
     this.writer.Write(" ");
     sqlUnaryScalarExpression.Expression.Accept(this);
     this.writer.Write(")");
 }
        public override int Visit(SqlUnaryScalarExpression sqlUnaryScalarExpression)
        {
            int hashCode = SqlUnaryScalarExpressionHashCode;

            hashCode = CombineHashes(hashCode, SqlObjectHasher.SqlUnaryScalarOperatorKindGetHashCode(sqlUnaryScalarExpression.OperatorKind));
            hashCode = CombineHashes(hashCode, sqlUnaryScalarExpression.Expression.Accept(this));
            return(hashCode);
        }
Пример #6
0
                public override bool Visit(SqlUnaryScalarExpression scalarExpression)
                {
                    if (this.MatchesGroupByExpression(scalarExpression))
                    {
                        return(true);
                    }

                    if (!scalarExpression.Expression.Accept(this))
                    {
                        return(false);
                    }

                    return(true);
                }
        public override bool Visit(SqlUnaryScalarExpression first, SqlObject secondAsObject)
        {
            if (!(secondAsObject is SqlUnaryScalarExpression second))
            {
                return(false);
            }

            if (!Equals(first.Expression, second.Expression))
            {
                return(false);
            }

            return(true);
        }
Пример #8
0
        public override SqlObject VisitUnaryScalarExpression([NotNull] sqlParser.UnaryScalarExpressionContext context)
        {
            Contract.Requires(context != null);
            // unary_operator scalar_expression
            Contract.Requires(context.ChildCount == 2);

            string unaryOperatorText = context.unary_operator().GetText();

            if (!CstToAstVisitor.unaryOperatorKindLookup.TryGetValue(
                    unaryOperatorText,
                    out SqlUnaryScalarOperatorKind unaryOperator))
            {
                throw new ArgumentOutOfRangeException($"Unknown unary operator: {unaryOperatorText}.");
            }

            SqlScalarExpression expression = (SqlScalarExpression)this.Visit(context.scalar_expression());

            return(SqlUnaryScalarExpression.Create(unaryOperator, expression));
        }
        public void Unary()
        {
            List <SqlParserBaselineTestInput> inputs = new List <SqlParserBaselineTestInput>();

            // Positive
            foreach (SqlUnaryScalarOperatorKind unaryOperator in (SqlUnaryScalarOperatorKind[])Enum.GetValues(typeof(SqlUnaryScalarOperatorKind)))
            {
                inputs.Add(
                    CreateInput(
                        description: unaryOperator.ToString(),
                        scalarExpression: SqlUnaryScalarExpression.Create(
                            unaryOperator,
                            SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(42))).ToString()));
            }

            // Negative
            inputs.Add(CreateInput(description: "unknown operator", scalarExpression: "$42"));

            this.ExecuteTestSuite(inputs);
        }
Пример #10
0
 public override bool Visit(SqlUnaryScalarExpression sqlUnaryScalarExpression)
 {
     return(sqlUnaryScalarExpression.Expression.Accept(this));
 }
Пример #11
0
 public abstract TOutput Visit(SqlUnaryScalarExpression sqlObject, TArg input);
Пример #12
0
 public abstract TResult Visit(SqlUnaryScalarExpression sqlObject);
 public override SqlObject Visit(SqlUnaryScalarExpression sqlUnaryScalarExpression)
 {
     return(SqlUnaryScalarExpression.Create(
                sqlUnaryScalarExpression.OperatorKind,
                sqlUnaryScalarExpression.Expression.Accept(this) as SqlScalarExpression));
 }
Пример #14
0
 public override void Visit(SqlUnaryScalarExpression codeObject)
 {
 }
 public abstract void Visit(SqlUnaryScalarExpression scalarExpression);
Пример #16
0
 public abstract TResult Visit(SqlUnaryScalarExpression scalarExpression);
Пример #17
0
        public static SqlScalarExpression Substitute(SqlScalarExpression replacement, SqlIdentifier toReplace, SqlScalarExpression into)
        {
            if (into == null)
            {
                return(null);
            }

            if (replacement == null)
            {
                throw new ArgumentNullException("replacement");
            }

            switch (into.Kind)
            {
            case SqlObjectKind.ArrayCreateScalarExpression:
            {
                SqlArrayCreateScalarExpression arrayExp = into as SqlArrayCreateScalarExpression;
                if (arrayExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlArrayCreateScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression[] items = new SqlScalarExpression[arrayExp.Items.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = arrayExp.Items[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlArrayCreateScalarExpression.Create(items));
            }

            case SqlObjectKind.BinaryScalarExpression:
            {
                SqlBinaryScalarExpression binaryExp = into as SqlBinaryScalarExpression;
                if (binaryExp == null)
                {
                    throw new DocumentQueryException("Expected a BinaryScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression replleft  = Substitute(replacement, toReplace, binaryExp.LeftExpression);
                SqlScalarExpression replright = Substitute(replacement, toReplace, binaryExp.RightExpression);
                return(SqlBinaryScalarExpression.Create(binaryExp.OperatorKind, replleft, replright));
            }

            case SqlObjectKind.UnaryScalarExpression:
            {
                SqlUnaryScalarExpression unaryExp = into as SqlUnaryScalarExpression;
                if (unaryExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlUnaryScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression repl = Substitute(replacement, toReplace, unaryExp.Expression);
                return(SqlUnaryScalarExpression.Create(unaryExp.OperatorKind, repl));
            }

            case SqlObjectKind.LiteralScalarExpression:
            {
                return(into);
            }

            case SqlObjectKind.FunctionCallScalarExpression:
            {
                SqlFunctionCallScalarExpression funcExp = into as SqlFunctionCallScalarExpression;
                if (funcExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlFunctionCallScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression[] items = new SqlScalarExpression[funcExp.Arguments.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = funcExp.Arguments[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlFunctionCallScalarExpression.Create(funcExp.Name, funcExp.IsUdf, items));
            }

            case SqlObjectKind.ObjectCreateScalarExpression:
            {
                SqlObjectCreateScalarExpression objExp = into as SqlObjectCreateScalarExpression;
                if (objExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlObjectCreateScalarExpression, got a " + into.GetType());
                }

                return(SqlObjectCreateScalarExpression.Create(
                           objExp
                           .Properties
                           .Select(prop => SqlObjectProperty.Create(prop.Name, Substitute(replacement, toReplace, prop.Expression)))));
            }

            case SqlObjectKind.MemberIndexerScalarExpression:
            {
                SqlMemberIndexerScalarExpression memberExp = into as SqlMemberIndexerScalarExpression;
                if (memberExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlMemberIndexerScalarExpression, got a " + into.GetType());
                }

                SqlScalarExpression replMember = Substitute(replacement, toReplace, memberExp.MemberExpression);
                SqlScalarExpression replIndex  = Substitute(replacement, toReplace, memberExp.IndexExpression);
                return(SqlMemberIndexerScalarExpression.Create(replMember, replIndex));
            }

            case SqlObjectKind.PropertyRefScalarExpression:
            {
                // This is the leaf of the recursion
                SqlPropertyRefScalarExpression propExp = into as SqlPropertyRefScalarExpression;
                if (propExp == null)
                {
                    throw new DocumentQueryException("Expected a SqlPropertyRefScalarExpression, got a " + into.GetType());
                }

                if (propExp.MemberExpression == null)
                {
                    if (propExp.PropertyIdentifier.Value == toReplace.Value)
                    {
                        return(replacement);
                    }
                    else
                    {
                        return(propExp);
                    }
                }
                else
                {
                    SqlScalarExpression replMember = Substitute(replacement, toReplace, propExp.MemberExpression);
                    return(SqlPropertyRefScalarExpression.Create(replMember, propExp.PropertyIdentifier));
                }
            }

            case SqlObjectKind.ConditionalScalarExpression:
            {
                SqlConditionalScalarExpression conditionalExpression = (SqlConditionalScalarExpression)into;
                if (conditionalExpression == null)
                {
                    throw new ArgumentException();
                }

                SqlScalarExpression condition = Substitute(replacement, toReplace, conditionalExpression.ConditionExpression);
                SqlScalarExpression first     = Substitute(replacement, toReplace, conditionalExpression.FirstExpression);
                SqlScalarExpression second    = Substitute(replacement, toReplace, conditionalExpression.SecondExpression);

                return(SqlConditionalScalarExpression.Create(condition, first, second));
            }

            case SqlObjectKind.InScalarExpression:
            {
                SqlInScalarExpression inExpression = (SqlInScalarExpression)into;
                if (inExpression == null)
                {
                    throw new ArgumentException();
                }

                SqlScalarExpression expression = Substitute(replacement, toReplace, inExpression.Expression);

                SqlScalarExpression[] items = new SqlScalarExpression[inExpression.Items.Count];
                for (int i = 0; i < items.Length; i++)
                {
                    items[i] = Substitute(replacement, toReplace, inExpression.Items[i]);
                }

                return(SqlInScalarExpression.Create(expression, inExpression.Not, items));
            }

            default:
                throw new ArgumentOutOfRangeException("Unexpected Sql Scalar expression kind " + into.Kind);
            }
        }
Пример #18
0
 public abstract void Visit(SqlUnaryScalarExpression sqlObject);
        public static SqlScalarExpression Substitute(SqlScalarExpression replacement, SqlIdentifier toReplace, SqlScalarExpression into)
        {
            if (into == null)
            {
                return(null);
            }

            if (replacement == null)
            {
                throw new ArgumentNullException("replacement");
            }

            switch (into)
            {
            case SqlArrayCreateScalarExpression arrayExp:
            {
                SqlScalarExpression[] items = new SqlScalarExpression[arrayExp.Items.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = arrayExp.Items[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlArrayCreateScalarExpression.Create(items));
            }

            case SqlBinaryScalarExpression binaryExp:
            {
                SqlScalarExpression replleft  = Substitute(replacement, toReplace, binaryExp.LeftExpression);
                SqlScalarExpression replright = Substitute(replacement, toReplace, binaryExp.RightExpression);
                return(SqlBinaryScalarExpression.Create(binaryExp.OperatorKind, replleft, replright));
            }

            case SqlUnaryScalarExpression unaryExp:
            {
                SqlScalarExpression repl = Substitute(replacement, toReplace, unaryExp.Expression);
                return(SqlUnaryScalarExpression.Create(unaryExp.OperatorKind, repl));
            }

            case SqlLiteralScalarExpression literalScalarExpression:
            {
                return(into);
            }

            case SqlFunctionCallScalarExpression funcExp:
            {
                SqlScalarExpression[] items = new SqlScalarExpression[funcExp.Arguments.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    SqlScalarExpression item     = funcExp.Arguments[i];
                    SqlScalarExpression replitem = Substitute(replacement, toReplace, item);
                    items[i] = replitem;
                }

                return(SqlFunctionCallScalarExpression.Create(funcExp.Name, funcExp.IsUdf, items));
            }

            case SqlObjectCreateScalarExpression objExp:
            {
                return(SqlObjectCreateScalarExpression.Create(
                           objExp
                           .Properties
                           .Select(prop => SqlObjectProperty.Create(prop.Name, Substitute(replacement, toReplace, prop.Value)))
                           .ToImmutableArray()));
            }

            case SqlMemberIndexerScalarExpression memberExp:
            {
                SqlScalarExpression replMember = Substitute(replacement, toReplace, memberExp.Member);
                SqlScalarExpression replIndex  = Substitute(replacement, toReplace, memberExp.Indexer);
                return(SqlMemberIndexerScalarExpression.Create(replMember, replIndex));
            }

            case SqlPropertyRefScalarExpression propExp:
            {
                // This is the leaf of the recursion
                if (propExp.Member == null)
                {
                    if (propExp.Identifier.Value == toReplace.Value)
                    {
                        return(replacement);
                    }
                    else
                    {
                        return(propExp);
                    }
                }
                else
                {
                    SqlScalarExpression replMember = Substitute(replacement, toReplace, propExp.Member);
                    return(SqlPropertyRefScalarExpression.Create(replMember, propExp.Identifier));
                }
            }

            case SqlConditionalScalarExpression conditionalExpression:
            {
                SqlScalarExpression condition = Substitute(replacement, toReplace, conditionalExpression.Condition);
                SqlScalarExpression first     = Substitute(replacement, toReplace, conditionalExpression.Consequent);
                SqlScalarExpression second    = Substitute(replacement, toReplace, conditionalExpression.Alternative);

                return(SqlConditionalScalarExpression.Create(condition, first, second));
            }

            case SqlInScalarExpression inExpression:
            {
                SqlScalarExpression expression = Substitute(replacement, toReplace, inExpression.Needle);

                SqlScalarExpression[] items = new SqlScalarExpression[inExpression.Haystack.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    items[i] = Substitute(replacement, toReplace, inExpression.Haystack[i]);
                }

                return(SqlInScalarExpression.Create(expression, inExpression.Not, items));
            }

            default:
                throw new ArgumentOutOfRangeException("Unexpected Sql Scalar expression kind " + into.GetType());
            }
        }