Skip to content

Commit

Permalink
0.0.176
Browse files Browse the repository at this point in the history
  • Loading branch information
joocer committed Oct 17, 2024
1 parent 2d304da commit af4809a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
8 changes: 3 additions & 5 deletions orso/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import datetime
import time
from functools import cached_property
from typing import Any
from typing import Dict
from typing import List
Expand Down Expand Up @@ -70,7 +71,6 @@ def extract_columns(table: List[Dict[str, Any]], columns: List[str]) -> Tuple[Li
class Row(tuple):
__slots__ = ()
_fields: Tuple[str, ...] = None
_cached_map: Tuple[Tuple[str, Any]] = None
_cached_byte_size: int = None
_key: Tuple[int, int] = None

Expand Down Expand Up @@ -98,17 +98,15 @@ def get(self, item, default=None):
return default
return self[index]

@property
@cached_property
def as_map(self) -> Tuple[Tuple[str, Any], ...]:
"""
Returns the Row as a tuple of key-value pair tuples (a 'map').
Returns:
A tuple of key-value pair tuples.
"""
if self._cached_map is None:
self._cached_map = tuple(zip(self._fields, self)) # type:ignore
return self._cached_map
return tuple(zip(self._fields, self))

@property
def as_dict(self) -> Dict[str, Any]:
Expand Down
11 changes: 7 additions & 4 deletions orso/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
from dataclasses import dataclass
from dataclasses import field
from dataclasses import fields
from decimal import Decimal
from decimal import getcontext
from enum import Enum
from typing import Any
Expand Down Expand Up @@ -219,7 +218,7 @@ def materialize(self):
raise TypeError("Cannot materialize FlatColumns")

@classmethod
def from_arrow(cls, arrow_field) -> "FlatColumn":
def from_arrow(cls, arrow_field, mappable_as_binary: bool = False) -> "FlatColumn":
"""
Converts a PyArrow field to an Orso FlatColumn object.
Expand All @@ -230,21 +229,25 @@ def from_arrow(cls, arrow_field) -> "FlatColumn":
Returns:
FlatColumn: A FlatColumn object containing the converted information.
"""
from orso.tools import DecimalFactory

# Fetch the native type mapping from Arrow to Python native types
native_type = arrow_type_map(arrow_field.type)
# Initialize variables to hold optional decimal properties
scale: Optional[int] = None
precision: Optional[int] = None
# Check if the type is Decimal and populate scale and precision
if isinstance(native_type, Decimal):
if isinstance(native_type, DecimalFactory):
field_type = OrsoTypes.DECIMAL
scale = native_type.scale # type:ignore
precision = native_type.precision # type:ignore
elif mappable_as_binary and native_type == dict:
field_type = OrsoTypes.BLOB
else:
# Fall back to the generic mapping
field_type = PYTHON_TO_ORSO_MAP.get(native_type, None)
if field_type is None:
raise ValueError(f"Unsupported type: {native_type}")
field_type = OrsoTypes.VARCHAR

return FlatColumn(
name=str(arrow_field.name),
Expand Down
2 changes: 1 addition & 1 deletion orso/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__: str = "0.0.175"
__version__: str = "0.0.176"
__author__: str = "@joocer"
14 changes: 14 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def test_validate_with_valid_data():
assert cities.schema.validate(data) == True


def test_struct_to_blob():
import pyarrow

struct_type = pyarrow.struct([pyarrow.field('subfield', pyarrow.int32())])
arrow_col = pyarrow.field(name="column", type=struct_type)

assert arrow_col.type == struct_type, arrow_col.type

orso_reformed = FlatColumn.from_arrow(arrow_col)
assert orso_reformed.type == OrsoTypes.STRUCT

orso_struct_as_blob_col = FlatColumn.from_arrow(arrow_col, mappable_as_binary=True)
assert orso_struct_as_blob_col.type == OrsoTypes.BLOB, orso_struct_as_blob_col.type

def test_validate_with_missing_column():
# Test with missing column
data = {
Expand Down

0 comments on commit af4809a

Please sign in to comment.