Skip to content

Distinguish nils in firstBuffer #3030

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
merged 2 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions enginetest/queries/order_by_group_by_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,30 @@ var OrderByGroupByScriptTests = []ScriptTest{
},
},
},
{
Name: "Group by null = 1",
// https://github.com/dolthub/dolt/issues/9035
SetUpScript: []string{
"create table t0(c0 int, c1 int)",
"insert into t0(c0, c1) values(NULL,1),(1,NULL)",
"create table t1(id int primary key, c0 int, c1 int)",
"insert into t1(id, c0, c1) values(1,NULL,NULL),(2,1,1),(3,1,NULL),(4,2,1),(5,NULL,1)",
},
Assertions: []ScriptTestAssertion{
{
Query: "select t0.c0 = t0.c1 as ref0, sum(1) as ref1 from t0 group by ref0",
Expected: []sql.Row{
{nil, float64(2)},
},
},
{
Query: "select t1.c0 = t1.c1 as ref0, sum(1) as ref1 from t1 group by ref0",
Expected: []sql.Row{
{nil, float64(3)},
{true, float64(1)},
{false, float64(1)},
},
},
},
},
}
11 changes: 7 additions & 4 deletions sql/expression/function/aggregation/unary_agg_buffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,19 @@ func (c *countBuffer) Dispose() {
}

type firstBuffer struct {
val interface{}
expr sql.Expression
val interface{}
// writtenNil means that val is supposed to be nil and should not be overwritten
writtenNil bool
expr sql.Expression
}

func NewFirstBuffer(child sql.Expression) *firstBuffer {
return &firstBuffer{nil, child}
return &firstBuffer{nil, false, child}
}

// Update implements the AggregationBuffer interface.
func (f *firstBuffer) Update(ctx *sql.Context, row sql.Row) error {
if f.val != nil {
if f.val != nil || f.writtenNil {
return nil
}

Expand All @@ -520,6 +522,7 @@ func (f *firstBuffer) Update(ctx *sql.Context, row sql.Row) error {
}

if v == nil {
f.writtenNil = true
return nil
}

Expand Down