Coverage Report

Created: 2024-10-13 08:39

/Users/andrewlamb/Software/datafusion/datafusion/functions-aggregate/src/grouping.rs
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
//! Defines physical expressions that can evaluated at runtime during query execution
19
20
use std::any::Any;
21
use std::fmt;
22
23
use arrow::datatypes::DataType;
24
use arrow::datatypes::Field;
25
use datafusion_common::{not_impl_err, Result};
26
use datafusion_expr::function::AccumulatorArgs;
27
use datafusion_expr::function::StateFieldsArgs;
28
use datafusion_expr::utils::format_state_name;
29
use datafusion_expr::{Accumulator, AggregateUDFImpl, Signature, Volatility};
30
31
make_udaf_expr_and_func!(
32
    Grouping,
33
    grouping,
34
    expression,
35
    "Returns 1 if the data is aggregated across the specified column or 0 for not aggregated in the result set.",
36
    grouping_udaf
37
);
38
39
pub struct Grouping {
40
    signature: Signature,
41
}
42
43
impl fmt::Debug for Grouping {
44
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
45
0
        f.debug_struct("Grouping")
46
0
            .field("name", &self.name())
47
0
            .field("signature", &self.signature)
48
0
            .finish()
49
0
    }
50
}
51
52
impl Default for Grouping {
53
0
    fn default() -> Self {
54
0
        Self::new()
55
0
    }
56
}
57
58
impl Grouping {
59
    /// Create a new GROUPING aggregate function.
60
0
    pub fn new() -> Self {
61
0
        Self {
62
0
            signature: Signature::any(1, Volatility::Immutable),
63
0
        }
64
0
    }
65
}
66
67
impl AggregateUDFImpl for Grouping {
68
0
    fn as_any(&self) -> &dyn Any {
69
0
        self
70
0
    }
71
72
0
    fn name(&self) -> &str {
73
0
        "grouping"
74
0
    }
75
76
0
    fn signature(&self) -> &Signature {
77
0
        &self.signature
78
0
    }
79
80
0
    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
81
0
        Ok(DataType::Int32)
82
0
    }
83
84
0
    fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
85
0
        Ok(vec![Field::new(
86
0
            format_state_name(args.name, "grouping"),
87
0
            DataType::Int32,
88
0
            true,
89
0
        )])
90
0
    }
91
92
0
    fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
93
0
        not_impl_err!(
94
0
            "physical plan is not yet implemented for GROUPING aggregate function"
95
0
        )
96
0
    }
97
}