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

georgia-tech-db / eva / e6605954-d2af-406f-a2e4-075433fee53b

16 Sep 2023 06:13PM UTC coverage: 80.158% (+10.2%) from 69.982%
e6605954-d2af-406f-a2e4-075433fee53b

push

circle-ci

Chitti-Ankith
lint fixes

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

9522 of 11879 relevant lines covered (80.16%)

1.44 hits per line

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

94.67
/evadb/parser/lark_visitor/_create_statements.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 Tree
2✔
17

18
from evadb.catalog.catalog_type import ColumnType, NdArrayType, VectorStoreType
2✔
19
from evadb.expression.tuple_value_expression import TupleValueExpression
2✔
20
from evadb.parser.create_index_statement import CreateIndexStatement
2✔
21
from evadb.parser.create_statement import (
2✔
22
    ColConstraintInfo,
23
    ColumnDefinition,
24
    CreateDatabaseStatement,
25
    CreateTableStatement,
26
)
27
from evadb.parser.table_ref import TableRef
2✔
28
from evadb.parser.types import ColumnConstraintEnum
2✔
29

30

31
##################################################################
32
# CREATE STATEMENTS
33
##################################################################
34
class CreateTable:
2✔
35
    def create_table(self, tree):
2✔
36
        table_info = None
2✔
37
        if_not_exists = False
2✔
38
        create_definitions = []
2✔
39
        query = None
2✔
40

41
        for child in tree.children:
2✔
42
            if isinstance(child, Tree):
2✔
43
                if child.data == "if_not_exists":
2✔
44
                    if_not_exists = True
2✔
45
                elif child.data == "table_name":
2✔
46
                    table_info = self.visit(child)
2✔
47
                elif child.data == "create_definitions":
2✔
48
                    create_definitions = self.visit(child)
2✔
49
                elif child.data == "simple_select":
1✔
50
                    query = self.visit(child)
1✔
51

52
        create_stmt = CreateTableStatement(
2✔
53
            table_info, if_not_exists, create_definitions, query=query
54
        )
55
        return create_stmt
2✔
56

57
    def create_definitions(self, tree):
2✔
58
        column_definitions = []
2✔
59
        for child in tree.children:
2✔
60
            if isinstance(child, Tree):
2✔
61
                create_definition = None
2✔
62
                if child.data == "column_declaration":
2✔
63
                    create_definition = self.visit(child)
2✔
64
                column_definitions.append(create_definition)
2✔
65

66
        return column_definitions
2✔
67

68
    def column_declaration(self, tree):
2✔
69
        column_name = None
2✔
70
        data_type = None
2✔
71
        array_type = None
2✔
72
        dimensions = None
2✔
73
        column_constraint_information = None
2✔
74

75
        for child in tree.children:
2✔
76
            if isinstance(child, Tree):
2✔
77
                if child.data == "uid":
2✔
78
                    column_name = self.visit(child)
2✔
79
                elif child.data == "column_definition":
2✔
80
                    (
2✔
81
                        data_type,
82
                        array_type,
83
                        dimensions,
84
                        column_constraint_information,
85
                    ) = self.visit(child)
86

87
        if column_name is not None:
2✔
88
            return ColumnDefinition(
2✔
89
                column_name,
90
                data_type,
91
                array_type,
92
                dimensions,
93
                column_constraint_information,
94
            )
95

96
    def column_definition(self, tree):
2✔
97
        data_type = None
2✔
98
        array_type = None
2✔
99
        dimensions = None
2✔
100
        column_constraint_information = ColConstraintInfo()
2✔
101
        not_null_set = False
2✔
102

103
        for child in tree.children:
2✔
104
            if isinstance(child, Tree):
2✔
105
                if child.data.endswith("data_type"):
2✔
106
                    data_type, array_type, dimensions = self.visit(child)
2✔
107
                elif child.data.endswith("column_constraint"):
2✔
108
                    return_type = self.visit(child)
2✔
109
                    if return_type == ColumnConstraintEnum.UNIQUE:
2✔
110
                        column_constraint_information.unique = True
2✔
111
                        column_constraint_information.nullable = False
2✔
112
                        not_null_set = True
2✔
113
                    elif return_type == ColumnConstraintEnum.NOTNULL:
1✔
114
                        column_constraint_information.nullable = False
1✔
115
                        not_null_set = True
1✔
116

117
        if not not_null_set:
2✔
118
            column_constraint_information.nullable = True
2✔
119

120
        return data_type, array_type, dimensions, column_constraint_information
2✔
121

122
    def unique_key_column_constraint(self, tree):
2✔
123
        return ColumnConstraintEnum.UNIQUE
2✔
124

125
    def null_column_constraint(self, tree):
2✔
126
        return ColumnConstraintEnum.NOTNULL
1✔
127

128
    def simple_data_type(self, tree):
2✔
129
        data_type = None
×
130
        array_type = None
×
131
        dimensions = []
×
132

133
        token = tree.children[0]
×
134
        if str.upper(token) == "BOOLEAN":
×
135
            data_type = ColumnType.BOOLEAN
×
136

137
        return data_type, array_type, dimensions
×
138

139
    def integer_data_type(self, tree):
2✔
140
        data_type = None
2✔
141
        array_type = None
2✔
142
        dimensions = []
2✔
143

144
        token = tree.children[0]
2✔
145
        if str.upper(token) == "INTEGER":
2✔
146
            data_type = ColumnType.INTEGER
2✔
147

148
        return data_type, array_type, dimensions
2✔
149

150
    def dimension_data_type(self, tree):
2✔
151
        data_type = None
2✔
152
        array_type = None
2✔
153
        dimensions = []
2✔
154

155
        token = tree.children[0]
2✔
156
        if str.upper(token) == "FLOAT":
2✔
157
            data_type = ColumnType.FLOAT
2✔
158
            dimensions = self.visit(tree.children[1])
2✔
159
        elif str.upper(token) == "TEXT":
2✔
160
            data_type = ColumnType.TEXT
2✔
161
            dimensions = self.visit(tree.children[1])
2✔
162

163
        return data_type, array_type, dimensions
2✔
164

165
    def array_data_type(self, tree):
2✔
166
        data_type = ColumnType.NDARRAY
2✔
167
        array_type = NdArrayType.ANYTYPE
2✔
168
        dimensions = None
2✔
169

170
        for child in tree.children:
2✔
171
            if isinstance(child, Tree):
2✔
172
                if child.data == "array_type":
2✔
173
                    array_type = self.visit(child)
2✔
174
                elif child.data == "length_dimension_list":
2✔
175
                    dimensions = self.visit(child)
2✔
176

177
        return data_type, array_type, dimensions
2✔
178

179
    def any_data_type(self, tree):
2✔
180
        return ColumnType.ANY, None, []
2✔
181

182
    def array_type(self, tree):
2✔
183
        array_type = None
2✔
184

185
        token = tree.children[0]
2✔
186
        if str.upper(token) == "INT8":
2✔
187
            array_type = NdArrayType.INT8
1✔
188
        elif str.upper(token) == "UINT8":
2✔
189
            array_type = NdArrayType.UINT8
2✔
190
        elif str.upper(token) == "INT16":
2✔
191
            array_type = NdArrayType.INT16
1✔
192
        elif str.upper(token) == "INT32":
2✔
193
            array_type = NdArrayType.INT32
1✔
194
        elif str.upper(token) == "INT64":
2✔
195
            array_type = NdArrayType.INT64
1✔
196
        elif str.upper(token) == "UNICODE":
2✔
197
            array_type = NdArrayType.UNICODE
1✔
198
        elif str.upper(token) == "BOOLEAN":
2✔
199
            array_type = NdArrayType.BOOL
1✔
200
        elif str.upper(token) == "FLOAT32":
2✔
201
            array_type = NdArrayType.FLOAT32
2✔
202
        elif str.upper(token) == "FLOAT64":
2✔
203
            array_type = NdArrayType.FLOAT64
1✔
204
        elif str.upper(token) == "DECIMAL":
2✔
205
            array_type = NdArrayType.DECIMAL
1✔
206
        elif str.upper(token) == "STR":
2✔
207
            array_type = NdArrayType.STR
2✔
208
        elif str.upper(token) == "DATETIME":
2✔
209
            array_type = NdArrayType.DATETIME
1✔
210
        elif str.upper(token) == "ANYTYPE":
2✔
211
            array_type = NdArrayType.ANYTYPE
2✔
212
        return array_type
2✔
213

214
    def dimension_helper(self, tree):
2✔
215
        dimensions = []
2✔
216
        for child in tree.children:
2✔
217
            if isinstance(child, Tree):
2✔
218
                if child.data == "decimal_literal":
2✔
219
                    decimal = self.visit(child)
2✔
220
                    dimensions.append(decimal)
2✔
221
        return tuple(dimensions)
2✔
222

223
    def length_one_dimension(self, tree):
2✔
224
        dimensions = self.dimension_helper(tree)
2✔
225
        return dimensions
2✔
226

227
    def length_two_dimension(self, tree):
2✔
228
        dimensions = self.dimension_helper(tree)
2✔
229
        return dimensions
2✔
230

231
    def length_dimension_list(self, tree):
2✔
232
        dimensions = self.dimension_helper(tree)
2✔
233
        return dimensions
2✔
234

235
    def vector_store_type(self, tree):
2✔
236
        vector_store_type = None
2✔
237
        token = tree.children[1]
2✔
238

239
        if str.upper(token) == "FAISS":
2✔
240
            vector_store_type = VectorStoreType.FAISS
2✔
241
        elif str.upper(token) == "QDRANT":
×
242
            vector_store_type = VectorStoreType.QDRANT
×
243
        elif str.upper(token) == "PINECONE":
×
244
            vector_store_type = VectorStoreType.PINECONE
×
245
        return vector_store_type
2✔
246

247
    # INDEX CREATION
248
    def create_index(self, tree):
2✔
249
        index_name = None
2✔
250
        if_not_exists = False
2✔
251
        table_name = None
2✔
252
        vector_store_type = None
2✔
253
        index_elem = None
2✔
254

255
        for child in tree.children:
2✔
256
            if isinstance(child, Tree):
2✔
257
                if child.data == "uid":
2✔
258
                    index_name = self.visit(child)
2✔
259
                if child.data == "if_not_exists":
2✔
260
                    if_not_exists = True
1✔
261
                elif child.data == "table_name":
2✔
262
                    table_name = self.visit(child)
2✔
263
                    table_ref = TableRef(table_name)
2✔
264
                elif child.data == "vector_store_type":
2✔
265
                    vector_store_type = self.visit(child)
2✔
266
                elif child.data == "index_elem":
2✔
267
                    index_elem = self.visit(child)
2✔
268

269
        # Parse either a single function call or column list.
270
        col_list, function = None, None
2✔
271
        if not isinstance(index_elem, list):
2✔
272
            function = index_elem
1✔
273

274
            # Traverse to the tuple value expression.
275
            while not isinstance(index_elem, TupleValueExpression):
1✔
276
                index_elem = index_elem.children[0]
1✔
277
            index_elem = [index_elem]
1✔
278

279
        col_list = [
2✔
280
            ColumnDefinition(tv_expr.name, None, None, None) for tv_expr in index_elem
281
        ]
282

283
        return CreateIndexStatement(
2✔
284
            index_name, if_not_exists, table_ref, col_list, vector_store_type, function
285
        )
286

287

288
class CreateDatabase:
2✔
289
    def create_database(self, tree):
2✔
290
        database_name = None
2✔
291
        if_not_exists = False
2✔
292
        engine = None
2✔
293
        param_dict = {}
2✔
294

295
        for child in tree.children:
2✔
296
            if isinstance(child, Tree):
2✔
297
                if child.data == "if_not_exists":
2✔
298
                    if_not_exists = True
×
299
                elif child.data == "uid":
2✔
300
                    database_name = self.visit(child)
2✔
301
                elif child.data == "create_database_engine_clause":
2✔
302
                    engine, param_dict = self.visit(child)
2✔
303

304
        create_stmt = CreateDatabaseStatement(
2✔
305
            database_name, if_not_exists, engine, param_dict
306
        )
307
        return create_stmt
2✔
308

309
    def create_database_engine_clause(self, tree):
2✔
310
        engine = None
2✔
311
        param_dict = {}
2✔
312
        for child in tree.children:
2✔
313
            if isinstance(child, Tree):
2✔
314
                if child.data == "string_literal":
2✔
315
                    engine = self.visit(child).value
2✔
316
                elif child.data == "colon_param_dict":
2✔
317
                    param_dict = self.visit(child)
2✔
318

319
        return engine, param_dict
2✔
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