public void TestParseMappedProp()
        {
            MappedPropertyParseResult result = ParseMappedProperty("a.b('c')");

            Assert.AreEqual("a", result.ClassName);
            Assert.AreEqual("b", result.MethodName);
            Assert.AreEqual("c", result.ArgString);

            result = ParseMappedProperty("SupportStaticMethodLib.DelimitPipe('POLYGON ((100.0 100, \", 100 100, 400 400))')");
            Assert.AreEqual("SupportStaticMethodLib", result.ClassName);
            Assert.AreEqual("DelimitPipe", result.MethodName);
            Assert.AreEqual("POLYGON ((100.0 100, \", 100 100, 400 400))", result.ArgString);

            result = ParseMappedProperty("a.b.c.d.e('f.g.h,u.h')");
            Assert.AreEqual("a.b.c.d", result.ClassName);
            Assert.AreEqual("e", result.MethodName);
            Assert.AreEqual("f.g.h,u.h", result.ArgString);

            result = ParseMappedProperty("a.b.c.d.E(\"hfhf f f f \")");
            Assert.AreEqual("a.b.c.d", result.ClassName);
            Assert.AreEqual("E", result.MethodName);
            Assert.AreEqual("hfhf f f f ", result.ArgString);

            result = ParseMappedProperty("c.d.getEnumerationSource(\"kf\"kf'kf\")");
            Assert.AreEqual("c.d", result.ClassName);
            Assert.AreEqual("getEnumerationSource", result.MethodName);
            Assert.AreEqual("kf\"kf'kf", result.ArgString);

            result = ParseMappedProperty("c.d.getEnumerationSource('kf\"kf'kf\"')");
            Assert.AreEqual("c.d", result.ClassName);
            Assert.AreEqual("getEnumerationSource", result.MethodName);
            Assert.AreEqual("kf\"kf'kf\"", result.ArgString);

            result = ParseMappedProperty("f('a')");
            Assert.AreEqual(null, result.ClassName);
            Assert.AreEqual("f", result.MethodName);
            Assert.AreEqual("a", result.ArgString);

            result = ParseMappedProperty("f('.')");
            Assert.AreEqual(null, result.ClassName);
            Assert.AreEqual("f", result.MethodName);
            Assert.AreEqual(".", result.ArgString);

            Assert.IsNull(ParseMappedProperty("('a')"));
            Assert.IsNull(ParseMappedProperty(""));
        }
Example #2
0
        // Since static method calls such as "Class.method('a')" and mapped properties "Stream.property('key')"
        // look the same, however as the validation could not resolve "Stream.property('key')" before calling this method,
        // this method tries to resolve the mapped property as a static method.
        // Assumes that this is an ExprIdentNode.
        private static ExprNode ResolveStaticMethodOrField(
            ExprIdentNode identNode,
            ExprValidationException propertyException,
            ExprValidationContext validationContext)
        {
            // Reconstruct the original string
            StringBuilder mappedProperty = new StringBuilder(identNode.UnresolvedPropertyName);
            if (identNode.StreamOrPropertyName != null) {
                mappedProperty.Insert(0, identNode.StreamOrPropertyName + '.');
            }

            // Parse the mapped property format into a class name, method and single string parameter
            MappedPropertyParseResult parse = ParseMappedProperty(mappedProperty.ToString());
            if (parse == null) {
                ExprConstantNode constNode = ResolveIdentAsEnumConst(
                    mappedProperty.ToString(),
                    validationContext.ImportService);
                if (constNode == null) {
                    throw propertyException;
                }
                else {
                    return constNode;
                }
            }

            // If there is a class name, assume a static method is possible.
            if (parse.ClassName != null) {
                IList<ExprNode> parameters =
                    Collections.SingletonList((ExprNode) new ExprConstantNodeImpl(parse.ArgString));
                IList<ExprChainedSpec> chain = new List<ExprChainedSpec>();
                chain.Add(new ExprChainedSpec(parse.ClassName, Collections.GetEmptyList<ExprNode>(), false));
                chain.Add(new ExprChainedSpec(parse.MethodName, parameters, false));
                ConfigurationCompilerExpression exprConfig =
                    validationContext.StatementCompileTimeService.Configuration.Compiler.Expression;
                ExprNode result = new ExprDotNodeImpl(chain, exprConfig.IsDuckTyping, exprConfig.IsUdfCache);

                // Validate
                try {
                    result.Validate(validationContext);
                }
                catch (ExprValidationException e) {
                    throw new ExprValidationException(
                        $"Failed to resolve enumeration method, date-time method or mapped property '{mappedProperty}': {e.Message}",
                        e);
                }

                return result;
            }

            // There is no class name, try a single-row function
            string functionName = parse.MethodName;
            try {
                Pair<Type, ImportSingleRowDesc> classMethodPair =
                    validationContext.ImportService.ResolveSingleRow(functionName);
                IList<ExprNode> parameters =
                    Collections.SingletonList((ExprNode) new ExprConstantNodeImpl(parse.ArgString));
                IList<ExprChainedSpec> chain = Collections.SingletonList(
                    new ExprChainedSpec(classMethodPair.Second.MethodName, parameters, false));
                ExprNode result = new ExprPlugInSingleRowNode(
                    functionName,
                    classMethodPair.First,
                    chain,
                    classMethodPair.Second);

                // Validate
                try {
                    result.Validate(validationContext);
                }
                catch (EPException) {
                    throw;
                }
                catch (Exception ex) {
                    throw new ExprValidationException(
                        "Plug-in aggregation function '" + parse.MethodName + "' failed validation: " + ex.Message);
                }

                return result;
            }
            catch (ImportUndefinedException) {
                // Not an single-row function
            }
            catch (ImportException e) {
                throw new IllegalStateException("Error resolving single-row function: " + e.Message, e);
            }

            // Try an aggregation function factory
            try {
                AggregationFunctionForge aggregationForge =
                    validationContext.ImportService.ResolveAggregationFunction(parse.MethodName);
                ExprNode result = new ExprPlugInAggNode(false, aggregationForge, parse.MethodName);
                result.AddChildNode(new ExprConstantNodeImpl(parse.ArgString));

                // Validate
                try {
                    result.Validate(validationContext);
                }
                catch (EPException) {
                    throw;
                }
                catch (Exception e) {
                    throw new ExprValidationException(
                        "Plug-in aggregation function '" + parse.MethodName + "' failed validation: " + e.Message);
                }

                return result;
            }
            catch (ImportUndefinedException) {
                // Not an aggregation function
            }
            catch (ImportException e) {
                throw new IllegalStateException("Error resolving aggregation: " + e.Message, e);
            }

            // absolutely cannot be resolved
            throw propertyException;
        }