Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
15 changes: 4 additions & 11 deletions src/Cassandra.IntegrationTests/Cassandra.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<SignAssembly>true</SignAssembly>
<PackageId>Cassandra.IntegrationTests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>7.1</LangVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net4\d'))">
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
Expand Down Expand Up @@ -63,7 +63,7 @@
<SetTargetFramework>TargetFramework=net8</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\Cassandra\Cassandra.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
<SetTargetFramework>TargetFramework=netstandard2.1</SetTargetFramework>
</ProjectReference>
</ItemGroup>

Expand All @@ -75,7 +75,7 @@
<SetTargetFramework>TargetFramework=net7</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\Cassandra\Cassandra.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
<SetTargetFramework>TargetFramework=netstandard2.1</SetTargetFramework>
</ProjectReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6' ">
Expand All @@ -86,7 +86,7 @@
<SetTargetFramework>TargetFramework=net6</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\Cassandra\Cassandra.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
<SetTargetFramework>TargetFramework=netstandard2.1</SetTargetFramework>
</ProjectReference>
</ItemGroup>

Expand All @@ -102,20 +102,13 @@
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="NunitXml.TestLogger" Version="2.1.41" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.0.2" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="XunitXml.TestLogger" Version="2.1.26" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' Or '$(TargetFramework)' == 'net472' Or '$(TargetFramework)' == 'net481' ">
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6' Or '$(TargetFramework)' == 'net7' Or '$(TargetFramework)' == 'net8'">
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' Or '$(TargetFramework)' == 'net472' Or '$(TargetFramework)' == 'net481' ">
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.2.20220510" />
<Reference Include="System" />
Expand Down
13 changes: 7 additions & 6 deletions src/Cassandra.IntegrationTests/Core/PreparedStatementsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,12 @@ public void Bound_Manual_Paging()
Assert.False(rs.AutoPage);
Assert.NotNull(rs.PagingState);
//Dequeue all via Linq
var ids = rs.Select(r => r.GetValue<Guid>("id")).ToList();
var ids = Enumerable.Select(rs, r => r.GetValue<Guid>("id")).ToList();
Assert.AreEqual(pageSize, ids.Count);
//Retrieve the next page
var rs2 = Session.Execute(ps.Bind().SetAutoPage(false).SetPagingState(rs.PagingState));
Assert.Null(rs2.PagingState);
var ids2 = rs2.Select(r => r.GetValue<Guid>("id")).ToList();
var ids2 = Enumerable.Select(rs2, r => r.GetValue<Guid>("id")).ToList();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RowSet now implements both IEnumerable and IAsyncEnumerable which both have a select method. I double checked if it was an issue with my AsyncEnumerable implementation but if you create a new .NET 10 project (that has AsyncEnumerable built-in) and have a class that implements both interface, you'll have this error too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the driver project only targets netstandard 2.1 so it will never have the built in AsyncEnumerable right? Or is this an issue if a client application targeting net10 installs the driver which uses netstandard2.1 and then tries to call Select on the RowSet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the driver project only targets netstandard 2.1 so it will never have the built in AsyncEnumerable right?

Correct, but I provided my own implementation of AsyncEnumerable. It's internal but internals are visible to the test assemblies.

Or is this an issue if a client application targeting net10 installs the driver which uses netstandard2.1 and then tries to call Select on the RowSet?

Yes this could be an issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this an issue if a client application targeting net10 installs the driver which uses netstandard2.1 and then tries to call Select on the RowSet?

Yes this could be an issue.

The custom AsyncEnumerable implementation is internal, how would it affect applications installing it?

Copy link
Collaborator

@joao-r-reis joao-r-reis Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the driver project only targets netstandard 2.1 so it will never have the built in AsyncEnumerable right?

Correct, but I provided my own implementation of AsyncEnumerable. It's internal but internals are visible to the test assemblies.

But the test assemblies don't target net10 currently so it shouldn't be an issue now right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will indeed not impact projects referencing Cassandra because it's internal. It will impact Cassandra + test projects because they all have access to the this internal class.

Assert.AreEqual(totalRowLength - pageSize, ids2.Count);
Assert.AreEqual(totalRowLength, ids.Union(ids2).Count());
}
Expand Down Expand Up @@ -1047,10 +1047,11 @@ public void Batch_PreparedStatement_With_Unprepared_Flow()
session.Execute(new BatchStatement()
.Add(ps1.Bind(3, "label3_u"))
.Add(ps2.Bind("label4_u", 4)));
var result = session.Execute("SELECT id, label FROM tbl_unprepared_flow")
.Select(r => new object[] { r.GetValue<int>(0), r.GetValue<string>(1) })
.OrderBy(arr => (int)arr[0])
.ToArray();
var result = Enumerable.Select(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session.Execute("SELECT id, label FROM tbl_unprepared_flow"),
r => new object[] { r.GetValue<int>(0), r.GetValue<string>(1) })
.OrderBy(arr => (int)arr[0])
.ToArray();
Assert.AreEqual(Enumerable.Range(1, 4).Select(i => new object[] { i, $"label{i}_u" }), result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void RandomValuesTest()

var selectQuery = $"SELECT id, timeuuid_sample, {GetToDateFunction()}(timeuuid_sample) FROM {AllTypesTableName} LIMIT 10000";
Assert.DoesNotThrow(() =>
Session.Execute(selectQuery).Select(r => r.GetValue<TimeUuid>("timeuuid_sample")).ToArray());
Enumerable.Select(Session.Execute(selectQuery), r => r.GetValue<TimeUuid>("timeuuid_sample")).ToArray());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

[Test]
Expand Down
26 changes: 26 additions & 0 deletions src/Cassandra.IntegrationTests/Mapping/Tests/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cassandra.Data.Linq;
using Cassandra.IntegrationTests.Mapping.Structures;
using Cassandra.IntegrationTests.TestBase;
Expand Down Expand Up @@ -97,6 +98,31 @@ public void FetchAsync_Using_Select_Cql_And_PageSize()
CollectionAssert.AreEquivalent(ids, authors.Select(a => a.AuthorId));
}

#if NET6_0_OR_GREATER
[Test]
public async Task FetchAsAsyncEnumerable_Using_Select_Cql_And_PageSize()
{
var table = new Table<Author>(_session, new MappingConfiguration());
await table.CreateAsync();

var mapper = new Mapper(_session, new MappingConfiguration().Define(new FluentUserMapping()));
var ids = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };

await mapper.InsertAsync(new Author { AuthorId = ids[0] });
await mapper.InsertAsync(new Author { AuthorId = ids[1] });

var authors = new List<Author>();
await foreach (var author in mapper.FetchAsAsyncEnumerable<Author>(Cql.New("SELECT * FROM " + table.Name)
.WithOptions(o => o.SetPageSize(int.MaxValue))))
{
authors.Add(author);
}

Assert.AreEqual(2, authors.Count);
CollectionAssert.AreEquivalent(ids, authors.Select(a => a.AuthorId));
}
#endif

/// <summary>
/// Successfully Fetch mapped records by passing in a Cql Object
/// </summary>
Expand Down
7 changes: 1 addition & 6 deletions src/Cassandra.Tests/Cassandra.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<SignAssembly>true</SignAssembly>
<PackageId>Cassandra.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>7.1</LangVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net4\d'))">
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
Expand Down Expand Up @@ -48,7 +48,6 @@
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="NunitXml.TestLogger" Version="2.1.41" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<PackageReference Include="XunitXml.TestLogger" Version="2.1.26" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand All @@ -58,10 +57,6 @@
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6' Or '$(TargetFramework)' == 'net7' Or '$(TargetFramework)' == 'net8' ">
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
<PackageReference Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Extensions\Cassandra.OpenTelemetry\Cassandra.OpenTelemetry.csproj" />
</ItemGroup>
Expand Down
14 changes: 8 additions & 6 deletions src/Cassandra/Cassandra.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<VersionPrefix>3.22.0</VersionPrefix>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<Authors>DataStax</Authors>
<TargetFrameworks Condition="'$(BuildCoreOnly)' != 'True'">net452;netstandard2.0</TargetFrameworks>
<TargetFramework Condition="'$(BuildCoreOnly)' == 'True'">netstandard2.0</TargetFramework>
<TargetFrameworks Condition="'$(BuildCoreOnly)' != 'True'">net452;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition="'$(BuildCoreOnly)' == 'True'">netstandard2.0;netstandard2.1</TargetFrameworks>
<NoWarn>$(NoWarn);1591</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
Expand All @@ -24,16 +24,16 @@
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<RepositoryUrl>https://github.com/datastax/csharp-driver</RepositoryUrl>
<PackageProjectUrl>https://github.com/datastax/csharp-driver</PackageProjectUrl>
<LangVersion>7.1</LangVersion>
<NuGetAudit>false</NuGetAudit>
<LangVersion>8.0</LangVersion>
<NuGetAudit>false</NuGetAudit>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net4\d'))">
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d$'))">
<DefineConstants>$(DefineConstants);NETCOREAPP</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
</ItemGroup>
Expand All @@ -44,9 +44,11 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" NoWarn="NU1903" />
<PackageReference Include="System.Management" Version="4.7.0" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.0.0" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.6.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
Expand Down
2 changes: 1 addition & 1 deletion src/Cassandra/Data/Linq/ClientProjectionCqlQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal override IEnumerable<TResult> AdaptResult(string cql, RowSet rs)
if (!_canCompile)
{
var mapper = MapperFactory.GetMapperWithProjection<TResult>(cql, rs, _projectionExpression);
result = rs.Select(mapper);
result = Enumerable.Select(rs, mapper);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't comment more on this to avoid spamming the same comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cassandra/Data/Linq/CqlConditionalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected internal override string GetCql(out object[] values)
Cql.New(cql, values).WithExecutionProfile(executionProfile)).ConfigureAwait(false);
this.CopyQueryPropertiesTo(stmt);
var rs = await session.ExecuteAsync(stmt, executionProfile).ConfigureAwait(false);
return AppliedInfo<TEntity>.FromRowSet(_mapperFactory, cql, rs);
return await AppliedInfo<TEntity>.FromRowSetAsync(_mapperFactory, cql, rs).ConfigureAwait(false);
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions src/Cassandra/Data/Linq/CqlQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,18 @@ public async Task<IPage<TEntity>> ExecutePagedAsync(string executionProfile)
{
throw new ArgumentNullException(nameof(executionProfile));
}

SetAutoPage(false);
var visitor = new CqlExpressionVisitor(PocoData, Table.Name, Table.KeyspaceName);
var cql = visitor.GetSelect(Expression, out object[] values);
var rs = await InternalExecuteWithProfileAsync(executionProfile, cql, values).ConfigureAwait(false);
var mapper = MapperFactory.GetMapper<TEntity>(cql, rs);
return new Page<TEntity>(rs.Select(mapper), PagingState, rs.PagingState);
#if NETSTANDARD2_1_OR_GREATER
var items = await AsyncEnumerable.Select(rs, mapper).ToListAsync().ConfigureAwait(false);
#else
var items = Enumerable.Select(rs, mapper).ToList();
#endif
return new Page<TEntity>(items, PagingState, rs.PagingState);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Cassandra/Data/Linq/CqlQueryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected async Task<RowSet> InternalExecuteWithProfileAsync(string executionPro
internal virtual IEnumerable<TEntity> AdaptResult(string cql, RowSet rs)
{
var mapper = MapperFactory.GetMapper<TEntity>(cql, rs);
return rs.Select(mapper);
return Enumerable.Select(rs ,mapper);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Cassandra/DataStax/Graph/GraphResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IEnumerator<GraphNode> GetEnumerator()
/// </summary>
private IEnumerable<GraphNode> YieldNodes()
{
foreach (var node in _rs.Select(_factory))
foreach (var node in Enumerable.Select(_rs, _factory))
{
for (var i = 0; i < node.Bulk; i++)
{
Expand Down
111 changes: 111 additions & 0 deletions src/Cassandra/Helpers/AsyncEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (C) DataStax Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if NETSTANDARD2_1_OR_GREATER && !NET10_OR_GREATER // These methods are implemented in .NET 10.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

// ReSharper disable once CheckNamespace
namespace System.Linq
{
internal static class AsyncEnumerable
{
public static async ValueTask<TSource> FirstAsync<TSource>(
this IAsyncEnumerable<TSource> source,
CancellationToken cancellationToken = default)
{
await using var e = source.GetAsyncEnumerator(cancellationToken);

if (!await e.MoveNextAsync())
{
throw new InvalidOperationException("Sequence contains no elements");
}

return e.Current;
}

public static async ValueTask<TSource> FirstOrDefaultAsync<TSource>(
this IAsyncEnumerable<TSource> source,
CancellationToken cancellationToken = default)
{
await using var e = source.GetAsyncEnumerator(cancellationToken);
return await e.MoveNextAsync() ? e.Current : default;
}

public static async ValueTask<TSource> SingleAsync<TSource>(
this IAsyncEnumerable<TSource> source,
CancellationToken cancellationToken = default)
{
await using var e = source.GetAsyncEnumerator(cancellationToken);

if (!await e.MoveNextAsync())
{
throw new InvalidOperationException("Sequence contains no elements");
}

TSource result = e.Current;
if (await e.MoveNextAsync())
{
throw new InvalidOperationException("Sequence contains more than one element");
}

return result;
}

public static async ValueTask<TSource> SingleOrDefaultAsync<TSource>(
this IAsyncEnumerable<TSource> source,
CancellationToken cancellationToken = default)
{
await using var e = source.GetAsyncEnumerator(cancellationToken);

if (!await e.MoveNextAsync())
{
return default;
}

TSource result = e.Current;
if (await e.MoveNextAsync())
{
throw new InvalidOperationException("Sequence contains more than one element");
}

return result;
}

public static async IAsyncEnumerable<TResult> Select<TSource, TResult>(
this IAsyncEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
await foreach (TSource element in source)
{
yield return selector(element);
}
}

public static async ValueTask<List<TSource>> ToListAsync<TSource>(
this IAsyncEnumerable<TSource> source,
CancellationToken cancellationToken = default)
{
var list = new List<TSource>();
await foreach (TSource element in source.WithCancellation(cancellationToken))
{
list.Add(element);
}

return list;
}
}
}
#endif
2 changes: 2 additions & 0 deletions src/Cassandra/Helpers/PlatformHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static string GetTargetFramework()
return ".NET Framework 4.5.2";
#elif NETSTANDARD2_0
return ".NET Standard 2.0";
#elif NETSTANDARD2_1
return ".NET Standard 2.1";
#else
return null;
#endif
Expand Down
Loading