Class: Nanoarrow::CArrayBuilder

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

Instance Method Summary collapse

Instance Method Details

#append_bools(obj) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/nanoarrow/array_builder.c', line 76

static VALUE array_builder_append_bools(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
            code = ArrowArrayAppendInt(builder->ptr, RTEST(v));

        raise_error_not_ok("ArrowArrayAppendInt()", code);
    }

    return self;
}

#append_bytes(obj) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'ext/nanoarrow/array_builder.c', line 205

static VALUE array_builder_append_bytes(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
        {
            Check_Type(v, T_STRING);
            struct ArrowBufferView bv;
            bv.data.data = StringValuePtr(v);
            bv.size_bytes = RSTRING_LEN(v);
            code = ArrowArrayAppendBytes(builder->ptr, bv);
        }

        raise_error_not_ok("ArrowArrayAppendBytes()", code);
    }

    return self;
}

#append_decimals(obj, bitwidth, precision, scale) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'ext/nanoarrow/array_builder.c', line 234

static VALUE array_builder_append_decimals(VALUE self, VALUE obj, VALUE bitwidth, VALUE precision, VALUE scale)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    int pi = NUM2INT(precision);
    int si = NUM2INT(scale);
    VALUE pad = rb_str_new_cstr("0");

    struct ArrowDecimal decimal;
    ArrowDecimalInit(&decimal, NUM2INT(bitwidth), pi, si);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
        {
            VALUE split = rb_funcall(v, rb_intern("split"), 0);
            VALUE sign = rb_ary_entry(split, 0);
            VALUE digits = rb_ary_entry(split, 1);
            VALUE base = rb_ary_entry(split, 2);
            VALUE exponent = rb_ary_entry(split, 3);

            if (NUM2INT(base) != 10)
                raise_todo();

            Check_Type(digits, T_STRING);

            // exceeds precision
            // TODO better error
            if (RSTRING_LEN(digits) > pi)
                raise_todo();

            int target = si + NUM2INT(exponent);

            // exceeds scale
            // TODO either round in Ruby or better error
            if (RSTRING_LEN(digits) > target)
                raise_todo();

            digits = rb_funcall(digits, rb_intern("ljust"), 2, INT2NUM(target), pad);

            struct ArrowStringView sv;
            sv.data = StringValuePtr(digits);
            sv.size_bytes = RSTRING_LEN(digits);
            code = ArrowDecimalSetDigits(&decimal, sv);
            raise_error_not_ok("ArrowDecimalSetDigits()", code);

            if (NUM2INT(sign) == -1)
                ArrowDecimalNegate(&decimal);

            code = ArrowArrayAppendDecimal(builder->ptr, &decimal);
        }

        raise_error_not_ok("ArrowArrayAppendDecimal()", code);
    }

    return self;
}

#append_doubles(obj) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/nanoarrow/array_builder.c', line 153

static VALUE array_builder_append_doubles(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
            code = ArrowArrayAppendDouble(builder->ptr, NUM2DBL(v));

        raise_error_not_ok("ArrowArrayAppendDouble()", code);
    }

    return self;
}

#append_ints(obj) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ext/nanoarrow/array_builder.c', line 99

static VALUE array_builder_append_ints(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
            code = ArrowArrayAppendInt(builder->ptr, NUM2LL(v));

        if (code != NANOARROW_OK)
            rb_raise(rb_eRangeError, "integer out of range");
    }

    return self;
}

#append_strings(obj) ⇒ Object



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
201
202
203
# File 'ext/nanoarrow/array_builder.c', line 176

static VALUE array_builder_append_strings(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
        {
            Check_Type(v, T_STRING);
            struct ArrowStringView sv;
            sv.data = StringValuePtr(v);
            sv.size_bytes = RSTRING_LEN(v);
            code = ArrowArrayAppendString(builder->ptr, sv);
        }

        raise_error_not_ok("ArrowArrayAppendString()", code);
    }

    return self;
}

#append_uints(obj) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/nanoarrow/array_builder.c', line 123

static VALUE array_builder_append_uints(VALUE self, VALUE obj)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    Check_Type(obj, T_ARRAY);

    for (long i = 0; i < RARRAY_LEN(obj); i++)
    {
        VALUE v = rb_ary_entry(obj, i);
        int code;

        if (NIL_P(v))
            code = ArrowArrayAppendNull(builder->ptr, 1);
        else
        {
            // prevent primitive types from wrapping
            if ((FIXNUM_P(v) && FIX2LONG(v) < 0) || (RB_FLOAT_TYPE_P(v) && RFLOAT_VALUE(v) < 0))
                rb_raise(rb_eRangeError, "integer out of range");

            code = ArrowArrayAppendUInt(builder->ptr, NUM2ULL(v));
        }

        if (code != NANOARROW_OK)
            rb_raise(rb_eRangeError, "integer out of range");
    }

    return self;
}

#finishObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/nanoarrow/array_builder.c', line 392

static VALUE array_builder_finish(VALUE self)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    struct ArrowError error;
    int code = ArrowArrayFinishBuildingDefault(builder->ptr, &error);
    raise_message_not_ok(&error, "ArrowArrayFinishBuildingDefault()", code);

    VALUE out = builder->c_array;

    array_t* array;
    VALUE c_array = rb_funcall(cCArray, rb_intern("allocate"), 0);
    GetArray(c_array, array);
    array->schema = rb_funcall(cCSchema, rb_intern("allocate"), 0);
    builder->c_array = c_array;
    builder->ptr = array->ptr;

    return out;
}

#init_from_schema(schema) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/nanoarrow/array_builder.c', line 44

static VALUE array_builder_init_from_schema(VALUE self, VALUE schema)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    if (builder->ptr->release != NULL)
        rb_raise(rb_eRuntimeError, "CArrayBuilder is already initialized");

    schema_t* schema_ptr;
    GetSchema(schema, schema_ptr);

    struct ArrowError error;
    int code = ArrowArrayInitFromSchema(builder->ptr, schema_ptr->ptr, &error);
    raise_message_not_ok(&error, "ArrowArrayInitFromType()", code);

    array_t* c_array;
    GetArray(builder->c_array, c_array);

    c_array->schema = schema;
    return self;
}

#resolve_null_countObject



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
# File 'ext/nanoarrow/array_builder.c', line 330

static VALUE array_builder_resolve_null_count(VALUE self)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    rb_funcall(builder->c_array, rb_intern("assert_valid"), 0);

    VALUE format = rb_funcall(rb_funcall(builder->c_array, rb_intern("schema"), 0), rb_intern("format"), 0);
    if (RTEST(rb_funcall(format, rb_intern("start_with?"), 1, rb_str_new_cstr("+us:"))) || RTEST(rb_funcall(format, rb_intern("start_with?"), 1, rb_str_new_cstr("+ud:"))))
        return self;

    if (builder->ptr->null_count != -1)
        return self;

    struct ArrowBuffer* validity_buffer = ArrowArrayBuffer(builder->ptr, 0);
    if (validity_buffer->size_bytes == 0)
    {
        builder->ptr->null_count = 0;
        return self;
    }

    int64_t bits = builder->ptr->offset + builder->ptr->length;
    int64_t bytes_required = (bits >> 3) + ((bits & 7) != 0);

    if (validity_buffer->size_bytes < bytes_required)
        rb_raise(rb_eArgError, "validity bitmap mismatch");

    int64_t count = ArrowBitCountSet(
        validity_buffer->data,
        builder->ptr->offset,
        builder->ptr->length
    );
    builder->ptr->null_count = builder->ptr->length - count;

    return self;
}

#set_child(idx, c_array) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'ext/nanoarrow/array_builder.c', line 367

static VALUE array_builder_set_child(VALUE self, VALUE idx, VALUE c_array)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    array_t* c_array_ptr;
    GetArray(c_array, c_array_ptr);

    VALUE child = rb_funcall(builder->c_array, rb_intern("child"), 1, idx);
    array_t* child_ptr;
    GetArray(child, child_ptr);
    if (child_ptr->ptr->release != NULL)
        ArrowArrayRelease(child_ptr->ptr);

    // TODO move to parameter
    VALUE move = Qfalse;

    if (!RTEST(move))
        c_array_shallow_copy(c_array_ptr->base, c_array_ptr->ptr, child_ptr->ptr);
    else
        ArrowArrayMove(c_array_ptr->ptr, child_ptr->ptr);

    return self;
}

#set_length(length) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'ext/nanoarrow/array_builder.c', line 310

static VALUE array_builder_set_length(VALUE self, VALUE length)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    rb_funcall(builder->c_array, rb_intern("assert_valid"), 0);
    builder->ptr->length = NUM2LL(length);
    return self;
}

#set_null_count(null_count) ⇒ Object



320
321
322
323
324
325
326
327
328
# File 'ext/nanoarrow/array_builder.c', line 320

static VALUE array_builder_set_null_count(VALUE self, VALUE null_count)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    rb_funcall(builder->c_array, rb_intern("assert_valid"), 0);
    builder->ptr->null_count = NUM2LL(null_count);
    return self;
}

#set_offset(offset) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'ext/nanoarrow/array_builder.c', line 300

static VALUE array_builder_set_offset(VALUE self, VALUE offset)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    rb_funcall(builder->c_array, rb_intern("assert_valid"), 0);
    builder->ptr->offset = NUM2LL(offset);
    return self;
}

#start_appendingObject



66
67
68
69
70
71
72
73
74
# File 'ext/nanoarrow/array_builder.c', line 66

static VALUE array_builder_start_appending(VALUE self)
{
    array_builder_t* builder;
    GetArrayBuilder(self, builder);

    int code = ArrowArrayStartAppending(builder->ptr);
    raise_error_not_ok("ArrowArrayStartAppending()", code);
    return self;
}