Example #1
0
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     EnsureSubqueryNotNestedInAggregate();
     base.VisitExistsSubselect(expression);
     EnsureQueryHasNoOrderByUnlessTopSpecified(expression.Query);
     return(expression);
 }
Example #2
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			ExistsSubselect result = new ExistsSubselect();
			result.Negated = _negated;
			result.Query = (QueryNode)Query.Clone(alreadyClonedElements);
			return result;
		}
Example #3
0
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            ExistsSubselect result = new ExistsSubselect();

            result.Negated = _negated;
            result.Query   = (QueryNode)Query.Clone(alreadyClonedElements);
            return(result);
        }
Example #4
0
        public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
        {
            _writer.Write("EXISTS (");
            Visit(expression.Query);
            _writer.Write(")");

            return(expression);
        }
Example #5
0
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     _subqueryContextCounter++;
     try
     {
         return(base.VisitExistsSubselect(expression));
     }
     finally
     {
         _subqueryContextCounter--;
     }
 }
		public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
		{
			_subqueryContextCounter++;
			try
			{
				return base.VisitExistsSubselect(expression);
			}
			finally
			{
				_subqueryContextCounter--;
			}
		}
Example #7
0
        public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
        {
            _xmlWriter.WriteStartElement("existsSubselect");
            _xmlWriter.WriteAttributeString("negated", XmlConvert.ToString(expression.Negated));
            WriteTypeAttribute(expression.ExpressionType);

            WriteAstNode("query", expression.Query);

            _xmlWriter.WriteEndElement();

            return(expression);
        }
Example #8
0
        public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
        {
            AlgebraNode       input           = GetAndResetLastNode();
            ResultAlgebraNode algebrizedQuery = Algebrizer.Convert(expression.Query);

            if (!expression.Negated && AstUtil.WillProduceAtLeastOneRow(algebrizedQuery))
            {
                if (input == null)
                {
                    SetLastAlgebraNode(CreateConstantScan());
                }
                else
                {
                    SetLastAlgebraNode(input);
                }

                return(LiteralExpression.FromBoolean(true));
            }


            if (!expression.Negated && !ProbingEnabled && input == null)
            {
                SetLastAlgebraNode(algebrizedQuery);
                return(LiteralExpression.FromBoolean(true));
            }
            else
            {
                if (input == null)
                {
                    input = CreateConstantScan();
                }

                RowBufferEntry probeColumn = CreateProbeColumn();

                JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
                joinAlgebraNode.PassthruPredicate = CurrentPassthruPredicate;
                joinAlgebraNode.ProbeBufferEntry  = probeColumn;
                joinAlgebraNode.Left  = input;
                joinAlgebraNode.Right = algebrizedQuery;
                joinAlgebraNode.Op    = expression.Negated ? JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin : JoinAlgebraNode.JoinOperator.LeftSemiJoin;
                SetLastAlgebraNode(joinAlgebraNode);

                return(CreateProbeColumnRef(probeColumn));
            }
        }
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     return(expression);
 }
Example #10
0
        public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
        {
            AlgebraNode input = GetAndResetLastNode();
            ResultAlgebraNode algebrizedQuery = Algebrizer.Convert(expression.Query);

            if (!expression.Negated && AstUtil.WillProduceAtLeastOneRow(algebrizedQuery))
            {
                if (input == null)
                    SetLastAlgebraNode(CreateConstantScan());
                else
                    SetLastAlgebraNode(input);

                return LiteralExpression.FromBoolean(true);
            }

            if (!expression.Negated && !ProbingEnabled && input == null)
            {
                SetLastAlgebraNode(algebrizedQuery);
                return LiteralExpression.FromBoolean(true);
            }
            else
            {
                if (input == null)
                    input = CreateConstantScan();

                RowBufferEntry probeColumn = CreateProbeColumn();

                JoinAlgebraNode joinAlgebraNode = new JoinAlgebraNode();
                joinAlgebraNode.PassthruPredicate = CurrentPassthruPredicate;
                joinAlgebraNode.ProbeBufferEntry = probeColumn;
                joinAlgebraNode.Left = input;
                joinAlgebraNode.Right = algebrizedQuery;
                joinAlgebraNode.Op = expression.Negated ? JoinAlgebraNode.JoinOperator.LeftAntiSemiJoin : JoinAlgebraNode.JoinOperator.LeftSemiJoin;
                SetLastAlgebraNode(joinAlgebraNode);

                return CreateProbeColumnRef(probeColumn);
            }
        }
Example #11
0
		private static bool VisitExistsSubselect(ExistsSubselect node1, ExistsSubselect node2)
		{
			return node2 != null &&
				   node1.Negated == node2.Negated &&
				   Visit(node1.Query, node2.Query);
		}
Example #12
0
 private static bool VisitExistsSubselect(ExistsSubselect node1, ExistsSubselect node2)
 {
     return(node2 != null &&
            node1.Negated == node2.Negated &&
            Visit(node1.Query, node2.Query));
 }
Example #13
0
		public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
		{
			_writer.Write("EXISTS (");
			Visit(expression.Query);
			_writer.Write(")");

			return expression;
		}
Example #14
0
        private ExpressionNode ParsePrimaryExpression()
        {
            switch (_token.Id)
            {
                case TokenId.NULL:
                    NextToken();
                    return LiteralExpression.FromNull();

                case TokenId.TRUE:
                case TokenId.FALSE:
                    return ParseBooleanLiteral();

                case TokenId.Date:
                    return ParseDateLiteral();

                case TokenId.Number:
                    return ParseNumberLiteral();

                case TokenId.String:
                    return LiteralExpression.FromString(ParseString());

                case TokenId.EXISTS:
                {
                    ExistsSubselect result = new ExistsSubselect();
                    NextToken();
                    Match(TokenId.LeftParentheses);
                    result.Query = ParseQuery();
                    Match(TokenId.RightParentheses);

                    return result;
                }

                case TokenId.ParameterMarker:
                {
                    _rangeRecorder.Begin();
                    NextToken();
                    Identifier name = ParseIdentifier();
                    SourceRange nameSourceRange = _rangeRecorder.End();

                    ParameterExpression result = new ParameterExpression();
                    result.Name = name;
                    result.NameSourceRange = nameSourceRange;
                    return result;
                }

                case TokenId.CAST:
                {
                    NextToken();
                    CastExpression castExpression = new CastExpression();
                    Match(TokenId.LeftParentheses);
                    castExpression.Expression = ParseExpression();
                    Match(TokenId.AS);
                    castExpression.TypeReference = ParseTypeReference();
                    Match(TokenId.RightParentheses);

                    return castExpression;
                }

                case TokenId.CASE:
                {
                    NextToken();

                    CaseExpression caseExpression = new CaseExpression();

                    if (_token.Id != TokenId.WHEN && _token.Id != TokenId.ELSE && _token.Id != TokenId.END)
                        caseExpression.InputExpression = ParseExpression();

                    List<ExpressionNode> whenExpressionList = new List<ExpressionNode>();
                    List<ExpressionNode> expressionList = new List<ExpressionNode>();

                    if (_token.Id != TokenId.WHEN)
                    {
                        Match(TokenId.WHEN);
                    }
                    else
                    {
                        while (_token.Id == TokenId.WHEN)
                        {
                            NextToken();

                            whenExpressionList.Add(ParseExpression());
                            Match(TokenId.THEN);
                            expressionList.Add(ParseExpression());
                        }
                    }

                    caseExpression.WhenExpressions = whenExpressionList.ToArray();
                    caseExpression.ThenExpressions = expressionList.ToArray();

                    if (_token.Id == TokenId.ELSE)
                    {
                        NextToken();
                        caseExpression.ElseExpression = ParseExpression();
                    }

                    Match(TokenId.END);

                    return caseExpression;
                }

                case TokenId.COALESCE:
                {
                    NextToken();
                    CoalesceExpression coalesceExpression = new CoalesceExpression();
                    coalesceExpression.Expressions = ParseExpressionList();
                    return coalesceExpression;
                }

                case TokenId.NULLIF:
                {
                    NextToken();
                    NullIfExpression nullIfExpression = new NullIfExpression();
                    Match(TokenId.LeftParentheses);
                    nullIfExpression.LeftExpression = ParseExpression();
                    Match(TokenId.Comma);
                    nullIfExpression.RightExpression = ParseExpression();
                    Match(TokenId.RightParentheses);
                    return nullIfExpression;
                }

                case TokenId.Identifier:
                {
                    _rangeRecorder.Begin();
                    Identifier name = ParseIdentifier();
                    SourceRange nameSourceRange = _rangeRecorder.End();

                    if (_token.Id != TokenId.LeftParentheses)
                    {
                        NameExpression result = new NameExpression();
                        result.Name = name;
                        result.NameSourceRange = nameSourceRange;
                        return result;
                    }
                    else
                    {
                        bool hasAsteriskModifier;
                        ExpressionNode[] args;

                        if (_lookahead.Id != TokenId.Multiply)
                        {
                            hasAsteriskModifier = false;
                            args = ParseExpressionList();
                        }
                        else
                        {
                            NextToken();
                            NextToken();
                            Match(TokenId.RightParentheses);

                            hasAsteriskModifier = true;
                            args = new ExpressionNode[0];
                        }

                        FunctionInvocationExpression result = new FunctionInvocationExpression();
                        result.Name = name;
                        result.NameSourceRange = nameSourceRange;
                        result.Arguments = args;
                        result.HasAsteriskModifier = hasAsteriskModifier;
                        return result;
                    }
                }

                case TokenId.LeftParentheses:
                {
                    NextToken();

                    ExpressionNode expr;

                    if (_token.Id != TokenId.SELECT)
                    {
                        expr = ParseExpression();
                    }
                    else
                    {
                        SingleRowSubselect singleRowSubselect = new SingleRowSubselect();
                        singleRowSubselect.Query = ParseQuery();
                        expr = singleRowSubselect;
                    }

                    Match(TokenId.RightParentheses);
                    return expr;
                }

                default:
                {
                    _errorReporter.SimpleExpressionExpected(_token.Range, _token.Text);
                    return LiteralExpression.FromNull();
                }
            }
        }
Example #15
0
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     _containsExistenceSubselects = true;
     VisitQueryExpression(expression.Query);
     return(expression);
 }
Example #16
0
 public virtual ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     expression.Query = VisitQuery(expression.Query);
     return(expression);
 }
Example #17
0
		public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
		{
			_xmlWriter.WriteStartElement("existsSubselect");
			_xmlWriter.WriteAttributeString("negated", XmlConvert.ToString(expression.Negated));
			WriteTypeAttribute(expression.ExpressionType);

			WriteAstNode("query", expression.Query);

			_xmlWriter.WriteEndElement();

			return expression;
		}
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     return expression;
 }
Example #19
0
        private ExpressionNode ParsePrimaryExpression()
        {
            switch (_token.Id)
            {
            case TokenId.NULL:
                NextToken();
                return(LiteralExpression.FromNull());

            case TokenId.TRUE:
            case TokenId.FALSE:
                return(ParseBooleanLiteral());

            case TokenId.Date:
                return(ParseDateLiteral());

            case TokenId.Number:
                return(ParseNumberLiteral());

            case TokenId.String:
                return(LiteralExpression.FromString(ParseString()));

            case TokenId.EXISTS:
            {
                ExistsSubselect result = new ExistsSubselect();
                NextToken();
                Match(TokenId.LeftParentheses);
                result.Query = ParseQuery();
                Match(TokenId.RightParentheses);

                return(result);
            }

            case TokenId.ParameterMarker:
            {
                _rangeRecorder.Begin();
                NextToken();
                Identifier  name            = ParseIdentifier();
                SourceRange nameSourceRange = _rangeRecorder.End();

                ParameterExpression result = new ParameterExpression();
                result.Name            = name;
                result.NameSourceRange = nameSourceRange;
                return(result);
            }

            case TokenId.CAST:
            {
                NextToken();
                CastExpression castExpression = new CastExpression();
                Match(TokenId.LeftParentheses);
                castExpression.Expression = ParseExpression();
                Match(TokenId.AS);
                castExpression.TypeReference = ParseTypeReference();
                Match(TokenId.RightParentheses);

                return(castExpression);
            }

            case TokenId.CASE:
            {
                NextToken();

                CaseExpression caseExpression = new CaseExpression();

                if (_token.Id != TokenId.WHEN && _token.Id != TokenId.ELSE && _token.Id != TokenId.END)
                {
                    caseExpression.InputExpression = ParseExpression();
                }

                List <ExpressionNode> whenExpressionList = new List <ExpressionNode>();
                List <ExpressionNode> expressionList     = new List <ExpressionNode>();

                if (_token.Id != TokenId.WHEN)
                {
                    Match(TokenId.WHEN);
                }
                else
                {
                    while (_token.Id == TokenId.WHEN)
                    {
                        NextToken();

                        whenExpressionList.Add(ParseExpression());
                        Match(TokenId.THEN);
                        expressionList.Add(ParseExpression());
                    }
                }

                caseExpression.WhenExpressions = whenExpressionList.ToArray();
                caseExpression.ThenExpressions = expressionList.ToArray();

                if (_token.Id == TokenId.ELSE)
                {
                    NextToken();
                    caseExpression.ElseExpression = ParseExpression();
                }

                Match(TokenId.END);

                return(caseExpression);
            }

            case TokenId.COALESCE:
            {
                NextToken();
                CoalesceExpression coalesceExpression = new CoalesceExpression();
                coalesceExpression.Expressions = ParseExpressionList();
                return(coalesceExpression);
            }

            case TokenId.NULLIF:
            {
                NextToken();
                NullIfExpression nullIfExpression = new NullIfExpression();
                Match(TokenId.LeftParentheses);
                nullIfExpression.LeftExpression = ParseExpression();
                Match(TokenId.Comma);
                nullIfExpression.RightExpression = ParseExpression();
                Match(TokenId.RightParentheses);
                return(nullIfExpression);
            }

            case TokenId.Identifier:
            {
                _rangeRecorder.Begin();
                Identifier  name            = ParseIdentifier();
                SourceRange nameSourceRange = _rangeRecorder.End();

                if (_token.Id != TokenId.LeftParentheses)
                {
                    NameExpression result = new NameExpression();
                    result.Name            = name;
                    result.NameSourceRange = nameSourceRange;
                    return(result);
                }
                else
                {
                    bool             hasAsteriskModifier;
                    ExpressionNode[] args;

                    if (_lookahead.Id != TokenId.Multiply)
                    {
                        hasAsteriskModifier = false;
                        args = ParseExpressionList();
                    }
                    else
                    {
                        NextToken();
                        NextToken();
                        Match(TokenId.RightParentheses);

                        hasAsteriskModifier = true;
                        args = new ExpressionNode[0];
                    }

                    FunctionInvocationExpression result = new FunctionInvocationExpression();
                    result.Name                = name;
                    result.NameSourceRange     = nameSourceRange;
                    result.Arguments           = args;
                    result.HasAsteriskModifier = hasAsteriskModifier;
                    return(result);
                }
            }

            case TokenId.LeftParentheses:
            {
                NextToken();

                ExpressionNode expr;

                if (_token.Id != TokenId.SELECT)
                {
                    expr = ParseExpression();
                }
                else
                {
                    SingleRowSubselect singleRowSubselect = new SingleRowSubselect();
                    singleRowSubselect.Query = ParseQuery();
                    expr = singleRowSubselect;
                }

                Match(TokenId.RightParentheses);
                return(expr);
            }

            default:
            {
                _errorReporter.SimpleExpressionExpected(_token.Range, _token.Text);
                return(LiteralExpression.FromNull());
            }
            }
        }
Example #20
0
        public override ExpressionNode VisitUnaryExpression(UnaryExpression expression)
        {
            // First visit arguments
            base.VisitUnaryExpression(expression);

            if (expression.Op == UnaryOperator.LogicalNot)
            {
                // Replace "NOT NOT expr" by "expr"

                UnaryExpression unOp = expression.Operand as UnaryExpression;
                if (unOp != null)
                {
                    if (unOp.Op == UnaryOperator.LogicalNot)
                    {
                        return(VisitExpression(unOp.Operand));
                    }
                }

                // Replace "NOT expr IS NULL" and "NOT expr IS NOT NULL" by
                //         "expr IS NOT NULL" and "expr IS NULL" resp.

                IsNullExpression isNull = expression.Operand as IsNullExpression;
                if (isNull != null)
                {
                    isNull.Negated = !isNull.Negated;
                    return(VisitExpression(isNull));
                }

                // Apply negation on EXISTS

                ExistsSubselect existsSubselect = expression.Operand as ExistsSubselect;
                if (existsSubselect != null)
                {
                    existsSubselect.Negated = !existsSubselect.Negated;
                    return(existsSubselect);
                }

                // Apply negation on ALL/ANY subquery

                AllAnySubselect allAnySubselect = expression.Operand as AllAnySubselect;
                if (allAnySubselect != null)
                {
                    allAnySubselect.Op   = AstUtil.NegateBinaryOp(allAnySubselect.Op);
                    allAnySubselect.Type = (allAnySubselect.Type == AllAnySubselect.AllAnyType.All) ? AllAnySubselect.AllAnyType.Any : AllAnySubselect.AllAnyType.All;
                    return(allAnySubselect);
                }

                // Apply De Morgan's law

                BinaryExpression binOp = expression.Operand as BinaryExpression;

                if (binOp != null)
                {
                    BinaryOperator negatedOp = AstUtil.NegateBinaryOp(binOp.Op);

                    if (negatedOp != null)
                    {
                        ExpressionNode newLeft;
                        ExpressionNode newRight;

                        if (binOp.Op == BinaryOperator.LogicalAnd || binOp.Op == BinaryOperator.LogicalOr)
                        {
                            newLeft  = new UnaryExpression(expression.Op, binOp.Left);
                            newRight = new UnaryExpression(expression.Op, binOp.Right);
                        }
                        else
                        {
                            newLeft  = binOp.Left;
                            newRight = binOp.Right;
                        }

                        binOp.Op    = negatedOp;
                        binOp.Left  = newLeft;
                        binOp.Right = newRight;
                        return(VisitExpression(binOp));
                    }
                }
            }

            return(expression);
        }
Example #21
0
 public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
 {
     EnsureSubqueryNotNestedInAggregate();
     base.VisitExistsSubselect(expression);
     EnsureQueryHasNoOrderByUnlessTopSpecified(expression.Query);
     return expression;
 }
Example #22
0
		public virtual ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
		{
			expression.Query = VisitQuery(expression.Query);
			return expression;
		}
Example #23
0
		public override ExpressionNode VisitExistsSubselect(ExistsSubselect expression)
		{
            _containsExistenceSubselects = true;
            VisitQueryExpression(expression.Query);
			return expression;
		}