Пример #1
0
        /// <summary>
        /// Resolve "table.property", not chainable
        /// </summary>
        public static Pair <ExprTableAccessNode, ExprDotNode> CheckTableNameGetExprForSubproperty(TableService tableService, string tableName, string subproperty)
        {
            var metadata = tableService.GetTableMetadata(tableName);

            if (metadata == null)
            {
                return(null);
            }

            var index = ASTUtil.UnescapedIndexOfDot(subproperty);

            if (index == -1)
            {
                if (metadata.KeyTypes.Length > 0)
                {
                    return(null);
                }
                var tableNodeX = new ExprTableAccessNodeSubprop(tableName, subproperty);
                return(new Pair <ExprTableAccessNode, ExprDotNode>(tableNodeX, null));
            }

            // we have a nested subproperty such as "tablename.subproperty.abc"
            IList <ExprChainedSpec> chainedSpecs = new List <ExprChainedSpec>(1);

            chainedSpecs.Add(new ExprChainedSpec(subproperty.Substring(index + 1), Collections.GetEmptyList <ExprNode>(), true));
            var tableNode = new ExprTableAccessNodeSubprop(tableName, subproperty.Substring(0, index));
            var dotNode   = new ExprDotNode(chainedSpecs, false, false);

            dotNode.AddChildNode(tableNode);
            return(new Pair <ExprTableAccessNode, ExprDotNode>(tableNode, dotNode));
        }
Пример #2
0
        private static ExprNode HandleVariable(
            IList <Chainable> chain,
            VariableMetaData variable,
            StatementSpecMapContext mapContext,
            Func <IList <Chainable>, ExprDotNodeImpl> dotNodeFunction)
        {
            var message = VariableUtil.CheckVariableContextName(mapContext.ContextName, variable);

            if (message != null)
            {
                throw new ValidationException(message);
            }

            ExprNode rootNode = new ExprVariableNodeImpl(variable, null);

            if (chain.Count == 1)
            {
                return(rootNode);
            }

            // Handle simple-subproperty by means of variable node
            if (chain.Count == 2 && chain[1] is ChainableName)
            {
                return(new ExprVariableNodeImpl(variable, chain[1].GetRootNameOrEmptyString()));
            }

            var         subchain = chain.SubList(1, chain.Count);
            ExprDotNode dot      = dotNodeFunction.Invoke(subchain);

            dot.AddChildNode(rootNode);
            return(dot);
        }
Пример #3
0
        private static void AnalyzeDotNode(ExprDotNode dotNode, QueryGraph queryGraph)
        {
            var interval = dotNode.ExprDotNodeFilterAnalyzerDesc;

            if (interval == null)
            {
                return;
            }
            interval.Apply(queryGraph);
        }
Пример #4
0
 public void Visit(ExprNode exprNode)
 {
     if (exprNode is ExprStreamUnderlyingNode)
     {
         hasStreamSelect = true;
     }
     if (exprNode is ExprDotNode)
     {
         ExprDotNode streamRef = (ExprDotNode)exprNode;
         if (streamRef.StreamReferencedIfAny != null)
         {
             hasStreamSelect = true;
         }
     }
 }
Пример #5
0
        private static ExprNode HandleScript(
            ExprNodeScript scriptNode,
            IList <Chainable> chain,
            Func <IList <Chainable>, ExprDotNodeImpl> dotNodeFunction)
        {
            if (chain.Count == 1)
            {
                return(scriptNode);
            }

            var         subchain = chain.SubList(1, chain.Count);
            ExprDotNode dot      = dotNodeFunction.Invoke(subchain);

            dot.AddChildNode(scriptNode);
            return(dot);
        }
        private void ValidateIndexNamedParameter(ExprValidationContext validationContext)
        {
            if (_indexNamedParameter.Count != 1 || !(_indexNamedParameter[0] is ExprDeclaredNode))
            {
                throw GetIndexNameMessage("requires an expression name");
            }

            ExprDeclaredNode node = (ExprDeclaredNode)_indexNamedParameter[0];

            if (!(node.Body is ExprDotNode))
            {
                throw GetIndexNameMessage("requires an index expression");
            }

            ExprDotNode dotNode = (ExprDotNode)node.Body;

            if (dotNode.ChainSpec.Count > 1)
            {
                throw GetIndexNameMessage("invalid chained index expression");
            }

            IList <ExprNode> @params       = dotNode.ChainSpec[0].Parameters;
            string           indexTypeName = dotNode.ChainSpec[0].Name;

            _optionalIndexName = node.Prototype.Name;

            AdvancedIndexFactoryProvider provider;

            try
            {
                provider = validationContext.EngineImportService.ResolveAdvancedIndexProvider(indexTypeName);
            }
            catch (EngineImportException e)
            {
                throw new ExprValidationException(e.Message, e);
            }

            if (indexTypeName.ToLowerInvariant() != IndexTypeName)
            {
                throw new ExprValidationException("Invalid index type '" + indexTypeName + "', expected '" +
                                                  IndexTypeName + "'");
            }

            _optionalIndexConfig = provider.ValidateConfigureFilterIndex(_optionalIndexName, indexTypeName,
                                                                         ExprNodeUtility.ToArray(@params), validationContext);
        }
Пример #7
0
 public void QExprStreamEventMethod(ExprDotNode exprDotNode)
 {
 }
Пример #8
0
 public void QExprStreamUndMethod(ExprDotNode exprDotEvalStreamMethod)
 {
 }
Пример #9
0
 public void QExprDot(ExprDotNode exprDotNode)
 {
 }
Пример #10
0
        public static void HandleLibFunc(
            CommonTokenStream tokenStream,
            EsperEPL2GrammarParser.LibFunctionContext ctx,
            ConfigurationInformation configurationInformation,
            EngineImportService engineImportService,
            IDictionary <ITree, ExprNode> astExprNodeMap,
            LazyAllocatedMap <ConfigurationPlugInAggregationMultiFunction, PlugInAggregationMultiFunctionFactory> plugInAggregations,
            string engineURI,
            ExpressionDeclDesc expressionDeclarations,
            ExprDeclaredService exprDeclaredService,
            IList <ExpressionScriptProvided> scriptExpressions,
            ContextDescriptor contextDescriptor,
            TableService tableService,
            StatementSpecRaw statementSpec,
            VariableService variableService)
        {
            var model    = GetModel(ctx, tokenStream);
            var duckType = configurationInformation.EngineDefaults.ExpressionConfig.IsDuckTyping;
            var udfCache = configurationInformation.EngineDefaults.ExpressionConfig.IsUdfCache;

            // handle "some.xyz(...)" or "some.other.xyz(...)"
            if (model.ChainElements.Count == 1 &&
                model.OptionalClassIdent != null &&
                ASTTableExprHelper.CheckTableNameGetExprForProperty(tableService, model.OptionalClassIdent) == null)
            {
                var chainSpec = GetLibFunctionChainSpec(model.ChainElements[0], astExprNodeMap);

                var declaredNodeX = ExprDeclaredHelper.GetExistsDeclaredExpr(model.OptionalClassIdent, Collections.GetEmptyList <ExprNode>(), expressionDeclarations.Expressions, exprDeclaredService, contextDescriptor);
                if (declaredNodeX != null)
                {
                    ExprNode exprNode = new ExprDotNode(Collections.SingletonList(chainSpec), duckType, udfCache);
                    exprNode.AddChildNode(declaredNodeX);
                    ASTExprHelper.ExprCollectAddSubNodesAddParentNode(exprNode, ctx, astExprNodeMap);
                    return;
                }

                IList <ExprChainedSpec> chainX = new List <ExprChainedSpec>(2);
                chainX.Add(new ExprChainedSpec(model.OptionalClassIdent, Collections.GetEmptyList <ExprNode>(), true));
                chainX.Add(chainSpec);
                var dotNodeX = new ExprDotNode(chainX, configurationInformation.EngineDefaults.ExpressionConfig.IsDuckTyping, configurationInformation.EngineDefaults.ExpressionConfig.IsUdfCache);
                if (dotNodeX.IsVariableOp(variableService))
                {
                    statementSpec.HasVariables = true;
                }
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(dotNodeX, ctx, astExprNodeMap);
                return;
            }

            // try additional built-in single-row function
            ExprNode singleRowExtNode = engineImportService.ResolveSingleRowExtendedBuiltin(model.ChainElements[0].FuncName);

            if (singleRowExtNode != null)
            {
                if (model.ChainElements.Count == 1)
                {
                    ASTExprHelper.ExprCollectAddSubNodesAddParentNode(singleRowExtNode, ctx, astExprNodeMap);
                    return;
                }
                var spec = new List <ExprChainedSpec>();
                EsperEPL2GrammarParser.LibFunctionArgsContext firstArgs = model.ChainElements[0].Args;
                var childExpressions = ASTLibFunctionHelper.GetExprNodesLibFunc(firstArgs, astExprNodeMap);
                singleRowExtNode.AddChildNodes(childExpressions);
                AddChainRemainderFromOffset(model.ChainElements, 1, spec, astExprNodeMap);
                var dotNodeX = new ExprDotNode(spec, configurationInformation.EngineDefaults.ExpressionConfig.IsDuckTyping, configurationInformation.EngineDefaults.ExpressionConfig.IsUdfCache);
                dotNodeX.AddChildNode(singleRowExtNode);
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(dotNodeX, ctx, astExprNodeMap);
                return;
            }

            // try plug-in single-row function
            try {
                var firstFunctionX           = model.ChainElements[0].FuncName;
                var firstFunctionIsProperty  = !model.ChainElements[0].HasLeftParen;
                var classMethodPair          = engineImportService.ResolveSingleRow(firstFunctionX);
                IList <ExprChainedSpec> spec = new List <ExprChainedSpec>();
                var firstArgs        = model.ChainElements[0].Args;
                var childExpressions = ASTLibFunctionHelper.GetExprNodesLibFunc(firstArgs, astExprNodeMap);
                spec.Add(new ExprChainedSpec(classMethodPair.Second.MethodName, childExpressions, firstFunctionIsProperty));
                AddChainRemainderFromOffset(model.ChainElements, 1, spec, astExprNodeMap);
                var plugin = new ExprPlugInSingleRowNode(firstFunctionX, classMethodPair.First, spec, classMethodPair.Second);
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(plugin, ctx, astExprNodeMap);
                return;
            }
            catch (EngineImportUndefinedException e) {
                // Not an single-row function
            }
            catch (EngineImportException e) {
                throw new IllegalStateException("Error resolving single-row function: " + e.Message, e);
            }

            // special case for min,max
            var firstFunction = model.ChainElements[0].FuncName;

            if ((firstFunction.ToLower().Equals("max")) || (firstFunction.ToLower().Equals("min")) ||
                (firstFunction.ToLower().Equals("fmax")) || (firstFunction.ToLower().Equals("fmin")))
            {
                var firstArgs = model.ChainElements[0].Args;
                HandleMinMax(firstFunction, firstArgs, astExprNodeMap);
                return;
            }

            // obtain chain with actual expressions
            IList <ExprChainedSpec> chain = new List <ExprChainedSpec>();

            AddChainRemainderFromOffset(model.ChainElements, 0, chain, astExprNodeMap);

            // add chain element for class info, if any
            var distinct = model.ChainElements[0].Args != null && model.ChainElements[0].Args.DISTINCT() != null;

            if (model.OptionalClassIdent != null)
            {
                chain.Insert(0, new ExprChainedSpec(model.OptionalClassIdent, Collections.GetEmptyList <ExprNode>(), true));
                distinct = false;
            }
            firstFunction = chain[0].Name;

            // try plug-in aggregation function
            var aggregationNode = ASTAggregationHelper.TryResolveAsAggregation(engineImportService, distinct, firstFunction, plugInAggregations, engineURI);

            if (aggregationNode != null)
            {
                var firstSpec = chain.Pluck(0);
                aggregationNode.AddChildNodes(firstSpec.Parameters);
                ExprNode exprNode;
                if (chain.IsEmpty())
                {
                    exprNode = aggregationNode;
                }
                else
                {
                    exprNode = new ExprDotNode(chain, duckType, udfCache);
                    exprNode.AddChildNode(aggregationNode);
                }
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(exprNode, ctx, astExprNodeMap);
                return;
            }

            // try declared or alias expression
            var declaredNode = ExprDeclaredHelper.GetExistsDeclaredExpr(firstFunction, chain[0].Parameters, expressionDeclarations.Expressions, exprDeclaredService, contextDescriptor);

            if (declaredNode != null)
            {
                chain.RemoveAt(0);
                ExprNode exprNode;
                if (chain.IsEmpty())
                {
                    exprNode = declaredNode;
                }
                else
                {
                    exprNode = new ExprDotNode(chain, duckType, udfCache);
                    exprNode.AddChildNode(declaredNode);
                }
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(exprNode, ctx, astExprNodeMap);
                return;
            }

            // try script
            var scriptNode = ExprDeclaredHelper.GetExistsScript(configurationInformation.EngineDefaults.ScriptsConfig.DefaultDialect, chain[0].Name, chain[0].Parameters, scriptExpressions, exprDeclaredService);

            if (scriptNode != null)
            {
                chain.RemoveAt(0);
                ExprNode exprNode;
                if (chain.IsEmpty())
                {
                    exprNode = scriptNode;
                }
                else
                {
                    exprNode = new ExprDotNode(chain, duckType, udfCache);
                    exprNode.AddChildNode(scriptNode);
                }
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(exprNode, ctx, astExprNodeMap);
                return;
            }

            // try table
            var tableInfo = ASTTableExprHelper.CheckTableNameGetLibFunc(tableService, engineImportService, plugInAggregations, engineURI, firstFunction, chain);

            if (tableInfo != null)
            {
                ASTTableExprHelper.AddTableExpressionReference(statementSpec, tableInfo.First);
                chain = tableInfo.Second;
                ExprNode exprNode;
                if (chain.IsEmpty())
                {
                    exprNode = tableInfo.First;
                }
                else
                {
                    exprNode = new ExprDotNode(chain, duckType, udfCache);
                    exprNode.AddChildNode(tableInfo.First);
                }
                ASTExprHelper.ExprCollectAddSubNodesAddParentNode(exprNode, ctx, astExprNodeMap);
                return;
            }

            // Could be a mapped property with an expression-parameter "mapped(expr)" or array property with an expression-parameter "array(expr)".
            ExprDotNode dotNode;

            if (chain.Count == 1)
            {
                dotNode = new ExprDotNode(chain, false, false);
            }
            else
            {
                dotNode = new ExprDotNode(chain, duckType, udfCache);
            }
            ASTExprHelper.ExprCollectAddSubNodesAddParentNode(dotNode, ctx, astExprNodeMap);
        }
Пример #11
0
        public void TestSPI()
        {
            EPAdministratorSPI spi = (EPAdministratorSPI)_epService.EPAdministrator;

            ExprDotNode funcnode = (ExprDotNode)spi.CompileExpression("func()");

            Assert.IsFalse(funcnode.ChainSpec[0].IsProperty);

            ExprNode node = spi.CompileExpression("value=5 and /* comment */ True");

            Assert.AreEqual("value=5 and true", node.ToExpressionStringMinPrecedenceSafe());

            Expression   expr = spi.CompileExpressionToSODA("value=5 and True");
            StringWriter buf  = new StringWriter();

            expr.ToEPL(buf, ExpressionPrecedenceEnum.MINIMUM);
            Assert.AreEqual("value=5 and true", buf.ToString());

            expr = spi.CompileExpressionToSODA("5 sec");
            buf  = new StringWriter();
            expr.ToEPL(buf, ExpressionPrecedenceEnum.MINIMUM);
            Assert.AreEqual("5 seconds", buf.ToString());

            EvalFactoryNode pattern = spi.CompilePatternToNode("every A -> B");

            Assert.That(pattern, Is.InstanceOf <EvalFollowedByFactoryNode>());

            PatternExpr patternExpr = spi.CompilePatternToSODA("every A -> B");

            Assert.AreEqual(typeof(PatternFollowedByExpr), patternExpr.GetType());

            EPStatementObjectModel modelPattern = spi.CompilePatternToSODAModel("@Name('test') every A -> B");

            Assert.AreEqual("Name", modelPattern.Annotations[0].Name);
            Assert.AreEqual(typeof(PatternFollowedByExpr), ((PatternStream)modelPattern.FromClause.Streams[0]).Expression.GetType());

            AnnotationPart part = spi.CompileAnnotationToSODA("@somevalue(a='test', b=5)");

            Assert.AreEqual("somevalue", part.Name);
            Assert.AreEqual(2, part.Attributes.Count);
            Assert.AreEqual("a", part.Attributes[0].Name);
            Assert.AreEqual("test", part.Attributes[0].Value);
            Assert.AreEqual("b", part.Attributes[1].Name);
            Assert.AreEqual(5, part.Attributes[1].Value);

            MatchRecognizeRegEx regex = spi.CompileMatchRecognizePatternToSODA("a b* c+ d? e?");

            Assert.AreEqual(5, regex.Children.Count);

            // test fail cases
            string expected = "Incorrect syntax near 'in' (a reserved keyword) at line 1 column 42 [goofy in in]";
            String compiled = "goofy in in";

            try
            {
                spi.CompileExpression(compiled);
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }

            try
            {
                spi.CompileExpressionToSODA(compiled);
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }

            expected = "Incorrect syntax near 'in' (a reserved keyword) at line 1 column 6 [goofy in in]";
            try
            {
                spi.CompilePatternToNode(compiled);
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }

            try
            {
                spi.CompilePatternToSODA(compiled);
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }

            try
            {
                spi.CompileAnnotationToSODA("not an annotation");
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual("Incorrect syntax near 'not' (a reserved keyword) [not an annotation]", ex.Message);
            }

            try
            {
                spi.CompileMatchRecognizePatternToSODA("a b???");
                Assert.Fail();
            }
            catch (EPException ex)
            {
                Assert.AreEqual("Incorrect syntax near '?' expecting a closing parenthesis ')' but found a questionmark '?' at line 1 column 76 [a b???]", ex.Message);
            }

            StatementSpecRaw raw = spi.CompileEPLToRaw("select * from System.Object");

            Assert.NotNull(raw);
            EPStatementObjectModel model = spi.MapRawToSODA(raw);

            Assert.NotNull(model);

            // try control characters
            TryInvalidControlCharacters();
        }