-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrest_api.py
More file actions
executable file
·719 lines (625 loc) · 27 KB
/
Copy pathrest_api.py
File metadata and controls
executable file
·719 lines (625 loc) · 27 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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 logging
from typing import Callable, Dict, List, Optional, Union
import re
from pypaimon.api.api_request import (AlterDatabaseRequest, AlterFunctionRequest,
AlterTableRequest, CommitTableRequest,
CreateBranchRequest, CreateDatabaseRequest,
CreateFunctionRequest, CreateTableRequest,
CreateTagRequest, ForwardBranchRequest,
RenameBranchRequest, RenameTableRequest,
RollbackTableRequest)
from pypaimon.api.api_response import (CommitTableResponse, ConfigResponse,
GetDatabaseResponse, GetFunctionResponse,
GetTableResponse,
GetTableTokenResponse, GetTagResponse,
ListBranchesResponse,
ListDatabasesResponse,
ListFunctionDetailsResponse,
ListFunctionsGloballyResponse,
ListFunctionsResponse,
ListPartitionsResponse,
ListTablesResponse, ListTagsResponse,
PagedList,
PagedResponse, GetTableSnapshotResponse,
Partition,
AuthTableQueryRequest,
AuthTableQueryResponse)
from pypaimon.api.auth import AuthProviderFactory, RESTAuthFunction
from pypaimon.api.client import HttpClient
from pypaimon.api.resource_paths import ResourcePaths
from pypaimon.api.rest_util import RESTUtil
from pypaimon.api.typedef import T
from pypaimon.common.options import Options
from pypaimon.common.options.config import CatalogOptions
from pypaimon.common.identifier import Identifier
from pypaimon.schema.schema import Schema
from pypaimon.snapshot.snapshot import Snapshot
from pypaimon.snapshot.snapshot_commit import PartitionStatistics
class RESTApi:
HEADER_PREFIX = "header."
MAX_RESULTS = "maxResults"
PAGE_TOKEN = "pageToken"
DATABASE_NAME_PATTERN = "databaseNamePattern"
TABLE_NAME_PATTERN = "tableNamePattern"
TABLE_TYPE = "tableType"
FUNCTION_NAME_PATTERN = "functionNamePattern"
PARTITION_NAME_PATTERN = "partitionNamePattern"
TAG_NAME_PREFIX = "tagNamePrefix"
TOKEN_EXPIRATION_SAFE_TIME_MILLIS = 3_600_000
# Function name validation pattern
_FUNCTION_NAME_PATTERN = re.compile(r'^(?=.*[A-Za-z])[A-Za-z0-9._-]+$')
def __init__(self, options: Union[Options, Dict[str, str]], config_required: bool = True):
if isinstance(options, dict):
options = Options(options)
if not options:
raise ValueError("Options cannot be None or empty")
uri = options.get(CatalogOptions.URI)
if not uri or not uri.strip():
raise ValueError("URI cannot be empty")
self.logger = logging.getLogger(self.__class__.__name__)
self.client = HttpClient(uri)
auth_provider = AuthProviderFactory.create_auth_provider(options)
base_headers = RESTUtil.extract_prefix_map(options, self.HEADER_PREFIX)
if config_required:
warehouse = options.get(CatalogOptions.WAREHOUSE)
if not warehouse or not warehouse.strip():
raise ValueError("Warehouse name cannot be empty")
query_params = {
CatalogOptions.WAREHOUSE.key(): RESTUtil.encode_string(warehouse)
}
config_response = self.client.get_with_params(
ResourcePaths.config(),
query_params,
ConfigResponse,
RESTAuthFunction(base_headers, auth_provider),
)
options = config_response.merge(options)
base_headers.update(
RESTUtil.extract_prefix_map(options, self.HEADER_PREFIX)
)
self.rest_auth_function = RESTAuthFunction(base_headers, auth_provider)
self.options = options
self.resource_paths = ResourcePaths.for_catalog_properties(options)
def __build_paged_query_params(
self,
max_results: Optional[int],
page_token: Optional[str],
name_patterns: Dict[str, str],
) -> Dict[str, str]:
query_params = {}
if max_results is not None and max_results > 0:
query_params[RESTApi.MAX_RESULTS] = str(max_results)
if page_token is not None and page_token.strip():
query_params[RESTApi.PAGE_TOKEN] = page_token
for key, value in name_patterns.items():
if key and value and key.strip() and value.strip():
query_params[key] = value
return query_params
def __list_data_from_page_api(
self, page_api: Callable[[Dict[str, str]], PagedResponse[T]]
) -> List[T]:
results = []
query_params = {}
page_token = None
while True:
if page_token:
query_params[RESTApi.PAGE_TOKEN] = page_token
elif RESTApi.PAGE_TOKEN in query_params:
del query_params[RESTApi.PAGE_TOKEN]
response = page_api(query_params)
if response.data:
results.extend(response.data())
page_token = response.next_page_token
if not page_token or not response.data:
break
return results
def get_options(self) -> Options:
return self.options
def list_databases(self) -> List[str]:
return self.__list_data_from_page_api(
lambda query_params: self.client.get_with_params(
self.resource_paths.databases(),
query_params,
ListDatabasesResponse,
self.rest_auth_function,
)
)
def list_databases_paged(
self,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
database_name_pattern: Optional[str] = None,
) -> PagedList[str]:
response = self.client.get_with_params(
self.resource_paths.databases(),
self.__build_paged_query_params(
max_results,
page_token,
{self.DATABASE_NAME_PATTERN: database_name_pattern},
),
ListDatabasesResponse,
self.rest_auth_function,
)
databases = response.data() or []
return PagedList(databases, response.get_next_page_token())
def create_database(self, name: str, properties: Dict[str, str]) -> None:
if not name or not name.strip():
raise ValueError("Database name cannot be empty")
request = CreateDatabaseRequest(name, properties)
self.client.post(
self.resource_paths.databases(), request, self.rest_auth_function
)
def get_database(self, name: str) -> GetDatabaseResponse:
if not name or not name.strip():
raise ValueError("Database name cannot be empty")
return self.client.get(
self.resource_paths.database(name),
GetDatabaseResponse,
self.rest_auth_function,
)
def drop_database(self, name: str) -> None:
if not name or not name.strip():
raise ValueError("Database name cannot be empty")
self.client.delete(
self.resource_paths.database(name),
self.rest_auth_function)
def alter_database(
self,
name: str,
removals: Optional[List[str]] = None,
updates: Optional[Dict[str, str]] = None,
):
if not name or not name.strip():
raise ValueError("Database name cannot be empty")
removals = removals or []
updates = updates or {}
request = AlterDatabaseRequest(removals, updates)
return self.client.post(
self.resource_paths.database(name),
request,
self.rest_auth_function)
def list_tables(self, database_name: str) -> List[str]:
if not database_name or not database_name.strip():
raise ValueError("Database name cannot be empty")
return self.__list_data_from_page_api(
lambda query_params: self.client.get_with_params(
self.resource_paths.tables(database_name),
query_params,
ListTablesResponse,
self.rest_auth_function,
)
)
def list_tables_paged(
self,
database_name: str,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
table_name_pattern: Optional[str] = None,
table_type: Optional[str] = None,
) -> PagedList[str]:
if not database_name or not database_name.strip():
raise ValueError("Database name cannot be empty")
response = self.client.get_with_params(
self.resource_paths.tables(database_name),
self.__build_paged_query_params(
max_results,
page_token,
{
self.TABLE_NAME_PATTERN: table_name_pattern,
self.TABLE_TYPE: table_type,
},
),
ListTablesResponse,
self.rest_auth_function,
)
tables = response.data() or []
return PagedList(tables, response.get_next_page_token())
def create_table(self, identifier: Identifier, schema: Schema) -> None:
database_name, _ = self.__validate_identifier(identifier)
if not schema:
raise ValueError("Schema cannot be None")
request = CreateTableRequest(identifier, schema)
return self.client.post(
self.resource_paths.tables(database_name),
request,
self.rest_auth_function)
def get_table(self, identifier: Identifier) -> GetTableResponse:
database_name, table_name = self.__validate_identifier(identifier)
return self.client.get(
self.resource_paths.table(
database_name,
table_name),
GetTableResponse,
self.rest_auth_function,
)
def drop_table(self, identifier: Identifier) -> GetTableResponse:
database_name, table_name = self.__validate_identifier(identifier)
return self.client.delete(
self.resource_paths.table(
database_name,
table_name),
self.rest_auth_function,
)
def rename_table(self, source_identifier: Identifier, target_identifier: Identifier) -> None:
if not source_identifier:
raise ValueError("Source identifier cannot be None")
if not target_identifier:
raise ValueError("Target identifier cannot be None")
self.__validate_identifier(source_identifier)
self.__validate_identifier(target_identifier)
request = RenameTableRequest(source_identifier, target_identifier)
return self.client.post(
self.resource_paths.rename_table(),
request,
self.rest_auth_function)
def alter_table(self, identifier: Identifier, changes: List):
database_name, table_name = self.__validate_identifier(identifier)
if not changes:
raise ValueError("Changes cannot be empty")
request = AlterTableRequest(changes)
return self.client.post(
self.resource_paths.table(database_name, table_name),
request,
self.rest_auth_function)
def load_table_token(self, identifier: Identifier) -> GetTableTokenResponse:
database_name, table_name = self.__validate_identifier(identifier)
return self.client.get(
self.resource_paths.table_token(
database_name,
table_name),
GetTableTokenResponse,
self.rest_auth_function,
)
def commit_snapshot(
self,
identifier: Identifier,
table_uuid: Optional[str],
snapshot: Snapshot,
statistics: List[PartitionStatistics]
) -> bool:
"""
Commit snapshot for table.
Args:
identifier: Database name and table name
table_uuid: UUID of the table to avoid wrong commit
snapshot: Snapshot for committing
statistics: Statistics for this snapshot incremental
Returns:
True if commit success
Raises:
NoSuchResourceException: Exception thrown on HTTP 404 means the table not exists
ForbiddenException: Exception thrown on HTTP 403 means don't have the permission for this table
"""
database_name, table_name = self.__validate_identifier(identifier)
if not snapshot:
raise ValueError("Snapshot cannot be None")
if statistics is None:
raise ValueError("Statistics cannot be None")
request = CommitTableRequest(table_uuid, snapshot, statistics)
response = self.client.post_with_response_type(
self.resource_paths.commit_table(
database_name, table_name),
request,
CommitTableResponse,
self.rest_auth_function
)
return response.is_success()
def rollback_to(self, identifier, instant, from_snapshot=None):
"""Rollback table to the given instant.
Args:
identifier: The table identifier.
instant: The Instant (SnapshotInstant or TagInstant) to rollback to.
from_snapshot: Optional snapshot ID. Success only occurs when the
latest snapshot is this snapshot.
Raises:
NoSuchResourceException: If the table, snapshot or tag does not exist.
ForbiddenException: If no permission to access this table.
"""
database_name, table_name = self.__validate_identifier(identifier)
request = RollbackTableRequest(instant=instant, from_snapshot=from_snapshot)
self.client.post(
self.resource_paths.rollback_table(database_name, table_name),
request,
self.rest_auth_function
)
def load_snapshot(self, identifier: Identifier) -> Optional['TableSnapshot']:
"""Load latest snapshot for table.
Args:
identifier: Database name and table name.
Returns:
TableSnapshot instance or None if snapshot not found.
"""
database_name, table_name = self.__validate_identifier(identifier)
response = self.client.get(
self.resource_paths.table_snapshot(database_name, table_name),
GetTableSnapshotResponse,
self.rest_auth_function
)
if response is None:
return None
return response.get_snapshot()
def list_partitions_paged(
self,
identifier: Identifier,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
partition_name_pattern: Optional[str] = None,
) -> PagedList[Partition]:
database_name, table_name = self.__validate_identifier(identifier)
response = self.client.get_with_params(
self.resource_paths.partitions(database_name, table_name),
self.__build_paged_query_params(
max_results,
page_token,
{self.PARTITION_NAME_PATTERN: partition_name_pattern},
),
ListPartitionsResponse,
self.rest_auth_function,
)
partitions = response.data() or []
return PagedList(partitions, response.get_next_page_token())
# Tag CRUD wrappers — mirror Java RESTApi tag methods.
def create_tag(
self,
identifier: Identifier,
tag_name: str,
snapshot_id: Optional[int] = None,
time_retained: Optional[str] = None,
) -> None:
database_name, table_name = self.__validate_identifier(identifier)
request = CreateTagRequest(
tag_name=tag_name,
snapshot_id=snapshot_id,
time_retained=time_retained,
)
self.client.post(
self.resource_paths.tags(database_name, table_name),
request,
self.rest_auth_function,
)
def get_tag(self, identifier: Identifier, tag_name: str) -> GetTagResponse:
database_name, table_name = self.__validate_identifier(identifier)
return self.client.get(
self.resource_paths.tag(database_name, table_name, tag_name),
GetTagResponse,
self.rest_auth_function,
)
def list_tags_paged(
self,
identifier: Identifier,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
tag_name_prefix: Optional[str] = None,
) -> PagedList[str]:
database_name, table_name = self.__validate_identifier(identifier)
response = self.client.get_with_params(
self.resource_paths.tags(database_name, table_name),
self.__build_paged_query_params(
max_results,
page_token,
{self.TAG_NAME_PREFIX: tag_name_prefix},
),
ListTagsResponse,
self.rest_auth_function,
)
tags = response.data() or []
return PagedList(tags, response.get_next_page_token())
def delete_tag(self, identifier: Identifier, tag_name: str) -> None:
database_name, table_name = self.__validate_identifier(identifier)
self.client.delete(
self.resource_paths.tag(database_name, table_name, tag_name),
self.rest_auth_function,
)
# Branch CRUD wrappers — mirror Java RESTApi branch methods.
def create_branch(
self,
identifier: Identifier,
branch_name: str,
tag_name: Optional[str] = None,
) -> None:
database_name, table_name = self.__validate_identifier(identifier)
if not branch_name or not branch_name.strip():
raise ValueError("Branch name cannot be empty")
request = CreateBranchRequest(branch=branch_name, from_tag=tag_name)
self.client.post(
self.resource_paths.branches(database_name, table_name),
request,
self.rest_auth_function,
)
def drop_branch(self, identifier: Identifier, branch_name: str) -> None:
database_name, table_name = self.__validate_identifier(identifier)
self.client.delete(
self.resource_paths.branch(database_name, table_name, branch_name),
self.rest_auth_function,
)
def rename_branch(
self,
identifier: Identifier,
from_branch: str,
to_branch: str,
) -> None:
database_name, table_name = self.__validate_identifier(identifier)
if not to_branch or not to_branch.strip():
raise ValueError("Target branch name cannot be empty")
request = RenameBranchRequest(to_branch=to_branch)
self.client.post(
self.resource_paths.rename_branch(database_name, table_name, from_branch),
request,
self.rest_auth_function,
)
def fast_forward(self, identifier: Identifier, branch_name: str) -> None:
database_name, table_name = self.__validate_identifier(identifier)
self.client.post(
self.resource_paths.forward_branch(database_name, table_name, branch_name),
ForwardBranchRequest(),
self.rest_auth_function,
)
def list_branches(self, identifier: Identifier) -> List[str]:
database_name, table_name = self.__validate_identifier(identifier)
response = self.client.get(
self.resource_paths.branches(database_name, table_name),
ListBranchesResponse,
self.rest_auth_function,
)
return response.branches or []
@staticmethod
def is_valid_function_name(name: str) -> bool:
if not name:
return False
return RESTApi._FUNCTION_NAME_PATTERN.match(name) is not None
@staticmethod
def check_function_name(name: str) -> None:
if not RESTApi.is_valid_function_name(name):
raise IllegalArgumentError("Invalid function name: " + str(name))
def list_functions(self, database_name: str) -> List[str]:
return self.__list_data_from_page_api(
lambda query_params: self.client.get_with_params(
self.resource_paths.functions(database_name),
query_params,
ListFunctionsResponse,
self.rest_auth_function,
)
)
def list_functions_paged(
self,
database_name: str,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
function_name_pattern: Optional[str] = None,
) -> PagedList[str]:
response = self.client.get_with_params(
self.resource_paths.functions(database_name),
self.__build_paged_query_params(
max_results,
page_token,
{self.FUNCTION_NAME_PATTERN: function_name_pattern},
),
ListFunctionsResponse,
self.rest_auth_function,
)
functions = response.functions if response.functions else []
return PagedList(functions, response.get_next_page_token())
def list_function_details_paged(
self,
database_name: str,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
function_name_pattern: Optional[str] = None,
) -> PagedList[GetFunctionResponse]:
response = self.client.get_with_params(
self.resource_paths.function_details(database_name),
self.__build_paged_query_params(
max_results,
page_token,
{self.FUNCTION_NAME_PATTERN: function_name_pattern},
),
ListFunctionDetailsResponse,
self.rest_auth_function,
)
function_details = response.data() if response.data() else []
return PagedList(function_details, response.get_next_page_token())
def list_functions_paged_globally(
self,
database_name_pattern: Optional[str] = None,
function_name_pattern: Optional[str] = None,
max_results: Optional[int] = None,
page_token: Optional[str] = None,
) -> PagedList:
response = self.client.get_with_params(
self.resource_paths.functions(),
self.__build_paged_query_params(
max_results,
page_token,
{
self.DATABASE_NAME_PATTERN: database_name_pattern,
self.FUNCTION_NAME_PATTERN: function_name_pattern,
},
),
ListFunctionsGloballyResponse,
self.rest_auth_function,
)
functions = response.data() if response.data() else []
return PagedList(functions, response.get_next_page_token())
def get_function(self, identifier: Identifier) -> GetFunctionResponse:
from pypaimon.api.rest_exception import NoSuchResourceException
if not self.is_valid_function_name(identifier.get_object_name()):
raise NoSuchResourceException(
"FUNCTION",
identifier.get_object_name(),
"Invalid function name: " + identifier.get_object_name(),
)
return self.client.get(
self.resource_paths.function(
identifier.get_database_name(), identifier.get_object_name()),
GetFunctionResponse,
self.rest_auth_function,
)
def create_function(self, identifier: Identifier, function) -> None:
self.check_function_name(identifier.get_object_name())
request = CreateFunctionRequest(
name=function.name(),
input_params=function.input_params(),
return_params=function.return_params(),
deterministic=function.is_deterministic(),
definitions=function.definitions(),
comment=function.comment(),
options=function.options(),
)
self.client.post(
self.resource_paths.functions(identifier.get_database_name()),
request,
self.rest_auth_function,
)
def drop_function(self, identifier: Identifier) -> None:
self.check_function_name(identifier.get_object_name())
self.client.delete(
self.resource_paths.function(
identifier.get_database_name(), identifier.get_object_name()),
self.rest_auth_function,
)
def alter_function(self, identifier: Identifier, changes: List) -> None:
self.check_function_name(identifier.get_object_name())
request = AlterFunctionRequest(changes=changes)
self.client.post(
self.resource_paths.function(
identifier.get_database_name(), identifier.get_object_name()),
request,
self.rest_auth_function,
)
def auth_table_query(self, identifier: Identifier, select: Optional[List[str]]) -> AuthTableQueryResponse:
database_name, table_name = self.__validate_identifier(identifier)
request = AuthTableQueryRequest(select=select)
return self.client.post_with_response_type(
self.resource_paths.auth_table(database_name, table_name),
request,
AuthTableQueryResponse,
self.rest_auth_function,
)
@staticmethod
def __validate_identifier(identifier: Identifier):
if not identifier:
raise ValueError("Identifier cannot be None")
database_name = identifier.get_database_name()
if not database_name or not database_name.strip():
raise ValueError("Database name cannot be empty")
table_name = identifier.get_object_name()
if not table_name or not table_name.strip():
raise ValueError("Table name cannot be None")
return database_name.strip(), table_name.strip()
from pypaimon.catalog.catalog_exception import IllegalArgumentError