Class: Groonga::DataColumn

Inherits:
Column show all
Defined in:
ext/groonga/rb-grn-data-column.c,
ext/groonga/rb-grn-data-column.c

Overview

The superclass for data columns such as FixSizeColumn and VariableSizeColumn.

Direct Known Subclasses

FixSizeColumn, VariableSizeColumn

Instance Method Summary collapse

Methods inherited from Column

#clear_lock, #disk_usage, #find_indexes, #index?, #indexes, #local_name, #lock, #locked?, #reference?, #rename, #scalar?, #select, #table, #truncate, #unlock, #vector?, #with_weight?

Methods included from Flushable

#flush

Methods inherited from Object

#==, #[], #[]=, #accessor?, #append, #builtin?, #close, #closed?, #column?, #dirty?, #domain, #function_procedure?, #id, #index_column?, #inspect, #key_accessor?, #last_modified, #name, #path, #persistent?, #prepend, #procedure?, #range, #reference_column?, #remove, #scorer_procedure?, #selector_only_procedure?, #selector_procedure?, #table?, #temporary?, #touch, #unlink, #window_function_procedure?

Instance Method Details

#apply_window_function(options = {}) {|record| ... } ⇒ Object

Applies the window function to records in the table of the column. Results are stored into the column.

Examples:

Set the nth record.


Groonga::Schema.define do |schema|
  schema.create_table("Comments") do |table|
    # The column for storing window function result.
    table.uint32("nth")
  end
end
comments = Groonga["Comments"]
nth = Groonga["Comments.nth"]

5.times do
  comments.add
end

options = {
  # Sort by _id in descending order.
  :sort_keys => [["_id", "desc"]],
}
nth.apply_window_function(options) do |record|
  # record_number() returns Nth record in the sorted order.
  record.call("record_number")
end

comments.each do |comment|
  p [comment.id, comment.nth]
    # -> [1, 5]
    # -> [2, 4]
    # -> [3, 3]
    # -> [4, 2]
    # -> [5, 1]
end

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs.

Options Hash (options):

  • :sort_keys (::Array<String>)

Yields:

  • (record)

    It yields an object that builds expression. The block must build an expression that calls a window function.

Yield Parameters:

  • record (Groonga::RecordExpressionBuilder)

    The expression builder to create a window function call.

Yield Returns:

  • (Groonga::ExpressionBuilder)

    It must be an expression that calls a window function.

Since:

  • 6.0.4



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/groonga/rb-grn-data-column.c', line 84

static VALUE
rb_grn_data_column_apply_window_function (int argc, VALUE *argv, VALUE self)
{
    grn_rc rc;
    grn_ctx *context;
    grn_obj *column;
    grn_obj *table;
    VALUE rb_table;
    grn_window_definition definition;
    grn_obj *window_function_call = NULL;
    VALUE rb_options;
    VALUE rb_sort_keys;
    VALUE rb_builder;
    VALUE rb_window_function_call;

    rb_grn_column_deconstruct(SELF(self), &column, &context,
                              NULL, &table,
                              NULL, NULL, NULL);
    rb_table = GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);

    memset(&definition, 0, sizeof(grn_window_definition));

    rb_scan_args(argc, argv, "01", &rb_options);
    rb_grn_scan_options(rb_options,
                        "sort_keys", &rb_sort_keys,
                        NULL);

    if (!NIL_P(rb_sort_keys)) {
        VALUE rb_table;

        if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys, rb_cArray)))
            rb_raise(rb_eArgError, ":sort_keys should be an array of key: <%s>",
                     rb_grn_inspect(rb_sort_keys));

        definition.n_sort_keys = RARRAY_LEN(rb_sort_keys);
        definition.sort_keys = ALLOCA_N(grn_table_sort_key,
                                        definition.n_sort_keys);
        rb_table = GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);
        rb_grn_table_sort_keys_fill(context,
                                    definition.sort_keys,
                                    definition.n_sort_keys,
                                    rb_sort_keys,
                                    rb_table);
    }

    rb_builder = rb_grn_record_expression_builder_new(rb_table, Qnil);
    rb_window_function_call =
        rb_grn_record_expression_builder_build(rb_builder);
    rb_grn_object_deconstruct(RB_GRN_OBJECT(DATA_PTR(rb_window_function_call)),
                              &window_function_call, NULL,
                              NULL, NULL, NULL, NULL);

    rc = grn_table_apply_window_function(context,
                                         table,
                                         column,
                                         &definition,
                                         window_function_call);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return self;
}