nanoarrow 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,186 @@
1
+ #include <ruby/ruby.h>
2
+
3
+ #include "ext.h"
4
+ #include "nanoarrow/nanoarrow.h"
5
+
6
+ VALUE cCMatArrayStream;
7
+
8
+ typedef struct mat_array_stream {
9
+ VALUE schema;
10
+ VALUE arrays;
11
+ int64_t total_length;
12
+ } mat_array_stream_t;
13
+
14
+ static void mat_array_stream_mark(void* ptr)
15
+ {
16
+ mat_array_stream_t* stream = (mat_array_stream_t*) ptr;
17
+ rb_gc_mark_movable(stream->schema);
18
+ rb_gc_mark_movable(stream->arrays);
19
+ }
20
+
21
+ const rb_data_type_t mat_array_stream_data_type = {
22
+ .wrap_struct_name = "Nanoarrow::CMaterializedArrayStream",
23
+ .function = {
24
+ .dmark = mat_array_stream_mark,
25
+ .dfree = RUBY_TYPED_DEFAULT_FREE,
26
+ },
27
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
28
+ };
29
+
30
+ static VALUE mat_array_stream_allocate(VALUE klass)
31
+ {
32
+ mat_array_stream_t* stream;
33
+ VALUE obj = TypedData_Make_Struct(klass, mat_array_stream_t, &mat_array_stream_data_type, stream);
34
+ stream->schema = rb_funcall(cCSchema, rb_intern("allocate"), 0);
35
+ stream->arrays = rb_ary_new();
36
+ stream->total_length = 0;
37
+ return obj;
38
+ }
39
+
40
+ static VALUE mat_array_stream_from_c_arrays(VALUE klass, VALUE arrays, VALUE schema, VALUE validate)
41
+ {
42
+ mat_array_stream_t* out_ptr;
43
+ VALUE out = mat_array_stream_allocate(klass);
44
+ GetMatArrayStream(out, out_ptr);
45
+
46
+ Check_Type(arrays, T_ARRAY);
47
+
48
+ for (long i = 0; i < RARRAY_LEN(arrays); i++)
49
+ {
50
+ VALUE array = rb_ary_entry(arrays, i);
51
+
52
+ if (!rb_obj_is_instance_of(array, cCArray))
53
+ rb_raise(rb_eTypeError, "Expected CArray");
54
+
55
+ int64_t len = NUM2LL(rb_funcall(array, rb_intern("length"), 0));
56
+ if (len == 0)
57
+ continue;
58
+
59
+ if (RTEST(validate))
60
+ {
61
+ array_t* array_ptr;
62
+ GetArray(array, array_ptr);
63
+ assert_type_equal(array_ptr->schema, schema, Qfalse);
64
+ }
65
+
66
+ out_ptr->total_length += len;
67
+
68
+ rb_ary_push(out_ptr->arrays, array);
69
+ }
70
+
71
+ out_ptr->schema = schema;
72
+
73
+ return out;
74
+ }
75
+
76
+ static VALUE mat_array_stream_from_c_array(VALUE klass, VALUE array)
77
+ {
78
+ VALUE schema = rb_funcall(array, rb_intern("schema"), 0);
79
+ return mat_array_stream_from_c_arrays(klass, rb_ary_new3(1, array), schema, Qfalse);
80
+ }
81
+
82
+ static VALUE mat_array_stream_from_c_array_stream(VALUE klass, VALUE stream)
83
+ {
84
+ VALUE arrays = rb_ary_new();
85
+ while (true)
86
+ {
87
+ VALUE v = rb_funcall(stream, rb_intern("get_next"), 0);
88
+ if (NIL_P(v))
89
+ break;
90
+ rb_ary_push(arrays, v);
91
+ }
92
+
93
+ VALUE schema = rb_funcall(stream, rb_intern("get_cached_schema"), 0);
94
+ return mat_array_stream_from_c_arrays(klass, arrays, schema, Qfalse);
95
+ }
96
+
97
+ static VALUE mat_array_stream_schema(VALUE self)
98
+ {
99
+ mat_array_stream_t* stream;
100
+ GetMatArrayStream(self, stream);
101
+
102
+ return stream->schema;
103
+ }
104
+
105
+ static VALUE mat_array_stream_length(VALUE self)
106
+ {
107
+ mat_array_stream_t* stream;
108
+ GetMatArrayStream(self, stream);
109
+
110
+ return LL2NUM(stream->total_length);
111
+ }
112
+
113
+ static VALUE mat_array_stream_array(VALUE self, VALUE idx)
114
+ {
115
+ mat_array_stream_t* stream;
116
+ GetMatArrayStream(self, stream);
117
+
118
+ return rb_funcall(stream->arrays, rb_intern("fetch"), 1, idx);
119
+ }
120
+
121
+ static VALUE mat_array_stream_n_arrays(VALUE self)
122
+ {
123
+ mat_array_stream_t* stream;
124
+ GetMatArrayStream(self, stream);
125
+
126
+ Check_Type(stream->arrays, T_ARRAY);
127
+ return LONG2NUM(RARRAY_LEN(stream->arrays));
128
+ }
129
+
130
+ static VALUE mat_array_stream_arrays(VALUE self)
131
+ {
132
+ mat_array_stream_t* stream;
133
+ GetMatArrayStream(self, stream);
134
+
135
+ return stream->arrays;
136
+ }
137
+
138
+ static VALUE mat_array_stream_arrow_c_stream(VALUE self)
139
+ {
140
+ mat_array_stream_t* stream;
141
+ GetMatArrayStream(self, stream);
142
+
143
+ VALUE stream_ptr = rb_funcall(cCArrayStream, rb_intern("from_c_arrays"), 3, stream->arrays, stream->schema, Qfalse);
144
+ return rb_funcall(stream_ptr, rb_intern("arrow_c_stream"), 0);
145
+ }
146
+
147
+ static VALUE mat_array_stream_child(VALUE self, VALUE idx)
148
+ {
149
+ mat_array_stream_t* stream;
150
+ GetMatArrayStream(self, stream);
151
+
152
+ mat_array_stream_t* out_ptr;
153
+ VALUE out = mat_array_stream_allocate(cCMatArrayStream);
154
+ GetMatArrayStream(out, out_ptr);
155
+
156
+ out_ptr->schema = rb_funcall(stream->schema, rb_intern("child"), 1, idx);
157
+
158
+ Check_Type(stream->arrays, T_ARRAY);
159
+ long arrays_len = RARRAY_LEN(stream->arrays);
160
+ out_ptr->arrays = rb_ary_new_capa(arrays_len);
161
+
162
+ for (long i = 0; i < arrays_len; i++)
163
+ {
164
+ VALUE child = rb_funcall(rb_ary_entry(stream->arrays, i), rb_intern("child"), 1, idx);
165
+ out_ptr->total_length += NUM2LL(rb_funcall(child, rb_intern("length"), 0));
166
+ rb_ary_push(out_ptr->arrays, child);
167
+ }
168
+
169
+ return out;
170
+ }
171
+
172
+ void Init_materialized(void)
173
+ {
174
+ cCMatArrayStream = rb_define_class_under(mNanoarrow, "CMaterializedArrayStream", rb_cObject);
175
+ rb_define_alloc_func(cCMatArrayStream, mat_array_stream_allocate);
176
+ rb_define_singleton_method(cCMatArrayStream, "from_c_arrays", mat_array_stream_from_c_arrays, 3);
177
+ rb_define_singleton_method(cCMatArrayStream, "from_c_array", mat_array_stream_from_c_array, 1);
178
+ rb_define_singleton_method(cCMatArrayStream, "from_c_array_stream", mat_array_stream_from_c_array_stream, 1);
179
+ rb_define_method(cCMatArrayStream, "schema", mat_array_stream_schema, 0);
180
+ rb_define_method(cCMatArrayStream, "length", mat_array_stream_length, 0);
181
+ rb_define_method(cCMatArrayStream, "array", mat_array_stream_array, 1);
182
+ rb_define_method(cCMatArrayStream, "n_arrays", mat_array_stream_n_arrays, 0);
183
+ rb_define_method(cCMatArrayStream, "arrays", mat_array_stream_arrays, 0);
184
+ rb_define_method(cCMatArrayStream, "arrow_c_stream", mat_array_stream_arrow_c_stream, 0);
185
+ rb_define_method(cCMatArrayStream, "child", mat_array_stream_child, 1);
186
+ }