Skip to content

Commit 7bc7b16

Browse files
committed
more work on scalar package
1 parent 6b09d3b commit 7bc7b16

File tree

8 files changed

+75
-6
lines changed

8 files changed

+75
-6
lines changed

cloudquery/sdk/scalar/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from .scalar import Scalar, ScalarInvalidTypeError
1+
from .scalar import Scalar, ScalarInvalidTypeError, NULL_VALUE
22
from .scalar_factory import ScalarFactory
33
from .binary import Binary
44
from .bool import Bool
55
from .date32 import Date32
66
from .float64 import Float64
7-
from .int64 import Int64
7+
from .int64 import Int64
8+
from .uuid import UUID

cloudquery/sdk/scalar/binary.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
2+
from .scalar import NULL_VALUE
23

34
class Binary(Scalar):
45
def __init__(self, valid: bool = False, value: bytes = None):
@@ -11,6 +12,9 @@ def __eq__(self, scalar: Scalar) -> bool:
1112
if type(scalar) == Binary:
1213
return self._value == scalar._value and self._valid == scalar._valid
1314
return False
15+
16+
def __str__(self) -> str:
17+
return str(self._value) if self._valid else NULL_VALUE
1418

1519
@property
1620
def value(self):

cloudquery/sdk/scalar/bool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
2+
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError, NULL_VALUE
33
from typing import Any
44

55
def parse_string_to_bool(input_string):
@@ -27,6 +27,9 @@ def __eq__(self, scalar: Scalar) -> bool:
2727
return self._value == scalar._value and self._valid == scalar._valid
2828
return False
2929

30+
def __str__(self) -> str:
31+
return str(self._value) if self._valid else NULL_VALUE
32+
3033
@property
3134
def value(self):
3235
return self._value

cloudquery/sdk/scalar/date32.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
2+
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError, NULL_VALUE
33
from datetime import datetime, time
44
from typing import Any
55

@@ -15,6 +15,9 @@ def __eq__(self, scalar: Scalar) -> bool:
1515
return self._value == scalar._value and self._valid == scalar._valid
1616
return False
1717

18+
def __str__(self) -> str:
19+
return str(self._value) if self._valid else NULL_VALUE
20+
1821
@property
1922
def value(self):
2023
return self._value

cloudquery/sdk/scalar/scalar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
NULL_VALUE = "null"
3+
24
class ScalarInvalidTypeError(Exception):
35
pass
46

cloudquery/sdk/scalar/uuid.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import uuid
2+
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
3+
4+
class UUID(Scalar):
5+
def __init__(self, valid: bool = False, value: uuid.UUID = None):
6+
self._valid = valid
7+
self._value = value
8+
9+
def __eq__(self, scalar: Scalar) -> bool:
10+
if scalar is None:
11+
return False
12+
if type(scalar) == UUID:
13+
return self._value == scalar._value and self._valid == scalar._valid
14+
return False
15+
16+
@property
17+
def value(self):
18+
return self._value
19+
20+
def set(self, value):
21+
if value is None:
22+
return
23+
24+
if type(value) == uuid.UUID:
25+
self._value = value
26+
elif type(value) == str:
27+
try:
28+
self._value = uuid.UUID(value)
29+
except ValueError as e:
30+
raise ScalarInvalidTypeError("Invalid type for UUID scalar") from e
31+
else:
32+
raise ScalarInvalidTypeError("Invalid type {} for UUID scalar".format(type(value)))
33+
self._valid = True

cloudquery/sdk/types/uuid.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import pyarrow as pa
2+
import pyarrow
3+
import sys
24

35
class UuidType(pa.PyExtensionType):
4-
56
def __init__(self):
67
pa.PyExtensionType.__init__(self, pa.binary(16))
78

89
def __reduce__(self):
9-
return UuidType, ()
10+
return UuidType, ()
11+
12+
def __arrow_ext_serialize__(self):
13+
# since we don't have a parameterized type, we don't need extra
14+
# metadata to be deserialized
15+
return b'uuid-serialized'
16+
17+
@classmethod
18+
def __arrow_ext_deserialize__(self, storage_type, serialized):
19+
# return an instance of this subclass given the serialized
20+
# metadata.
21+
return UuidType()

tests/scalar/uuid.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
import uuid
3+
from cloudquery.sdk.scalar import UUID
4+
5+
@pytest.mark.parametrize("input_value,expected_scalar", [
6+
("550e8400-e29b-41d4-a716-446655440000", UUID(True, uuid.UUID("550e8400-e29b-41d4-a716-446655440000"))),
7+
])
8+
def test_binary_set(input_value, expected_scalar):
9+
b = UUID()
10+
b.set(input_value)
11+
assert b == expected_scalar

0 commit comments

Comments
 (0)