(table_source)
| 167 | |
| 168 | |
| 169 | def test_aggregate_scalar(table_source): |
| 170 | decl = Declaration.from_sequence([ |
| 171 | table_source, |
| 172 | Declaration("aggregate", AggregateNodeOptions([("a", "sum", None, "a_sum")])) |
| 173 | ]) |
| 174 | result = decl.to_table() |
| 175 | assert result.schema.names == ["a_sum"] |
| 176 | assert result["a_sum"].to_pylist() == [6] |
| 177 | |
| 178 | # with options class |
| 179 | table = pa.table({'a': [1, 2, None]}) |
| 180 | aggr_opts = AggregateNodeOptions( |
| 181 | [("a", "sum", pc.ScalarAggregateOptions(skip_nulls=False), "a_sum")] |
| 182 | ) |
| 183 | decl = Declaration.from_sequence([ |
| 184 | Declaration("table_source", TableSourceNodeOptions(table)), |
| 185 | Declaration("aggregate", aggr_opts), |
| 186 | ]) |
| 187 | result = decl.to_table() |
| 188 | assert result.schema.names == ["a_sum"] |
| 189 | assert result["a_sum"].to_pylist() == [None] |
| 190 | |
| 191 | # test various ways of specifying the target column |
| 192 | for target in ["a", field("a"), 0, field(0), ["a"], [field("a")], [0]]: |
| 193 | aggr_opts = AggregateNodeOptions([(target, "sum", None, "a_sum")]) |
| 194 | decl = Declaration.from_sequence( |
| 195 | [table_source, Declaration("aggregate", aggr_opts)] |
| 196 | ) |
| 197 | result = decl.to_table() |
| 198 | assert result.schema.names == ["a_sum"] |
| 199 | assert result["a_sum"].to_pylist() == [6] |
| 200 | |
| 201 | # proper error when specifying the wrong number of target columns |
| 202 | aggr_opts = AggregateNodeOptions([(["a", "b"], "sum", None, "a_sum")]) |
| 203 | decl = Declaration.from_sequence( |
| 204 | [table_source, Declaration("aggregate", aggr_opts)] |
| 205 | ) |
| 206 | with pytest.raises( |
| 207 | ValueError, match="Function 'sum' accepts 1 arguments but 2 passed" |
| 208 | ): |
| 209 | _ = decl.to_table() |
| 210 | |
| 211 | # proper error when using hash aggregation without keys |
| 212 | aggr_opts = AggregateNodeOptions([("a", "hash_sum", None, "a_sum")]) |
| 213 | decl = Declaration.from_sequence( |
| 214 | [table_source, Declaration("aggregate", aggr_opts)] |
| 215 | ) |
| 216 | with pytest.raises(ValueError, match="is a hash aggregate function"): |
| 217 | _ = decl.to_table() |
| 218 | |
| 219 | |
| 220 | def test_aggregate_hash(): |
nothing calls this directly
no test coverage detected