-
Notifications
You must be signed in to change notification settings - Fork 261
/
_expressions.py
176 lines (154 loc) · 6.5 KB
/
_expressions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# coding=utf-8
# Copyright 2018-2023 EvaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from lark import Tree
from evadb.catalog.catalog_type import ColumnType
from evadb.expression.abstract_expression import ExpressionType
from evadb.expression.comparison_expression import ComparisonExpression
from evadb.expression.constant_value_expression import ConstantValueExpression
from evadb.expression.logical_expression import LogicalExpression
from evadb.utils.generic_utils import string_comparison_case_insensitive
##################################################################
# EXPRESSIONS
##################################################################
class Expressions:
def string_literal(self, tree):
text = tree.children[0]
assert text is not None
return ConstantValueExpression(text[1:-1], ColumnType.TEXT)
def array_literal(self, tree):
array_elements = []
for child in tree.children:
if isinstance(child, Tree):
array_element = self.visit(child).value
array_elements.append(array_element)
res = ConstantValueExpression(np.array(array_elements), ColumnType.NDARRAY)
return res
def boolean_literal(self, tree):
text = tree.children[0]
if text == "TRUE":
return ConstantValueExpression(True, ColumnType.BOOLEAN)
return ConstantValueExpression(False, ColumnType.BOOLEAN)
def constant(self, tree):
for child in tree.children:
if isinstance(child, Tree):
if child.data == "real_literal":
real_literal = self.visit(child)
return ConstantValueExpression(real_literal, ColumnType.FLOAT)
elif child.data == "decimal_literal":
decimal_literal = self.visit(child)
return ConstantValueExpression(decimal_literal, ColumnType.INTEGER)
return self.visit_children(tree)
def logical_expression(self, tree):
left = self.visit(tree.children[0])
op = self.visit(tree.children[1])
right = self.visit(tree.children[2])
return LogicalExpression(op, left, right)
def binary_comparison_predicate(self, tree):
left = self.visit(tree.children[0])
op = self.visit(tree.children[1])
right = self.visit(tree.children[2])
return ComparisonExpression(op, left, right)
def nested_expression_atom(self, tree):
# Todo Can there be >1 expression in this case
expr = tree.children[0]
return self.visit(expr)
def comparison_operator(self, tree):
op = str(tree.children[0])
if op == "=":
return ExpressionType.COMPARE_EQUAL
elif op == "<":
return ExpressionType.COMPARE_LESSER
elif op == ">":
return ExpressionType.COMPARE_GREATER
elif op == ">=":
return ExpressionType.COMPARE_GEQ
elif op == "<=":
return ExpressionType.COMPARE_LEQ
elif op == "!=":
return ExpressionType.COMPARE_NEQ
elif op == "@>":
return ExpressionType.COMPARE_CONTAINS
elif op == "<@":
return ExpressionType.COMPARE_IS_CONTAINED
elif op == "LIKE":
return ExpressionType.COMPARE_LIKE
def logical_operator(self, tree):
op = str(tree.children[0])
if string_comparison_case_insensitive(op, "OR"):
return ExpressionType.LOGICAL_OR
elif string_comparison_case_insensitive(op, "AND"):
return ExpressionType.LOGICAL_AND
else:
raise NotImplementedError("Unsupported logical operator: {}".format(op))
def expressions_with_defaults(self, tree):
expr_list = []
for child in tree.children:
if isinstance(child, Tree):
if child.data == "expression_or_default":
expression = self.visit(child)
expr_list.append(expression)
return expr_list
def sample_params(self, tree):
sample_type = None
sample_freq = None
for child in tree.children:
if child.data == "sample_clause":
sample_freq = self.visit(child)
elif child.data == "sample_clause_with_type":
sample_type, sample_freq = self.visit(child)
return sample_type, sample_freq
def sample_clause(self, tree):
sample_list = self.visit_children(tree)
assert len(sample_list) == 2
return ConstantValueExpression(sample_list[1])
def sample_clause_with_type(self, tree):
sample_list = self.visit_children(tree)
assert len(sample_list) == 3 or len(sample_list) == 2
if len(sample_list) == 3:
return ConstantValueExpression(sample_list[1]), ConstantValueExpression(
sample_list[2]
)
else:
return ConstantValueExpression(sample_list[1]), ConstantValueExpression(1)
def chunk_params(self, tree):
chunk_params = self.visit_children(tree)
assert len(chunk_params) == 2 or len(chunk_params) == 4
if len(chunk_params) == 4:
return {
"chunk_size": chunk_params[1],
"chunk_overlap": chunk_params[3],
}
elif len(chunk_params) == 2:
if chunk_params[0] == "CHUNK_SIZE":
return {
"chunk_size": chunk_params[1],
}
elif chunk_params[0] == "CHUNK_OVERLAP":
return {
"chunk_overlap": chunk_params[1],
}
else:
assert f"incorrect keyword found {chunk_params[0]}"
def colon_param_dict(self, tree):
param_dict = {}
for child in tree.children:
if isinstance(child, Tree):
if child.data == "colon_param":
param = self.visit(child)
key = param[0].value
value = param[1].value
param_dict[key] = value
return param_dict