Exemplo n.º 1
0
        public void ARRAY_CONCAT()
        {
            // ARRAY_CONCAT(["apples"], ["strawberries"], ["bananas"])
            AssertEvaluation(
                CosmosArray.Create(CosmosString.Create("apples"), CosmosString.Create("strawberries"), CosmosString.Create("bananas")),
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples")),
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));

            // ARRAY_CONCAT("apples", ["strawberries"], ["bananas"])
            AssertEvaluation(
                Undefined,
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    JTokenToSqlScalarExpression.Convert("apples"),
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));

            // ARRAY_CONCAT(undefined, ["strawberries"], ["bananas"])
            AssertEvaluation(
                Undefined,
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    SqlLiteralScalarExpression.SqlUndefinedLiteralScalarExpression,
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));
        }
Exemplo n.º 2
0
        public void ARRAY_CONTAINS()
        {
            // ARRAY_CONTAINS(["apples", "strawberries", "bananas"], "apples")
            AssertEvaluation(
                expected: CosmosBoolean.Create(new string[] { "apples", "strawberries", "bananas" }.Contains("apples")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("apples"))));

            // ARRAY_CONTAINS(["apples", "strawberries", "bananas"], "mangoes")
            AssertEvaluation(
                expected: CosmosBoolean.Create(new string[] { "apples", "strawberries", "bananas" }.Contains("mangoes")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("mangoes"))));

            // ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "apples"}, true)
            AssertEvaluation(
                expected: CosmosBoolean.Create(true),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray(
                                                            JObject.FromObject(new { name = "apples", fresh = true }),
                                                            JObject.FromObject(new { name = "strawberries", fresh = true }))),
                    JTokenToSqlScalarExpression.Convert(JObject.FromObject(new { name = "apples" })),
                    trueBoolean));

            // ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "apples"}, undefined)
            AssertEvaluation(
                expected: CosmosBoolean.Create(false),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray(
                                                            JObject.FromObject(new { name = "apples", fresh = true }),
                                                            JObject.FromObject(new { name = "strawberries", fresh = true }))),
                    JTokenToSqlScalarExpression.Convert(JObject.FromObject(new { name = "apples" })),
                    SqlLiteralScalarExpression.Create(SqlUndefinedLiteral.Create())));

            // ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "apples"})
            AssertEvaluation(
                expected: CosmosBoolean.Create(false),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray(
                                                            JObject.FromObject(new { name = "apples", fresh = true }),
                                                            JObject.FromObject(new { name = "strawberries", fresh = true }))),
                    JTokenToSqlScalarExpression.Convert(JObject.FromObject(new { name = "apples" }))));

            // ARRAY_CONTAINS([{"name": "apples", "fresh": true}, {"name": "strawberries", "fresh": true}], {"name": "mangoes"}, true)
            AssertEvaluation(
                expected: CosmosBoolean.Create(false),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    JTokenToSqlScalarExpression.Convert(new JArray(
                                                            JObject.FromObject(new { name = "apples", fresh = true }),
                                                            JObject.FromObject(new { name = "strawberries", fresh = true }))),
                    JTokenToSqlScalarExpression.Convert(JObject.FromObject(new { name = "mangoes" })),
                    trueBoolean));

            // ARRAY_CONTAINS([SQUARE(2), POWER(2, 2)] 2)
            AssertEvaluation(
                expected: CosmosBoolean.Create(new double[] { 2 * 2, Math.Pow(2, 2) }.Contains(2)),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayContains,
                    SqlArrayCreateScalarExpression.Create(
                        SqlFunctionCallScalarExpression.CreateBuiltin(
                            SqlFunctionCallScalarExpression.Identifiers.Square,
                            SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2))),
                        SqlFunctionCallScalarExpression.CreateBuiltin(
                            SqlFunctionCallScalarExpression.Identifiers.Power,
                            SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2)),
                            SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2)))),
                    JTokenToSqlScalarExpression.Convert(2)));
        }
Exemplo n.º 3
0
        public void ARRAY_SLICE()
        {
            // ARRAY_SLICE(["apples", "strawberries", "bananas"], 1)
            AssertEvaluation(
                expected: CosmosArray.Create(CosmosString.Create("strawberries"), CosmosString.Create("bananas")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], 1, 1)
            AssertEvaluation(
                expected: CosmosArray.Create(CosmosString.Create("strawberries")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], -2, 1)
            AssertEvaluation(
                expected: CosmosArray.Create(CosmosString.Create("strawberries")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(-2)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], -2, 2)
            AssertEvaluation(
                expected: CosmosArray.Create(CosmosString.Create("strawberries"), CosmosString.Create("bananas")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(-2)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], 1, 0)
            AssertEvaluation(
                expected: CosmosArray.Create(),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(0))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], 1, 0)
            AssertEvaluation(
                expected: CosmosArray.Create(CosmosString.Create("strawberries"), CosmosString.Create("bananas")),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1000))));

            // ARRAY_SLICE(["apples", "strawberries", "bananas"], 1, 0)
            AssertEvaluation(
                expected: CosmosArray.Create(),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArraySlice,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples", "strawberries", "bananas")),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                    SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(-100))));
        }
        /// <summary>
        /// Converts a JToken to a semantically equivalent SqlScalarExpression.
        /// </summary>
        /// <param name="token">The JToken to convert.</param>
        /// <returns>The semantically equivalent SqlScalarExpression.</returns>
        public static SqlScalarExpression Convert(JToken token)
        {
            if (token == null)
            {
                return(Undefined);
            }

            switch (token.Type)
            {
            case JTokenType.Array:
            {
                List <SqlScalarExpression> items = new List <SqlScalarExpression>();
                foreach (JToken element in token)
                {
                    items.Add(JTokenToSqlScalarExpression.Convert(element));
                }

                return(SqlArrayCreateScalarExpression.Create(items.ToArray()));
            }

            case JTokenType.Boolean:
            {
                SqlBooleanLiteral literal = SqlBooleanLiteral.Create(token.ToObject <bool>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Null:
            {
                SqlNullLiteral literal = SqlNullLiteral.Singleton;
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Integer:
            case JTokenType.Float:
            {
                SqlNumberLiteral literal = SqlNumberLiteral.Create(token.ToObject <double>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            case JTokenType.Object:
            {
                List <SqlObjectProperty> properties = new List <SqlObjectProperty>();

                foreach (JProperty prop in (JToken)token)
                {
                    SqlPropertyName     name       = SqlPropertyName.Create(prop.Name);
                    JToken              value      = prop.Value;
                    SqlScalarExpression expression = JTokenToSqlScalarExpression.Convert(value);
                    SqlObjectProperty   property   = SqlObjectProperty.Create(name, expression);
                    properties.Add(property);
                }

                return(SqlObjectCreateScalarExpression.Create(properties.ToArray()));
            }

            case JTokenType.String:
            {
                SqlStringLiteral literal = SqlStringLiteral.Create(token.ToObject <string>());
                return(SqlLiteralScalarExpression.Create(literal));
            }

            default:
                throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, "Unsupported JsonType {0}", token.Type));
            }
        }