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

georgia-tech-db / eva / #754

04 Sep 2023 09:54PM UTC coverage: 74.807% (-5.5%) from 80.336%
#754

push

circle-ci

jiashenC
update case

8727 of 11666 relevant lines covered (74.81%)

0.75 hits per line

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

90.38
/evadb/parser/lark_visitor/_functions.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

16
from lark import Token, Tree
1✔
17

18
from evadb.expression.abstract_expression import ExpressionType
1✔
19
from evadb.expression.aggregation_expression import AggregationExpression
1✔
20
from evadb.expression.constant_value_expression import ConstantValueExpression
1✔
21
from evadb.expression.function_expression import FunctionExpression
1✔
22
from evadb.expression.tuple_value_expression import TupleValueExpression
1✔
23
from evadb.parser.create_function_statement import CreateFunctionStatement
1✔
24

25

26
##################################################################
27
# Functions - Functions, Aggregate Windowed functions
28
##################################################################
29
class Functions:
1✔
30
    def function(self, tree):
1✔
31
        function_name = None
1✔
32
        function_output = None
1✔
33
        function_args = None
1✔
34

35
        for child in tree.children:
1✔
36
            if isinstance(child, Token):
1✔
37
                if child.value == "*":
1✔
38
                    function_args = [TupleValueExpression(name="*")]
1✔
39
            if isinstance(child, Tree):
1✔
40
                if child.data == "simple_id":
1✔
41
                    function_name = self.visit(child)
1✔
42
                elif child.data == "dotted_id":
1✔
43
                    function_output = self.visit(child)
1✔
44
                elif child.data == "function_args":
1✔
45
                    function_args = self.visit(child)
1✔
46

47
        func_expr = FunctionExpression(None, name=function_name, output=function_output)
1✔
48
        for arg in function_args:
1✔
49
            func_expr.append_child(arg)
1✔
50

51
        return func_expr
1✔
52

53
    def function_args(self, tree):
1✔
54
        args = []
1✔
55
        for child in tree.children:
1✔
56
            if isinstance(child, Tree):
1✔
57
                args.append(self.visit(child))
1✔
58
        return args
1✔
59

60
    # Create function
61
    def create_function(self, tree):
1✔
62
        function_name = None
1✔
63
        if_not_exists = False
1✔
64
        input_definitions = []
1✔
65
        output_definitions = []
1✔
66
        impl_path = None
1✔
67
        function_type = None
1✔
68
        query = None
1✔
69
        metadata = []
1✔
70

71
        create_definitions_index = 0
1✔
72
        for child in tree.children:
1✔
73
            if isinstance(child, Tree):
1✔
74
                if child.data == "function_name":
1✔
75
                    function_name = self.visit(child)
1✔
76
                elif child.data == "if_not_exists":
1✔
77
                    if_not_exists = True
1✔
78
                elif child.data == "create_definitions":
1✔
79
                    # There should be 2 createDefinition
80
                    # idx 0 describing function INPUT
81
                    # idx 1 describing function OUTPUT
82
                    if create_definitions_index == 0:
1✔
83
                        input_definitions = self.visit(child)
1✔
84
                        create_definitions_index += 1
1✔
85
                    elif create_definitions_index == 1:
1✔
86
                        output_definitions = self.visit(child)
1✔
87
                elif child.data == "function_type":
1✔
88
                    function_type = self.visit(child)
1✔
89
                elif child.data == "function_impl":
1✔
90
                    impl_path = self.visit(child).value
1✔
91
                elif child.data == "simple_select":
1✔
92
                    query = self.visit(child)
×
93
                elif child.data == "function_metadata":
1✔
94
                    # Each function metadata is a key value pair
95
                    key_value_pair = self.visit(child)
1✔
96
                    # value can be an integer or string
97
                    value = key_value_pair[1]
1✔
98
                    if isinstance(value, ConstantValueExpression):
1✔
99
                        value = value.value
1✔
100
                    metadata.append((key_value_pair[0].value, value)),
1✔
101

102
        return CreateFunctionStatement(
1✔
103
            function_name,
104
            if_not_exists,
105
            impl_path,
106
            input_definitions,
107
            output_definitions,
108
            function_type,
109
            query,
110
            metadata,
111
        )
112

113
    def get_aggregate_function_type(self, agg_func_name):
1✔
114
        agg_func_type = None
1✔
115
        if agg_func_name == "COUNT":
1✔
116
            agg_func_type = ExpressionType.AGGREGATION_COUNT
×
117
        elif agg_func_name == "MIN":
1✔
118
            agg_func_type = ExpressionType.AGGREGATION_MIN
1✔
119
        elif agg_func_name == "MAX":
1✔
120
            agg_func_type = ExpressionType.AGGREGATION_MAX
1✔
121
        elif agg_func_name == "SUM":
1✔
122
            agg_func_type = ExpressionType.AGGREGATION_SUM
1✔
123
        elif agg_func_name == "AVG":
1✔
124
            agg_func_type = ExpressionType.AGGREGATION_AVG
×
125
        elif agg_func_name == "FIRST":
1✔
126
            agg_func_type = ExpressionType.AGGREGATION_FIRST
1✔
127
        elif agg_func_name == "LAST":
1✔
128
            agg_func_type = ExpressionType.AGGREGATION_LAST
×
129
        elif agg_func_name == "SEGMENT":
1✔
130
            agg_func_type = ExpressionType.AGGREGATION_SEGMENT
×
131
        return agg_func_type
1✔
132

133
    def aggregate_windowed_function(self, tree):
1✔
134
        agg_func_arg = None
1✔
135
        agg_func_name = None
1✔
136

137
        for child in tree.children:
1✔
138
            if isinstance(child, Tree):
1✔
139
                if child.data == "function_arg":
1✔
140
                    agg_func_arg = self.visit(child)
1✔
141
                elif child.data == "aggregate_function_name":
1✔
142
                    agg_func_name = self.visit(child).value
1✔
143
            elif isinstance(child, Token):
×
144
                token = child.value
×
145
                # Support for COUNT(*)
146
                if token != "*":
×
147
                    agg_func_name = token
×
148
                else:
149
                    agg_func_arg = TupleValueExpression(name="id")
×
150

151
        agg_func_type = self.get_aggregate_function_type(agg_func_name)
1✔
152
        agg_expr = AggregationExpression(agg_func_type, None, agg_func_arg)
1✔
153
        return agg_expr
1✔
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

© 2025 Coveralls, Inc