Skip to content

Commit

Permalink
Data validity check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Sep 6, 2023
1 parent 649a925 commit ec81162
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions DuckDB.NET.Bindings/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static class DataChunks
public static extern IntPtr DuckDBVectorGetData(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_vector_get_validity")]
public static extern UIntPtr DuckDBVectorGetValidity(IntPtr vector);
public static extern IntPtr DuckDBVectorGetValidity(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_list_vector_get_child")]
public static extern long DuckDBListVectorGetChild(IntPtr vector);
Expand All @@ -113,7 +113,7 @@ public static class DataChunks
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_struct_vector_get_child")]
public static extern long DuckDBStructVectorGetChild(IntPtr vector, long index);
}

public static class Types
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_boolean")]
Expand Down Expand Up @@ -169,7 +169,7 @@ public static class Types

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_timestamp")]
public static extern DuckDBTimestampStruct DuckDBValueTimestamp([In, Out] DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_result_get_chunk")]
public static extern IntPtr DuckDBResultGetChunk(DuckDBResult result, long chunkIndex);

Expand Down Expand Up @@ -255,7 +255,7 @@ public static class PreparedStatements
public static extern DuckDBState DuckDBExecutePrepared(DuckDBPreparedStatement preparedStatement, [In, Out] DuckDBResult result);
}

public static class ExtractStatements
public static class ExtractStatements
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_extract_statements")]
public static extern int DuckDBExtractStatements(DuckDBNativeConnection connection, string query, out DuckDBExtractedStatements extractedStatements);
Expand Down
28 changes: 20 additions & 8 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class DuckDBDataReader : DbDataReader

private int fieldCount;
private int recordsAffected;
private Dictionary<int, IntPtr> vectors = new();
private readonly Dictionary<int, IntPtr> vectors = new();
private readonly Dictionary<int, IntPtr> vectorValidityMask = new();

internal DuckDBDataReader(DuckDbCommand command, List<DuckDBResult> queryResults, CommandBehavior behavior)
{
Expand All @@ -50,6 +51,7 @@ private void InitReaderData()
for (int i = 0; i < columnCount; i++)
{
vectors[i] = NativeMethods.DataChunks.DuckDBDataChunkGetVector(chunk, i);
vectorValidityMask[i] = (NativeMethods.DataChunks.DuckDBVectorGetValidity(vectors[i]));
}

currentRow = -1;
Expand Down Expand Up @@ -120,6 +122,7 @@ private DuckDBTimeOnly GetTimeOnly(int ordinal)

public override decimal GetDecimal(int ordinal)
{
return 0;
return decimal.Parse(GetString(ordinal), CultureInfo.InvariantCulture);

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (ubuntu-20.04)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (windows-latest)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (windows-latest)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (windows-latest)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (windows-latest)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (macos-12)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (macos-12)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (macos-12)

Unreachable code detected

Check warning on line 126 in DuckDB.NET.Data/DuckDBDataReader.cs

View workflow job for this annotation

GitHub Actions / Build library (macos-12)

Unreachable code detected
}

Expand Down Expand Up @@ -153,8 +156,7 @@ public override Type GetFieldType(int ordinal)
DuckDBType.DuckdbTypeVarchar => typeof(string),
DuckDBType.DuckdbTypeDecimal => typeof(decimal),
DuckDBType.DuckdbTypeBlob => typeof(Stream),
var type => throw new ArgumentException(
$"Unrecognised type {type} ({(int)type}) in column {ordinal + 1}")
var type => throw new ArgumentException($"Unrecognised type {type} ({(int)type}) in column {ordinal + 1}")
};
}

Expand Down Expand Up @@ -235,6 +237,11 @@ public override int GetOrdinal(string name)

public override string GetString(int ordinal)
{
if (IsDBNull(ordinal))
{
return null;
}

var data = NativeMethods.DataChunks.DuckDBVectorGetData(vectors[ordinal]);
data += currentRow * Marshal.SizeOf<DuckDBString>();

Expand Down Expand Up @@ -309,9 +316,14 @@ public override Stream GetStream(int ordinal)

public override bool IsDBNull(int ordinal)
{
return false;
var nullMask = NativeMethods.Query.DuckDBNullmaskData(currentResult, ordinal);
return Marshal.ReadByte(nullMask, currentRow) != 0;
var validityMaskEntryIndex = currentRow / 64;
var validityBitIndex = currentRow % 64;

var validityMaskEntryPtr = vectorValidityMask[ordinal] + validityMaskEntryIndex;
var validityBit = 1ul << validityBitIndex;

var isValid = (Marshal.PtrToStructure<ulong>(validityMaskEntryPtr) & validityBit) != 0;
return !isValid;
}

public override int FieldCount => fieldCount;
Expand All @@ -329,11 +341,11 @@ public override bool IsDBNull(int ordinal)
public override bool NextResult()
{
currentResultIndex++;

if (currentResultIndex < queryResults.Count)
{
currentResult = queryResults[currentResultIndex];

InitReaderData();
return true;
}
Expand Down

0 comments on commit ec81162

Please sign in to comment.