Example #1
0
        public override bool Evaluate(RawRecord record)
        {
            //string value1 = firstScalarFunction.Evaluate(record);
            //string value2 = secondScalarFunction.Evaluate(record);
            string value1 = firstScalarFunction.Evaluate(record)?.ToValue;
            string value2 = secondScalarFunction.Evaluate(record)?.ToValue;

            JsonDataType type1 = firstScalarFunction.DataType();
            JsonDataType type2 = secondScalarFunction.DataType();

            JsonDataType targetType = type1 > type2 ? type1 : type2;

            if (value1 == null || value2 == null)
            {
                return(false);
            }

            switch (targetType)
            {
            case JsonDataType.Boolean:
                bool bool_value1, bool_value2;
                if (bool.TryParse(value1, out bool_value1) && bool.TryParse(value2, out bool_value2))
                {
                    switch (comparisonType)
                    {
                    case BooleanComparisonType.Equals:
                        return(bool_value1 == bool_value2);

                    case BooleanComparisonType.NotEqualToBrackets:
                    case BooleanComparisonType.NotEqualToExclamation:
                        return(bool_value1 != bool_value2);

                    default:
                        throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"boolean\".");
                    }
                }
                else
                {
                    throw new QueryCompilationException(string.Format("Cannot cast \"{0}\" or \"{1}\" to values of type \"boolean\"",
                                                                      value1, value2));
                }

            case JsonDataType.Bytes:
                switch (comparisonType)
                {
                case BooleanComparisonType.Equals:
                    return(value1 == value2);

                case BooleanComparisonType.NotEqualToBrackets:
                case BooleanComparisonType.NotEqualToExclamation:
                    return(value1 != value2);

                default:
                    throw new NotImplementedException();
                }

            case JsonDataType.Int:
                int int_value1, int_value2;
                if (int.TryParse(value1, out int_value1) && int.TryParse(value2, out int_value2))
                {
                    switch (comparisonType)
                    {
                    case BooleanComparisonType.Equals:
                        return(int_value1 == int_value2);

                    case BooleanComparisonType.GreaterThan:
                        return(int_value1 > int_value2);

                    case BooleanComparisonType.GreaterThanOrEqualTo:
                        return(int_value1 >= int_value2);

                    case BooleanComparisonType.LessThan:
                        return(int_value1 < int_value2);

                    case BooleanComparisonType.LessThanOrEqualTo:
                        return(int_value1 <= int_value2);

                    case BooleanComparisonType.NotEqualToBrackets:
                    case BooleanComparisonType.NotEqualToExclamation:
                        return(int_value1 != int_value2);

                    case BooleanComparisonType.NotGreaterThan:
                        return(!(int_value1 > int_value2));

                    case BooleanComparisonType.NotLessThan:
                        return(!(int_value1 < int_value2));

                    default:
                        throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"int\".");
                    }
                }
                else
                {
                    throw new QueryCompilationException(string.Format("Cannot cast \"{0}\" or \"{1}\" to values of type \"int\"",
                                                                      value1, value2));
                }

            case JsonDataType.Long:
                long long_value1, long_value2;
                if (long.TryParse(value1, out long_value1) && long.TryParse(value2, out long_value2))
                {
                    switch (comparisonType)
                    {
                    case BooleanComparisonType.Equals:
                        return(long_value1 == long_value2);

                    case BooleanComparisonType.GreaterThan:
                        return(long_value1 > long_value2);

                    case BooleanComparisonType.GreaterThanOrEqualTo:
                        return(long_value1 >= long_value2);

                    case BooleanComparisonType.LessThan:
                        return(long_value1 < long_value2);

                    case BooleanComparisonType.LessThanOrEqualTo:
                        return(long_value1 <= long_value2);

                    case BooleanComparisonType.NotEqualToBrackets:
                    case BooleanComparisonType.NotEqualToExclamation:
                        return(long_value1 != long_value2);

                    case BooleanComparisonType.NotGreaterThan:
                        return(!(long_value1 > long_value2));

                    case BooleanComparisonType.NotLessThan:
                        return(!(long_value1 < long_value2));

                    default:
                        throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"long\".");
                    }
                }
                else
                {
                    throw new QueryCompilationException(string.Format("Cannot cast \"{0}\" or \"{1}\" to values of type \"long\"",
                                                                      value1, value2));
                }

            case JsonDataType.Double:
                double double_value1, double_value2;
                if (double.TryParse(value1, out double_value1) && double.TryParse(value2, out double_value2))
                {
                    switch (comparisonType)
                    {
                    case BooleanComparisonType.Equals:
                        return(double_value1 == double_value2);

                    case BooleanComparisonType.GreaterThan:
                        return(double_value1 > double_value2);

                    case BooleanComparisonType.GreaterThanOrEqualTo:
                        return(double_value1 >= double_value2);

                    case BooleanComparisonType.LessThan:
                        return(double_value1 < double_value2);

                    case BooleanComparisonType.LessThanOrEqualTo:
                        return(double_value1 <= double_value2);

                    case BooleanComparisonType.NotEqualToBrackets:
                    case BooleanComparisonType.NotEqualToExclamation:
                        return(double_value1 != double_value2);

                    case BooleanComparisonType.NotGreaterThan:
                        return(!(double_value1 > double_value2));

                    case BooleanComparisonType.NotLessThan:
                        return(!(double_value1 < double_value2));

                    default:
                        throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"double\".");
                    }
                }
                else
                {
                    throw new QueryCompilationException(string.Format("Cannot cast \"{0}\" or \"{1}\" to values of type \"double\"",
                                                                      value1, value2));
                }

            case JsonDataType.Float:
                float float_value1, float_value2;
                if (float.TryParse(value1, out float_value1) && float.TryParse(value2, out float_value2))
                {
                    switch (comparisonType)
                    {
                    case BooleanComparisonType.Equals:
                        return(float_value1 == float_value2);

                    case BooleanComparisonType.GreaterThan:
                        return(float_value1 > float_value2);

                    case BooleanComparisonType.GreaterThanOrEqualTo:
                        return(float_value1 >= float_value2);

                    case BooleanComparisonType.LessThan:
                        return(float_value1 < float_value2);

                    case BooleanComparisonType.LessThanOrEqualTo:
                        return(float_value1 <= float_value2);

                    case BooleanComparisonType.NotEqualToBrackets:
                    case BooleanComparisonType.NotEqualToExclamation:
                        return(float_value1 != float_value2);

                    case BooleanComparisonType.NotGreaterThan:
                        return(!(float_value1 > float_value2));

                    case BooleanComparisonType.NotLessThan:
                        return(!(float_value1 < float_value2));

                    default:
                        throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"float\".");
                    }
                }
                else
                {
                    throw new QueryCompilationException(string.Format("Cannot cast \"{0}\" or \"{1}\" to values of type \"float\"",
                                                                      value1, value2));
                }

            case JsonDataType.String:
                switch (comparisonType)
                {
                case BooleanComparisonType.Equals:
                    return(value1 == value2);

                case BooleanComparisonType.GreaterThan:
                case BooleanComparisonType.GreaterThanOrEqualTo:
                    return(value1.CompareTo(value2) > 0);

                case BooleanComparisonType.LessThan:
                case BooleanComparisonType.LessThanOrEqualTo:
                    return(value1.CompareTo(value2) > 0);

                case BooleanComparisonType.NotEqualToBrackets:
                case BooleanComparisonType.NotEqualToExclamation:
                    return(value1 != value2);

                case BooleanComparisonType.NotGreaterThan:
                    return(value1.CompareTo(value2) <= 0);

                case BooleanComparisonType.NotLessThan:
                    return(value1.CompareTo(value2) >= 0);

                default:
                    throw new QueryCompilationException("Operator " + comparisonType.ToString() + " cannot be applied to operands of type \"string\".");
                }

            case JsonDataType.Date:
                throw new NotImplementedException();

            case JsonDataType.Null:
                return(false);

            default:
                throw new QueryCompilationException("Unsupported data type.");
            }
        }