Skip to content

Query: Added remaining Cosmos Type checking functions to CosmosLinqExtensions #3724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
20 commits merged into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2a478ab
Added the remaining Cosmos Type checking functions to the CosmosLinqE…
onionhammer Feb 23, 2023
6198c37
Merge branch 'master' into new-builtin-functions
onionhammer Mar 2, 2023
89e72c1
Merge branch 'master' into new-builtin-functions
onionhammer Mar 8, 2023
d88cbc8
Merge branch 'master' into new-builtin-functions
onionhammer Mar 9, 2023
6d00ef2
Added comments requested
onionhammer Mar 9, 2023
58857ab
Merge branch 'new-builtin-functions' of https://github.com/onionhamme…
onionhammer Mar 9, 2023
63744ca
Updated comment
onionhammer Mar 9, 2023
f102b1e
Updated baseline
onionhammer Mar 9, 2023
8f73230
Merge branch 'master' into new-builtin-functions
onionhammer Apr 6, 2023
10a2d1e
Improve readability of dictionary initialization
onionhammer Apr 6, 2023
90d5e42
Aligned with code style guide
onionhammer Apr 6, 2023
a12682a
Merge branch 'master' into new-builtin-functions
onionhammer Apr 6, 2023
2bf69fe
Revert change to baseline
onionhammer Apr 7, 2023
9990f77
Executed update baseline script
onionhammer Apr 7, 2023
cf5067f
Merge branch 'master' into new-builtin-functions
onionhammer Apr 7, 2023
8ce0399
Merge branch 'master' into new-builtin-functions
neildsh Apr 10, 2023
5244b1c
Merge branch 'master' into new-builtin-functions
leminh98 Apr 10, 2023
a506a4f
Merge branch 'master' into new-builtin-functions
leminh98 May 1, 2023
2b3bb49
Merge branch 'master' into new-builtin-functions
leminh98 May 8, 2023
bf0d07d
Merge branch 'master' into new-builtin-functions
leminh98 May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,25 @@ static TypeCheckFunctions()
TypeCheckFunctionsDefinitions = new Dictionary<string, BuiltinFunctionVisitor>
{
{
"IsDefined",
nameof(CosmosLinqExtensions.IsArray),
new SqlBuiltinFunctionVisitor("IS_ARRAY",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
},
{
nameof(CosmosLinqExtensions.IsBool),
new SqlBuiltinFunctionVisitor("IS_BOOL",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
},
{
nameof(CosmosLinqExtensions.IsDefined),
new SqlBuiltinFunctionVisitor("IS_DEFINED",
true,
new List<Type[]>()
Expand All @@ -28,7 +46,7 @@ static TypeCheckFunctions()
})
},
{
"IsNull",
nameof(CosmosLinqExtensions.IsNull),
new SqlBuiltinFunctionVisitor("IS_NULL",
true,
new List<Type[]>()
Expand All @@ -37,13 +55,40 @@ static TypeCheckFunctions()
})
},
{
"IsPrimitive",
nameof(CosmosLinqExtensions.IsNumber),
new SqlBuiltinFunctionVisitor("IS_NUMBER",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
},
{
nameof(CosmosLinqExtensions.IsObject),
new SqlBuiltinFunctionVisitor("IS_OBJECT",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
},
{
nameof(CosmosLinqExtensions.IsPrimitive),
new SqlBuiltinFunctionVisitor("IS_PRIMITIVE",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
},
{
nameof(CosmosLinqExtensions.IsString),
new SqlBuiltinFunctionVisitor("IS_STRING",
true,
new List<Type[]>()
{
new Type[]{typeof(object)},
})
}
};
}
Expand Down
90 changes: 88 additions & 2 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ namespace Microsoft.Azure.Cosmos.Linq
/// </summary>
public static class CosmosLinqExtensions
{
/// <summary>
/// Returns a Boolean value indicating if the type of the specified expression is an array.
/// </summary>
/// <param name="obj"></param>
/// <returns>Returns true if the type of the specified expression is an array; otherwise, false.</returns>
/// <example>
/// <code>
/// <![CDATA[
/// var isArrayQuery = documents.Where(document => document.Names.IsArray());
/// ]]>
/// </code>
/// </example>
public static bool IsArray(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Returns a Boolean value indicating if the type of the specified expression is a boolean.
/// </summary>
/// <param name="obj"></param>
/// <returns>Returns true if the type of the specified expression is a boolean; otherwise, false.</returns>
/// <example>
/// <code>
/// <![CDATA[
/// var isBoolQuery = documents.Where(document => document.IsRegistered.IsBool());
/// ]]>
/// </code>
/// </example>
public static bool IsBool(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Determines if a certain property is defined or not.
/// This method is to be used in LINQ expressions only and will be evaluated on server.
Expand Down Expand Up @@ -52,12 +86,46 @@ public static bool IsDefined(this object obj)
/// var isNullQuery = documents.Where(document => document.Name.IsNull());
/// ]]>
/// </code>
/// </example>s>
/// </example>
public static bool IsNull(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Returns a Boolean value indicating if the type of the specified expression is a number.
/// </summary>
/// <param name="obj"></param>
/// <returns>Returns true if the type of the specified expression is a number; otherwise, false.</returns>
/// <example>
/// <code>
/// <![CDATA[
/// var isNumberQuery = documents.Where(document => document.Age.IsNumber());
/// ]]>
/// </code>
/// </example>
public static bool IsNumber(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Returns a Boolean value indicating if the type of the specified expression is an object.
/// </summary>
/// <param name="obj"></param>
/// <returns>Returns true if the type of the specified expression is an object; otherwise, false.</returns>
/// <example>
/// <code>
/// <![CDATA[
/// var isObjectQuery = documents.Where(document => document.Address.IsObject());
/// ]]>
/// </code>
/// </example>
public static bool IsObject(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Determines if a certain property is of primitive JSON type.
/// This method is to be used in LINQ expressions only and will be evaluated on server.
Expand All @@ -74,12 +142,29 @@ public static bool IsNull(this object obj)
/// var isPrimitiveQuery = documents.Where(document => document.Name.IsPrimitive());
/// ]]>
/// </code>
/// </example>s>
/// </example>
public static bool IsPrimitive(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// Returns a Boolean value indicating if the type of the specified expression is a string.
/// </summary>
/// <param name="obj"></param>
/// <returns>Returns true if the type of the specified expression is a string; otherwise, false.</returns>
/// <example>
/// <code>
/// <![CDATA[
/// var isStringQuery = documents.Where(document => document.Name.IsString());
/// ]]>
/// </code>
/// </example>
public static bool IsString(this object obj)
{
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented);
}

/// <summary>
/// This method generate query definition from LINQ query.
/// </summary>
Expand All @@ -98,6 +183,7 @@ public static bool IsPrimitive(this object obj)
/// ]]>
/// </code>
/// </example>

#if PREVIEW
public
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ internal class DataObject : LinqTestObject
public int? NullableField;
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value false
public bool BooleanField;
public SimpleObject ObjectField = new SimpleObject();
public Guid GuidField;
#pragma warning restore // Field is never assigned to, and will always have its default value false

Expand Down Expand Up @@ -155,6 +156,11 @@ internal class DataObject : LinqTestObject
public string Pk;
}

internal class SimpleObject
{
public string Field { get; set; }
}

class DateJsonConverter : IsoDateTimeConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down Expand Up @@ -266,12 +272,22 @@ public void TestTypeCheckFunctions()

List<LinqTestInput> inputs = new List<LinqTestInput>
{
new LinqTestInput("IsArray array", b => getQuery(b).Where(doc => doc.ArrayField.IsArray())),
new LinqTestInput("IsArray string", b => getQuery(b).Where(doc => doc.StringField.IsArray())),
new LinqTestInput("IsBool bool", b => getQuery(b).Where(doc => doc.BooleanField.IsBool())),
new LinqTestInput("IsBool string", b => getQuery(b).Where(doc => doc.StringField.IsBool())),
new LinqTestInput("IsDefined array", b => getQuery(b).Select(doc => doc.ArrayField.IsDefined())),
new LinqTestInput("IsDefined string", b => getQuery(b).Where(doc => doc.StringField.IsDefined())),
new LinqTestInput("IsNull array", b => getQuery(b).Select(doc => doc.ArrayField.IsNull())),
new LinqTestInput("IsNull string", b => getQuery(b).Where(doc => doc.StringField.IsNull())),
new LinqTestInput("IsNumber number", b => getQuery(b).Select(doc => doc.NumericField.IsNumber())),
new LinqTestInput("IsNumber string", b => getQuery(b).Where(doc => doc.StringField.IsNumber())),
new LinqTestInput("IsObject object", b => getQuery(b).Select(doc => doc.ObjectField.IsObject())),
new LinqTestInput("IsObject string", b => getQuery(b).Where(doc => doc.StringField.IsObject())),
new LinqTestInput("IsPrimitive array", b => getQuery(b).Select(doc => doc.ArrayField.IsPrimitive())),
new LinqTestInput("IsPrimitive string", b => getQuery(b).Where(doc => doc.StringField.IsPrimitive()))
new LinqTestInput("IsPrimitive string", b => getQuery(b).Where(doc => doc.StringField.IsPrimitive())),
new LinqTestInput("IsString string", b => getQuery(b).Where(doc => doc.StringField.IsString())),
new LinqTestInput("IsString number", b => getQuery(b).Select(doc => doc.NumericField.IsString())),
};
this.ExecuteTestSuite(inputs);
}
Expand Down