Class: Nanoarrow::CSchema

Inherits:
Object
  • Object
show all
Defined in:
ext/nanoarrow/schema.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import_from_c_capsule(schema_capsule) ⇒ Object



31
32
33
34
35
36
37
38
# File 'ext/nanoarrow/schema.c', line 31

static VALUE schema_import_from_c_capsule(VALUE klass, VALUE schema_capsule)
{
    schema_t* schema;
    VALUE obj = TypedData_Make_Struct(klass, schema_t, &schema_data_type, schema);
    schema->base = schema_capsule;
    schema->ptr = (struct ArrowSchema*) NUM2ULL(rb_funcall(schema_capsule, rb_intern("to_i"), 0));
    return obj;
}

Instance Method Details

#arrow_c_schemaObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'ext/nanoarrow/schema.c', line 270

static VALUE schema_arrow_c_schema(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    struct ArrowSchema* c_schema_out;
    VALUE schema_capsule = alloc_c_schema(&c_schema_out);

    int code = ArrowSchemaDeepCopy(schema->ptr, c_schema_out);
    raise_error_not_ok("ArrowSchemaDeepCopy()", code);
    return schema_capsule;
}

#assert_validObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/nanoarrow/schema.c', line 54

VALUE schema_assert_valid(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    if (schema->ptr == NULL)
        rb_raise(rb_eRuntimeError, "schema is NULL");
    if (schema->ptr->release == NULL)
        rb_raise(rb_eRuntimeError, "schema is released");

    return Qnil;
}

#child(rb_i) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'ext/nanoarrow/schema.c', line 212

static VALUE schema_child(VALUE self, VALUE rb_i)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    int64_t i = NUM2LL(rb_i);
    if (i < 0 || i >= schema->ptr->n_children)
        rb_raise(rb_eIndexError, "out of range");

    schema_t* schema_ptr;
    VALUE obj = TypedData_Make_Struct(cCSchema, schema_t, &schema_data_type, schema_ptr);
    schema_ptr->base = schema->base;
    schema_ptr->ptr = schema->ptr->children[i];
    return obj;
}

#childrenObject



230
231
232
233
234
235
236
237
# File 'ext/nanoarrow/schema.c', line 230

static VALUE schema_children(VALUE self)
{
    int64_t n_children = NUM2LL(schema_n_children(self));
    VALUE children = rb_ary_new();
    for (int64_t i = 0; i < n_children; i++)
        rb_ary_push(children, schema_child(self, LL2NUM(i)));
    return children;
}

#deep_dupObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/nanoarrow/schema.c', line 40

static VALUE schema_deep_dup(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_t* out_ptr;
    VALUE out = schema_allocate(cCSchema);
    GetSchema(out, out_ptr);

    int code = ArrowSchemaDeepCopy(schema->ptr, out_ptr->ptr);
    raise_error_not_ok("ArrowSchemaDeepCopy()", code);
    return out;
}

#dictionaryObject



239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/nanoarrow/schema.c', line 239

static VALUE schema_dictionary(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    if (schema->ptr->dictionary != NULL)
        raise_todo();

    return Qnil;
}

#flagsObject



168
169
170
171
172
173
174
# File 'ext/nanoarrow/schema.c', line 168

static VALUE schema_flags(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    return LL2NUM(schema->ptr->flags);
}

#formatObject



148
149
150
151
152
153
154
155
156
# File 'ext/nanoarrow/schema.c', line 148

static VALUE schema_format(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    return schema->ptr->format != NULL ? rb_utf8_str_new_cstr(schema->ptr->format) : Qnil;
}

#metadataObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/nanoarrow/schema.c', line 176

static VALUE schema_metadata(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    if (schema->ptr->metadata == NULL)
        return rb_hash_new();

    struct ArrowMetadataReader reader;
    int code = ArrowMetadataReaderInit(&reader, schema->ptr->metadata);
    raise_error_not_ok("ArrowMetadataReaderInit()", code);

    VALUE items = rb_hash_new_capa(reader.remaining_keys);
    while (reader.remaining_keys > 0)
    {
        struct ArrowStringView key;
        struct ArrowStringView value;
        code = ArrowMetadataReaderRead(&reader, &key, &value);
        raise_error_not_ok("ArrowMetadataReaderRead()", code);
        rb_hash_aset(items, rb_str_new(key.data, key.size_bytes), rb_str_new(value.data, value.size_bytes));
    }
    return items;
}

#modify(*args) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/nanoarrow/schema.c', line 285

static VALUE schema_modify(int argc, VALUE* argv, VALUE self)
{
    VALUE kwargs;
    rb_scan_args(argc, argv, "0:", &kwargs);

    ID keywords[8];
    VALUE values[8];
    keywords[0] = rb_intern("format");
    keywords[1] = rb_intern("name");
    keywords[2] = rb_intern("flags");
    keywords[3] = rb_intern("nullable");
    keywords[4] = rb_intern("metadata");
    keywords[5] = rb_intern("children");
    keywords[6] = rb_intern("dictionary");
    keywords[7] = rb_intern("validate");
    rb_get_kwargs(kwargs, keywords, 0, 8, values);

    for (int i = 0; i < 8; i++)
    {
        if (values[i] == Qundef)
            values[i] = Qnil;
    }

    VALUE format = values[0];
    VALUE name = values[1];
    VALUE flags = values[2];
    VALUE nullable = values[3];
    VALUE metadata = values[4];
    VALUE children = values[5];
    VALUE dictionary = values[6];
    VALUE validate = values[7];

    if (NIL_P(validate))
        validate = Qtrue;

    VALUE builder = rb_funcall(cCSchemaBuilder, rb_intern("allocate"), 0);

    if (NIL_P(format))
        rb_funcall(builder, rb_intern("set_format"), 1, rb_funcall(self, rb_intern("format"), 0));
    else
        rb_funcall(builder, rb_intern("set_format"), 1, format);

    if (NIL_P(name))
        rb_funcall(builder, rb_intern("set_name"), 1, rb_funcall(self, rb_intern("name"), 0));
    else
        rb_funcall(builder, rb_intern("set_name"), 1, name);

    if (NIL_P(flags))
        rb_funcall(builder, rb_intern("set_flags"), 1, rb_funcall(self, rb_intern("flags"), 0));
    else
        rb_funcall(builder, rb_intern("set_flags"), 1, flags);

    if (!NIL_P(nullable))
        rb_funcall(builder, rb_intern("set_nullable"), 1, nullable);

    if (NIL_P(metadata))
    {
        VALUE self_metadata = rb_funcall(self, rb_intern("metadata"), 0);
        if (!NIL_P(self_metadata))
            rb_funcall(builder, rb_intern("append_metadata"), 1, self_metadata);
    }
    else
        rb_funcall(builder, rb_intern("append_metadata"), 1, metadata);

    if (NIL_P(children))
    {
        int64_t n_children = NUM2LL(rb_funcall(self, rb_intern("n_children"), 0));
        if (n_children > 0)
        {
            rb_funcall(builder, rb_intern("allocate_children"), 1, LL2NUM(n_children));
            VALUE self_children = rb_funcall(self, rb_intern("children"), 0);
            for (long i = 0; i < RARRAY_LEN(self_children); i++)
                rb_funcall(builder, rb_intern("set_child"), 3, LONG2NUM(i), Qnil, rb_ary_entry(self_children, i));
        }
    }
    else if (RB_TYPE_P(children, T_HASH))
    {
        // not as efficient as rb_hash_foreach, but simpler
        VALUE keys = rb_funcall(children, rb_intern("keys"), 0);
        Check_Type(keys, T_ARRAY);
        rb_funcall(builder, rb_intern("allocate_children"), 1, LONG2NUM(RARRAY_LEN(keys)));
        for (long i = 0; i < RARRAY_LEN(keys); i++)
        {
            VALUE key = rb_ary_entry(keys, i);
            rb_funcall(builder, rb_intern("set_child"), 3, LONG2NUM(i), key, rb_hash_lookup(children, key));
        }
    }
    else
    {
        Check_Type(children, T_ARRAY);
        rb_funcall(builder, rb_intern("allocate_children"), 1, LONG2NUM(RARRAY_LEN(children)));
        for (long i = 0; i < RARRAY_LEN(children); i++)
            rb_funcall(builder, rb_intern("set_child"), 3, LONG2NUM(i), Qnil, rb_ary_entry(children, i));
    }

    if (NIL_P(dictionary))
    {
        if (!NIL_P(rb_funcall(self, rb_intern("dictionary"), 0)))
            raise_todo();
    }
    else
        raise_todo();

    if (RTEST(validate))
        rb_funcall(builder, rb_intern("validate"), 0);

    return rb_funcall(builder, rb_intern("finish"), 0);
}

#n_childrenObject



202
203
204
205
206
207
208
209
210
# File 'ext/nanoarrow/schema.c', line 202

static VALUE schema_n_children(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    return LL2NUM(schema->ptr->n_children);
}

#nameObject



158
159
160
161
162
163
164
165
166
# File 'ext/nanoarrow/schema.c', line 158

static VALUE schema_name(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    schema_assert_valid(self);

    return schema->ptr->name != NULL ? rb_utf8_str_new_cstr(schema->ptr->name) : Qnil;
}

#to_sObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'ext/nanoarrow/schema.c', line 252

static VALUE schema_to_s(VALUE self)
{
    schema_t* schema;
    GetSchema(self, schema);

    int64_t n_chars = 1024;
    bool recursive = true;

    char* out = ArrowMalloc(n_chars + 1);
    if (out == NULL)
        rb_raise(rb_eNoMemError, "out of memory");

    int64_t len = ArrowSchemaToString(schema->ptr, out, n_chars + 1, recursive);
    VALUE out_str = rb_str_new(out, len);
    ArrowFree(out);
    return out_str;
}