Skip to content

Commit e645f63

Browse files
Fix generic type validation to support datetimes and non-ASCII strings. (#291)
* Throw `ArgumentException` on type mismatches. * Fix and centralize generic type validation. And also document which type is compatible with which `DataType`. * Improve type checking for `FragmentInfo` methods when the type is string. If the generic type is string, we check ourselves that the `DataType` is a string one. This ensures the exception thrown is `ArgumentException` instead of `TileDBException`. The exception message also is more descriptive. * Obsolete the converters between TileDB data types and .NET types. * Use the correct obsoletion code in two cases. The obsoletions in question have not yet shipped. * Validate the data type in a couple more cases. * Suppress obsoletion warning `TILEDB0013` in the `TileDB.CSharp` project.
1 parent 7ca45f8 commit e645f63

18 files changed

+320
-89
lines changed

docs/obsoletions.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Following [the deprecation policy of TileDB Embedded][core-deprecation], obsolet
77
|Diagnostic codes|Deprecated in version|Removed in version|
88
|----------------|---------------------|------------------|
99
|[`TILEDB0001`](#TILEDB0001)[`TILEDB0011`](#TILEDB0011)|5.3.0|5.5.0|
10-
|[`TILEDB0012`](#TILEDB0012)[`TILEDB0012`](#TILEDB0012)|5.7.0|5.9.0|
10+
|[`TILEDB0012`](#TILEDB0012)[`TILEDB0013`](#TILEDB0013)|5.7.0|5.9.0|
1111

1212
## `TILEDB0001` - Enum value names that start with `TILEDB_` were replaced with C#-friendly names.
1313

@@ -320,3 +320,17 @@ The obsoleted APIs fall into the following categories:
320320
- Types with the name `tiledb_***_t` were made public again only to support the APIs of the safe handles above. They have little other use on their own. You should use APIs in the `TileDB.CSharp` namespace instead.
321321

322322
[core-deprecation]: https://github.com/TileDB-Inc/TileDB/blob/dev/doc/policy/api_changes.md
323+
324+
## `TILEDB0013` - The `EnumUtils.TypeToDataType` and `EnumUtils.DataTypeToType` methods are obsolete and will be removed in a future version.
325+
326+
<a name="TILEDB0013"></a>
327+
328+
The `EnumUtils.TypeToDataType` and `EnumUtils.DataTypeToType` methods convert between TileDB data types and .NET types. Given that there is no one-to-one correspondence between these two and for legacy reasons, these methods sometimes return wrong results and were obsoleted.
329+
330+
### Version introduced
331+
332+
5.7.0
333+
334+
### Recommended action
335+
336+
If you are performing queries on arrays of unknown schema, you can use the `Query.UnsafeSetDataBuffer` and `Query.UnsafeSetWriteDataBuffer` methods to set a data buffer to a query without type validation.

sources/TileDB.CSharp/Array.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,11 @@ static void GetDomain<T>(Array array, string dimName, uint i, NonEmptyDomain non
501501
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not the dimension's type.</exception>
502502
public (T Start, T End, bool IsEmpty) NonEmptyDomain<T>(uint index) where T : struct
503503
{
504-
var datatype = EnumUtil.TypeToDataType(typeof(T));
505504
using (var schema = Schema())
506505
using (var domain = schema.Domain())
507506
using (var dimension = domain.Dimension(index))
508507
{
509-
if (datatype != dimension.Type())
510-
{
511-
throw new ArgumentException("Array.NonEmptyDomain, not valid datatype!");
512-
}
508+
ErrorHandling.CheckDataType<T>(dimension.Type());
513509
}
514510

515511
SequentialPair<T> data;
@@ -531,15 +527,11 @@ static void GetDomain<T>(Array array, string dimName, uint i, NonEmptyDomain non
531527
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not the dimension's type.</exception>
532528
public (T Start, T End, bool IsEmpty) NonEmptyDomain<T>(string name) where T : struct
533529
{
534-
var datatype = EnumUtil.TypeToDataType(typeof(T));
535530
using (var schema = Schema())
536531
using (var domain = schema.Domain())
537532
using (var dimension = domain.Dimension(name))
538533
{
539-
if (datatype != dimension.Type())
540-
{
541-
throw new ArgumentException("Array.NonEmptyDomain, not valid datatype!");
542-
}
534+
ErrorHandling.CheckDataType<T>(dimension.Type());
543535
}
544536

545537
using var ms_name = new MarshaledString(name);

sources/TileDB.CSharp/ArrayMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ IEnumerator IEnumerable.GetEnumerator()
300300

301301
private void put_metadata<T>(string key, T[] value, tiledb_datatype_t tiledb_datatype) where T : struct
302302
{
303-
ErrorHandling.ThrowIfManagedType<T>();
303+
ErrorHandling.CheckDataType<T>((DataType)tiledb_datatype);
304304
if (string.IsNullOrEmpty(key) || value.Length == 0)
305305
{
306306
throw new ArgumentException("ArrayMetadata.put_metadata, null or empty key-value!");

sources/TileDB.CSharp/Attribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public ulong CellSize()
195195
/// <param name="data">An array of values that will be used as the fill value.</param>
196196
private void SetFillValue<T>(T[] data) where T : struct
197197
{
198-
ErrorHandling.ThrowIfManagedType<T>();
198+
ErrorHandling.CheckDataType<T>(Type());
199199
if (data.Length == 0)
200200
{
201201
throw new ArgumentException("Attribute.SetFillValue, data is empty!");

sources/TileDB.CSharp/Dimension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public DataType Type()
146146
/// <returns></returns>
147147
public (T Start, T End) GetDomain<T>() where T : struct
148148
{
149-
ErrorHandling.ThrowIfManagedType<T>();
149+
ErrorHandling.CheckDataType<T>(Type());
150150
void* value_p;
151151

152152
using var ctxHandle = _ctx.Handle.Acquire();
@@ -163,7 +163,7 @@ public DataType Type()
163163
/// <returns></returns>
164164
public T TileExtent<T>() where T : struct
165165
{
166-
ErrorHandling.ThrowIfManagedType<T>();
166+
ErrorHandling.CheckDataType<T>(Type());
167167
using var ctxHandle = _ctx.Handle.Acquire();
168168
using var handle = _handle.Acquire();
169169
void* value_p;

0 commit comments

Comments
 (0)