Class: Nanoarrow::CArrayStream

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_c_arrays(arrays, schema, validate) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'ext/nanoarrow/array_stream.c', line 52

static VALUE array_stream_from_c_arrays(VALUE klass, VALUE arrays, VALUE schema, VALUE validate)
{
    array_stream_t* stream;
    VALUE out = array_stream_allocate(klass);
    GetArrayStream(out, stream);

    // TODO move to parameter
    VALUE move = Qfalse;

    VALUE out_schema;
    VALUE validate_schema;
    if (RTEST(validate) && !RTEST(move))
    {
        validate_schema = schema;
        out_schema = rb_funcall(schema, rb_intern("deep_dup"), 0);
    }
    else if (RTEST(validate))
    {
        validate_schema = rb_funcall(schema, rb_intern("deep_dup"), 0);
        out_schema = schema;
    }
    else
        out_schema = rb_funcall(schema, rb_intern("deep_dup"), 0);

    schema_t* schema_ptr;
    GetSchema(out_schema, schema_ptr);

    Check_Type(arrays, T_ARRAY);
    long arrays_len = RARRAY_LEN(arrays);

    stream->base = alloc_c_array_stream(&stream->ptr);

    int code = ArrowBasicArrayStreamInit(stream->ptr, schema_ptr->ptr, arrays_len);
    raise_error_not_ok("ArrowBasicArrayStreamInit()", code);

    struct ArrowArrayStream* c_array_stream_out = stream->ptr;
    struct ArrowArray tmp;
    for (long i = 0; i < arrays_len; i++)
    {
        array_t* array;
        GetArray(rb_ary_entry(arrays, i), array);

        if (RTEST(validate))
            assert_type_equal(array->schema, validate_schema, Qfalse);

        if (!RTEST(move))
        {
            c_array_shallow_copy(array->base, array->ptr, &tmp);
            ArrowBasicArrayStreamSetArray(c_array_stream_out, i, &tmp);
        }
        else
            ArrowBasicArrayStreamSetArray(c_array_stream_out, i, array->ptr);
    }

    if (RTEST(validate))
    {
        struct ArrowError error;
        int code = ArrowBasicArrayStreamValidate(c_array_stream_out, &error);
        raise_message_not_ok(&error, "ArrowBasicArrayStreamValidate()", code);
    }

    return out;
}

.import_from_c_capsule(schema_capsule) ⇒ Object



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

static VALUE array_stream_import_from_c_capsule(VALUE klass, VALUE schema_capsule)
{
    array_stream_t* stream;
    VALUE obj = array_stream_allocate(klass);
    GetArrayStream(obj, stream);

    VALUE address = rb_funcall(schema_capsule, rb_intern("to_i"), 0);
    stream->base = schema_capsule;
    stream->ptr = (struct ArrowArrayStream*) NUM2ULL(address);
    return obj;
}

Instance Method Details

#arrow_c_streamObject



179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/nanoarrow/array_stream.c', line 179

static VALUE array_stream_arrow_c_stream(VALUE self)
{
    array_stream_t* stream;
    GetArrayStream(self, stream);

    array_stream_assert_valid(self);

    struct ArrowArrayStream* c_array_stream_out;
    VALUE array_stream_capsule = alloc_c_array_stream(&c_array_stream_out);
    ArrowArrayStreamMove(stream->ptr, c_array_stream_out);
    return array_stream_capsule;
}

#get_cached_schemaObject



144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/nanoarrow/array_stream.c', line 144

static VALUE array_stream_get_cached_schema(VALUE self)
{
    array_stream_t* stream;
    GetArrayStream(self, stream);

    if (NIL_P(stream->cached_schema))
    {
        stream->cached_schema = rb_funcall(cCSchema, rb_intern("allocate"), 0);
        array_stream_get_schema(self, stream->cached_schema);
    }
    return stream->cached_schema;
}

#get_nextObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/nanoarrow/array_stream.c', line 157

static VALUE array_stream_get_next(VALUE self)
{
    array_stream_t* stream;
    GetArrayStream(self, stream);

    array_stream_assert_valid(self);

    array_t* array_ptr;
    VALUE array = rb_funcall(cCArray, rb_intern("allocate"), 0);
    GetArray(array, array_ptr);
    array_ptr->schema = array_stream_get_cached_schema(self);

    struct ArrowError error;
    int code = ArrowArrayStreamGetNext(stream->ptr, array_ptr->ptr, &error);
    raise_message_not_ok(&error, "ArrowArrayStreamGetNext()", code);

    if (!RTEST(rb_funcall(array, rb_intern("valid?"), 0)))
        return Qnil;

    return array;
}