• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / a4c010ba-78be-4818-8e6f-1da08c6af280

31 Aug 2023 11:59PM UTC coverage: 70.992% (-10.6%) from 81.552%
a4c010ba-78be-4818-8e6f-1da08c6af280

push

circle-ci

web-flow
Merge branch 'staging' into evadb_staging

54 of 54 new or added lines in 3 files covered. (100.0%)

8020 of 11297 relevant lines covered (70.99%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/evadb/expression/aggregation_expression.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
from evadb.expression.abstract_expression import (
1✔
16
    AbstractExpression,
17
    ExpressionReturnType,
18
    ExpressionType,
19
)
20
from evadb.models.storage.batch import Batch
1✔
21

22

23
class AggregationExpression(AbstractExpression):
1✔
24
    def __init__(
1✔
25
        self,
26
        exp_type: ExpressionType,
27
        left: AbstractExpression,
28
        right: AbstractExpression,
29
    ):
30
        children = []
1✔
31
        if left is not None:
1✔
32
            children.append(left)
×
33
        if right is not None:
1✔
34
            children.append(right)
1✔
35
        super().__init__(
1✔
36
            exp_type, rtype=ExpressionReturnType.INTEGER, children=children
37
        )  # can also be a float
38

39
    def evaluate(self, *args, **kwargs):
1✔
40
        batch: Batch = self.get_child(0).evaluate(*args, **kwargs)
1✔
41
        if self.etype == ExpressionType.AGGREGATION_FIRST:
1✔
42
            batch = batch[0]
1✔
43
        elif self.etype == ExpressionType.AGGREGATION_LAST:
1✔
44
            batch = batch[-1]
1✔
45
        elif self.etype == ExpressionType.AGGREGATION_SEGMENT:
1✔
46
            batch = Batch.stack(batch)
1✔
47
        elif self.etype == ExpressionType.AGGREGATION_SUM:
1✔
48
            batch.aggregate("sum")
×
49
        elif self.etype == ExpressionType.AGGREGATION_COUNT:
1✔
50
            batch.aggregate("count")
1✔
51
        elif self.etype == ExpressionType.AGGREGATION_AVG:
1✔
52
            batch.aggregate("mean")
1✔
53
        elif self.etype == ExpressionType.AGGREGATION_MIN:
×
54
            batch.aggregate("min")
×
55
        elif self.etype == ExpressionType.AGGREGATION_MAX:
×
56
            batch.aggregate("max")
×
57
        batch.reset_index()
1✔
58

59
        column_name = self.etype.name
1✔
60
        if column_name.find("AGGREGATION_") != -1:
1✔
61
            # AGGREGATION_MAX -> MAX
62
            updated_column_name = column_name.replace("AGGREGATION_", "")
1✔
63
            batch.modify_column_alias(updated_column_name)
1✔
64

65
        # TODO: Raise exception if data type doesn't match
66
        return batch
1✔
67

68
    def get_symbol(self) -> str:
1✔
69
        if self.etype == ExpressionType.AGGREGATION_FIRST:
×
70
            return "FIRST"
×
71
        if self.etype == ExpressionType.AGGREGATION_LAST:
×
72
            return "LAST"
×
73
        if self.etype == ExpressionType.AGGREGATION_SEGMENT:
×
74
            return "SEGMENT"
×
75
        if self.etype == ExpressionType.AGGREGATION_SUM:
×
76
            return "SUM"
×
77
        elif self.etype == ExpressionType.AGGREGATION_COUNT:
×
78
            return "COUNT"
×
79
        elif self.etype == ExpressionType.AGGREGATION_AVG:
×
80
            return "AVG"
×
81
        elif self.etype == ExpressionType.AGGREGATION_MIN:
×
82
            return "MIN"
×
83
        elif self.etype == ExpressionType.AGGREGATION_MAX:
×
84
            return "MAX"
×
85
        else:
86
            raise NotImplementedError
87

88
    def signature(self) -> str:
1✔
89
        child_sigs = []
×
90
        for child in self.children:
×
91
            child_sigs.append(child.signature())
×
92
        return f"{self.get_symbol().lower()}({','.join(child_sigs)})"
×
93

94
    def __str__(self) -> str:
1✔
95
        expr_str = ""
×
96
        if self.etype:
×
97
            expr_str = f"{str(self.get_symbol())}()"
×
98
        return expr_str
×
99

100
    def __eq__(self, other):
1✔
101
        is_subtree_equal = super().__eq__(other)
×
102
        if not isinstance(other, AggregationExpression):
×
103
            return False
×
104
        return is_subtree_equal and self.etype == other.etype
×
105

106
    def __hash__(self) -> int:
1✔
107
        return hash(
1✔
108
            (
109
                super().__hash__(),
110
                self.etype,
111
            )
112
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc