Skip to content

Commit 108363f

Browse files
committed
use pybase64
1 parent 1961b8b commit 108363f

11 files changed

Lines changed: 50 additions & 88 deletions

File tree

opteryx/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__build__ = 1383
1+
__build__ = 1384
22

33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

opteryx/functions/string_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def get_sha512(item):
135135

136136
def get_base64_encode(item):
137137
"""calculate BASE64 encoding of a string"""
138-
import base64
138+
import pybase64 as base64
139139

140140
if item is None:
141141
return None
@@ -147,7 +147,7 @@ def get_base64_encode(item):
147147

148148
def get_base64_decode(item):
149149
"""calculate BASE64 encoding of a string"""
150-
import base64
150+
import pybase64 as base64
151151

152152
if item is None:
153153
return None

opteryx/models/relation_statistics.py

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
# See the License at http://www.apache.org/licenses/LICENSE-2.0
44
# Distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
55

6-
import base64
76
import decimal
8-
from base64 import b85decode as _b85decode
97
from dataclasses import asdict
108
from dataclasses import dataclass
119
from dataclasses import field
@@ -17,68 +15,34 @@
1715
from typing import Tuple
1816

1917
import orjson
18+
import pybase64 as base64
2019

2120

2221
def orjson_default(obj):
2322
if type(obj) is decimal.Decimal:
2423
return {"__decimal__": str(obj)}
2524
if type(obj) is bytes:
26-
return {"__bytes__": base64.b85encode(obj).decode("utf-8")}
25+
return {"__bytes__": base64.b64encode(obj).decode("utf-8")}
2726
raise TypeError(f"Type not serializable: {type(obj)}")
2827

2928

3029
def decode_object(obj):
31-
"""
32-
Decode an object that was encoded with orjson_default.
33-
34-
This is part of an optimization path to avoid loading files, so is written to be fast
35-
over readability. It uses a stack to traverse the object structure and decode it in place.
36-
37-
Before this stack-based approach, the recursive version was the third slowest function
38-
call in performance tests, so this is a significant improvement.
39-
"""
40-
stack = [(None, None, obj)] # (parent, key/index, child)
41-
root = None
42-
43-
while stack:
44-
parent, key, item = stack.pop()
45-
46-
t = type(item)
47-
48-
if t is dict:
49-
if "__decimal__" in item:
50-
val = _Decimal(item["__decimal__"])
51-
elif "__bytes__" in item:
52-
val = _b85decode(item["__bytes__"])
53-
else:
54-
val = {}
55-
if parent is not None:
56-
parent[key] = val
57-
else:
58-
root = val
59-
for k in reversed(list(item.keys())):
60-
stack.append((val, k, item[k]))
61-
continue
62-
63-
elif t is list:
64-
val = [None] * len(item)
65-
if parent is not None:
66-
parent[key] = val
67-
else:
68-
root = val
69-
for i in reversed(range(len(item))):
70-
stack.append((val, i, item[i]))
71-
continue
72-
73-
else:
74-
val = item
75-
76-
if parent is not None:
77-
parent[key] = val
78-
else:
79-
root = val
80-
81-
return root
30+
_decode = decode_object
31+
t = type(obj)
32+
33+
if t is dict:
34+
if "__decimal__" in obj:
35+
return _Decimal(obj["__decimal__"])
36+
if "__bytes__" in obj:
37+
return base64.b64decode(obj["__bytes__"])
38+
return {k: _decode(v) for k, v in obj.items()}
39+
40+
if t is list:
41+
for i, v in enumerate(obj):
42+
obj[i] = _decode(v)
43+
return obj
44+
45+
return obj
8246

8347

8448
@dataclass

opteryx/planner/sql_rewriter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ def sql_parts(string):
161161
elif part[0] in ("r", "R"):
162162
# We take the raw string and encode it, pass it into the
163163
# plan as the encoded string and let the engine decode it
164-
from base64 import b85encode
164+
from pybase64 import b64encode
165165

166-
encoded_part = b85encode(part[2:-1].encode()).decode()
167-
parts.append(f"BASE85_DECODE('{encoded_part}')")
166+
encoded_part = b64encode(part[2:-1].encode()).decode()
167+
parts.append(f"BASE64_DECODE('{encoded_part}')")
168168
else:
169169
parts.append(part)
170170
else:

opteryx/third_party/travers/graph.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -524,22 +524,12 @@ def draw(self, ascii_safe: bool = False) -> str:
524524

525525
def copy(self) -> "Graph":
526526
"""
527-
Intelligently make a copy of this Graph, handling situations where Deepcopy
528-
does not work.
527+
Intelligently make a copy of this Graph, avoiding __init__ and handling
528+
deepcopy-resistant structures.
529529
"""
530530
import copy
531531

532532
def _inner_copy(obj: Any) -> Any:
533-
"""
534-
Create an independent inner copy of the given object.
535-
536-
Parameters:
537-
obj: Any
538-
The object to be deep copied.
539-
540-
Returns:
541-
Any: The new, independent deep copy.
542-
"""
543533
obj_type = type(obj)
544534
if obj_type is list:
545535
return [_inner_copy(item) for item in obj]
@@ -553,12 +543,16 @@ def _inner_copy(obj: Any) -> Any:
553543
return obj.copy()
554544
try:
555545
return copy.deepcopy(obj)
556-
except:
546+
except Exception:
557547
return obj
558548

559-
graph = Graph()
549+
# Create a new instance without invoking __init__
550+
graph = self.__class__.__new__(self.__class__)
551+
552+
# Manually assign attributes
560553
graph._nodes = _inner_copy(self._nodes)
561554
graph._edges = self.copy_edges()
555+
graph._cached_edges = None
562556

563557
return graph
564558

opteryx/virtual_datasets/astronaut_data.py

Lines changed: 5 additions & 3 deletions
Large diffs are not rendered by default.

opteryx/virtual_datasets/missions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from orso.schema import FlatColumn
2121
from orso.schema import RelationSchema
22+
from orso.tools import single_item_cache
2223
from orso.types import OrsoTypes
2324

2425
from opteryx.models import RelationStatistics
@@ -28,6 +29,7 @@
2829
_decoded: bytes = None
2930

3031

32+
@single_item_cache
3133
def read(*args):
3234
import base64
3335
import io

opteryx/virtual_datasets/no_table_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
import pyarrow
1616
from orso.schema import FlatColumn
1717
from orso.schema import RelationSchema
18+
from orso.tools import single_item_cache
1819
from orso.types import OrsoTypes
1920

2021
from opteryx.models import RelationStatistics
2122

2223
__all__ = ("read", "schema")
2324

2425

26+
@single_item_cache
2527
def read(*args) -> pyarrow.Table:
2628
# Create a PyArrow table with one column and one row
2729
arrow_schema = pyarrow.schema([pyarrow.field("$column", pyarrow.int64())])

opteryx/virtual_datasets/planet_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import pyarrow
3131
from orso.schema import FlatColumn
3232
from orso.schema import RelationSchema
33+
from orso.tools import single_item_cache
3334
from orso.types import OrsoTypes
3435

3536
from opteryx.models import RelationStatistics
3637

3738
__all__ = ("read", "schema")
3839

3940

41+
@single_item_cache
4042
def read(end_date=None, *args) -> pyarrow.Table:
4143
# fmt:off
4244
# Define the data

opteryx/virtual_datasets/satellite_data.py

Lines changed: 6 additions & 11 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)