Skip to content

Aggregated column using argMaxIf is returning a boolean instead of the column value (aggregated state) #175

@ottony

Description

@ottony

When using AggregateFunction(argMaxIf) the aggregated field returns "true" instead of the original column value. It also invalidates the usability of the instance attribute because it's always set to "true".

This issue does not occur when using argMax instead of argMaxIf.

How to reproduce

Given the following table/model with any value:

CREATE TABLE tests (
  id UUID,
  aggregated_field AggregateFunction(argMaxIf, Nullable(String), DateTime64(3), Bool)
) ENGINE = AggregatingMergeTree ORDER BY (id)


INSERT INTO tests (id, aggregated_field) SELECT
    '468832a5-40c3-4d41-b804-da204abefd06',
    argMaxIfState(toNullable('aggregatedValue'), toDateTime64('2024-10-11 18:01:37.794', 3), true)


   ┌─id───────────────────────────────────┬─aggregated_field──┐
1. │ 468832a5-40c3-4d41-b804-da204abefd06 │ aggregatedValueG| │
   └──────────────────────────────────────┴───────────────────┘

The aggregated field returns "true" instead of the column value / aggregated state:

> Test.find('468832a5-40c3-4d41-b804-da204abefd06').aggregated_field
=> true                                                                        

> Test.pluck(:aggregated_field)
=> [true]

Of course, aggregated fields should be requested using Aggregate Functions, but as it's a column / attribute I think it makes sense to return and keep the original value from the database otherwise it's misleading.

As a consequence, it's not possible to set an instance value for the aggregated field as it would usually be:

> test = Test.new(id: SecureRandom.uuid, aggregated_field: 'some_value')
=> #<Test:0x0000713cc4ed4258 id: "3774134f-db02-44f9-8b6f-5a27ef27b6a7", aggregated_field: true>

> test.aggregated_field
=> true                                                                        

💡 This problem does not occur when using argMax instead of ArgMaxIf. For example, using aggregated_field AggregateFunction(argMax, Nullable(String), DateTime64(3)) instead.

INSERT INTO arg_max_tests (id, aggregated_field) SELECT
  '3870e519-cc81-430a-9400-7a73d35ea49e',
  argMaxState(toNullable('aggregatedValue'), toDateTime64('2024-10-11 18:01:37.794', 3))

   ┌─id───────────────────────────────────┬─aggregated_field──┐
1. │ 3870e519-cc81-430a-9400-7a73d35ea49e │ aggregatedValueG| │
   └──────────────────────────────────────┴───────────────────┘

Fetching it with clickhouse-activerecord returns the right column value / aggregated state and it's possible to use the instance attribute as usual.

> ArgMaxTest.find('3870e519-cc81-430a-9400-7a73d35ea49e').aggregated_field
=> "\u0001\u0010\u0000\u0000\u0000aggregatedValue\u0000\u0001\u0002G\xBE|\x92\u0001\u0000\u0000"

> ArgMaxTest.pluck(:aggregated_field)
=> ["\u0001\u0010\u0000\u0000\u0000aggregatedValue\u0000\u0001\u0002G\xBE|\x92\u0001\u0000\u0000"]

> test = ArgMaxTest.new(id: SecureRandom.uuid, aggregated_field: 'some_value')
=> #<ArgMaxTest:0x000070259c7b4760 id: "df5a52cf-6846-4b42-9907-b25259d09650", aggregated_field: "some_value">

> test.aggregated_field
=> "some_value"

Version:

rails 7.2.1
clickhouse-activerecord 1.1.3
ClickHouse 24.3.5.47.altinitystable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions