question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CreateAggregate state is shared, breaking multiple use of an aggregate function in the same query

See original GitHub issue
using SqliteConnection connection = new SqliteConnection(new SqliteConnectionStringBuilder
{
    DataSource = "A",
    Mode = SqliteOpenMode.Memory
}.ToString());
connection.Open();

using (var command = connection.CreateCommand())
{
    command.CommandText = "CREATE TABLE [test](a, b, c);";
    command.ExecuteNonQuery();

    command.CommandText = "INSERT INTO [test](a, b, c) VALUES(1, 2, 3)";
    command.ExecuteNonQuery();
    command.ExecuteNonQuery();
}
connection.CreateAggregate(
    "testCount",
    new AggClass(),
    (AggClass agg, int number) =>
    {
        agg.Counter++;
        return agg;
    },
    agg => agg.Counter);
using (var command = connection.CreateCommand())
{
    command.CommandText = "SELECT testCount(a), testCount(b), count(a), count(b) FROM [test];";
    using var reader = command.ExecuteReader();
    reader.Read();
    Console.WriteLine((reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3))); // (4, 4, 2, 2)
}

Microsoft.Data.Sqlite version: 5.0.10 Target framework: (e.g. .NET 5.0) .NET 5.0 Operating system: Windows 10 / MacOS 11.5.2

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wzchuacommented, Sep 25, 2021

It doesn’t work with struct.

as a workaround I’m using something like this

var handle = connection.Handle;
var map = new Dictionary<sqlite3_context, AggClass>();
raw.sqlite3_create_function(handle, "testCount2", 1, map,
    static (ctx, data, values) =>
    {
        var map = (Dictionary<sqlite3_context, AggClass>)data;
        if (!map.TryGetValue(ctx, out AggClass? i))
        {
            i = new AggClass();
            map.Add(ctx, i);
        }

        i.Counter++;
    },
     static(ctx, data) =>
    {
        var map = (Dictionary<sqlite3_context, AggClass>)data;
        if (!map.Remove(ctx, out AggClass? i))
        {
            raw.sqlite3_result_int(ctx, 0);
            return;
        }
        
        raw.sqlite3_result_int(ctx, i.Counter);
    });
0reactions
bricelamcommented, Apr 4, 2023

@pimbrouwers https://github.com/dotnet/efcore/issues/26070#issuecomment-926817699 gives a sketch of the solution if you’re interested in giving it a go.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Combine Two Aggregate Functions in SQL
Learn how to combine two aggregate functions in one SQL query by using a subquery or a Common Table Expression (CTE).
Read more >
Learn SQL: Aggregate Functions
Aggregate functions are a very powerful tool in databases. They serve the same purpose as their equivalents in MS Excel.
Read more >
Overview of Aggregate Functions
An aggregate function performs a task in relation to one or more values from a single column and returns a single value. The...
Read more >
Understanding the SQL SUM() function and its use cases
Today, I will describe the SQL SUM () function along with its use cases in this article. There are various mathematical calculations we...
Read more >
Aggregate and Group Data to a Different Grain
You can use the following aggregate functions on measure columns: sum, unique, avg, count, max, and min. To prevent double counting, exclude aggregated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found