|
8 | 8 | ) |
9 | 9 | from skyflow_flowvault.utils import SkyflowMessages |
10 | 10 | from skyflow_flowvault.utils.enums import UpsertType |
11 | | -from skyflow_flowvault.vault.data import Upsert |
12 | 11 |
|
13 | 12 | VALID_INSERT_RECORD_KEYS = ["values", "table", "upsert"] |
| 13 | +VALID_UPSERT_KEYS = ["update_type", "unique_columns"] |
14 | 14 |
|
15 | 15 | invalid_input_error_code = CommonMessages.ErrorCodes.INVALID_INPUT.value |
16 | 16 |
|
|
19 | 19 | # identical to v2's, confirmed, so both variants now share one implementation. |
20 | 20 |
|
21 | 21 |
|
22 | | -def _validate_upsert(upsert): |
| 22 | +def _validate_upsert(logger, upsert): |
23 | 23 | if upsert is None: |
24 | 24 | return |
25 | | - if not isinstance(upsert, Upsert): |
| 25 | + if not isinstance(upsert, dict): |
26 | 26 | raise SkyflowError(SkyflowMessages.Error.INVALID_UPSERT_TYPE_IN_INSERT.value, invalid_input_error_code) |
27 | | - if (not isinstance(upsert.unique_columns, list) or not upsert.unique_columns |
28 | | - or not all(isinstance(c, str) for c in upsert.unique_columns)): |
| 27 | + validate_keys(logger, upsert, VALID_UPSERT_KEYS) |
| 28 | + unique_columns = upsert.get("unique_columns") |
| 29 | + if (not isinstance(unique_columns, list) or not unique_columns |
| 30 | + or not all(isinstance(c, str) for c in unique_columns)): |
29 | 31 | raise SkyflowError(SkyflowMessages.Error.INVALID_UPSERT_UNIQUE_COLUMNS_IN_INSERT.value, invalid_input_error_code) |
30 | | - if upsert.update_type is not None and not isinstance(upsert.update_type, UpsertType): |
| 32 | + update_type = upsert.get("update_type") |
| 33 | + if update_type is not None and not isinstance(update_type, UpsertType): |
31 | 34 | raise SkyflowError(SkyflowMessages.Error.INVALID_UPSERT_UPDATE_TYPE_IN_INSERT.value, invalid_input_error_code) |
32 | 35 |
|
33 | 36 |
|
34 | 37 | MAX_INSERT_RECORDS = 10000 # matches Java's v3 Validations.validateInsertRequest (hardcoded, not configurable) |
35 | 38 |
|
36 | 39 |
|
37 | 40 | def validate_insert_request(logger, request): |
38 | | - if not isinstance(request.records, list) or not all(isinstance(r, dict) for r in request.records): |
| 41 | + if not isinstance(request.values, list) or not all(isinstance(r, dict) for r in request.values): |
39 | 42 | raise SkyflowError(SkyflowMessages.Error.INVALID_RECORDS_TYPE_IN_INSERT.value, invalid_input_error_code) |
40 | 43 |
|
41 | | - if not request.records: |
| 44 | + if not request.values: |
42 | 45 | raise SkyflowError(SkyflowMessages.Error.EMPTY_RECORDS_IN_INSERT.value, invalid_input_error_code) |
43 | 46 |
|
44 | | - if len(request.records) > MAX_INSERT_RECORDS: |
| 47 | + if len(request.values) > MAX_INSERT_RECORDS: |
45 | 48 | raise SkyflowError(SkyflowMessages.Error.TOO_MANY_RECORDS_IN_INSERT.value, invalid_input_error_code) |
46 | 49 |
|
47 | 50 | # request.table/record["table"] format and record["values"] emptiness/key/value validity are |
48 | 51 | # checked by the controller via the shared BaseVaultController._validate_table_name_if_present() |
49 | 52 | # / _validate_field_values() -- not here, to avoid duplicating that logic. |
50 | 53 |
|
51 | | - _validate_upsert(request.upsert) |
| 54 | + _validate_upsert(logger, request.upsert) |
52 | 55 |
|
53 | | - for record in request.records: |
| 56 | + for record in request.values: |
54 | 57 | validate_keys(logger, record, VALID_INSERT_RECORD_KEYS) |
55 | | - _validate_upsert(record.get("upsert")) |
| 58 | + _validate_upsert(logger, record.get("upsert")) |
56 | 59 |
|
57 | 60 | # table must be set in exactly one place -- request-level (every record) or per-record (no |
58 | 61 | # partial mix) -- and upsert must live at that same place (mirrors Java's v3 Validations). |
59 | 62 | table_at_request_level = request.table is not None |
60 | 63 |
|
61 | 64 | if table_at_request_level: |
62 | | - for record in request.records: |
| 65 | + for record in request.values: |
63 | 66 | if record.get("table") is not None: |
64 | 67 | raise SkyflowError(SkyflowMessages.Error.TABLE_NAME_IN_BOTH_PLACES_IN_INSERT.value, invalid_input_error_code) |
65 | 68 | else: |
66 | | - for record in request.records: |
| 69 | + for record in request.values: |
67 | 70 | if record.get("table") is None: |
68 | 71 | raise SkyflowError(SkyflowMessages.Error.TABLE_NAME_MISSING_IN_INSERT.value, invalid_input_error_code) |
69 | 72 |
|
70 | 73 | if table_at_request_level: |
71 | | - for record in request.records: |
| 74 | + for record in request.values: |
72 | 75 | if record.get("upsert") is not None: |
73 | 76 | raise SkyflowError(SkyflowMessages.Error.RECORD_LEVEL_UPSERT_NOT_ALLOWED_IN_INSERT.value, invalid_input_error_code) |
74 | 77 | else: |
|
0 commit comments