(table_source)
| 258 | |
| 259 | |
| 260 | def test_aggregate_scalar(table_source): |
| 261 | decl = Declaration.from_sequence([ |
| 262 | table_source, |
| 263 | Declaration("aggregate", AggregateNodeOptions([("a", "sum", None, "a_sum")])) |
| 264 | ]) |
| 265 | result = decl.to_table() |
| 266 | assert result.schema.names == ["a_sum"] |
| 267 | assert result["a_sum"].to_pylist() == [6] |
| 268 | |
| 269 | # with options class |
| 270 | table = pa.table({'a': [1, 2, None]}) |
| 271 | aggr_opts = AggregateNodeOptions( |
| 272 | [("a", "sum", pc.ScalarAggregateOptions(skip_nulls=False), "a_sum")] |
| 273 | ) |
| 274 | decl = Declaration.from_sequence([ |
| 275 | Declaration("table_source", TableSourceNodeOptions(table)), |
| 276 | Declaration("aggregate", aggr_opts), |
| 277 | ]) |
| 278 | result = decl.to_table() |
| 279 | assert result.schema.names == ["a_sum"] |
| 280 | assert result["a_sum"].to_pylist() == [None] |
| 281 | |
| 282 | # test various ways of specifying the target column |
| 283 | for target in ["a", field("a"), 0, field(0), ["a"], [field("a")], [0]]: |
| 284 | aggr_opts = AggregateNodeOptions([(target, "sum", None, "a_sum")]) |
| 285 | decl = Declaration.from_sequence( |
| 286 | [table_source, Declaration("aggregate", aggr_opts)] |
| 287 | ) |
| 288 | result = decl.to_table() |
| 289 | assert result.schema.names == ["a_sum"] |
| 290 | assert result["a_sum"].to_pylist() == [6] |
| 291 | |
| 292 | # proper error when specifying the wrong number of target columns |
| 293 | aggr_opts = AggregateNodeOptions([(["a", "b"], "sum", None, "a_sum")]) |
| 294 | decl = Declaration.from_sequence( |
| 295 | [table_source, Declaration("aggregate", aggr_opts)] |
| 296 | ) |
| 297 | with pytest.raises( |
| 298 | ValueError, match="Function 'sum' accepts 1 arguments but 2 passed" |
| 299 | ): |
| 300 | _ = decl.to_table() |
| 301 | |
| 302 | # proper error when using hash aggregation without keys |
| 303 | aggr_opts = AggregateNodeOptions([("a", "hash_sum", None, "a_sum")]) |
| 304 | decl = Declaration.from_sequence( |
| 305 | [table_source, Declaration("aggregate", aggr_opts)] |
| 306 | ) |
| 307 | with pytest.raises(ValueError, match="is a hash aggregate function"): |
| 308 | _ = decl.to_table() |
| 309 | |
| 310 | |
| 311 | def test_aggregate_hash(): |
nothing calls this directly
no test coverage detected