pixeltable 0.1.1__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pixeltable might be problematic. Click here for more details.
- pixeltable/__init__.py +34 -6
- pixeltable/catalog/__init__.py +13 -0
- pixeltable/catalog/catalog.py +159 -0
- pixeltable/catalog/column.py +200 -0
- pixeltable/catalog/dir.py +32 -0
- pixeltable/catalog/globals.py +33 -0
- pixeltable/catalog/insertable_table.py +191 -0
- pixeltable/catalog/named_function.py +36 -0
- pixeltable/catalog/path.py +58 -0
- pixeltable/catalog/path_dict.py +139 -0
- pixeltable/catalog/schema_object.py +39 -0
- pixeltable/catalog/table.py +581 -0
- pixeltable/catalog/table_version.py +749 -0
- pixeltable/catalog/table_version_path.py +133 -0
- pixeltable/catalog/view.py +203 -0
- pixeltable/client.py +520 -30
- pixeltable/dataframe.py +540 -349
- pixeltable/env.py +373 -45
- pixeltable/exceptions.py +12 -21
- pixeltable/exec/__init__.py +9 -0
- pixeltable/exec/aggregation_node.py +78 -0
- pixeltable/exec/cache_prefetch_node.py +113 -0
- pixeltable/exec/component_iteration_node.py +79 -0
- pixeltable/exec/data_row_batch.py +95 -0
- pixeltable/exec/exec_context.py +22 -0
- pixeltable/exec/exec_node.py +61 -0
- pixeltable/exec/expr_eval_node.py +217 -0
- pixeltable/exec/in_memory_data_node.py +69 -0
- pixeltable/exec/media_validation_node.py +43 -0
- pixeltable/exec/sql_scan_node.py +225 -0
- pixeltable/exprs/__init__.py +24 -0
- pixeltable/exprs/arithmetic_expr.py +102 -0
- pixeltable/exprs/array_slice.py +71 -0
- pixeltable/exprs/column_property_ref.py +77 -0
- pixeltable/exprs/column_ref.py +105 -0
- pixeltable/exprs/comparison.py +77 -0
- pixeltable/exprs/compound_predicate.py +98 -0
- pixeltable/exprs/data_row.py +187 -0
- pixeltable/exprs/expr.py +586 -0
- pixeltable/exprs/expr_set.py +39 -0
- pixeltable/exprs/function_call.py +380 -0
- pixeltable/exprs/globals.py +69 -0
- pixeltable/exprs/image_member_access.py +115 -0
- pixeltable/exprs/image_similarity_predicate.py +58 -0
- pixeltable/exprs/inline_array.py +107 -0
- pixeltable/exprs/inline_dict.py +101 -0
- pixeltable/exprs/is_null.py +38 -0
- pixeltable/exprs/json_mapper.py +121 -0
- pixeltable/exprs/json_path.py +159 -0
- pixeltable/exprs/literal.py +54 -0
- pixeltable/exprs/object_ref.py +41 -0
- pixeltable/exprs/predicate.py +44 -0
- pixeltable/exprs/row_builder.py +355 -0
- pixeltable/exprs/rowid_ref.py +94 -0
- pixeltable/exprs/type_cast.py +53 -0
- pixeltable/exprs/variable.py +45 -0
- pixeltable/func/__init__.py +9 -0
- pixeltable/func/aggregate_function.py +194 -0
- pixeltable/func/batched_function.py +53 -0
- pixeltable/func/callable_function.py +69 -0
- pixeltable/func/expr_template_function.py +82 -0
- pixeltable/func/function.py +110 -0
- pixeltable/func/function_registry.py +227 -0
- pixeltable/func/globals.py +36 -0
- pixeltable/func/nos_function.py +202 -0
- pixeltable/func/signature.py +166 -0
- pixeltable/func/udf.py +163 -0
- pixeltable/functions/__init__.py +52 -103
- pixeltable/functions/eval.py +216 -0
- pixeltable/functions/fireworks.py +61 -0
- pixeltable/functions/huggingface.py +120 -0
- pixeltable/functions/image.py +16 -0
- pixeltable/functions/openai.py +88 -0
- pixeltable/functions/pil/image.py +148 -7
- pixeltable/functions/string.py +13 -0
- pixeltable/functions/together.py +27 -0
- pixeltable/functions/util.py +41 -0
- pixeltable/functions/video.py +62 -0
- pixeltable/iterators/__init__.py +3 -0
- pixeltable/iterators/base.py +48 -0
- pixeltable/iterators/document.py +311 -0
- pixeltable/iterators/video.py +89 -0
- pixeltable/metadata/__init__.py +54 -0
- pixeltable/metadata/converters/convert_10.py +18 -0
- pixeltable/metadata/schema.py +211 -0
- pixeltable/plan.py +656 -0
- pixeltable/store.py +413 -182
- pixeltable/tests/conftest.py +143 -87
- pixeltable/tests/test_audio.py +65 -0
- pixeltable/tests/test_catalog.py +27 -0
- pixeltable/tests/test_client.py +14 -14
- pixeltable/tests/test_component_view.py +372 -0
- pixeltable/tests/test_dataframe.py +433 -0
- pixeltable/tests/test_dirs.py +78 -62
- pixeltable/tests/test_document.py +117 -0
- pixeltable/tests/test_exprs.py +591 -135
- pixeltable/tests/test_function.py +297 -67
- pixeltable/tests/test_functions.py +283 -1
- pixeltable/tests/test_migration.py +43 -0
- pixeltable/tests/test_nos.py +54 -0
- pixeltable/tests/test_snapshot.py +208 -0
- pixeltable/tests/test_table.py +1085 -262
- pixeltable/tests/test_transactional_directory.py +42 -0
- pixeltable/tests/test_types.py +5 -11
- pixeltable/tests/test_video.py +149 -34
- pixeltable/tests/test_view.py +530 -0
- pixeltable/tests/utils.py +186 -45
- pixeltable/tool/create_test_db_dump.py +149 -0
- pixeltable/type_system.py +490 -126
- pixeltable/utils/__init__.py +17 -46
- pixeltable/utils/clip.py +12 -15
- pixeltable/utils/coco.py +136 -0
- pixeltable/utils/documents.py +39 -0
- pixeltable/utils/filecache.py +195 -0
- pixeltable/utils/help.py +11 -0
- pixeltable/utils/media_store.py +76 -0
- pixeltable/utils/parquet.py +126 -0
- pixeltable/utils/pytorch.py +172 -0
- pixeltable/utils/s3.py +13 -0
- pixeltable/utils/sql.py +17 -0
- pixeltable/utils/transactional_directory.py +35 -0
- pixeltable-0.2.0.dist-info/LICENSE +18 -0
- pixeltable-0.2.0.dist-info/METADATA +117 -0
- pixeltable-0.2.0.dist-info/RECORD +125 -0
- {pixeltable-0.1.1.dist-info → pixeltable-0.2.0.dist-info}/WHEEL +1 -1
- pixeltable/catalog.py +0 -1421
- pixeltable/exprs.py +0 -1745
- pixeltable/function.py +0 -269
- pixeltable/functions/clip.py +0 -10
- pixeltable/functions/pil/__init__.py +0 -23
- pixeltable/functions/tf.py +0 -21
- pixeltable/index.py +0 -57
- pixeltable/tests/test_dict.py +0 -24
- pixeltable/tests/test_tf.py +0 -69
- pixeltable/tf.py +0 -33
- pixeltable/utils/tf.py +0 -33
- pixeltable/utils/video.py +0 -32
- pixeltable-0.1.1.dist-info/METADATA +0 -31
- pixeltable-0.1.1.dist-info/RECORD +0 -36
pixeltable/client.py
CHANGED
|
@@ -1,44 +1,534 @@
|
|
|
1
|
-
from typing import List, Dict
|
|
1
|
+
from typing import List, Optional, Dict, Type, Any, Union
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import logging
|
|
4
|
+
import dataclasses
|
|
5
|
+
from uuid import UUID
|
|
6
|
+
from collections import defaultdict
|
|
2
7
|
|
|
8
|
+
import sqlalchemy as sql
|
|
3
9
|
import sqlalchemy.orm as orm
|
|
4
10
|
|
|
5
|
-
from pixeltable import
|
|
11
|
+
from pixeltable.metadata import schema
|
|
6
12
|
from pixeltable.env import Env
|
|
13
|
+
import pixeltable.func as func
|
|
14
|
+
import pixeltable.catalog as catalog
|
|
15
|
+
from pixeltable import exceptions as excs
|
|
16
|
+
from pixeltable.exprs import Predicate
|
|
17
|
+
from pixeltable.iterators import ComponentIterator
|
|
7
18
|
|
|
8
19
|
__all__ = [
|
|
9
20
|
'Client',
|
|
10
21
|
]
|
|
11
22
|
|
|
12
23
|
|
|
24
|
+
_logger = logging.getLogger('pixeltable')
|
|
25
|
+
|
|
13
26
|
class Client:
|
|
14
|
-
"""Missing docstring.
|
|
15
27
|
"""
|
|
28
|
+
Client for interacting with a Pixeltable environment.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, reload: bool = False) -> None:
|
|
32
|
+
"""Constructs a client.
|
|
33
|
+
"""
|
|
34
|
+
env = Env.get()
|
|
35
|
+
env.set_up()
|
|
36
|
+
env.upgrade_metadata()
|
|
37
|
+
if reload:
|
|
38
|
+
catalog.Catalog.clear()
|
|
39
|
+
self.catalog = catalog.Catalog.get()
|
|
40
|
+
|
|
41
|
+
def logging(
|
|
42
|
+
self, *, to_stdout: Optional[bool] = None, level: Optional[int] = None,
|
|
43
|
+
add: Optional[str] = None, remove: Optional[str] = None
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Configure logging.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
to_stdout: if True, also log to stdout
|
|
49
|
+
level: default log level
|
|
50
|
+
add: comma-separated list of 'module name:log level' pairs; ex.: add='video:10'
|
|
51
|
+
remove: comma-separated list of module names
|
|
52
|
+
"""
|
|
53
|
+
if to_stdout is not None:
|
|
54
|
+
Env.get().log_to_stdout(to_stdout)
|
|
55
|
+
if level is not None:
|
|
56
|
+
Env.get().set_log_level(level)
|
|
57
|
+
if add is not None:
|
|
58
|
+
for module, level in [t.split(':') for t in add.split(',')]:
|
|
59
|
+
Env.get().set_module_log_level(module, int(level))
|
|
60
|
+
if remove is not None:
|
|
61
|
+
for module in remove.split(','):
|
|
62
|
+
Env.get().set_module_log_level(module, None)
|
|
63
|
+
if to_stdout is None and level is None and add is None and remove is None:
|
|
64
|
+
Env.get().print_log_config()
|
|
65
|
+
|
|
66
|
+
def list_functions(self) -> pd.DataFrame:
|
|
67
|
+
"""Returns information about all registered functions.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
Pandas DataFrame with columns 'Path', 'Name', 'Parameters', 'Return Type', 'Is Agg', 'Library'
|
|
71
|
+
"""
|
|
72
|
+
functions = func.FunctionRegistry.get().list_functions()
|
|
73
|
+
paths = ['.'.join(f.self_path.split('.')[:-1]) for f in functions]
|
|
74
|
+
names = [f.name for f in functions]
|
|
75
|
+
params = [
|
|
76
|
+
', '.join(
|
|
77
|
+
[param_name + ': ' + str(param_type) for param_name, param_type in f.signature.parameters.items()])
|
|
78
|
+
for f in functions
|
|
79
|
+
]
|
|
80
|
+
pd_df = pd.DataFrame({
|
|
81
|
+
'Path': paths,
|
|
82
|
+
'Function Name': names,
|
|
83
|
+
'Parameters': params,
|
|
84
|
+
'Return Type': [str(f.signature.get_return_type()) for f in functions],
|
|
85
|
+
})
|
|
86
|
+
pd_df = pd_df.style.set_properties(**{'text-align': 'left'}) \
|
|
87
|
+
.set_table_styles([dict(selector='th', props=[('text-align', 'center')])]) # center-align headings
|
|
88
|
+
return pd_df.hide(axis='index')
|
|
89
|
+
|
|
90
|
+
def get_path(self, schema_obj: catalog.SchemaObject) -> str:
|
|
91
|
+
"""Returns the path to a SchemaObject.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
schema_obj: SchemaObject to get the path for.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Path to the SchemaObject.
|
|
98
|
+
"""
|
|
99
|
+
path_elements: List[str] = []
|
|
100
|
+
dir_id = schema_obj._dir_id
|
|
101
|
+
while dir_id is not None:
|
|
102
|
+
dir = self.catalog.paths.get_schema_obj(dir_id)
|
|
103
|
+
if dir._dir_id is None:
|
|
104
|
+
# this is the root dir with name '', which we don't want to include in the path
|
|
105
|
+
break
|
|
106
|
+
path_elements.insert(0, dir._name)
|
|
107
|
+
dir_id = dir._dir_id
|
|
108
|
+
path_elements.append(schema_obj._name)
|
|
109
|
+
return '.'.join(path_elements)
|
|
110
|
+
|
|
111
|
+
def create_table(
|
|
112
|
+
self, path_str: str, schema: Dict[str, Any], primary_key: Optional[Union[str, List[str]]] = None,
|
|
113
|
+
num_retained_versions: int = 10, comment: str = ''
|
|
114
|
+
) -> catalog.InsertableTable:
|
|
115
|
+
"""Create a new `InsertableTable`.
|
|
16
116
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
117
|
+
Args:
|
|
118
|
+
path_str: Path to the table.
|
|
119
|
+
schema: dictionary mapping column names to column types, value expressions, or to column specifications.
|
|
120
|
+
num_retained_versions: Number of versions of the table to retain.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
The newly created table.
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
Error: if the path already exists or is invalid.
|
|
127
|
+
|
|
128
|
+
Examples:
|
|
129
|
+
Create a table with an int and a string column:
|
|
130
|
+
|
|
131
|
+
>>> table = cl.create_table('my_table', schema={'col1': IntType(), 'col2': StringType()})
|
|
132
|
+
|
|
133
|
+
Create a table with a single indexed image column:
|
|
134
|
+
|
|
135
|
+
>>> table = cl.create_table('my_table', schema={'col1': {'type': ImageType(), 'indexed': True}})
|
|
136
|
+
"""
|
|
137
|
+
path = catalog.Path(path_str)
|
|
138
|
+
self.catalog.paths.check_is_valid(path, expected=None)
|
|
139
|
+
dir = self.catalog.paths[path.parent]
|
|
140
|
+
|
|
141
|
+
if len(schema) == 0:
|
|
142
|
+
raise excs.Error(f'Table schema is empty: `{path_str}`')
|
|
143
|
+
|
|
144
|
+
if primary_key is None:
|
|
145
|
+
primary_key = []
|
|
146
|
+
elif isinstance(primary_key, str):
|
|
147
|
+
primary_key = [primary_key]
|
|
32
148
|
else:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return
|
|
41
|
-
|
|
42
|
-
def
|
|
43
|
-
|
|
44
|
-
|
|
149
|
+
if not isinstance(primary_key, list) or not all(isinstance(pk, str) for pk in primary_key):
|
|
150
|
+
raise excs.Error('primary_key must be a single column name or a list of column names')
|
|
151
|
+
|
|
152
|
+
tbl = catalog.InsertableTable.create(
|
|
153
|
+
dir._id, path.name, schema, primary_key=primary_key, num_retained_versions=num_retained_versions, comment=comment)
|
|
154
|
+
self.catalog.paths[path] = tbl
|
|
155
|
+
_logger.info(f'Created table `{path_str}`.')
|
|
156
|
+
return tbl
|
|
157
|
+
|
|
158
|
+
def create_view(
|
|
159
|
+
self, path_str: str, base: catalog.Table, *, schema: Optional[Dict[str, Any]] = None,
|
|
160
|
+
filter: Optional[Predicate] = None,
|
|
161
|
+
is_snapshot: bool = False, iterator_class: Optional[Type[ComponentIterator]] = None,
|
|
162
|
+
iterator_args: Optional[Dict[str, Any]] = None, num_retained_versions: int = 10, comment: str = '',
|
|
163
|
+
ignore_errors: bool = False) -> catalog.View:
|
|
164
|
+
"""Create a new `View`.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
path_str: Path to the view.
|
|
168
|
+
base: Table (ie, table or view or snapshot) to base the view on.
|
|
169
|
+
schema: dictionary mapping column names to column types, value expressions, or to column specifications.
|
|
170
|
+
filter: Predicate to filter rows of the base table.
|
|
171
|
+
is_snapshot: Whether the view is a snapshot.
|
|
172
|
+
iterator_class: Class of the iterator to use for the view.
|
|
173
|
+
iterator_args: Arguments to pass to the iterator class.
|
|
174
|
+
num_retained_versions: Number of versions of the view to retain.
|
|
175
|
+
ignore_errors: if True, fail silently if the path already exists or is invalid.
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
The newly created view.
|
|
179
|
+
|
|
180
|
+
Raises:
|
|
181
|
+
Error: if the path already exists or is invalid.
|
|
182
|
+
|
|
183
|
+
Examples:
|
|
184
|
+
Create a view with an additional int and a string column and a filter:
|
|
185
|
+
|
|
186
|
+
>>> view = cl.create_view(
|
|
187
|
+
'my_view', base, schema={'col3': IntType(), 'col4': StringType()}, filter=base.col1 > 10)
|
|
188
|
+
|
|
189
|
+
Create a table snapshot:
|
|
190
|
+
|
|
191
|
+
>>> snapshot_view = cl.create_view('my_snapshot_view', base, is_snapshot=True)
|
|
192
|
+
|
|
193
|
+
Create an immutable view with additional computed columns and a filter:
|
|
194
|
+
|
|
195
|
+
>>> snapshot_view = cl.create_view(
|
|
196
|
+
'my_snapshot', base, schema={'col3': base.col2 + 1}, filter=base.col1 > 10, is_snapshot=True)
|
|
197
|
+
"""
|
|
198
|
+
assert (iterator_class is None) == (iterator_args is None)
|
|
199
|
+
assert isinstance(base, catalog.Table)
|
|
200
|
+
path = catalog.Path(path_str)
|
|
201
|
+
try:
|
|
202
|
+
self.catalog.paths.check_is_valid(path, expected=None)
|
|
203
|
+
except Exception as e:
|
|
204
|
+
if ignore_errors:
|
|
205
|
+
return
|
|
206
|
+
else:
|
|
207
|
+
raise e
|
|
208
|
+
dir = self.catalog.paths[path.parent]
|
|
209
|
+
|
|
210
|
+
if schema is None:
|
|
211
|
+
schema = {}
|
|
212
|
+
view = catalog.View.create(
|
|
213
|
+
dir._id, path.name, base=base, schema=schema, predicate=filter, is_snapshot=is_snapshot,
|
|
214
|
+
iterator_cls=iterator_class, iterator_args=iterator_args, num_retained_versions=num_retained_versions, comment=comment)
|
|
215
|
+
self.catalog.paths[path] = view
|
|
216
|
+
_logger.info(f'Created view `{path_str}`.')
|
|
217
|
+
return view
|
|
218
|
+
|
|
219
|
+
def get_table(self, path: str) -> catalog.Table:
|
|
220
|
+
"""Get a handle to a table (including views and snapshots).
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
path: Path to the table.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
A `InsertableTable` or `View` object.
|
|
227
|
+
|
|
228
|
+
Raises:
|
|
229
|
+
Error: If the path does not exist or does not designate a table.
|
|
230
|
+
|
|
231
|
+
Examples:
|
|
232
|
+
Get handle for a table in the top-level directory:
|
|
233
|
+
|
|
234
|
+
>>> table = cl.get_table('my_table')
|
|
235
|
+
|
|
236
|
+
For a table in a subdirectory:
|
|
237
|
+
|
|
238
|
+
>>> table = cl.get_table('subdir.my_table')
|
|
239
|
+
|
|
240
|
+
For a snapshot in the top-level directory:
|
|
241
|
+
|
|
242
|
+
>>> table = cl.get_table('my_snapshot')
|
|
243
|
+
"""
|
|
244
|
+
p = catalog.Path(path)
|
|
245
|
+
self.catalog.paths.check_is_valid(p, expected=catalog.Table)
|
|
246
|
+
obj = self.catalog.paths[p]
|
|
247
|
+
return obj
|
|
248
|
+
|
|
249
|
+
def move(self, path: str, new_path: str) -> None:
|
|
250
|
+
"""Move a schema object to a new directory and/or rename a schema object.
|
|
251
|
+
|
|
252
|
+
Args:
|
|
253
|
+
path: absolute path to the existing schema object.
|
|
254
|
+
new_path: absolute new path for the schema object.
|
|
255
|
+
|
|
256
|
+
Raises:
|
|
257
|
+
Error: If path does not exist or new_path already exists.
|
|
258
|
+
|
|
259
|
+
Examples:
|
|
260
|
+
Move a table to a different directory:
|
|
261
|
+
|
|
262
|
+
>>>> cl.move('dir1.my_table', 'dir2.my_table')
|
|
263
|
+
|
|
264
|
+
Rename a table:
|
|
265
|
+
|
|
266
|
+
>>>> cl.move('dir1.my_table', 'dir1.new_name')
|
|
267
|
+
"""
|
|
268
|
+
p = catalog.Path(path)
|
|
269
|
+
self.catalog.paths.check_is_valid(p, expected=catalog.SchemaObject)
|
|
270
|
+
new_p = catalog.Path(new_path)
|
|
271
|
+
self.catalog.paths.check_is_valid(new_p, expected=None)
|
|
272
|
+
obj = self.catalog.paths[p]
|
|
273
|
+
self.catalog.paths.move(p, new_p)
|
|
274
|
+
new_dir = self.catalog.paths[new_p.parent]
|
|
275
|
+
obj.move(new_p.name, new_dir._id)
|
|
276
|
+
|
|
277
|
+
def list_tables(self, dir_path: str = '', recursive: bool = True) -> List[str]:
|
|
278
|
+
"""List the tables in a directory.
|
|
279
|
+
|
|
280
|
+
Args:
|
|
281
|
+
dir_path: Path to the directory. Defaults to the root directory.
|
|
282
|
+
recursive: Whether to list tables in subdirectories as well.
|
|
283
|
+
|
|
284
|
+
Returns:
|
|
285
|
+
A list of table paths.
|
|
286
|
+
|
|
287
|
+
Raises:
|
|
288
|
+
Error: If the path does not exist or does not designate a directory.
|
|
289
|
+
|
|
290
|
+
Examples:
|
|
291
|
+
List tables in top-level directory:
|
|
292
|
+
|
|
293
|
+
>>> cl.list_tables()
|
|
294
|
+
['my_table', ...]
|
|
295
|
+
|
|
296
|
+
List tables in 'dir1':
|
|
297
|
+
|
|
298
|
+
>>> cl.list_tables('dir1')
|
|
299
|
+
[...]
|
|
300
|
+
"""
|
|
301
|
+
assert dir_path is not None
|
|
302
|
+
path = catalog.Path(dir_path, empty_is_valid=True)
|
|
303
|
+
self.catalog.paths.check_is_valid(path, expected=catalog.Dir)
|
|
304
|
+
return [str(p) for p in self.catalog.paths.get_children(path, child_type=catalog.Table, recursive=recursive)]
|
|
305
|
+
|
|
306
|
+
def drop_table(self, path: str, force: bool = False, ignore_errors: bool = False) -> None:
|
|
307
|
+
"""Drop a table.
|
|
308
|
+
|
|
309
|
+
Args:
|
|
310
|
+
path: Path to the table.
|
|
311
|
+
force: Whether to drop the table even if it has unsaved changes.
|
|
312
|
+
ignore_errors: Whether to ignore errors if the table does not exist.
|
|
313
|
+
|
|
314
|
+
Raises:
|
|
315
|
+
Error: If the path does not exist or does not designate a table and ignore_errors is False.
|
|
316
|
+
|
|
317
|
+
Examples:
|
|
318
|
+
>>> cl.drop_table('my_table')
|
|
319
|
+
"""
|
|
320
|
+
path_obj = catalog.Path(path)
|
|
321
|
+
try:
|
|
322
|
+
self.catalog.paths.check_is_valid(path_obj, expected=catalog.Table)
|
|
323
|
+
except Exception as e:
|
|
324
|
+
if ignore_errors:
|
|
325
|
+
_logger.info(f'Skipped table `{path}` (does not exist).')
|
|
326
|
+
return
|
|
327
|
+
else:
|
|
328
|
+
raise e
|
|
329
|
+
tbl = self.catalog.paths[path_obj]
|
|
330
|
+
if len(self.catalog.tbl_dependents[tbl._id]) > 0:
|
|
331
|
+
dependent_paths = [self.get_path(dep) for dep in self.catalog.tbl_dependents[tbl._id]]
|
|
332
|
+
raise excs.Error(f'Table {path} has dependents: {", ".join(dependent_paths)}')
|
|
333
|
+
tbl._drop()
|
|
334
|
+
del self.catalog.paths[path_obj]
|
|
335
|
+
_logger.info(f'Dropped table `{path}`.')
|
|
336
|
+
|
|
337
|
+
def create_dir(self, path_str: str, ignore_errors: bool = False) -> None:
|
|
338
|
+
"""Create a directory.
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
path_str: Path to the directory.
|
|
342
|
+
ignore_errors: if True, silently returns on error
|
|
343
|
+
|
|
344
|
+
Raises:
|
|
345
|
+
Error: If the path already exists or the parent is not a directory.
|
|
346
|
+
|
|
347
|
+
Examples:
|
|
348
|
+
>>> cl.create_dir('my_dir')
|
|
349
|
+
|
|
350
|
+
Create a subdirectory:
|
|
351
|
+
|
|
352
|
+
>>> cl.create_dir('my_dir.sub_dir')
|
|
353
|
+
"""
|
|
354
|
+
try:
|
|
355
|
+
path = catalog.Path(path_str)
|
|
356
|
+
self.catalog.paths.check_is_valid(path, expected=None)
|
|
357
|
+
parent = self.catalog.paths[path.parent]
|
|
358
|
+
assert parent is not None
|
|
359
|
+
with orm.Session(Env.get().engine, future=True) as session:
|
|
360
|
+
dir_md = schema.DirMd(name=path.name)
|
|
361
|
+
dir_record = schema.Dir(parent_id=parent._id, md=dataclasses.asdict(dir_md))
|
|
362
|
+
session.add(dir_record)
|
|
363
|
+
session.flush()
|
|
364
|
+
assert dir_record.id is not None
|
|
365
|
+
self.catalog.paths[path] = catalog.Dir(dir_record.id, parent._id, path.name)
|
|
366
|
+
session.commit()
|
|
367
|
+
_logger.info(f'Created directory `{path_str}`.')
|
|
368
|
+
print(f'Created directory `{path_str}`.')
|
|
369
|
+
except excs.Error as e:
|
|
370
|
+
if ignore_errors:
|
|
371
|
+
return
|
|
372
|
+
else:
|
|
373
|
+
raise e
|
|
374
|
+
|
|
375
|
+
def rm_dir(self, path_str: str) -> None:
|
|
376
|
+
"""Remove a directory.
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
path_str: Path to the directory.
|
|
380
|
+
|
|
381
|
+
Raises:
|
|
382
|
+
Error: If the path does not exist or does not designate a directory or if the directory is not empty.
|
|
383
|
+
|
|
384
|
+
Examples:
|
|
385
|
+
>>> cl.rm_dir('my_dir')
|
|
386
|
+
|
|
387
|
+
Remove a subdirectory:
|
|
388
|
+
|
|
389
|
+
>>> cl.rm_dir('my_dir.sub_dir')
|
|
390
|
+
"""
|
|
391
|
+
path = catalog.Path(path_str)
|
|
392
|
+
self.catalog.paths.check_is_valid(path, expected=catalog.Dir)
|
|
393
|
+
|
|
394
|
+
# make sure it's empty
|
|
395
|
+
if len(self.catalog.paths.get_children(path, child_type=None, recursive=True)) > 0:
|
|
396
|
+
raise excs.Error(f'Directory {path_str} is not empty')
|
|
397
|
+
# TODO: figure out how to make force=True work in the presence of snapshots
|
|
398
|
+
# # delete tables
|
|
399
|
+
# for tbl_path in self.paths.get_children(path, child_type=MutableTable, recursive=True):
|
|
400
|
+
# self.drop_table(str(tbl_path), force=True)
|
|
401
|
+
# # rm subdirs
|
|
402
|
+
# for dir_path in self.paths.get_children(path, child_type=Dir, recursive=False):
|
|
403
|
+
# self.rm_dir(str(dir_path), force=True)
|
|
404
|
+
|
|
405
|
+
with Env.get().engine.begin() as conn:
|
|
406
|
+
dir = self.catalog.paths[path]
|
|
407
|
+
conn.execute(sql.delete(schema.Dir.__table__).where(schema.Dir.id == dir._id))
|
|
408
|
+
del self.catalog.paths[path]
|
|
409
|
+
_logger.info(f'Removed directory {path_str}')
|
|
410
|
+
|
|
411
|
+
def list_dirs(self, path_str: str = '', recursive: bool = True) -> List[str]:
|
|
412
|
+
"""List the directories in a directory.
|
|
413
|
+
|
|
414
|
+
Args:
|
|
415
|
+
path_str: Path to the directory.
|
|
416
|
+
recursive: Whether to list subdirectories recursively.
|
|
417
|
+
|
|
418
|
+
Returns:
|
|
419
|
+
List of directory paths.
|
|
420
|
+
|
|
421
|
+
Raises:
|
|
422
|
+
Error: If the path does not exist or does not designate a directory.
|
|
423
|
+
|
|
424
|
+
Examples:
|
|
425
|
+
>>> cl.list_dirs('my_dir', recursive=True)
|
|
426
|
+
['my_dir', 'my_dir.sub_dir1']
|
|
427
|
+
"""
|
|
428
|
+
path = catalog.Path(path_str, empty_is_valid=True)
|
|
429
|
+
self.catalog.paths.check_is_valid(path, expected=catalog.Dir)
|
|
430
|
+
return [str(p) for p in self.catalog.paths.get_children(path, child_type=catalog.Dir, recursive=recursive)]
|
|
431
|
+
|
|
432
|
+
# TODO: for now, named functions are deprecated, until we understand the use case and requirements better
|
|
433
|
+
# def create_function(self, path_str: str, fn: func.Function) -> None:
|
|
434
|
+
# """Create a stored function.
|
|
435
|
+
#
|
|
436
|
+
# Args:
|
|
437
|
+
# path_str: path where the function gets stored
|
|
438
|
+
# func: previously created Function object
|
|
439
|
+
#
|
|
440
|
+
# Raises:
|
|
441
|
+
# Error: if the path already exists or the parent is not a directory
|
|
442
|
+
#
|
|
443
|
+
# Examples:
|
|
444
|
+
# Create a function ``detect()`` that takes an image and returns a JSON object, and store it in ``my_dir``:
|
|
445
|
+
#
|
|
446
|
+
# >>> @pxt.udf(param_types=[ImageType()], return_type=JsonType())
|
|
447
|
+
# ... def detect(img):
|
|
448
|
+
# ... ...
|
|
449
|
+
# >>> cl.create_function('my_dir.detect', detect)
|
|
450
|
+
# """
|
|
451
|
+
# if fn.is_module_function:
|
|
452
|
+
# raise excs.Error(f'Cannot create a named function for a library function')
|
|
453
|
+
# path = catalog.Path(path_str)
|
|
454
|
+
# self.catalog.paths.check_is_valid(path, expected=None)
|
|
455
|
+
# dir = self.catalog.paths[path.parent]
|
|
456
|
+
#
|
|
457
|
+
# func.FunctionRegistry.get().create_function(fn, dir._id, path.name)
|
|
458
|
+
# self.catalog.paths[path] = catalog.NamedFunction(fn.id, dir._id, path.name)
|
|
459
|
+
# fn.md.fqn = str(path)
|
|
460
|
+
# _logger.info(f'Created function {path_str}')
|
|
461
|
+
#
|
|
462
|
+
# def update_function(self, path_str: str, fn: func.Function) -> None:
|
|
463
|
+
# """Update the implementation of a stored function.
|
|
464
|
+
#
|
|
465
|
+
# Args:
|
|
466
|
+
# path_str: path to the function to be updated
|
|
467
|
+
# func: new function implementation
|
|
468
|
+
#
|
|
469
|
+
# Raises:
|
|
470
|
+
# Error: if the path does not exist or ``func`` has a different signature than the stored function.
|
|
471
|
+
# """
|
|
472
|
+
# if fn.is_module_function:
|
|
473
|
+
# raise excs.Error(f'Cannot update a named function to a library function')
|
|
474
|
+
# path = catalog.Path(path_str)
|
|
475
|
+
# self.catalog.paths.check_is_valid(path, expected=catalog.NamedFunction)
|
|
476
|
+
# named_fn = self.catalog.paths[path]
|
|
477
|
+
# f = func.FunctionRegistry.get().get_function(id=named_fn._id)
|
|
478
|
+
# if f.md.signature != fn.md.signature:
|
|
479
|
+
# raise excs.Error(
|
|
480
|
+
# f'The function signature cannot be changed. The existing signature is {f.md.signature}')
|
|
481
|
+
# if f.is_aggregate != fn.is_aggregate:
|
|
482
|
+
# raise excs.Error(f'Cannot change an aggregate function into a non-aggregate function and vice versa')
|
|
483
|
+
# func.FunctionRegistry.get().update_function(named_fn._id, fn)
|
|
484
|
+
# _logger.info(f'Updated function {path_str}')
|
|
485
|
+
#
|
|
486
|
+
# def get_function(self, path_str: str) -> func.Function:
|
|
487
|
+
# """Get a handle to a stored function.
|
|
488
|
+
#
|
|
489
|
+
# Args:
|
|
490
|
+
# path_str: path to the function
|
|
491
|
+
#
|
|
492
|
+
# Returns:
|
|
493
|
+
# Function object
|
|
494
|
+
#
|
|
495
|
+
# Raises:
|
|
496
|
+
# Error: if the path does not exist or is not a function
|
|
497
|
+
#
|
|
498
|
+
# Examples:
|
|
499
|
+
# >>> detect = cl.get_function('my_dir.detect')
|
|
500
|
+
# """
|
|
501
|
+
# path = catalog.Path(path_str)
|
|
502
|
+
# self.catalog.paths.check_is_valid(path, expected=catalog.NamedFunction)
|
|
503
|
+
# named_fn = self.catalog.paths[path]
|
|
504
|
+
# assert isinstance(named_fn, catalog.NamedFunction)
|
|
505
|
+
# fn = func.FunctionRegistry.get().get_function(id=named_fn._id)
|
|
506
|
+
# fn.md.fqn = str(path)
|
|
507
|
+
# return fn
|
|
508
|
+
#
|
|
509
|
+
# def drop_function(self, path_str: str, ignore_errors: bool = False) -> None:
|
|
510
|
+
# """Deletes stored function.
|
|
511
|
+
#
|
|
512
|
+
# Args:
|
|
513
|
+
# path_str: path to the function
|
|
514
|
+
# ignore_errors: if True, does not raise if the function does not exist
|
|
515
|
+
#
|
|
516
|
+
# Raises:
|
|
517
|
+
# Error: if the path does not exist or is not a function
|
|
518
|
+
#
|
|
519
|
+
# Examples:
|
|
520
|
+
# >>> cl.drop_function('my_dir.detect')
|
|
521
|
+
# """
|
|
522
|
+
# path = catalog.Path(path_str)
|
|
523
|
+
# try:
|
|
524
|
+
# self.catalog.paths.check_is_valid(path, expected=catalog.NamedFunction)
|
|
525
|
+
# except excs.Error as e:
|
|
526
|
+
# if ignore_errors:
|
|
527
|
+
# return
|
|
528
|
+
# else:
|
|
529
|
+
# raise e
|
|
530
|
+
# named_fn = self.catalog.paths[path]
|
|
531
|
+
# func.FunctionRegistry.get().delete_function(named_fn._id)
|
|
532
|
+
# del self.catalog.paths[path]
|
|
533
|
+
# _logger.info(f'Dropped function {path_str}')
|
|
534
|
+
|