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

georgia-tech-db / eva / #840

18 Oct 2023 09:51PM UTC coverage: 68.616% (-9.8%) from 78.391%
#840

push

circle-ci

jiashenC
[BUMP]: v0.3.9+dev

2 of 2 new or added lines in 1 file covered. (100.0%)

8634 of 12583 relevant lines covered (68.62%)

0.69 hits per line

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

88.52
/evadb/executor/orderby_executor.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 typing import Iterator
1✔
16

17
from evadb.database import EvaDBDatabase
1✔
18
from evadb.executor.abstract_executor import AbstractExecutor
1✔
19
from evadb.executor.executor_utils import ExecutorError
1✔
20
from evadb.expression.function_expression import FunctionExpression
1✔
21
from evadb.expression.tuple_value_expression import TupleValueExpression
1✔
22
from evadb.models.storage.batch import Batch
1✔
23
from evadb.parser.types import ParserOrderBySortType
1✔
24
from evadb.plan_nodes.orderby_plan import OrderByPlan
1✔
25

26

27
class OrderByExecutor(AbstractExecutor):
1✔
28
    """
29
    Sort the frames which satisfy the condition
30

31
    Arguments:
32
        node (AbstractPlan): The OrderBy Plan
33

34
    """
35

36
    def __init__(self, db: EvaDBDatabase, node: OrderByPlan):
1✔
37
        super().__init__(db, node)
1✔
38
        self._orderby_list = node.orderby_list
1✔
39
        self._columns = node.columns
1✔
40
        self._sort_types = node.sort_types
1✔
41
        self.batch_sizes = []
1✔
42

43
    def _extract_column_name(self, col):
1✔
44
        col_name = []
1✔
45
        if isinstance(col, TupleValueExpression):
1✔
46
            col_name += [col.col_alias]
1✔
47
        elif isinstance(col, FunctionExpression):
×
48
            col_name += col.col_alias
×
49
        else:
50
            raise ExecutorError(
51
                "Expression type {} is not supported.".format(type(col))
52
            )
53
        return col_name
1✔
54

55
    def extract_column_names(self):
1✔
56
        """extracts the string name of the column"""
57
        # self._columns: List[TupleValueExpression]
58
        col_name_list = []
1✔
59
        for col in self._columns:
1✔
60
            col_name_list += self._extract_column_name(col)
1✔
61
        return col_name_list
1✔
62

63
    def extract_sort_types(self):
1✔
64
        """extracts the sort type for the column"""
65
        # self._sort_types: List[ParserOrderBySortType]
66
        sort_type_bools = []
1✔
67
        for st in self._sort_types:
1✔
68
            if st is ParserOrderBySortType.ASC:
1✔
69
                sort_type_bools.append(True)
1✔
70
            else:
71
                sort_type_bools.append(False)
×
72
        return sort_type_bools
1✔
73

74
    def exec(self, *args, **kwargs) -> Iterator[Batch]:
1✔
75
        child_executor = self.children[0]
1✔
76
        aggregated_batch_list = []
1✔
77

78
        # aggregates the batches into one large batch
79
        for batch in child_executor.exec(**kwargs):
1✔
80
            self.batch_sizes.append(len(batch))
1✔
81
            aggregated_batch_list.append(batch)
1✔
82
        aggregated_batch = Batch.concat(aggregated_batch_list, copy=False)
1✔
83

84
        # nothing to order by
85
        if not len(aggregated_batch):
1✔
86
            return
×
87

88
        # Column can be a functional expression, so if it
89
        # is not in columns, it needs to be re-evaluated.
90
        merge_batch_list = [aggregated_batch]
1✔
91
        for col in self._columns:
1✔
92
            col_name_list = self._extract_column_name(col)
1✔
93
            for col_name in col_name_list:
1✔
94
                if col_name not in aggregated_batch.columns:
1✔
95
                    batch = col.evaluate(aggregated_batch)
×
96
                    merge_batch_list.append(batch)
×
97
        if len(merge_batch_list) > 1:
1✔
98
            aggregated_batch = Batch.merge_column_wise(merge_batch_list)
×
99

100
        # sorts the batch
101
        try:
1✔
102
            aggregated_batch.sort_orderby(
1✔
103
                by=self.extract_column_names(),
104
                sort_type=self.extract_sort_types(),
105
            )
106
        except KeyError:
107
            # raise ExecutorError(str(e))
108
            pass
109

110
        # split the aggregated batch into smaller ones based
111
        #  on self.batch_sizes which holds the input batches sizes
112
        index = 0
1✔
113
        for i in self.batch_sizes:
1✔
114
            batch = aggregated_batch[index : index + i]
1✔
115
            batch.reset_index()
1✔
116
            index += i
1✔
117
            yield batch
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

© 2026 Coveralls, Inc