예제 #1
0
        public EvalSelectNoWildcardAvro(
            SelectExprForgeContext selectExprForgeContext,
            ExprForge[] exprForges,
            EventType resultEventTypeAvro,
            string statementName)
        {
            _selectExprForgeContext = selectExprForgeContext;
            _resultEventTypeAvro = (AvroEventType) resultEventTypeAvro;

            _forges = new ExprForge[selectExprForgeContext.ExprForges.Length];
            var typeWidenerCustomizer =
                selectExprForgeContext.EventTypeAvroHandler.GetTypeWidenerCustomizer(resultEventTypeAvro);
            for (var i = 0; i < _forges.Length; i++) {
                _forges[i] = selectExprForgeContext.ExprForges[i];
                var forge = exprForges[i];
                var forgeEvaluationType = forge.EvaluationType;

                if (forge is ExprEvalByGetterFragment) {
                    _forges[i] = HandleFragment((ExprEvalByGetterFragment) forge);
                }
                else if (forge is ExprEvalStreamInsertBean) {
                    var und = (ExprEvalStreamInsertBean) forge;
                    _forges[i] =
                        new SelectExprInsertEventBeanFactory.ExprForgeStreamUnderlying(und.StreamNum, typeof(object));
                }
                else if (forge is SelectExprProcessorTypableMapForge) {
                    var typableMap = (SelectExprProcessorTypableMapForge) forge;
                    _forges[i] = new SelectExprProcessorEvalAvroMapToAvro(
                        typableMap.InnerForge,
                        ((AvroEventType) resultEventTypeAvro).SchemaAvro,
                        selectExprForgeContext.ColumnNames[i]);
                }
                else if (forge is ExprEvalStreamInsertNamedWindow) {
                    var nw = (ExprEvalStreamInsertNamedWindow) forge;
                    _forges[i] =
                        new SelectExprInsertEventBeanFactory.ExprForgeStreamUnderlying(nw.StreamNum, typeof(object));
                }
                else if (forgeEvaluationType != null && forgeEvaluationType.IsArray) {
                    var widener = TypeWidenerFactory.GetArrayToCollectionCoercer(forgeEvaluationType.GetElementType());
                    var resultType = typeof(ICollection<object>);
                    _forges[i] = new SelectExprProcessorEvalAvroArrayCoercer(forge, widener, resultType);
                }
                else {
                    var propertyName = selectExprForgeContext.ColumnNames[i];
                    var propertyType = resultEventTypeAvro.GetPropertyType(propertyName);
                    TypeWidenerSPI widener;
                    try {
                        widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                            propertyName,
                            forgeEvaluationType,
                            propertyType,
                            propertyName,
                            true,
                            typeWidenerCustomizer,
                            statementName);
                    }
                    catch (TypeWidenerException ex) {
                        throw new ExprValidationException(ex.Message, ex);
                    }

                    if (widener != null) {
                        _forges[i] = new SelectExprProcessorEvalAvroArrayCoercer(forge, widener, propertyType);
                    }
                }
            }
        }
예제 #2
0
        public static SelectExprProcessorForge Make(
            EventType[] eventTypes,
            SelectExprForgeContext selectExprForgeContext,
            int streamNumber,
            AvroSchemaEventType targetType,
            ExprNode[] exprNodes,
            string statementName)
        {
            AvroEventType resultType = (AvroEventType) targetType;
            AvroEventType streamType = (AvroEventType) eventTypes[streamNumber];

            // (A) fully assignment-compatible: same number, name and type of fields, no additional expressions: Straight repackage
            if (resultType.Schema.Equals(streamType.Schema) && selectExprForgeContext.ExprForges.Length == 0) {
                return new AvroInsertProcessorSimpleRepackage(selectExprForgeContext, streamNumber, targetType);
            }

            // (B) not completely assignable: find matching properties
            var writables = EventTypeUtility.GetWriteableProperties(resultType, true, false);
            IList<Item> items = new List<Item>();
            IList<WriteablePropertyDescriptor> written = new List<WriteablePropertyDescriptor>();

            // find the properties coming from the providing source stream
            foreach (var writeable in writables) {
                var propertyName = writeable.PropertyName;

                Field streamTypeField = streamType.SchemaAvro.GetField(propertyName);
                Field resultTypeField = resultType.SchemaAvro.GetField(propertyName);

                if (streamTypeField != null && resultTypeField != null) {
                    if (streamTypeField.Schema.Equals(resultTypeField.Schema)) {
                        items.Add(new Item(resultTypeField, streamTypeField, null, null));
                    }
                    else {
                        throw new ExprValidationException(
                            "Type by name '" +
                            resultType.Name +
                            "' " +
                            "in property '" +
                            propertyName +
                            "' expected schema '" +
                            resultTypeField.Schema +
                            "' but received schema '" +
                            streamTypeField.Schema +
                            "'");
                    }
                }
            }

            // find the properties coming from the expressions of the select clause
            var typeWidenerCustomizer =
                selectExprForgeContext.EventTypeAvroHandler.GetTypeWidenerCustomizer(targetType);
            for (var i = 0; i < selectExprForgeContext.ExprForges.Length; i++) {
                var columnName = selectExprForgeContext.ColumnNames[i];
                var exprNode = exprNodes[i];

                var writable = FindWritable(columnName, writables);
                if (writable == null) {
                    throw new ExprValidationException(
                        "Failed to find column '" + columnName + "' in target type '" + resultType.Name + "'");
                }

                Field resultTypeField = resultType.SchemaAvro.GetField(writable.PropertyName);

                TypeWidenerSPI widener;
                try {
                    widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                        ExprNodeUtilityPrint.ToExpressionStringMinPrecedenceSafe(exprNode),
                        exprNode.Forge.EvaluationType,
                        writable.PropertyType,
                        columnName,
                        false,
                        typeWidenerCustomizer,
                        statementName);
                }
                catch (TypeWidenerException ex) {
                    throw new ExprValidationException(ex.Message, ex);
                }

                items.Add(new Item(resultTypeField, null, exprNode.Forge, widener));
                written.Add(writable);
            }

            // make manufacturer
            Item[] itemsArr = items.ToArray();
            return new AvroInsertProcessorAllocate(
                streamNumber,
                itemsArr,
                resultType,
                resultType.SchemaAvro,
                selectExprForgeContext.EventBeanTypedEventFactory);
        }
예제 #3
0
        public EvalSelectNoWildcardAvro(
            SelectExprContext selectExprContext,
            EventType resultEventType,
            string statementName,
            string engineURI)
        {
            _selectExprContext = selectExprContext;
            _resultEventType = (AvroEventType) resultEventType;

            _evaluator = new ExprEvaluator[selectExprContext.ExpressionNodes.Length];
            var typeWidenerCustomizer =
                selectExprContext.EventAdapterService.GetTypeWidenerCustomizer(resultEventType);
            for (var i = 0; i < _evaluator.Length; i++)
            {
                var eval = selectExprContext.ExpressionNodes[i];
                _evaluator[i] = eval;

                if (eval is SelectExprProcessorEvalByGetterFragment)
                {
                    _evaluator[i] = HandleFragment((SelectExprProcessorEvalByGetterFragment) eval);
                }
                else if (eval is SelectExprProcessorEvalStreamInsertUnd)
                {
                    var und = (SelectExprProcessorEvalStreamInsertUnd) eval;
                    _evaluator[i] = new ProxyExprEvaluator
                    {
                        ProcEvaluate = (evaluateParams) =>
                        {
                            var @event = evaluateParams.EventsPerStream[und.StreamNum];
                            if (@event == null)
                            {
                                return null;
                            }
                            return @event.Underlying;
                        },
                        ProcReturnType = () => { return typeof (GenericRecord); }
                    };
                }
                else if (eval is SelectExprProcessorEvalTypableMap)
                {
                    var typableMap = (SelectExprProcessorEvalTypableMap) eval;
                    _evaluator[i] = new SelectExprProcessorEvalAvroMapToAvro(
                        typableMap.InnerEvaluator, ((AvroEventType) resultEventType).SchemaAvro,
                        selectExprContext.ColumnNames[i]);
                }
                else if (eval is SelectExprProcessorEvalStreamInsertNamedWindow)
                {
                    var nw = (SelectExprProcessorEvalStreamInsertNamedWindow) eval;
                    _evaluator[i] = new ProxyExprEvaluator
                    {
                        ProcEvaluate = (evaluateParams) =>
                        {
                            var @event = evaluateParams.EventsPerStream[nw.StreamNum];
                            if (@event == null)
                            {
                                return null;
                            }
                            return @event.Underlying;
                        },
                        ProcReturnType = () => { return typeof (GenericRecord); }
                    };
                }
                else if (eval.ReturnType != null && eval.ReturnType.IsArray)
                {
                    var widener = TypeWidenerFactory.GetArrayToCollectionCoercer(eval.ReturnType.GetElementType());
                    //if (eval.ReturnType == typeof (byte[]))
                    //{
                    //    widener = TypeWidenerFactory.BYTE_ARRAY_TO_BYTE_BUFFER_COERCER;
                    //}
                    _evaluator[i] = new SelectExprProcessorEvalAvroArrayCoercer(eval, widener);
                }
                else
                {
                    var propertyName = selectExprContext.ColumnNames[i];
                    var propertyType = resultEventType.GetPropertyType(propertyName);
                    var widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                        propertyName, eval.ReturnType, propertyType, propertyName, true, typeWidenerCustomizer,
                        statementName, engineURI);
                    if (widener != null)
                    {
                        _evaluator[i] = new SelectExprProcessorEvalAvroArrayCoercer(eval, widener);
                    }
                }
            }
        }