Пример #1
0
        public virtual CompiledScript compile(ScriptEngine scriptEngine, string language, string src)
        {
            if (scriptEngine is Compilable && !scriptEngine.Factory.LanguageName.equalsIgnoreCase("ecmascript"))
            {
                Compilable compilingEngine = (Compilable)scriptEngine;

                try
                {
                    CompiledScript compiledScript = compilingEngine.compile(src);

                    LOG.debugCompiledScriptUsing(language);

                    return(compiledScript);
                }
                catch (ScriptException e)
                {
                    throw new ScriptCompilationException("Unable to compile script: " + e.Message, e);
                }
            }
            else
            {
                // engine does not support compilation
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        ///     Compile annotation objects from descriptors.
        /// </summary>
        /// <param name="annotationSpec">spec for annotations</param>
        /// <param name="importService">imports</param>
        /// <param name="compilable">statement expression</param>
        /// <returns>annotations</returns>
        /// <throws>StatementSpecCompileException compile exception</throws>
        public static Attribute[] CompileAnnotations(
            IList<AnnotationDesc> annotationSpec,
            ImportServiceCompileTime importService,
            Compilable compilable)
        {
            Attribute[] annotations;
            try {
                annotations = CompileAnnotations(annotationSpec, importService);
            }
            catch (AnnotationException e) {
                throw new StatementSpecCompileException(
                    "Failed to process statement annotations: " + e.Message,
                    e,
                    compilable.ToEPL());
            }
            catch (EPException) {
                throw;
            }
            catch (Exception ex) {
                var message =
                    "Unexpected exception compiling annotations in statement, please consult the log file and report the exception: " +
                    ex.Message;
                Log.Error(message, ex);
                throw new StatementSpecCompileException(message, ex, compilable.ToEPL());
            }

            return annotations;
        }
Пример #3
0
 public FAFQueryMethodIUDDeleteForge(
     StatementSpecCompiled spec,
     Compilable compilable,
     StatementRawInfo statementRawInfo,
     StatementCompileTimeServices services)
     : base(spec, compilable, statementRawInfo, services)
 {
 }
Пример #4
0
        public FAFQueryMethodIUDInsertIntoForge(
            StatementSpecCompiled specCompiled,
            Compilable compilable,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
            : base(AssociatedFromClause(specCompiled, services), compilable, statementRawInfo, services)

        {
        }
Пример #5
0
 public StatementBaseInfo(
     Compilable compilable,
     StatementSpecCompiled statementSpec,
     object userObjectCompileTime,
     StatementRawInfo statementRawInfo,
     string optionalModuleName)
 {
     Compilable = compilable;
     StatementSpec = statementSpec;
     UserObjectCompileTime = userObjectCompileTime;
     StatementRawInfo = statementRawInfo;
     ModuleName = optionalModuleName;
 }
Пример #6
0
        private static ClassProvidedPrecompileResult CompileAddExtensions(
            IList <string> classes,
            Compilable compilable,
            StatementCompileTimeServices compileTimeServices)
        {
            ClassProvidedPrecompileResult classesInlined;

            try {
                classesInlined = CompileClassProvided(classes, compileTimeServices, null);
                // add inlined classes including create-class
                compileTimeServices.ClassProvidedExtension.Add(classesInlined.Classes);
            }
            catch (ExprValidationException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }

            return(classesInlined);
        }
Пример #7
0
 public StatementRawInfo(
     int statementNumber,
     string statementName,
     Attribute[] annotations,
     StatementType statementType,
     ContextCompileTimeDescriptor optionalContextDescriptor,
     string intoTableName,
     Compilable compilable,
     string moduleName)
 {
     StatementNumber = statementNumber;
     StatementName = statementName;
     Annotations = annotations;
     StatementType = statementType;
     OptionalContextDescriptor = optionalContextDescriptor;
     IntoTableName = intoTableName;
     Compilable = compilable;
     ModuleName = moduleName;
 }
Пример #8
0
        private EPCompiled CompileQueryInternal(
            Compilable compilable,
            CompilerArguments arguments)
        {
            if (arguments == null) {
                arguments = new CompilerArguments(new Configuration());
            }

            // determine module name
            var moduleName = arguments.Options.ModuleName?.Invoke(new ModuleNameContext(null));
            var moduleUses = arguments.Options.ModuleUses?.Invoke(new ModuleUsesContext(moduleName, null));

            var compileTimeServices = GetCompileTimeServices(arguments, moduleName, moduleUses, true);
            try {
                return CompilerHelperFAFProvider.Compile(compilable, compileTimeServices, arguments);
            }
            catch (Exception ex) {
                throw new EPCompileException(
                    ex.Message + " [" + compilable.ToEPL() + "]",
                    ex,
                    new EmptyList<EPCompileExceptionItem>());
            }
        }
Пример #9
0
        internal static CompilerHelperSingleResult ParseCompileInlinedClassesWalk(
            Compilable compilable,
            StatementCompileTimeServices compileTimeServices)
        {
            CompilerHelperSingleResult result;

            try {
                if (compilable is CompilableEPL)
                {
                    var compilableEPL = (CompilableEPL)compilable;

                    // parse
                    var parseResult = Parse(compilableEPL.Epl);

                    // compile application-provided classes (both create-class as well as just class-keyword)
                    var classesInlined = CompileAddExtensions(parseResult.Classes, compilable, compileTimeServices);

                    // walk - this may use the new classes already such as for extension-single-row-function
                    var raw = Walk(parseResult, compilableEPL.Epl, compileTimeServices.StatementSpecMapEnv);
                    result = new CompilerHelperSingleResult(raw, classesInlined);
                }
                else if (compilable is CompilableSODA)
                {
                    var soda = ((CompilableSODA)compilable).Soda;

                    // compile application-provided classes (both create-class as well as just class-keyword)
                    ClassProvidedPrecompileResult classesInlined;
                    if ((soda.ClassProvidedExpressions != null && !soda.ClassProvidedExpressions.IsEmpty()) || soda.CreateClass != null)
                    {
                        IList <string> classTexts = new List <string>();
                        if (soda.ClassProvidedExpressions != null)
                        {
                            foreach (var inlined in soda.ClassProvidedExpressions)
                            {
                                classTexts.Add(inlined.ClassText);
                            }
                        }

                        if (soda.CreateClass != null)
                        {
                            classTexts.Add(soda.CreateClass.ClassProvidedExpression.ClassText);
                        }

                        classesInlined = CompileAddExtensions(classTexts, compilable, compileTimeServices);
                    }
                    else
                    {
                        classesInlined = ClassProvidedPrecompileResult.EMPTY;
                    }

                    // map from soda to raw
                    var raw = StatementSpecMapper.Map(soda, compileTimeServices.StatementSpecMapEnv);
                    result = new CompilerHelperSingleResult(raw, classesInlined);
                }
                else
                {
                    throw new IllegalStateException("Unrecognized compilable " + compilable);
                }
            }
            catch (StatementSpecCompileException) {
                throw;
            }
            catch (Exception ex) {
                throw new StatementSpecCompileException("Exception processing statement: " + ex.Message, ex, compilable.ToEPL());
            }

            return(result);
        }
Пример #10
0
        public static EPCompiled Compile(
            Compilable compilable,
            ModuleCompileTimeServices services,
            CompilerArguments args)
        {
            var compileTimeServices = new StatementCompileTimeServices(0, services);
            var walkResult = CompilerHelperSingleEPL.ParseCompileInlinedClassesWalk(compilable, compileTimeServices);
            var raw = walkResult.StatementSpecRaw;

            var statementType = StatementTypeUtil.GetStatementType(raw);
            if (statementType != StatementType.SELECT) {
                // the fire-and-forget spec is null for "select" and populated for I/U/D
                throw new StatementSpecCompileException(
                    "Provided EPL expression is a continuous query expression (not an on-demand query)",
                    compilable.ToEPL());
            }

            var annotations = AnnotationUtil.CompileAnnotations(
                raw.Annotations,
                services.ImportServiceCompileTime,
                compilable);

            // walk subselects, alias expressions, declared expressions, dot-expressions
            var visitor = StatementSpecRawWalkerSubselectAndDeclaredDot.WalkSubselectAndDeclaredDotExpr(raw);

            // compile context descriptor
            ContextCompileTimeDescriptor contextDescriptor = null;
            var optionalContextName = raw.OptionalContextName;
            if (optionalContextName != null) {
                var detail = services.ContextCompileTimeResolver.GetContextInfo(optionalContextName);
                if (detail == null) {
                    throw new StatementSpecCompileException(
                        "Context by name '" + optionalContextName + "' could not be found",
                        compilable.ToEPL());
                }

                contextDescriptor = new ContextCompileTimeDescriptor(
                    optionalContextName,
                    detail.ContextModuleName,
                    detail.ContextVisibility,
                    new ContextPropertyRegistry(detail),
                    detail.ValidationInfos);
            }

            var statementNameFromAnnotation = GetNameFromAnnotation(annotations);
            var statementName = statementNameFromAnnotation == null ? GetDefaultStatementName() : statementNameFromAnnotation.Trim();
            var statementRawInfo = new StatementRawInfo(
                0,
                statementName,
                annotations,
                statementType.Value,
                contextDescriptor,
                null,
                compilable,
                null);
            StatementSpecCompiledDesc compiledDesc = StatementRawCompiler.Compile(
                raw,
                compilable,
                false,
                true,
                annotations,
                visitor.Subselects,
                new List<ExprTableAccessNode>(raw.TableExpressions),
                statementRawInfo,
                compileTimeServices);
            StatementSpecCompiled specCompiled = compiledDesc.Compiled;

            var fafSpec = specCompiled.Raw.FireAndForgetSpec;

            //var @namespace = "generated";
            var classPostfix = IdentifierUtil.GetIdentifierMayStartNumeric(statementName);

            EPCompiledManifest manifest;
            Assembly assembly;

            FAFQueryMethodForge query;
            if (specCompiled.Raw.InsertIntoDesc != null) {
                query = new FAFQueryMethodIUDInsertIntoForge(
                    specCompiled,
                    compilable,
                    statementRawInfo,
                    compileTimeServices);
            }
            else if (fafSpec == null) { // null indicates a select-statement, same as continuous query
                var desc = new FAFQueryMethodSelectDesc(
                    specCompiled,
                    compilable,
                    statementRawInfo,
                    compileTimeServices);
                var classNameResultSetProcessor =
                    CodeGenerationIDGenerator.GenerateClassNameSimple(
                        typeof(ResultSetProcessorFactoryProvider),
                        classPostfix);
                query = new FAFQueryMethodSelectForge(desc, classNameResultSetProcessor, statementRawInfo);
            }
            else if (fafSpec is FireAndForgetSpecDelete) {
                query = new FAFQueryMethodIUDDeleteForge(
                    specCompiled,
                    compilable,
                    statementRawInfo,
                    compileTimeServices);
            }
            else if (fafSpec is FireAndForgetSpecUpdate) {
                query = new FAFQueryMethodIUDUpdateForge(
                    specCompiled,
                    compilable,
                    statementRawInfo,
                    compileTimeServices);
            }
            else {
                throw new IllegalStateException("Unrecognized FAF code " + fafSpec);
            }

            // verify substitution parameters
            VerifySubstitutionParams(raw.SubstitutionParameters);

            try {
                manifest = CompileToAssembly(query, classPostfix, args.Options, services, out assembly);
            }
            catch (EPCompileException) {
                throw;
            }
            catch (Exception ex) {
                throw new EPCompileException(
                    "Unexpected exception compiling module: " + ex.Message,
                    ex,
                    new EmptyList<EPCompileExceptionItem>());
            }

            return new EPCompiled(new [] { assembly }, manifest);
        }
Пример #11
0
        public static StatementSpecCompiled Compile(
            StatementSpecRaw spec,
            Compilable compilable,
            bool isSubquery,
            bool isOnDemandQuery,
            Attribute[] annotations,
            IList<ExprSubselectNode> subselectNodes,
            IList<ExprTableAccessNode> tableAccessNodes,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices compileTimeServices)
        {
            IList<StreamSpecCompiled> compiledStreams;
            ISet<string> eventTypeReferences = new HashSet<string>();

            if (!isOnDemandQuery && spec.FireAndForgetSpec != null) {
                throw new StatementSpecCompileException(
                    "Provided EPL expression is an on-demand query expression (not a continuous query)",
                    compilable.ToEPL());
            }

            // If not using a join and not specifying a data window, make the where-clause, if present, the filter of the stream
            // if selecting using filter spec, and not subquery in where clause
            if (spec.StreamSpecs.Count == 1 &&
                spec.StreamSpecs[0] is FilterStreamSpecRaw &&
                spec.StreamSpecs[0].ViewSpecs.Length == 0 &&
                spec.WhereClause != null &&
                spec.OnTriggerDesc == null &&
                !isSubquery &&
                !isOnDemandQuery &&
                (tableAccessNodes == null || tableAccessNodes.IsEmpty())) {
                bool disqualified;
                ExprNode whereClause = spec.WhereClause;

                var visitorX = new ExprNodeSubselectDeclaredDotVisitor();
                whereClause.Accept(visitorX);
                disqualified = visitorX.Subselects.Count > 0 ||
                               HintEnum.DISABLE_WHEREEXPR_MOVETO_FILTER.GetHint(annotations) != null;

                if (!disqualified) {
                    var viewResourceVisitor = new ExprNodeViewResourceVisitor();
                    whereClause.Accept(viewResourceVisitor);
                    disqualified = viewResourceVisitor.ExprNodes.Count > 0;
                }

                if (!disqualified) {
                    spec.WhereClause = null;
                    var streamSpec = (FilterStreamSpecRaw) spec.StreamSpecs[0];
                    streamSpec.RawFilterSpec.FilterExpressions.Add(whereClause);
                }
            }

            // compile select-clause
            var selectClauseCompiled = CompileSelectClause(spec.SelectClauseSpec);

            // Determine subselects in filter streams, these may need special handling for locking
            var visitor = new ExprNodeSubselectDeclaredDotVisitor();
            try {
                StatementSpecRawWalkerSubselectAndDeclaredDot.WalkStreamSpecs(spec, visitor);
            }
            catch (ExprValidationException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }

            foreach (var subselectNode in visitor.Subselects) {
                subselectNode.IsFilterStreamSubselect = true;
            }

            // Determine subselects for compilation, and lambda-expression shortcut syntax for named windows
            visitor.Reset();
            GroupByClauseExpressions groupByRollupExpressions;
            try {
                StatementSpecRawWalkerSubselectAndDeclaredDot.WalkSubselectAndDeclaredDotExpr(spec, visitor);

                var expressionCopier = new ExpressionCopier(
                    spec,
                    statementRawInfo.OptionalContextDescriptor,
                    compileTimeServices,
                    visitor);
                groupByRollupExpressions = GroupByExpressionHelper.GetGroupByRollupExpressions(
                    spec.GroupByExpressions,
                    spec.SelectClauseSpec,
                    spec.HavingClause,
                    spec.OrderByList,
                    expressionCopier);
            }
            catch (ExprValidationException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }

            if (isSubquery && !visitor.Subselects.IsEmpty()) {
                throw new StatementSpecCompileException(
                    "Invalid nested subquery, subquery-within-subquery is not supported",
                    compilable.ToEPL());
            }

            if (isOnDemandQuery && !visitor.Subselects.IsEmpty()) {
                throw new StatementSpecCompileException(
                    "Subqueries are not a supported feature of on-demand queries",
                    compilable.ToEPL());
            }

            foreach (var subselectNode in visitor.Subselects) {
                if (!subselectNodes.Contains(subselectNode)) {
                    subselectNodes.Add(subselectNode);
                }
            }

            // Compile subselects found
            var subselectNumber = 0;
            foreach (var subselect in subselectNodes) {
                StatementSpecRaw raw = subselect.StatementSpecRaw;
                var compiled = Compile(
                    raw,
                    compilable,
                    true,
                    isOnDemandQuery,
                    annotations,
                    Collections.GetEmptyList<ExprSubselectNode>(),
                    Collections.GetEmptyList<ExprTableAccessNode>(),
                    statementRawInfo,
                    compileTimeServices);
                subselect.SetStatementSpecCompiled(compiled, subselectNumber);
                subselectNumber++;
            }

            // Set table-access number
            var tableAccessNumber = 0;
            foreach (var tableAccess in tableAccessNodes) {
                tableAccess.TableAccessNumber = tableAccessNumber;
                tableAccessNumber++;
            }

            // compile each stream used
            try {
                compiledStreams = new List<StreamSpecCompiled>(spec.StreamSpecs.Count);
                var streamNum = 0;
                foreach (var rawSpec in spec.StreamSpecs) {
                    streamNum++;
                    var compiled = StreamSpecCompiler.Compile(
                        rawSpec,
                        eventTypeReferences,
                        spec.InsertIntoDesc != null,
                        spec.StreamSpecs.Count > 1,
                        false,
                        spec.OnTriggerDesc != null,
                        rawSpec.OptionalStreamName,
                        streamNum,
                        statementRawInfo,
                        compileTimeServices);
                    compiledStreams.Add(compiled);
                }
            }
            catch (ExprValidationException ex) {
                if (ex.Message == null) {
                    throw new StatementSpecCompileException(
                        "Unexpected exception compiling statement, please consult the log file and report the exception",
                        ex,
                        compilable.ToEPL());
                }

                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }
            catch (EPException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }
            catch (Exception ex) {
                var text = "Unexpected error compiling statement";
                Log.Error(text, ex);
                throw new StatementSpecCompileException(
                    text + ": " + ex.GetType().Name + ":" + ex.Message,
                    ex,
                    compilable.ToEPL());
            }

            return new StatementSpecCompiled(
                spec,
                compiledStreams.ToArray(),
                selectClauseCompiled,
                annotations,
                groupByRollupExpressions,
                subselectNodes,
                visitor.DeclaredExpressions,
                tableAccessNodes);
        }
        public static CompilableItem CompileItem(
            Compilable compilable,
            string optionalModuleName,
            string moduleIdentPostfix,
            int statementNumber,
            ISet<string> statementNames,
            ModuleCompileTimeServices moduleCompileTimeServices,
            CompilerOptions compilerOptions,
            out Assembly assembly)
        {
            var compileTimeServices = new StatementCompileTimeServices(statementNumber, moduleCompileTimeServices);

            // Stage 0 - parse and compile-inline-classes and walk statement
            var walked = ParseCompileInlinedClassesWalk(compilable, compileTimeServices);
            var raw = walked.StatementSpecRaw;
            string classNameCreateClass = null;
            if (raw.CreateClassProvided != null) {
                classNameCreateClass = DetermineClassNameCreateClass(walked.ClassesInlined);
            }
            
            try {
                // Stage 2(a) - precompile: compile annotations
                var annotations = AnnotationUtil.CompileAnnotations(
                    raw.Annotations,
                    compileTimeServices.ImportServiceCompileTime,
                    compilable);

                // Stage 2(b) - walk subselects, alias expressions, declared expressions, dot-expressions
                ExprNodeSubselectDeclaredDotVisitor visitor;
                try {
                    visitor = StatementSpecRawWalkerSubselectAndDeclaredDot.WalkSubselectAndDeclaredDotExpr(raw);
                }
                catch (ExprValidationException ex) {
                    throw new StatementSpecCompileException(ex.Message, compilable.ToEPL());
                }

                var subselectNodes = visitor.Subselects;

                // Determine a statement name
                var statementNameProvided = GetNameFromAnnotation(annotations);
                if (compilerOptions.StatementName != null) {
                    var assignedName = compilerOptions.StatementName.Invoke(
                        new StatementNameContext(
                            () => compilable.ToEPL(),
                            statementNameProvided,
                            optionalModuleName,
                            annotations,
                            statementNumber));
                    if (assignedName != null) {
                        statementNameProvided = assignedName;
                    }
                }

                var statementName = statementNameProvided ?? Convert.ToString(statementNumber);
                if (statementNames.Contains(statementName)) {
                    var count = 1;
                    var newStatementName = statementName + "-" + count;
                    while (statementNames.Contains(newStatementName)) {
                        count++;
                        newStatementName = statementName + "-" + count;
                    }

                    statementName = newStatementName;
                }

                statementName = statementName.Trim();
                statementNames.Add(statementName);

                // Determine table access nodes
                var tableAccessNodes = DetermineTableAccessNodes(raw.TableExpressions, visitor);

                // compile scripts once in this central place, may also compile later in expression
                ScriptValidationPrecompileUtil.ValidateScripts(
                    raw.ScriptExpressions,
                    raw.ExpressionDeclDesc,
                    compileTimeServices);

                // Determine subselects for compilation, and lambda-expression shortcut syntax for named windows
                if (!visitor.ChainedExpressionsDot.IsEmpty()) {
                    RewriteNamedWindowSubselect(
                        visitor.ChainedExpressionsDot,
                        subselectNodes,
                        compileTimeServices.NamedWindowCompileTimeResolver);
                }

                // Stage 2(c) compile context descriptor
                ContextCompileTimeDescriptor contextDescriptor = null;
                var optionalContextName = raw.OptionalContextName;
                if (optionalContextName != null) {
                    var detail = compileTimeServices.ContextCompileTimeResolver.GetContextInfo(optionalContextName);
                    if (detail == null) {
                        throw new StatementSpecCompileException(
                            "Context by name '" + optionalContextName + "' could not be found",
                            compilable.ToEPL());
                    }

                    contextDescriptor = new ContextCompileTimeDescriptor(
                        optionalContextName,
                        detail.ContextModuleName,
                        detail.ContextVisibility,
                        new ContextPropertyRegistry(detail),
                        detail.ValidationInfos);
                }

                // Stage 2(d) compile raw statement spec
                var statementType = StatementTypeUtil.GetStatementType(raw).Value;
                var statementRawInfo = new StatementRawInfo(
                    statementNumber,
                    statementName,
                    annotations,
                    statementType,
                    contextDescriptor,
                    raw.IntoTableSpec?.Name,
                    compilable,
                    optionalModuleName);
                var compiledDesc = StatementRawCompiler.Compile(
                    raw,
                    compilable,
                    false,
                    false,
                    annotations,
                    subselectNodes,
                    tableAccessNodes,
                    statementRawInfo,
                    compileTimeServices);
                var specCompiled = compiledDesc.Compiled;
                var statementIdentPostfix = IdentifierUtil.GetIdentifierMayStartNumeric(statementName);

                // get compile-time user object
                object userObjectCompileTime = null;
                if (compilerOptions.StatementUserObject != null) {
                    userObjectCompileTime = compilerOptions.StatementUserObject.Invoke(
                        new StatementUserObjectContext(
                            () => compilable.ToEPL(),
                            statementName,
                            optionalModuleName,
                            annotations,
                            statementNumber));
                }

                // handle hooks
                HandleStatementCompileHook(annotations, compileTimeServices, specCompiled);

                // Stage 3(a) - statement-type-specific forge building
                var @base = new StatementBaseInfo(
                    compilable,
                    specCompiled,
                    userObjectCompileTime,
                    statementRawInfo,
                    optionalModuleName);
                StmtForgeMethod forgeMethod;
                if (raw.UpdateDesc != null) {
                    forgeMethod = new StmtForgeMethodUpdate(@base);
                }
                else if (raw.OnTriggerDesc != null) {
                    forgeMethod = new StmtForgeMethodOnTrigger(@base);
                }
                else if (raw.CreateIndexDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateIndex(@base);
                }
                else if (raw.CreateVariableDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateVariable(@base);
                }
                else if (raw.CreateDataFlowDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateDataflow(@base);
                }
                else if (raw.CreateTableDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateTable(@base);
                }
                else if (raw.CreateExpressionDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateExpression(@base);
                }
                else if (raw.CreateClassProvided != null) {
                    forgeMethod = new StmtForgeMethodCreateClass(@base, walked.ClassesInlined, classNameCreateClass);
                }
                else if (raw.CreateWindowDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateWindow(@base);
                }
                else if (raw.CreateContextDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateContext(@base);
                }
                else if (raw.CreateSchemaDesc != null) {
                    forgeMethod = new StmtForgeMethodCreateSchema(@base);
                }
                else {
                    forgeMethod = new StmtForgeMethodSelect(@base);
                }

                // check context-validity conditions for this statement
                if (contextDescriptor != null) {
                    try {
                        foreach (var validator in contextDescriptor.ValidationInfos) {
                            validator.ValidateStatement(
                                contextDescriptor.ContextName,
                                specCompiled,
                                compileTimeServices);
                        }
                    }
                    catch (ExprValidationException ex) {
                        throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
                    }
                }

                // Stage 3(b) - forge-factory-to-forge
                var classPostfix = moduleIdentPostfix + "_" + statementIdentPostfix;
                var forgeables = new List<StmtClassForgeable>();
                
                // add forgeables from filter-related processing i.e. multikeys
                foreach (var additional in compiledDesc.AdditionalForgeables) {
                    var namespaceScope = new CodegenNamespaceScope(compileTimeServices.Namespace, null, false);
                    forgeables.Add(additional.Make(namespaceScope, classPostfix));
                }
                
                var filterSpecCompileds = new List<FilterSpecCompiled>();
                var scheduleHandleCallbackProviders = new List<ScheduleHandleCallbackProvider>();
                var namedWindowConsumers = new List<NamedWindowConsumerStreamSpec>();
                var filterBooleanExpressions = new List<FilterSpecParamExprNodeForge>();
                
                var result = forgeMethod.Make(compileTimeServices.Namespace, classPostfix, compileTimeServices);
                forgeables.AddAll(result.Forgeables);
                VerifyForgeables(forgeables);
                
                filterSpecCompileds.AddAll(result.Filtereds);
                scheduleHandleCallbackProviders.AddAll(result.Scheduleds);
                namedWindowConsumers.AddAll(result.NamedWindowConsumers);
                filterBooleanExpressions.AddAll(result.FilterBooleanExpressions);

                // Stage 3(c) - filter assignments: assign filter callback ids and filter-path-num for boolean expressions
                var filterId = -1;
                foreach (var provider in filterSpecCompileds) {
                    var assigned = ++filterId;
                    provider.FilterCallbackId = assigned;
                }

                // Stage 3(d) - schedule assignments: assign schedule callback ids
                var scheduleId = 0;
                foreach (var provider in scheduleHandleCallbackProviders) {
                    provider.ScheduleCallbackId = scheduleId++;
                }

                // Stage 3(e) - named window consumers: assign consumer id
                var namedWindowConsumerId = 0;
                foreach (var provider in namedWindowConsumers) {
                    provider.NamedWindowConsumerId = namedWindowConsumerId++;
                }

                // Stage 3(f) - filter boolean expression id assignment
                var filterBooleanExprNum = 0;
                foreach (var expr in filterBooleanExpressions) {
                    expr.FilterBoolExprId = filterBooleanExprNum++;
                }

                // Stage 3(f) - verify substitution parameters
                VerifySubstitutionParams(raw.SubstitutionParameters);

                // Stage 4 - forge-to-class (forge with statement-fields last)
                var classes = forgeables
                    .Select(forgeable => forgeable.Forge(true, false))
                    .ToList();

                // Stage 5 - refactor methods to make sure the constant pool does not grow too large for any given class
                CompilerHelperRefactorToStaticMethods.RefactorMethods(
                    classes, compileTimeServices.Configuration.Compiler.ByteCode.MaxMethodsPerClass);

                // Stage 6 - sort to make the "fields" class first and all the rest later
                var sorted = classes
                    .OrderBy(c => c.ClassType.GetSortCode())
                    .ToList();

                // We are making sure JsonEventType receives the underlying class itself
                CompilableItemPostCompileLatch postCompile = CompilableItemPostCompileLatchDefault.INSTANCE;
                foreach (var eventType in compileTimeServices.EventTypeCompileTimeRegistry.NewTypesAdded) {
                    if (eventType is JsonEventType) {
                        postCompile = new CompilableItemPostCompileLatchJson(
                            compileTimeServices.EventTypeCompileTimeRegistry.NewTypesAdded,
                            compileTimeServices.ParentClassLoader);
                        break;
                    }
                }

                var container = compileTimeServices.Container;
                var compiler = container
                    .RoslynCompiler()
                    .WithCodeLogging(compileTimeServices.Configuration.Compiler.Logging.IsEnableCode)
                    .WithCodeAuditDirectory(compileTimeServices.Configuration.Compiler.Logging.AuditDirectory)
                    .WithCodegenClasses(sorted);

                assembly = compiler.Compile();

                string statementProviderClassName = CodeGenerationIDGenerator.GenerateClassNameWithNamespace(
                    compileTimeServices.Namespace,
                    typeof(StatementProvider),
                    classPostfix);

                var additionalClasses = new HashSet<Type>();
                additionalClasses.AddAll(walked.ClassesInlined.Classes);
                compileTimeServices.ClassProvidedCompileTimeResolver.AddTo(additionalClasses);
                compileTimeServices.ClassProvidedCompileTimeRegistry.AddTo(additionalClasses);

                return new CompilableItem(
                    statementProviderClassName,
                    classes,
                    postCompile,
                    additionalClasses);
            }
            catch (StatementSpecCompileException) {
                throw;
            }
            catch (ExprValidationException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }
            catch (EPException ex) {
                throw new StatementSpecCompileException(ex.Message, ex, compilable.ToEPL());
            }
            catch (Exception ex) {
                var text = ex.Message ?? ex.GetType().FullName;
                throw new StatementSpecCompileException(text, ex, compilable.ToEPL());
            }
        }
Пример #13
0
        public FAFQueryMethodIUDBaseForge(
            StatementSpecCompiled spec,
            Compilable compilable,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
        {
            this.annotations = spec.Annotations;
            this.hasTableAccess = spec.Raw.IntoTableSpec != null ||
                                  (spec.TableAccessNodes != null && spec.TableAccessNodes.Count > 0);
            if (spec.Raw.InsertIntoDesc != null &&
                services.TableCompileTimeResolver.Resolve(spec.Raw.InsertIntoDesc.EventTypeName) != null) {
                hasTableAccess = true;
            }

            if (spec.Raw.FireAndForgetSpec is FireAndForgetSpecUpdate ||
                spec.Raw.FireAndForgetSpec is FireAndForgetSpecDelete) {
                hasTableAccess |= spec.StreamSpecs[0] is TableQueryStreamSpec;
            }

            hasTableAccess |= StatementLifecycleSvcUtil.IsSubqueryWithTable(
                spec.SubselectNodes, services.TableCompileTimeResolver);

            // validate general FAF criteria
            FAFQueryMethodHelper.ValidateFAFQuery(spec);

            // obtain processor
            StreamSpecCompiled streamSpec = spec.StreamSpecs[0];
            processor = FireAndForgetProcessorForgeFactory.ValidateResolveProcessor(streamSpec);

            // obtain name and type
            string processorName = processor.NamedWindowOrTableName;
            EventType eventType = processor.EventTypeRspInputEvents;

            // determine alias
            string aliasName = processorName;
            if (streamSpec.OptionalStreamName != null) {
                aliasName = streamSpec.OptionalStreamName;
            }
            
            // activate subselect activations
            var @base = new StatementBaseInfo(compilable, spec, null, statementRawInfo, null);
            var subqueryNamedWindowConsumers = new List<NamedWindowConsumerStreamSpec>();
            var subSelectActivationDesc = SubSelectHelperActivations.CreateSubSelectActivation(
                EmptyList<FilterSpecCompiled>.Instance, subqueryNamedWindowConsumers, @base, services);
            var subselectActivation = subSelectActivationDesc.Subselects;
            _additionalForgeables.AddAll(subSelectActivationDesc.AdditionalForgeables);

            // plan subselects
            var namesPerStream = new[] {aliasName};
            var typesPerStream = new[] { processor.EventTypePublic };
            var eventTypeNames = new[] {typesPerStream[0].Name};
            SubSelectHelperForgePlan subSelectForgePlan = SubSelectHelperForgePlanner.PlanSubSelect(
                @base, subselectActivation, namesPerStream, typesPerStream, eventTypeNames, services);
            _subselectForges = subSelectForgePlan.Subselects;
            _additionalForgeables.AddAll(subSelectForgePlan.AdditionalForgeables);

            // compile filter to optimize access to named window
            StreamTypeServiceImpl typeService = new StreamTypeServiceImpl(
                new[] {eventType},
                new[] {aliasName},
                new[] {true},
                true,
                false);
            ExcludePlanHint excludePlanHint = ExcludePlanHint.GetHint(
                typeService.StreamNames,
                statementRawInfo,
                services);
            if (spec.Raw.WhereClause != null) {
                queryGraph = new QueryGraphForge(1, excludePlanHint, false);
                EPLValidationUtil.ValidateFilterWQueryGraphSafe(
                    queryGraph,
                    spec.Raw.WhereClause,
                    typeService,
                    statementRawInfo,
                    services);
            }
            else {
                queryGraph = null;
            }

            // validate expressions
            whereClause = EPStatementStartMethodHelperValidate.ValidateNodes(
                spec.Raw,
                typeService,
                null,
                statementRawInfo,
                services);

            // get executor
            InitExec(aliasName, spec, statementRawInfo, services);

            // plan table access
            tableAccessForges = ExprTableEvalHelperPlan.PlanTableAccess(spec.Raw.TableExpressions);
        }
Пример #14
0
        public FAFQueryMethodSelectDesc(
            StatementSpecCompiled statementSpec,
            Compilable compilable,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
        {
            Annotations = statementSpec.Annotations;
            ContextName = statementSpec.Raw.OptionalContextName;

            var queryPlanLogging = services.Configuration.Common.Logging.IsEnableQueryPlan;
            if (queryPlanLogging) {
                QUERY_PLAN_LOG.Info("Query plans for Fire-and-forget query '" + compilable.ToEPL() + "'");
            }

            HasTableAccess = statementSpec.TableAccessNodes != null && statementSpec.TableAccessNodes.Count > 0;
            foreach (var streamSpec in statementSpec.StreamSpecs) {
                HasTableAccess |= streamSpec is TableQueryStreamSpec;
            }

            HasTableAccess |= StatementLifecycleSvcUtil.IsSubqueryWithTable(
                statementSpec.SubselectNodes, services.TableCompileTimeResolver);
            IsDistinct = statementSpec.SelectClauseCompiled.IsDistinct;

            FAFQueryMethodHelper.ValidateFAFQuery(statementSpec);

            var numStreams = statementSpec.StreamSpecs.Length;
            var typesPerStream = new EventType[numStreams];
            var namesPerStream = new string[numStreams];
            var eventTypeNames = new string[numStreams];
            Processors = new FireAndForgetProcessorForge[numStreams];
            ConsumerFilters = new ExprNode[numStreams];

            // check context partition use
            if (statementSpec.Raw.OptionalContextName != null) {
                if (numStreams > 1) {
                    throw new ExprValidationException(
                        "Joins in runtime queries for context partitions are not supported");
                }
            }

            // resolve types and processors
            for (var i = 0; i < numStreams; i++) {
                var streamSpec = statementSpec.StreamSpecs[i];
                Processors[i] = FireAndForgetProcessorForgeFactory.ValidateResolveProcessor(streamSpec);
                if (numStreams > 1 && Processors[i].ContextName != null) {
                    throw new ExprValidationException(
                        "Joins against named windows that are under context are not supported");
                }

                var streamName = Processors[i].NamedWindowOrTableName;
                if (streamSpec.OptionalStreamName != null) {
                    streamName = streamSpec.OptionalStreamName;
                }

                namesPerStream[i] = streamName;
                typesPerStream[i] = Processors[i].EventTypeRspInputEvents;
                eventTypeNames[i] = typesPerStream[i].Name;

                IList<ExprNode> consumerFilterExprs;
                if (streamSpec is NamedWindowConsumerStreamSpec) {
                    var namedSpec = (NamedWindowConsumerStreamSpec) streamSpec;
                    consumerFilterExprs = namedSpec.FilterExpressions;
                }
                else {
                    var tableSpec = (TableQueryStreamSpec) streamSpec;
                    consumerFilterExprs = tableSpec.FilterExpressions;
                }

                ConsumerFilters[i] = ExprNodeUtilityMake.ConnectExpressionsByLogicalAndWhenNeeded(consumerFilterExprs);
            }

            // compile filter to optimize access to named window
            var optionalStreamsIfAny = OuterJoinAnalyzer.OptionalStreamsIfAny(statementSpec.Raw.OuterJoinDescList);
            var types = new StreamTypeServiceImpl(
                typesPerStream,
                namesPerStream,
                new bool[numStreams],
                false,
                optionalStreamsIfAny);
            var excludePlanHint = ExcludePlanHint.GetHint(types.StreamNames, statementRawInfo, services);
            QueryGraph = new QueryGraphForge(numStreams, excludePlanHint, false);
            if (statementSpec.Raw.WhereClause != null) {
                for (var i = 0; i < numStreams; i++) {
                    try {
                        var validationContext = new ExprValidationContextBuilder(types, statementRawInfo, services)
                            .WithAllowBindingConsumption(true)
                            .WithIsFilterExpression(true)
                            .Build();
                        var validated = ExprNodeUtilityValidate.GetValidatedSubtree(
                            ExprNodeOrigin.FILTER,
                            statementSpec.Raw.WhereClause,
                            validationContext);
                        FilterExprAnalyzer.Analyze(validated, QueryGraph, false);
                    }
                    catch (Exception ex) {
                        Log.Warn("Unexpected exception analyzing filter paths: " + ex.Message, ex);
                    }
                }
            }
            
            // handle subselects
            // first we create streams for subselects, if there are any
            var @base = new StatementBaseInfo(compilable, statementSpec, null, statementRawInfo, null);
            var subqueryNamedWindowConsumers = new List<NamedWindowConsumerStreamSpec>();
            SubSelectActivationDesc subSelectActivationDesc = SubSelectHelperActivations.CreateSubSelectActivation(
                EmptyList<FilterSpecCompiled>.Instance, subqueryNamedWindowConsumers, @base, services);
            IDictionary<ExprSubselectNode, SubSelectActivationPlan> subselectActivation = subSelectActivationDesc.Subselects;
            AdditionalForgeables.AddAll(subSelectActivationDesc.AdditionalForgeables);

            SubSelectHelperForgePlan subSelectForgePlan = SubSelectHelperForgePlanner.PlanSubSelect(
                @base, subselectActivation, namesPerStream, typesPerStream, eventTypeNames, services);
            SubselectForges = subSelectForgePlan.Subselects;
            AdditionalForgeables.AddAll(subSelectForgePlan.AdditionalForgeables);

            // obtain result set processor
            var isIStreamOnly = new bool[namesPerStream.Length];
            isIStreamOnly.Fill(true);
            StreamTypeService typeService = new StreamTypeServiceImpl(
                typesPerStream,
                namesPerStream,
                isIStreamOnly,
                true,
                optionalStreamsIfAny);
            WhereClause = EPStatementStartMethodHelperValidate.ValidateNodes(
                statementSpec.Raw,
                typeService,
                null,
                statementRawInfo,
                services);

            var resultSetSpec = new ResultSetSpec(statementSpec);
            ResultSetProcessor = ResultSetProcessorFactoryFactory.GetProcessorPrototype(
                resultSetSpec,
                typeService,
                null,
                new bool[0],
                true,
                null,
                true,
                false,
                statementRawInfo,
                services);
            AdditionalForgeables.AddAll(ResultSetProcessor.AdditionalForgeables);

            // plan table access
            TableAccessForges = ExprTableEvalHelperPlan.PlanTableAccess(statementSpec.Raw.TableExpressions);

            // plan joins or simple queries
            if (numStreams > 1) {
                var streamJoinAnalysisResult = new StreamJoinAnalysisResultCompileTime(numStreams);
                CompatExtensions.Fill(streamJoinAnalysisResult.NamedWindowsPerStream, (NamedWindowMetaData) null);
                for (var i = 0; i < numStreams; i++) {
                    var uniqueIndexes = Processors[i].UniqueIndexes;
                    streamJoinAnalysisResult.UniqueKeys[i] = uniqueIndexes;
                }

                var hasAggregations = ResultSetProcessor.ResultSetProcessorType.IsAggregated();
                var desc = JoinSetComposerPrototypeForgeFactory.MakeComposerPrototype(
                    statementSpec,
                    streamJoinAnalysisResult,
                    types,
                    new HistoricalViewableDesc(numStreams),
                    true,
                    hasAggregations,
                    statementRawInfo,
                    services);
                AdditionalForgeables.AddAll(desc.AdditionalForgeables);
                Joins = desc.Forge;
            }
            else {
                Joins = null;
            }
            
            var multiKeyPlan = MultiKeyPlanner.PlanMultiKeyDistinct(
                IsDistinct, ResultSetProcessor.ResultEventType, statementRawInfo, SerdeCompileTimeResolverNonHA.INSTANCE);
            AdditionalForgeables.AddAll(multiKeyPlan.MultiKeyForgeables);
            DistinctMultiKey = multiKeyPlan.ClassRef;
        }
Пример #15
0
        public FAFQueryMethodIUDBaseForge(
            StatementSpecCompiled spec,
            Compilable compilable,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
        {
            this.annotations = spec.Annotations;
            this.hasTableAccess = spec.Raw.IntoTableSpec != null ||
                                  (spec.TableAccessNodes != null && spec.TableAccessNodes.Count > 0);
            if (spec.Raw.InsertIntoDesc != null &&
                services.TableCompileTimeResolver.Resolve(spec.Raw.InsertIntoDesc.EventTypeName) != null) {
                hasTableAccess = true;
            }

            if (spec.Raw.FireAndForgetSpec is FireAndForgetSpecUpdate ||
                spec.Raw.FireAndForgetSpec is FireAndForgetSpecDelete) {
                hasTableAccess |= spec.StreamSpecs[0] is TableQueryStreamSpec;
            }

            // validate general FAF criteria
            FAFQueryMethodHelper.ValidateFAFQuery(spec);

            // obtain processor
            StreamSpecCompiled streamSpec = spec.StreamSpecs[0];
            processor = FireAndForgetProcessorForgeFactory.ValidateResolveProcessor(streamSpec);

            // obtain name and type
            string processorName = processor.NamedWindowOrTableName;
            EventType eventType = processor.EventTypeRspInputEvents;

            // determine alias
            string aliasName = processorName;
            if (streamSpec.OptionalStreamName != null) {
                aliasName = streamSpec.OptionalStreamName;
            }

            // compile filter to optimize access to named window
            StreamTypeServiceImpl typeService = new StreamTypeServiceImpl(
                new EventType[] {eventType},
                new string[] {aliasName},
                new bool[] {true},
                true,
                false);
            ExcludePlanHint excludePlanHint = ExcludePlanHint.GetHint(
                typeService.StreamNames,
                statementRawInfo,
                services);
            if (spec.Raw.WhereClause != null) {
                queryGraph = new QueryGraphForge(1, excludePlanHint, false);
                EPLValidationUtil.ValidateFilterWQueryGraphSafe(
                    queryGraph,
                    spec.Raw.WhereClause,
                    typeService,
                    statementRawInfo,
                    services);
            }
            else {
                queryGraph = null;
            }

            // validate expressions
            whereClause = EPStatementStartMethodHelperValidate.ValidateNodes(
                spec.Raw,
                typeService,
                null,
                statementRawInfo,
                services);

            // get executor
            InitExec(aliasName, spec, statementRawInfo, services);

            // plan table access
            tableAccessForges = ExprTableEvalHelperPlan.PlanTableAccess(spec.Raw.TableExpressions);
        }