private LinearAggregationFactoryDesc HandleTableAccessWindow(
            ExprNode[] childNodes,
            AggregationStateType stateType,
            ExprValidationContext validationContext,
            TableMetadataColumnAggregation tableAccess)
        {
            var original = (ExprAggMultiFunctionLinearAccessNodeFactoryAccess) tableAccess.Factory;
            if (childNodes.Length == 0 ||
                childNodes.Length == 1 && childNodes[0] is ExprWildcard)
            {
                var componentType = original.ContainedEventType.UnderlyingType;
                var accessor = new AggregationAccessorWindowNoEval(componentType);
                var factory = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, accessor,
                    TypeHelper.GetArrayType(componentType), original.ContainedEventType, null, null, null);
                return new LinearAggregationFactoryDesc(factory, factory.ContainedEventType, null);
            }

            if (childNodes.Length == 1)
            {
                // Expressions apply to events held, thereby validate in terms of event value expressions
                var paramNode = childNodes[0];
                var streams = TableServiceUtil.StreamTypeFromTableColumn(tableAccess,
                    validationContext.StreamTypeService.EngineURIQualifier);
                var localValidationContext = new ExprValidationContext(streams, validationContext);
                paramNode = ExprNodeUtility.GetValidatedSubtree(ExprNodeOrigin.AGGPARAM, paramNode,
                    localValidationContext);
                var paramNodeEval = paramNode.ExprEvaluator;
                var factory = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this,
                    new AggregationAccessorWindowWEval(0, paramNodeEval, paramNodeEval.ReturnType),
                    TypeHelper.GetArrayType(paramNodeEval.ReturnType), original.ContainedEventType, null, null, null);
                return new LinearAggregationFactoryDesc(factory, null, paramNodeEval.ReturnType);
            }

            throw new ExprValidationException("Invalid number of parameters");
        }
예제 #2
0
        private static SelectExprProcessor GetProcessorInternal(ICollection <int> assignedTypeNumberStack, SelectClauseElementCompiled[] selectionList, bool isUsingWildcard, InsertIntoDesc insertIntoDesc, EventType optionalInsertIntoEventType, StreamTypeService typeService, EventAdapterService eventAdapterService, ValueAddEventService valueAddEventService, SelectExprEventTypeRegistry selectExprEventTypeRegistry, EngineImportService engineImportService, int statementId, Attribute[] annotations, ConfigurationInformation configuration, NamedWindowMgmtService namedWindowMgmtService, TableService tableService, GroupByRollupInfo groupByRollupInfo)
        {
            // Wildcard not allowed when insert into specifies column order
            if (isUsingWildcard && insertIntoDesc != null && !insertIntoDesc.ColumnNames.IsEmpty())
            {
                throw new ExprValidationException("Wildcard not allowed when insert-into specifies column order");
            }

            // Determine wildcard processor (select *)
            if (IsWildcardsOnly(selectionList))
            {
                // For joins
                if (typeService.StreamNames.Length > 1)
                {
                    Log.Debug(".getProcessor Using SelectExprJoinWildcardProcessor");
                    return(SelectExprJoinWildcardProcessorFactory.Create(
                               assignedTypeNumberStack, statementId, typeService.StreamNames, typeService.EventTypes,
                               eventAdapterService, insertIntoDesc, selectExprEventTypeRegistry, engineImportService,
                               annotations, configuration, tableService));
                }
                // Single-table selects with no insert-into
                // don't need extra processing
                else if (insertIntoDesc == null)
                {
                    Log.Debug(".getProcessor Using wildcard processor");
                    if (typeService.HasTableTypes)
                    {
                        var tableName = TableServiceUtil.GetTableNameFromEventType(typeService.EventTypes[0]);
                        return(new SelectExprWildcardTableProcessor(tableName, tableService));
                    }
                    return(new SelectExprWildcardProcessor(typeService.EventTypes[0]));
                }
            }

            // Verify the assigned or name used is unique
            if (insertIntoDesc == null)
            {
                VerifyNameUniqueness(selectionList);
            }

            // Construct processor
            var buckets = GetSelectExpressionBuckets(selectionList);

            var factory = new SelectExprProcessorHelper(
                assignedTypeNumberStack, buckets.Expressions, buckets.SelectedStreams, insertIntoDesc,
                optionalInsertIntoEventType, isUsingWildcard, typeService, eventAdapterService, valueAddEventService,
                selectExprEventTypeRegistry, engineImportService, statementId, annotations, configuration,
                namedWindowMgmtService, tableService, groupByRollupInfo);
            SelectExprProcessor processor = factory.Evaluator;

            // add reference to the type obtained
            var type = (EventTypeSPI)processor.ResultEventType;

            if (!typeService.IsOnDemandStreams && type.Metadata.TypeClass != TypeClass.ANONYMOUS)
            {
                selectExprEventTypeRegistry.Add(processor.ResultEventType);
            }
            return(processor);
        }
예제 #3
0
        internal static Pair<PropertyResolutionDescriptor, string> GetTypeFromStream(
            StreamTypeService streamTypeService,
            string unresolvedPropertyName,
            string streamOrPropertyName,
            bool obtainFragment)
        {
            PropertyResolutionDescriptor propertyInfo = null;

            // no stream/property name supplied
            if (streamOrPropertyName == null)
            {
                try
                {
                    propertyInfo = streamTypeService.ResolveByPropertyName(unresolvedPropertyName, obtainFragment);
                }
                catch (StreamTypesException ex)
                {
                    throw GetSuggestionException(ex);
                }
                catch (PropertyAccessException ex)
                {
                    throw new ExprValidationPropertyException(
                        "Failed to find property '" + unresolvedPropertyName +
                        "', the property name does not parse (are you sure?): " + ex.Message, ex);
                }

                // resolves without a stream name, return descriptor and null stream name
                return new Pair<PropertyResolutionDescriptor, string>(propertyInfo, propertyInfo.StreamName);
            }

            // try to resolve the property name and stream name as it is (ie. stream name as a stream name)
            StreamTypesException typeExceptionOne;
            try
            {
                propertyInfo = streamTypeService.ResolveByStreamAndPropName(
                    streamOrPropertyName, unresolvedPropertyName, obtainFragment);
                // resolves with a stream name, return descriptor and stream name
                return new Pair<PropertyResolutionDescriptor, string>(propertyInfo, streamOrPropertyName);
            }
            catch (StreamTypesException ex)
            {
                typeExceptionOne = ex;
            }

            // try to resolve the property name to a nested property 's0.p0'
            StreamTypesException typeExceptionTwo;
            var propertyNameCandidate = streamOrPropertyName + '.' + unresolvedPropertyName;
            try
            {
                propertyInfo = streamTypeService.ResolveByPropertyName(propertyNameCandidate, obtainFragment);
                // resolves without a stream name, return null for stream name
                return new Pair<PropertyResolutionDescriptor, string>(propertyInfo, null);
            }
            catch (StreamTypesException ex)
            {
                typeExceptionTwo = ex;
            }

            // not resolved yet, perhaps the table name did not match an event type
            if (streamTypeService.HasTableTypes && streamOrPropertyName != null)
            {
                for (var i = 0; i < streamTypeService.EventTypes.Length; i++)
                {
                    var eventType = streamTypeService.EventTypes[i];
                    var tableName = TableServiceUtil.GetTableNameFromEventType(eventType);
                    if (tableName != null && tableName.Equals(streamOrPropertyName))
                    {
                        try
                        {
                            propertyInfo = streamTypeService.ResolveByStreamAndPropName(
                                eventType.Name, unresolvedPropertyName, obtainFragment);
                        }
                        catch (Exception ex)
                        {
                        }
                        if (propertyInfo != null)
                        {
                            return new Pair<PropertyResolutionDescriptor, string>(propertyInfo, streamOrPropertyName);
                        }
                    }
                }
            }

            // see if the stream or property name (the prefix) can be resolved by itself, without suffix
            // the property available may be indexed or mapped
            try
            {
                var desc = streamTypeService.ResolveByPropertyName(streamOrPropertyName, false);
                if (desc != null)
                {
                    var d2 = desc.StreamEventType.GetPropertyDescriptor(streamOrPropertyName);
                    if (d2 != null)
                    {
                        string text = null;
                        if (d2.IsIndexed)
                        {
                            text = "an indexed property and requires an index or enumeration method to access values";
                        }
                        if (d2.IsMapped)
                        {
                            text = "a mapped property and requires keyed access";
                        }
                        if (text != null)
                        {
                            throw new ExprValidationPropertyException(
                                "Failed to resolve property '" + propertyNameCandidate + "' (property '" +
                                streamOrPropertyName + "' is " + text + ")");
                        }
                    }
                }
            }
            catch (StreamTypesException e)
            {
                // need not be handled
            }

            throw GetSuggestionExceptionSecondStep(propertyNameCandidate, typeExceptionOne, typeExceptionTwo);
        }
예제 #4
0
        public static SelectExprProcessor Create(
            ICollection <int> assignedTypeNumberStack,
            int statementId,
            string statementName,
            string[] streamNames,
            EventType[] streamTypes,
            EventAdapterService eventAdapterService,
            InsertIntoDesc insertIntoDesc,
            SelectExprEventTypeRegistry selectExprEventTypeRegistry,
            EngineImportService engineImportService,
            Attribute[] annotations,
            ConfigurationInformation configuration,
            TableService tableService,
            string engineURI)

        {
            if ((streamNames.Length < 2) || (streamTypes.Length < 2) || (streamNames.Length != streamTypes.Length))
            {
                throw new ArgumentException(
                          "Stream names and types parameter length is invalid, expected use of this class is for join statements");
            }

            // Create EventType of result join events
            var  selectProperties   = new LinkedHashMap <string, Object>();
            var  streamTypesWTables = new EventType[streamTypes.Length];
            bool hasTables          = false;

            for (int i = 0; i < streamTypes.Length; i++)
            {
                streamTypesWTables[i] = streamTypes[i];
                string tableName = TableServiceUtil.GetTableNameFromEventType(streamTypesWTables[i]);
                if (tableName != null)
                {
                    hasTables             = true;
                    streamTypesWTables[i] = tableService.GetTableMetadata(tableName).PublicEventType;
                }
                selectProperties.Put(streamNames[i], streamTypesWTables[i]);
            }

            // If we have a name for this type, add it
            EventUnderlyingType representation = EventRepresentationUtil.GetRepresentation(
                annotations, configuration, AssignedType.NONE);
            EventType resultEventType;

            SelectExprProcessor processor = null;

            if (insertIntoDesc != null)
            {
                EventType existingType = eventAdapterService.GetEventTypeByName(insertIntoDesc.EventTypeName);
                if (existingType != null)
                {
                    processor = SelectExprInsertEventBeanFactory.GetInsertUnderlyingJoinWildcard(
                        eventAdapterService, existingType, streamNames, streamTypesWTables, engineImportService,
                        statementName, engineURI);
                }
            }

            if (processor == null)
            {
                if (insertIntoDesc != null)
                {
                    try
                    {
                        if (representation == EventUnderlyingType.MAP)
                        {
                            resultEventType = eventAdapterService.AddNestableMapType(
                                insertIntoDesc.EventTypeName, selectProperties, null, false, false, false, false, true);
                        }
                        else if (representation == EventUnderlyingType.OBJECTARRAY)
                        {
                            resultEventType = eventAdapterService.AddNestableObjectArrayType(
                                insertIntoDesc.EventTypeName, selectProperties, null, false, false, false, false, true,
                                false, null);
                        }
                        else if (representation == EventUnderlyingType.AVRO)
                        {
                            resultEventType = eventAdapterService.AddAvroType(
                                insertIntoDesc.EventTypeName, selectProperties, false, false, false, false, true,
                                annotations, null, statementName, engineURI);
                        }
                        else
                        {
                            throw new IllegalStateException("Unrecognized code " + representation);
                        }
                        selectExprEventTypeRegistry.Add(resultEventType);
                    }
                    catch (EventAdapterException ex)
                    {
                        throw new ExprValidationException(ex.Message, ex);
                    }
                }
                else
                {
                    if (representation == EventUnderlyingType.MAP)
                    {
                        resultEventType =
                            eventAdapterService.CreateAnonymousMapType(
                                statementId + "_join_" + CollectionUtil.ToString(assignedTypeNumberStack, "_"),
                                selectProperties, true);
                    }
                    else if (representation == EventUnderlyingType.OBJECTARRAY)
                    {
                        resultEventType =
                            eventAdapterService.CreateAnonymousObjectArrayType(
                                statementId + "_join_" + CollectionUtil.ToString(assignedTypeNumberStack, "_"),
                                selectProperties);
                    }
                    else if (representation == EventUnderlyingType.AVRO)
                    {
                        resultEventType =
                            eventAdapterService.CreateAnonymousAvroType(
                                statementId + "_join_" + CollectionUtil.ToString(assignedTypeNumberStack, "_"),
                                selectProperties, annotations, statementName, engineURI);
                    }
                    else
                    {
                        throw new IllegalStateException("Unrecognized enum " + representation);
                    }
                }
                if (resultEventType is ObjectArrayEventType)
                {
                    processor = new SelectExprJoinWildcardProcessorObjectArray(
                        streamNames, resultEventType, eventAdapterService);
                }
                else if (resultEventType is MapEventType)
                {
                    processor = new SelectExprJoinWildcardProcessorMap(
                        streamNames, resultEventType, eventAdapterService);
                }
                else if (resultEventType is AvroSchemaEventType)
                {
                    processor = eventAdapterService.EventAdapterAvroHandler.GetOutputFactory().MakeJoinWildcard(
                        streamNames, resultEventType, eventAdapterService);
                }
            }

            if (!hasTables)
            {
                return(processor);
            }
            return(new SelectExprJoinWildcardProcessorTableRows(streamTypes, processor, tableService));
        }
        private LinearAggregationFactoryDesc HandleTableAccessFirstLast(
            ExprNode[] childNodes,
            AggregationStateType stateType,
            ExprValidationContext validationContext,
            TableMetadataColumnAggregation tableAccess)
        {
            var original = (ExprAggMultiFunctionLinearAccessNodeFactoryAccess) tableAccess.Factory;
            var resultType = original.ContainedEventType.UnderlyingType;
            var defaultAccessor = stateType == AggregationStateType.FIRST
                ? AggregationAccessorFirstNoEval.INSTANCE
                : (AggregationAccessor) AggregationAccessorLastNoEval.INSTANCE;
            if (childNodes.Length == 0)
            {
                var factoryAccess = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, defaultAccessor,
                    resultType, original.ContainedEventType, null, null, null);
                return new LinearAggregationFactoryDesc(factoryAccess, factoryAccess.ContainedEventType, null);
            }

            if (childNodes.Length == 1)
            {
                if (childNodes[0] is ExprWildcard)
                {
                    var factoryAccess = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, defaultAccessor,
                        resultType, original.ContainedEventType, null, null, null);
                    return new LinearAggregationFactoryDesc(factoryAccess, factoryAccess.ContainedEventType, null);
                }

                if (childNodes[0] is ExprStreamUnderlyingNode)
                    throw new ExprValidationException("Stream-wildcard is not allowed for table column access");
                // Expressions apply to events held, thereby validate in terms of event value expressions
                var paramNode = childNodes[0];
                var streams = TableServiceUtil.StreamTypeFromTableColumn(tableAccess,
                    validationContext.StreamTypeService.EngineURIQualifier);
                var localValidationContext = new ExprValidationContext(streams, validationContext);
                paramNode = ExprNodeUtility.GetValidatedSubtree(ExprNodeOrigin.AGGPARAM, paramNode,
                    localValidationContext);
                var paramNodeEval = paramNode.ExprEvaluator;
                AggregationAccessor accessor;
                if (stateType == AggregationStateType.FIRST)
                    accessor = new AggregationAccessorFirstWEval(0, paramNodeEval);
                else
                    accessor = new AggregationAccessorLastWEval(0, paramNodeEval);
                var factory = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(
                    this, accessor, paramNodeEval.ReturnType, original.ContainedEventType, null, null, null);
                return new LinearAggregationFactoryDesc(factory, factory.ContainedEventType, null);
            }

            if (childNodes.Length == 2)
            {
                var isFirst = stateType == AggregationStateType.FIRST;
                var constant = -1;
                var indexEvalNode = childNodes[1];
                if (indexEvalNode.IsConstantResult)
                    constant = indexEvalNode.ExprEvaluator.Evaluate(EvaluateParams.EmptyTrue).AsInt();
                var evaluatorIndex = indexEvalNode.ExprEvaluator;
                if (evaluatorIndex.ReturnType.IsInt32()) {
                    throw new ExprValidationException(
                        GetErrorPrefix(stateType) +
                        " requires a constant index expression that returns an integer value");
                }

                var accessor = new AggregationAccessorFirstLastIndexNoEval(evaluatorIndex, constant, isFirst);
                var factory = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, accessor, resultType,
                    original.ContainedEventType, null, null, null);
                return new LinearAggregationFactoryDesc(factory, factory.ContainedEventType, null);
            }

            throw new ExprValidationException("Invalid number of parameters");
        }
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="assignedTypeNumberStack">The assigned type number stack.</param>
        /// <param name="statementId">The statement identifier.</param>
        /// <param name="streamNames">name of each stream</param>
        /// <param name="streamTypes">type of each stream</param>
        /// <param name="eventAdapterService">service for generating events and handling event types</param>
        /// <param name="insertIntoDesc">describes the insert-into clause</param>
        /// <param name="selectExprEventTypeRegistry">registry for event type to statements</param>
        /// <param name="methodResolutionService">for resolving writable properties</param>
        /// <param name="annotations">The annotations.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="tableService">The table service.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Stream names and types parameter length is invalid, expected use of this class is for join statements</exception>
        /// <exception cref="ExprValidationException"></exception>
        /// <throws>com.espertech.esper.epl.expression.core.ExprValidationException if the expression validation failed</throws>
        public static SelectExprProcessor Create(
            ICollection <int> assignedTypeNumberStack,
            string statementId,
            string[] streamNames,
            EventType[] streamTypes,
            EventAdapterService eventAdapterService,
            InsertIntoDesc insertIntoDesc,
            SelectExprEventTypeRegistry selectExprEventTypeRegistry,
            MethodResolutionService methodResolutionService,
            Attribute[] annotations,
            ConfigurationInformation configuration,
            TableService tableService)
        {
            if ((streamNames.Length < 2) || (streamTypes.Length < 2) || (streamNames.Length != streamTypes.Length))
            {
                throw new ArgumentException(
                          "Stream names and types parameter length is invalid, expected use of this class is for join statements");
            }

            // Create EventType of result join events
            var eventTypeMap       = new LinkedHashMap <string, object>();
            var streamTypesWTables = new EventType[streamTypes.Length];
            var hasTables          = false;

            for (var i = 0; i < streamTypes.Length; i++)
            {
                streamTypesWTables[i] = streamTypes[i];
                var tableName = TableServiceUtil.GetTableNameFromEventType(streamTypesWTables[i]);
                if (tableName != null)
                {
                    hasTables             = true;
                    streamTypesWTables[i] = tableService.GetTableMetadata(tableName).PublicEventType;
                }
                eventTypeMap.Put(streamNames[i], streamTypesWTables[i]);
            }

            // If we have a name for this type, add it
            var       useMap = EventRepresentationUtil.IsMap(annotations, configuration, AssignedType.NONE);
            EventType resultEventType;

            SelectExprProcessor processor = null;

            if (insertIntoDesc != null)
            {
                EventType existingType = eventAdapterService.GetEventTypeByName(insertIntoDesc.EventTypeName);
                if (existingType != null)
                {
                    processor = SelectExprInsertEventBeanFactory.GetInsertUnderlyingJoinWildcard(
                        eventAdapterService, existingType, streamNames, streamTypesWTables,
                        methodResolutionService.EngineImportService);
                }
            }

            if (processor == null)
            {
                if (insertIntoDesc != null)
                {
                    try
                    {
                        if (useMap)
                        {
                            resultEventType = eventAdapterService.AddNestableMapType(
                                insertIntoDesc.EventTypeName, eventTypeMap, null, false, false, false, false, true);
                        }
                        else
                        {
                            resultEventType = eventAdapterService.AddNestableObjectArrayType(
                                insertIntoDesc.EventTypeName, eventTypeMap, null, false, false, false, false, true, false, null);
                        }
                        selectExprEventTypeRegistry.Add(resultEventType);
                    }
                    catch (EventAdapterException ex)
                    {
                        throw new ExprValidationException(ex.Message);
                    }
                }
                else
                {
                    if (useMap)
                    {
                        resultEventType =
                            eventAdapterService.CreateAnonymousMapType(
                                statementId + "_join_" + CollectionUtil.ToString(assignedTypeNumberStack, "_"), eventTypeMap);
                    }
                    else
                    {
                        resultEventType =
                            eventAdapterService.CreateAnonymousObjectArrayType(
                                statementId + "_join_" + CollectionUtil.ToString(assignedTypeNumberStack, "_"), eventTypeMap);
                    }
                }
                if (resultEventType is ObjectArrayEventType)
                {
                    processor = new SelectExprJoinWildcardProcessorObjectArray(
                        streamNames, resultEventType, eventAdapterService);
                }
                else
                {
                    processor = new SelectExprJoinWildcardProcessorMap(streamNames, resultEventType, eventAdapterService);
                }
            }

            if (!hasTables)
            {
                return(processor);
            }
            return(new SelectExprJoinWildcardProcessorTableRows(streamTypes, processor, tableService));
        }