Skip to content

Commit

Permalink
Update to Mojo 25.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunk committed Feb 15, 2025
1 parent 19d0efd commit 807add8
Show file tree
Hide file tree
Showing 8 changed files with 869 additions and 4,789 deletions.
6 changes: 3 additions & 3 deletions duckdb/chunk.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Chunk(Movable):
self._chunk = existing._chunk

fn __len__(self) -> Int:
return int(_impl().duckdb_data_chunk_get_size(self._chunk))
return Int(_impl().duckdb_data_chunk_get_size(self._chunk))

fn _get_vector(self, col: Int) -> Vector:
return Vector(
Expand Down Expand Up @@ -137,8 +137,8 @@ struct _ChunkIter[lifetime: ImmutableOrigin]:
# # return self.index

# # fn __len__(self) -> Int:
# # return int(self.result.rows - self.index) # TODO could overflow
# # return Int(self.result.rows - self.index) # TODO could overflow

# fn __next__(mut self) -> String:
# self.index += 1
# return str(self.index)
# return String(self.index)
46 changes: 23 additions & 23 deletions duckdb/duckdb_type.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ struct DuckDBType(

@always_inline("nodebug")
fn __repr__(self) -> String:
return "DuckDBType." + str(self)
return "DuckDBType." + String(self)

@always_inline("nodebug")
fn __str__(self) -> String:
Expand Down Expand Up @@ -295,10 +295,10 @@ struct Date(EqualityComparable, Writable, Representable, Stringable):
# return writer.write(self.year(), "-", self.month(), "-", self.day())

fn __str__(self) -> String:
return str(self.days)
return String(self.days)

fn __repr__(self) -> String:
return "Date(" + str(self.days) + ")"
return "Date(" + String(self.days) + ")"

fn __eq__(self, other: Date) -> Bool:
return self.days == other.days
Expand Down Expand Up @@ -333,14 +333,14 @@ struct Time(EqualityComparable, Writable, Representable, Stringable):
# )

fn __str__(self) -> String:
return str(self.micros)
return String(self.micros)

fn write_to[W: Writer](self, mut writer: W):
return writer.write(self.micros)
# return writer.write(self.hour(), ":", self.minute(), ":", self.second())

fn __repr__(self) -> String:
return "Time(" + str(self.micros) + ")"
return "Time(" + String(self.micros) + ")"

fn __eq__(self, other: Time) -> Bool:
return self.micros == other.micros
Expand Down Expand Up @@ -375,7 +375,7 @@ struct Timestamp(EqualityComparable, Writable, Stringable, Representable):
# )

fn __str__(self) -> String:
return str(self.micros)
return String(self.micros)

fn write_to[W: Writer](self, mut writer: W):
return writer.write(self.micros)
Expand All @@ -388,7 +388,7 @@ struct Timestamp(EqualityComparable, Writable, Stringable, Representable):
return not self == other

fn __repr__(self) -> String:
return "Timestamp(" + str(self.micros) + ")"
return "Timestamp(" + String(self.micros) + ")"

# fn date(self) -> Date:
# return _impl().duckdb_to_date(_impl().duckdb_from_timestamp(self).date)
Expand All @@ -406,21 +406,21 @@ struct Interval(Stringable, Representable):
fn __str__(self) -> String:
return (
"months: "
+ str(self.months)
+ String(self.months)
+ ", days: "
+ str(self.days)
+ String(self.days)
+ ", micros: "
+ str(self.micros)
+ String(self.micros)
)

fn __repr__(self) -> String:
return (
"Interval("
+ str(self.months)
+ String(self.months)
+ ", "
+ str(self.days)
+ String(self.days)
+ ", "
+ str(self.micros)
+ String(self.micros)
+ ")"
)

Expand All @@ -437,10 +437,10 @@ struct Int128(Stringable, Representable):
var upper: Int64

fn __str__(self) -> String:
return "lower: " + str(self.lower) + ", upper: " + str(self.upper)
return "lower: " + String(self.lower) + ", upper: " + String(self.upper)

fn __repr__(self) -> String:
return "Int128(" + str(self.lower) + ", " + str(self.upper) + ")"
return "Int128(" + String(self.lower) + ", " + String(self.upper) + ")"


@value
Expand All @@ -451,10 +451,10 @@ struct UInt128(Stringable, Representable):
var upper: UInt64

fn __str__(self) -> String:
return "lower: " + str(self.lower) + ", upper: " + str(self.upper)
return "lower: " + String(self.lower) + ", upper: " + String(self.upper)

fn __repr__(self) -> String:
return "UInt128(" + str(self.lower) + ", " + str(self.upper) + ")"
return "UInt128(" + String(self.lower) + ", " + String(self.upper) + ")"


@value
Expand All @@ -469,20 +469,20 @@ struct Decimal(Stringable, Representable):
fn __str__(self) -> String:
return (
"width: "
+ str(self.width)
+ String(self.width)
+ ", scale: "
+ str(self.scale)
+ String(self.scale)
+ ", value: "
+ str(self.value)
+ String(self.value)
)

fn __repr__(self) -> String:
return (
"Decimal("
+ str(self.width)
+ String(self.width)
+ ", "
+ str(self.scale)
+ String(self.scale)
+ ", "
+ str(self.value)
+ String(self.value)
+ ")"
)
41 changes: 22 additions & 19 deletions duckdb/duckdb_value.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from duckdb.vector import Vector
from collections import Dict, Optional
from utils import StringRef
from collections.string import StringSlice, StaticString


trait DuckDBValue(CollectionElement, Stringable):
Expand Down Expand Up @@ -42,7 +42,7 @@ struct DTypeValue[duckdb_type: DuckDBType](DuckDBKeyElement):

fn __init__(mut self, vector: Vector, length: Int, offset: Int) raises:
if vector.get_column_type().get_type_id() != duckdb_type:
raise "Expected type " + str(duckdb_type) + " but got " + str(
raise "Expected type " + String(duckdb_type) + " but got " + String(
vector.get_column_type().get_type_id()
)

Expand All @@ -68,12 +68,15 @@ alias Float64Val = DTypeValue[DuckDBType.double]

@value
struct FixedSizeValue[
duckdb_type: DuckDBType, underlying: StringableCollectionElement
duckdb_type: DuckDBType, underlying: WritableCollectionElement
](DuckDBValue):
var value: underlying

fn write_to[W: Writer](self, mut writer: W):
self.value.write_to(writer)

fn __str__(self) -> String:
return self.value.__str__()
return String.write(self)

# fn __hash__(self) -> UInt:
# return self.value.__hash__()
Expand All @@ -86,7 +89,7 @@ struct FixedSizeValue[

fn __init__(mut self, vector: Vector, length: Int, offset: Int) raises:
if vector.get_column_type().get_type_id() != duckdb_type:
raise "Expected type " + str(duckdb_type) + " but got " + str(
raise "Expected type " + String(duckdb_type) + " but got " + String(
vector.get_column_type().get_type_id()
)

Expand All @@ -109,22 +112,22 @@ struct DuckDBString(DuckDBValue):

fn __init__(mut self, vector: Vector, length: Int, offset: Int) raises:
if vector.get_column_type().get_type_id() != DuckDBType.varchar:
raise "Expected type " + str(
raise "Expected type " + String(
DuckDBType.varchar
) + " but got " + str(vector.get_column_type().get_type_id())
) + " but got " + String(vector.get_column_type().get_type_id())
var data_str_ptr = vector._get_data().bitcast[duckdb_string_t_pointer]()
# Short strings are inlined so need to check the length and then cast accordingly.
var string_length = int(data_str_ptr[offset].length)
var string_length = UInt(Int(data_str_ptr[offset].length))
# TODO use duckdb_string_is_inlined helper instead
if data_str_ptr[offset].length <= 12:
var data_str_inlined = data_str_ptr.bitcast[
duckdb_string_t_inlined
]()
self.value = StringRef(
data_str_inlined[offset].inlined.unsafe_ptr(), string_length
)
self.value = String(StaticString(
ptr=data_str_inlined[offset].inlined.unsafe_ptr().bitcast[UInt8](), length=string_length
))
else:
self.value = StringRef(data_str_ptr[offset].ptr, string_length)
self.value = String(StaticString(ptr=data_str_ptr[offset].ptr.bitcast[UInt8](), length=string_length))

fn __str__(self) -> String:
return self.value
Expand All @@ -147,9 +150,9 @@ struct DuckDBList[T: DuckDBValue](DuckDBValue):
fn __init__(mut self, vector: Vector, length: Int, offset: Int) raises:
var runtime_element_type = vector.get_column_type().get_type_id()
if runtime_element_type != Self.expected_element_type:
raise "Expected type " + str(
raise "Expected type " + String(
Self.expected_element_type
) + " but got " + str(runtime_element_type)
) + " but got " + String(runtime_element_type)
self.value = List[Optional[T]](capacity=length)

var data_ptr = vector._get_data().bitcast[T]()
Expand Down Expand Up @@ -211,8 +214,8 @@ struct DuckDBList[T: DuckDBValue](DuckDBValue):
Optional(
T(
child_vector,
length=int(list_entry.length),
offset=int(list_entry.offset),
length=Int(list_entry.length),
offset=Int(list_entry.offset),
)
)
)
Expand All @@ -229,16 +232,16 @@ struct DuckDBList[T: DuckDBValue](DuckDBValue):
Optional(
T(
child_vector,
length=int(list_entry.length),
offset=int(list_entry.offset),
length=Int(list_entry.length),
offset=Int(list_entry.offset),
)
)
)
else:
self.value.append(None)
else:
raise Error(
"Unsupported or invalid type: " + str(runtime_element_type)
"Unsupported or invalid type: " + String(runtime_element_type)
)

@staticmethod
Expand Down
10 changes: 5 additions & 5 deletions duckdb/logical_type.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ struct LogicalType(EqualityComparable, CollectionElement, Writable, Stringable):

fn __str__(self) -> String:
if self.get_type_id() == DuckDBType.list:
return "list(" + str(self.list_type_child_type()) + ")"
return "list(" + String(self.list_type_child_type()) + ")"
if self.get_type_id() == DuckDBType.map:
return (
"map("
+ str(self.map_type_key_type())
+ String(self.map_type_key_type())
+ ","
+ str(self.map_type_value_type())
+ String(self.map_type_value_type())
+ ")"
)
# TODO remaining nested types
return str(self.get_type_id())
return String(self.get_type_id())

fn write_to[W: Writer](self, mut writer: W):
writer.write(str(self))
writer.write(String(self))
10 changes: 5 additions & 5 deletions duckdb/result.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Column(Writable, Stringable):
)

fn __str__(self) -> String:
return str(self.type)
return String(self.type)


struct Result(Writable, Stringable):
Expand All @@ -32,7 +32,7 @@ struct Result(Writable, Stringable):
self.columns.append(col)

fn column_count(self) -> Int:
return int(
return Int(
_impl().duckdb_column_count(UnsafePointer.address_of(self._result))
)

Expand Down Expand Up @@ -120,7 +120,7 @@ struct MaterializedResult(Sized):
T: CollectionElement, //
](self, type: Col[T], col: UInt) raises -> List[Optional[T]]:
var result = List[Optional[T]](
capacity=len(self.chunks) * int(_impl().duckdb_vector_size())
capacity=len(self.chunks) * Int(_impl().duckdb_vector_size())
)
for chunk_ptr in self.chunks:
result.extend(chunk_ptr[][].get(type, col))
Expand All @@ -131,8 +131,8 @@ struct MaterializedResult(Sized):
](self, type: Col[T], col: UInt, row: UInt) raises -> Optional[T]:
if row < 0 or row >= self.size:
raise Error("Row index out of bounds")
var chunk_idx = int(row // _impl().duckdb_vector_size())
var chunk_offset = int(row % _impl().duckdb_vector_size())
var chunk_idx = Int(row // _impl().duckdb_vector_size())
var chunk_offset = Int(row % _impl().duckdb_vector_size())
return self.chunks[chunk_idx][].get(type, col=col, row=chunk_offset)

fn __del__(owned self):
Expand Down
4 changes: 2 additions & 2 deletions duckdb/vector.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Vector:
"""Recursively check that the runtime type of the vector matches the expected type.
"""
if self.get_column_type() != db_type:
raise "Expected type " + str(db_type) + " but got " + str(
raise "Expected type " + String(db_type) + " but got " + String(
self.get_column_type()
)

Expand Down Expand Up @@ -87,7 +87,7 @@ struct Vector:

# Columns are essentially lists so we can use the same logic for getting the values.
var result = DuckDBList[expected_type.Builder](
self, length=int(self.length), offset=0
self, length=Int(self.length), offset=0
).value
# The way we are building our Mojo representation of the data currently via the DuckDBValue
# trait, with different __init__ implementations depending on the concrete type, means
Expand Down
Loading

0 comments on commit 807add8

Please sign in to comment.