Example #1
0
            public void Add(AggregateExpression aggregateExpression)
            {
                AggregateExpression matchingAggregateExpression = null;

                foreach (AggregateExpression existingAggregateExpression in _aggregateExpressions)
                {
                    if (existingAggregateExpression.IsStructuralEqualTo(aggregateExpression))
                    {
                        matchingAggregateExpression = existingAggregateExpression;
                        break;
                    }
                }

                if (matchingAggregateExpression != null)
                {
                    aggregateExpression.ValueDefinition = matchingAggregateExpression.ValueDefinition;
                }
                else
                {
                    RowBufferEntry rowBufferEntry = new RowBufferEntry(aggregateExpression.Aggregator.ReturnType);

                    AggregatedValueDefinition aggregatedValueDefinition = new AggregatedValueDefinition();
                    aggregatedValueDefinition.Target     = rowBufferEntry;
                    aggregatedValueDefinition.Aggregate  = aggregateExpression.Aggregate;
                    aggregatedValueDefinition.Aggregator = aggregateExpression.Aggregator;
                    aggregatedValueDefinition.Argument   = aggregateExpression.Argument;
                    aggregateExpression.ValueDefinition  = aggregatedValueDefinition;
                    _aggregateExpressions.Add(aggregateExpression);
                }
            }
Example #2
0
			public void Add(AggregateExpression aggregateExpression)
			{
				AggregateExpression matchingAggregateExpression = null;
				foreach (AggregateExpression existingAggregateExpression in _aggregateExpressions)
				{
					if (existingAggregateExpression.IsStructuralEqualTo(aggregateExpression))
					{
						matchingAggregateExpression = existingAggregateExpression;
						break;
					}
				}

				if (matchingAggregateExpression != null)
				{
					aggregateExpression.ValueDefinition = matchingAggregateExpression.ValueDefinition;
				}
				else
				{
					RowBufferEntry rowBufferEntry = new RowBufferEntry(aggregateExpression.Aggregator.ReturnType);

					AggregatedValueDefinition aggregatedValueDefinition = new AggregatedValueDefinition();
					aggregatedValueDefinition.Target = rowBufferEntry;
					aggregatedValueDefinition.Aggregate = aggregateExpression.Aggregate;
					aggregatedValueDefinition.Aggregator = aggregateExpression.Aggregator;
					aggregatedValueDefinition.Argument = aggregateExpression.Argument;
					aggregateExpression.ValueDefinition = aggregatedValueDefinition;
					_aggregateExpressions.Add(aggregateExpression);
				}
			}
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			AggregatedValueDefinition result = new AggregatedValueDefinition();
			result.Target = Target;
			result.Aggregate = _aggregate;
			result.Aggregator = _aggregator;
			result.Argument = (ExpressionNode)_argument.Clone(alreadyClonedElements);
			return result;
		}
Example #4
0
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            AggregatedValueDefinition result = new AggregatedValueDefinition();

            result.Target     = Target;
            result.Aggregate  = _aggregate;
            result.Aggregator = _aggregator;
            result.Argument   = (ExpressionNode)_argument.Clone(alreadyClonedElements);
            return(result);
        }
Example #5
0
		private RuntimeAggregateValueOutput[] GetDefinedValues(RowBufferEntry[] outputList, AggregatedValueDefinition[] definedValues, params BoundRowBufferEntrySet[] boundRowBufferEntrySets)
		{
			RuntimeAggregateValueOutput[] result = new RuntimeAggregateValueOutput[definedValues.Length];

			for (int i = 0; i < definedValues.Length; i++)
			{
				RuntimeAggregateValueOutput definedValue = new RuntimeAggregateValueOutput();
				definedValue.Aggregator = definedValues[i].Aggregator;
				definedValue.Argument = CreateRuntimeExpression(definedValues[i].Argument, boundRowBufferEntrySets);
				definedValue.TargetIndex = Array.IndexOf(outputList, definedValues[i].Target);
				result[i] = definedValue;
			}

			return result;
		}
Example #6
0
        private static ResultAlgebraNode CreateAssertedSubquery(ResultAlgebraNode inputNode)
        {
            if (AstUtil.WillProduceAtMostOneRow(inputNode))
                return inputNode;

            RowBufferEntry inputEntry = inputNode.OutputList[0];

            AggregatedValueDefinition countDefinedValue = new AggregatedValueDefinition();
            countDefinedValue.Aggregate = new CountAggregateBinding("COUNT");
            countDefinedValue.Aggregator = countDefinedValue.Aggregate.CreateAggregator(typeof(int));
            countDefinedValue.Argument = LiteralExpression.FromInt32(0);

            RowBufferEntry countDefinedValueEntry = new RowBufferEntry(countDefinedValue.Aggregator.ReturnType);
            countDefinedValue.Target = countDefinedValueEntry;

            RowBufferEntryExpression anyAggregateArgument = new RowBufferEntryExpression();
            anyAggregateArgument.RowBufferEntry = inputEntry;

            AggregatedValueDefinition anyDefinedValue = new AggregatedValueDefinition();
            anyDefinedValue.Aggregate = new FirstAggregateBinding("ANY");
            anyDefinedValue.Aggregator = anyDefinedValue.Aggregate.CreateAggregator(inputEntry.DataType);
            anyDefinedValue.Argument = anyAggregateArgument;

            RowBufferEntry anyDefinedValueEntry = new RowBufferEntry(inputEntry.DataType);
            anyDefinedValue.Target = anyDefinedValueEntry;

            AggregateAlgebraNode aggregateAlgebraNode = new AggregateAlgebraNode();
            aggregateAlgebraNode.Input = inputNode.Input;
            aggregateAlgebraNode.DefinedValues = new AggregatedValueDefinition[] { countDefinedValue, anyDefinedValue };

            // CASE WHEN SubqueryCount > 1 THEN 0 ELSE NULL END

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();
            expressionBuilder.Push(new RowBufferEntryExpression(countDefinedValueEntry));
            expressionBuilder.Push(LiteralExpression.FromInt32(1));
            expressionBuilder.PushBinary(BinaryOperator.Greater);
            ExpressionNode whenExpression = expressionBuilder.Pop();
            ExpressionNode thenExpression = LiteralExpression.FromInt32(0);

            CaseExpression caseExpression = new CaseExpression();
            caseExpression.WhenExpressions = new ExpressionNode[] { whenExpression };
            caseExpression.ThenExpressions = new ExpressionNode[] { thenExpression };

            expressionBuilder.Push(caseExpression);
            ExpressionNode predicate = expressionBuilder.Pop();

            AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();
            assertAlgebraNode.Input = aggregateAlgebraNode;
            assertAlgebraNode.Predicate = predicate;
            assertAlgebraNode.AssertionType = AssertionType.MaxOneRow;

            ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();
            resultAlgebraNode.Input = assertAlgebraNode;
            resultAlgebraNode.OutputList = new RowBufferEntry[] { anyDefinedValueEntry };
            resultAlgebraNode.ColumnNames = inputNode.ColumnNames;

            return resultAlgebraNode;
        }
Example #7
0
        private static ResultAlgebraNode CreateAssertedSubquery(ResultAlgebraNode inputNode)
        {
            if (AstUtil.WillProduceAtMostOneRow(inputNode))
            {
                return(inputNode);
            }

            RowBufferEntry inputEntry = inputNode.OutputList[0];

            AggregatedValueDefinition countDefinedValue = new AggregatedValueDefinition();

            countDefinedValue.Aggregate  = new CountAggregateBinding("COUNT");
            countDefinedValue.Aggregator = countDefinedValue.Aggregate.CreateAggregator(typeof(int));
            countDefinedValue.Argument   = LiteralExpression.FromInt32(0);

            RowBufferEntry countDefinedValueEntry = new RowBufferEntry(countDefinedValue.Aggregator.ReturnType);

            countDefinedValue.Target = countDefinedValueEntry;

            RowBufferEntryExpression anyAggregateArgument = new RowBufferEntryExpression();

            anyAggregateArgument.RowBufferEntry = inputEntry;

            AggregatedValueDefinition anyDefinedValue = new AggregatedValueDefinition();

            anyDefinedValue.Aggregate  = new FirstAggregateBinding("ANY");
            anyDefinedValue.Aggregator = anyDefinedValue.Aggregate.CreateAggregator(inputEntry.DataType);
            anyDefinedValue.Argument   = anyAggregateArgument;

            RowBufferEntry anyDefinedValueEntry = new RowBufferEntry(inputEntry.DataType);

            anyDefinedValue.Target = anyDefinedValueEntry;

            AggregateAlgebraNode aggregateAlgebraNode = new AggregateAlgebraNode();

            aggregateAlgebraNode.Input         = inputNode.Input;
            aggregateAlgebraNode.DefinedValues = new AggregatedValueDefinition[] { countDefinedValue, anyDefinedValue };

            // CASE WHEN SubqueryCount > 1 THEN 0 ELSE NULL END

            ExpressionBuilder expressionBuilder = new ExpressionBuilder();

            expressionBuilder.Push(new RowBufferEntryExpression(countDefinedValueEntry));
            expressionBuilder.Push(LiteralExpression.FromInt32(1));
            expressionBuilder.PushBinary(BinaryOperator.Greater);
            ExpressionNode whenExpression = expressionBuilder.Pop();
            ExpressionNode thenExpression = LiteralExpression.FromInt32(0);

            CaseExpression caseExpression = new CaseExpression();

            caseExpression.WhenExpressions = new ExpressionNode[] { whenExpression };
            caseExpression.ThenExpressions = new ExpressionNode[] { thenExpression };

            expressionBuilder.Push(caseExpression);
            ExpressionNode predicate = expressionBuilder.Pop();

            AssertAlgebraNode assertAlgebraNode = new AssertAlgebraNode();

            assertAlgebraNode.Input         = aggregateAlgebraNode;
            assertAlgebraNode.Predicate     = predicate;
            assertAlgebraNode.AssertionType = AssertionType.MaxOneRow;

            ResultAlgebraNode resultAlgebraNode = new ResultAlgebraNode();

            resultAlgebraNode.Input       = assertAlgebraNode;
            resultAlgebraNode.OutputList  = new RowBufferEntry[] { anyDefinedValueEntry };
            resultAlgebraNode.ColumnNames = inputNode.ColumnNames;

            return(resultAlgebraNode);
        }