OSDK - Group By Null Unexpected Behavior

Let’s say I have an Object Type Car

With two cars:

miles: 3, Name: Fiat, Old Name: Null

miles: 5, Name: Opel, Old Name: Null

Now I perform such an aggregation:

cars.aggregate({

        $select: {

"miles:avg": "unordered"

        },

        $groupBy: {

            oldName: "exact",

            name: "exact",

        }

    })


I get as result: [] No results.

I would expect something like:

[ miles: 3, Name: Fiat, Old Name: Null, 

  miles: 5, Name: Opel, Old Name: Null]

I find it very confusing if it does not aggregate also over the null values.

Is this the expected behavior or a bug? Also is there a setting to also aggregate over null values?

Thank you

I’m on OSDK Typescript 2.5.

1 Like

As additional info:, Such a query returns results (because no Null Value):

cars.aggregate({
$select: {
"miles:avg": "unordered"
},
$groupBy: {
name: "exact",
}
})

Hi @emmanuel1 !

It’s expected behavior, and the fact that you tried to remove oldName from the $groupBy (the field that contains null), the aggregation works fine. This confirms the behavior

Solution 1

Instead of leaving oldName as null, replace it with a recognizable placeholder value:

// When storing cars without an old name:
{ miles: 3, name: "Fiat", oldName: "N/A" }
{ miles: 5, name: "Opel", oldName: "N/A" }

After the groupBy replace it again to Null if you want

Solution 2

Replace null values with empty strings

// When storing cars:
{ miles: 3, name: "Fiat", oldName: "" }
{ miles: 5, name: "Opel", oldName: "" }

Hope this solution fits to you!

In addition to @junjiewu’s solution, the TS 2.5 includes a way to include null values in the aggregations by supplying either a default value or by allowing nulls to be the keys of the returned bucket.

For example, the below query

cars.aggregate({

        $select: {

"miles:avg": "unordered"

        },

        $groupBy: {

            oldName: { "$exact": { $limit: 300, $includeNullValue: true } },

            name: "exact",

        }

    })

should return your expected result

2 Likes

Hi! @nihalb do you also have a solution for this one? https://community.palantir.com/t/osdk-aggregation-sorting/5844 Thank you