red-arrow 3.0.0 → 6.0.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.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +23 -0
  3. data/ext/arrow/arrow.cpp +3 -0
  4. data/ext/arrow/converters.cpp +5 -0
  5. data/ext/arrow/converters.hpp +126 -0
  6. data/ext/arrow/extconf.rb +13 -0
  7. data/ext/arrow/memory-view.cpp +311 -0
  8. data/ext/arrow/memory-view.hpp +26 -0
  9. data/ext/arrow/raw-records.cpp +1 -0
  10. data/ext/arrow/values.cpp +1 -0
  11. data/lib/arrow/aggregate-node-options.rb +35 -0
  12. data/lib/arrow/aggregation.rb +46 -0
  13. data/lib/arrow/array-builder.rb +5 -0
  14. data/lib/arrow/array.rb +130 -0
  15. data/lib/arrow/binary-dictionary-array-builder.rb +27 -0
  16. data/lib/arrow/buffer.rb +10 -6
  17. data/lib/arrow/column-containable.rb +100 -1
  18. data/lib/arrow/constructor-arguments-gc-guardable.rb +25 -0
  19. data/lib/arrow/data-type.rb +14 -5
  20. data/lib/arrow/datum.rb +100 -0
  21. data/lib/arrow/dense-union-data-type.rb +2 -2
  22. data/lib/arrow/dictionary-data-type.rb +2 -2
  23. data/lib/arrow/equal-options.rb +38 -0
  24. data/lib/arrow/expression.rb +48 -0
  25. data/lib/arrow/file-system.rb +34 -0
  26. data/lib/arrow/group.rb +116 -124
  27. data/lib/arrow/loader.rb +46 -0
  28. data/lib/arrow/map-array-builder.rb +109 -0
  29. data/lib/arrow/map-array.rb +26 -0
  30. data/lib/arrow/map-data-type.rb +89 -0
  31. data/lib/arrow/path-extension.rb +1 -1
  32. data/lib/arrow/record-batch-reader.rb +41 -0
  33. data/lib/arrow/record-batch.rb +0 -2
  34. data/lib/arrow/scalar.rb +32 -0
  35. data/lib/arrow/slicer.rb +44 -143
  36. data/lib/arrow/sort-key.rb +193 -0
  37. data/lib/arrow/sort-options.rb +109 -0
  38. data/lib/arrow/source-node-options.rb +32 -0
  39. data/lib/arrow/sparse-union-data-type.rb +2 -2
  40. data/lib/arrow/string-dictionary-array-builder.rb +27 -0
  41. data/lib/arrow/symbol-values-appendable.rb +34 -0
  42. data/lib/arrow/table-concatenate-options.rb +36 -0
  43. data/lib/arrow/table-formatter.rb +141 -17
  44. data/lib/arrow/table-list-formatter.rb +5 -3
  45. data/lib/arrow/table-loader.rb +41 -3
  46. data/lib/arrow/table-saver.rb +29 -3
  47. data/lib/arrow/table-table-formatter.rb +7 -31
  48. data/lib/arrow/table.rb +34 -40
  49. data/lib/arrow/time32-data-type.rb +2 -2
  50. data/lib/arrow/time64-data-type.rb +2 -2
  51. data/lib/arrow/timestamp-data-type.rb +2 -2
  52. data/lib/arrow/version.rb +1 -1
  53. data/red-arrow.gemspec +2 -1
  54. data/test/helper.rb +1 -0
  55. data/test/raw-records/test-dense-union-array.rb +14 -0
  56. data/test/raw-records/test-list-array.rb +19 -0
  57. data/test/raw-records/test-map-array.rb +441 -0
  58. data/test/raw-records/test-sparse-union-array.rb +14 -0
  59. data/test/raw-records/test-struct-array.rb +15 -0
  60. data/test/test-array-builder.rb +7 -0
  61. data/test/test-array.rb +154 -0
  62. data/test/test-binary-dictionary-array-builder.rb +103 -0
  63. data/test/test-boolean-scalar.rb +26 -0
  64. data/test/test-csv-loader.rb +8 -8
  65. data/test/test-decimal128-data-type.rb +2 -2
  66. data/test/test-expression.rb +40 -0
  67. data/test/test-float-scalar.rb +46 -0
  68. data/test/test-function.rb +176 -0
  69. data/test/test-group.rb +75 -51
  70. data/test/test-map-array-builder.rb +110 -0
  71. data/test/test-map-array.rb +33 -0
  72. data/test/test-map-data-type.rb +36 -0
  73. data/test/test-memory-view.rb +434 -0
  74. data/test/test-orc.rb +19 -23
  75. data/test/test-record-batch-reader.rb +46 -0
  76. data/test/test-record-batch.rb +42 -0
  77. data/test/test-slicer.rb +166 -167
  78. data/test/test-sort-indices.rb +40 -0
  79. data/test/test-sort-key.rb +81 -0
  80. data/test/test-sort-options.rb +58 -0
  81. data/test/test-string-dictionary-array-builder.rb +103 -0
  82. data/test/test-table.rb +190 -53
  83. data/test/values/test-dense-union-array.rb +14 -0
  84. data/test/values/test-list-array.rb +17 -0
  85. data/test/values/test-map-array.rb +433 -0
  86. data/test/values/test-sparse-union-array.rb +14 -0
  87. data/test/values/test-struct-array.rb +15 -0
  88. metadata +73 -6
@@ -0,0 +1,41 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Arrow
19
+ class RecordBatchReader
20
+ class << self
21
+ # @api private
22
+ def try_convert(value)
23
+ case value
24
+ when ::Array
25
+ return nil if value.empty?
26
+ if value.all? {|v| v.is_a?(RecordBatch)}
27
+ new(value)
28
+ else
29
+ nil
30
+ end
31
+ when RecordBatch
32
+ new([value])
33
+ when Table
34
+ TableBatchReader.new(value)
35
+ else
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -50,8 +50,6 @@ module Arrow
50
50
  alias_method :size, :n_rows
51
51
  alias_method :length, :n_rows
52
52
 
53
- alias_method :[], :find_column
54
-
55
53
  # Converts the record batch to {Arrow::Table}.
56
54
  #
57
55
  # @return [Arrow::Table]
@@ -0,0 +1,32 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Arrow
19
+ class Scalar
20
+ # @param other [Arrow::Scalar] The scalar to be compared.
21
+ # @param options [Arrow::EqualOptions, Hash] (nil)
22
+ # The options to custom how to compare.
23
+ #
24
+ # @return [Boolean]
25
+ # `true` if both of them have the same data, `false` otherwise.
26
+ #
27
+ # @since 5.0.0
28
+ def equal_scalar?(other, options=nil)
29
+ equal_options(other, options)
30
+ end
31
+ end
32
+ end
data/lib/arrow/slicer.rb CHANGED
@@ -16,9 +16,6 @@
16
16
  # under the License.
17
17
 
18
18
  module Arrow
19
- # Experimental
20
- #
21
- # TODO: Almost codes should be implemented in Apache Arrow C++.
22
19
  class Slicer
23
20
  def initialize(table)
24
21
  @table = table
@@ -43,6 +40,21 @@ module Arrow
43
40
  super
44
41
  end
45
42
 
43
+ module Helper
44
+ class << self
45
+ def ensure_boolean(column)
46
+ case column.data_type
47
+ when Arrow::BooleanDataType
48
+ column.data
49
+ else
50
+ options = CastOptions.new
51
+ options.to_data_type = Arrow::BooleanDataType.new
52
+ Function.find("cast").execute([column.data], options).value
53
+ end
54
+ end
55
+ end
56
+ end
57
+
46
58
  class Condition
47
59
  def evaluate
48
60
  message = "Slicer::Condition must define \#evaluate: #{inspect}"
@@ -69,43 +81,28 @@ module Arrow
69
81
  end
70
82
 
71
83
  def evaluate
72
- values1 = @condition1.evaluate.each
73
- values2 = @condition2.evaluate.each
74
- raw_array = []
75
- begin
76
- loop do
77
- value1 = values1.next
78
- value2 = values2.next
79
- if value1.nil? or value2.nil?
80
- raw_array << nil
81
- else
82
- raw_array << evaluate_value(value1, value2)
83
- end
84
- end
85
- rescue StopIteration
86
- end
87
- BooleanArray.new(raw_array)
84
+ function.execute([@condition1.evaluate, @condition2.evaluate]).value
88
85
  end
89
86
  end
90
87
 
91
88
  class AndCondition < LogicalCondition
92
89
  private
93
- def evaluate_value(value1, value2)
94
- value1 and value2
90
+ def function
91
+ Function.find("and")
95
92
  end
96
93
  end
97
94
 
98
95
  class OrCondition < LogicalCondition
99
96
  private
100
- def evaluate_value(value1, value2)
101
- value1 or value2
97
+ def function
98
+ Function.find("or")
102
99
  end
103
100
  end
104
101
 
105
102
  class XorCondition < LogicalCondition
106
103
  private
107
- def evaluate_value(value1, value2)
108
- value1 ^ value2
104
+ def function
105
+ Function.find("xor")
109
106
  end
110
107
  end
111
108
 
@@ -115,21 +112,7 @@ module Arrow
115
112
  end
116
113
 
117
114
  def evaluate
118
- data = @column.data
119
-
120
- case @column.data_type
121
- when BooleanDataType
122
- data
123
- else
124
- if data.n_chunks == 1
125
- data.get_chunk(0).cast(BooleanDataType.new, nil)
126
- else
127
- arrays = data.each_chunk.collect do |chunk|
128
- chunk.cast(BooleanDataType.new, nil)
129
- end
130
- ChunkedArray.new(arrays)
131
- end
132
- end
115
+ Helper.ensure_boolean(@column)
133
116
  end
134
117
 
135
118
  def !@
@@ -187,23 +170,8 @@ module Arrow
187
170
  end
188
171
 
189
172
  def evaluate
190
- data = @column.data
191
- raw_array = []
192
- data.each_chunk do |chunk|
193
- if chunk.is_a?(BooleanArray)
194
- boolean_array = chunk
195
- else
196
- boolean_array = chunk.cast(BooleanDataType.new, nil)
197
- end
198
- boolean_array.each do |value|
199
- if value.nil?
200
- raw_array << value
201
- else
202
- raw_array << !value
203
- end
204
- end
205
- end
206
- BooleanArray.new(raw_array)
173
+ data = Helper.ensure_boolean(@column)
174
+ Function.find("invert").execute([data]).value
207
175
  end
208
176
 
209
177
  def !@
@@ -222,19 +190,10 @@ module Arrow
222
190
  end
223
191
 
224
192
  def evaluate
225
- case @value
226
- when nil
227
- raw_array = @column.collect(&:nil?)
228
- BooleanArray.new(raw_array)
193
+ if @value.nil?
194
+ Function.find("is_null").execute([@column.data]).value
229
195
  else
230
- raw_array = @column.collect do |value|
231
- if value.nil?
232
- nil
233
- else
234
- @value == value
235
- end
236
- end
237
- BooleanArray.new(raw_array)
196
+ Function.find("equal").execute([@column.data, @value]).value
238
197
  end
239
198
  end
240
199
  end
@@ -250,25 +209,10 @@ module Arrow
250
209
  end
251
210
 
252
211
  def evaluate
253
- case @value
254
- when nil
255
- if @column.n_nulls.zero?
256
- raw_array = [true] * @column.n_rows
257
- else
258
- raw_array = @column.n_rows.times.collect do |i|
259
- @column.valid?(i)
260
- end
261
- end
262
- BooleanArray.new(raw_array)
212
+ if @value.nil?
213
+ Function.find("is_valid").execute([@column.data]).value
263
214
  else
264
- raw_array = @column.collect do |value|
265
- if value.nil?
266
- nil
267
- else
268
- @value != value
269
- end
270
- end
271
- BooleanArray.new(raw_array)
215
+ Function.find("not_equal").execute([@column.data, @value]).value
272
216
  end
273
217
  end
274
218
  end
@@ -284,14 +228,7 @@ module Arrow
284
228
  end
285
229
 
286
230
  def evaluate
287
- raw_array = @column.collect do |value|
288
- if value.nil?
289
- nil
290
- else
291
- @value > value
292
- end
293
- end
294
- BooleanArray.new(raw_array)
231
+ Function.find("less").execute([@column.data, @value]).value
295
232
  end
296
233
  end
297
234
 
@@ -306,14 +243,7 @@ module Arrow
306
243
  end
307
244
 
308
245
  def evaluate
309
- raw_array = @column.collect do |value|
310
- if value.nil?
311
- nil
312
- else
313
- @value >= value
314
- end
315
- end
316
- BooleanArray.new(raw_array)
246
+ Function.find("less_equal").execute([@column.data, @value]).value
317
247
  end
318
248
  end
319
249
 
@@ -328,14 +258,7 @@ module Arrow
328
258
  end
329
259
 
330
260
  def evaluate
331
- raw_array = @column.collect do |value|
332
- if value.nil?
333
- nil
334
- else
335
- @value < value
336
- end
337
- end
338
- BooleanArray.new(raw_array)
261
+ Function.find("greater").execute([@column.data, @value]).value
339
262
  end
340
263
  end
341
264
 
@@ -350,14 +273,7 @@ module Arrow
350
273
  end
351
274
 
352
275
  def evaluate
353
- raw_array = @column.collect do |value|
354
- if value.nil?
355
- nil
356
- else
357
- @value <= value
358
- end
359
- end
360
- BooleanArray.new(raw_array)
276
+ Function.find("greater_equal").execute([@column.data, @value]).value
361
277
  end
362
278
  end
363
279
 
@@ -372,18 +288,10 @@ module Arrow
372
288
  end
373
289
 
374
290
  def evaluate
375
- values_index = {}
376
- @values.each do |value|
377
- values_index[value] = true
378
- end
379
- raw_array = @column.collect do |value|
380
- if value.nil?
381
- nil
382
- else
383
- values_index.key?(value)
384
- end
385
- end
386
- BooleanArray.new(raw_array)
291
+ values = @values
292
+ values = Array.new(values) unless values.is_a?(Array)
293
+ options = SetLookupOptions.new(values)
294
+ Function.find("is_in").execute([@column.data], options).value
387
295
  end
388
296
  end
389
297
 
@@ -398,18 +306,11 @@ module Arrow
398
306
  end
399
307
 
400
308
  def evaluate
401
- values_index = {}
402
- @values.each do |value|
403
- values_index[value] = true
404
- end
405
- raw_array = @column.collect do |value|
406
- if value.nil?
407
- nil
408
- else
409
- not values_index.key?(value)
410
- end
411
- end
412
- BooleanArray.new(raw_array)
309
+ values = @values
310
+ values = Array.new(values) unless values.is_a?(Array)
311
+ options = SetLookupOptions.new(values)
312
+ booleans = Function.find("is_in").execute([@column.data], options).value
313
+ Function.find("invert").execute([booleans]).value
413
314
  end
414
315
  end
415
316
 
@@ -0,0 +1,193 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Arrow
19
+ class SortKey
20
+ class << self
21
+ # Ensure returning suitable {Arrow::SortKey}.
22
+ #
23
+ # @overload resolve(sort_key)
24
+ #
25
+ # Returns the given sort key itself. This is convenient to use
26
+ # this method as {Arrow::SortKey} converter.
27
+ #
28
+ # @param sort_key [Arrow::SortKey] The sort key.
29
+ #
30
+ # @return [Arrow::SortKey] The given sort key itself.
31
+ #
32
+ # @overload resolve(name)
33
+ #
34
+ # Creates a new suitable sort key from column name with
35
+ # leading order mark. See {#initialize} for details about
36
+ # order mark.
37
+ #
38
+ # @return [Arrow::SortKey] A new suitable sort key.
39
+ #
40
+ # @overload resolve(name, order)
41
+ #
42
+ # Creates a new suitable sort key from column name without
43
+ # leading order mark and order. See {#initialize} for details.
44
+ #
45
+ # @return [Arrow::SortKey] A new suitable sort key.
46
+ #
47
+ # @since 4.0.0
48
+ def resolve(name, order=nil)
49
+ return name if name.is_a?(self)
50
+ new(name, order)
51
+ end
52
+
53
+ # @api private
54
+ def try_convert(value)
55
+ case value
56
+ when Symbol, String
57
+ new(value.to_s, :ascending)
58
+ else
59
+ nil
60
+ end
61
+ end
62
+ end
63
+
64
+ alias_method :initialize_raw, :initialize
65
+ private :initialize_raw
66
+ # Creates a new {Arrow::SortKey}.
67
+ #
68
+ # @overload initialize(name)
69
+ #
70
+ # @param name [Symbol, String] The name of the sort column.
71
+ #
72
+ # If `name` is a String, the first character may be processed
73
+ # as the "leading order mark". If the first character is `"+"`
74
+ # or `"-"`, they are processed as a leading order mark. If the
75
+ # first character is processed as a leading order mark, the
76
+ # first character is removed from sort column name and
77
+ # corresponding order is used. `"+"` uses ascending order and
78
+ # `"-"` uses ascending order.
79
+ #
80
+ # If `name` is not a String nor `name` doesn't start with the
81
+ # leading order mark, sort column name is `name` as-is and
82
+ # ascending order is used.
83
+ #
84
+ # @example String without the leading order mark
85
+ # key = Arrow::SortKey.new("count")
86
+ # key.name # => "count"
87
+ # key.order # => Arrow::SortOrder::ASCENDING
88
+ #
89
+ # @example String with the "+" leading order mark
90
+ # key = Arrow::SortKey.new("+count")
91
+ # key.name # => "count"
92
+ # key.order # => Arrow::SortOrder::ASCENDING
93
+ #
94
+ # @example String with the "-" leading order mark
95
+ # key = Arrow::SortKey.new("-count")
96
+ # key.name # => "count"
97
+ # key.order # => Arrow::SortOrder::DESCENDING
98
+ #
99
+ # @example Symbol that starts with "-"
100
+ # key = Arrow::SortKey.new(:"-count")
101
+ # key.name # => "-count"
102
+ # key.order # => Arrow::SortOrder::ASCENDING
103
+ #
104
+ # @overload initialize(name, order)
105
+ #
106
+ # @param name [Symbol, String] The name of the sort column.
107
+ #
108
+ # No leading order mark processing. The given `name` is used
109
+ # as-is.
110
+ #
111
+ # @param order [Symbol, String, Arrow::SortOrder] How to order
112
+ # by this sort key.
113
+ #
114
+ # If this is a Symbol or String, this must be `:ascending`,
115
+ # `"ascending"`, `:asc`, `"asc"`, `:descending`,
116
+ # `"descending"`, `:desc` or `"desc"`.
117
+ #
118
+ # @example No leading order mark processing
119
+ # key = Arrow::SortKey.new("-count", :ascending)
120
+ # key.name # => "-count"
121
+ # key.order # => Arrow::SortOrder::ASCENDING
122
+ #
123
+ # @example Order by abbreviated name with Symbol
124
+ # key = Arrow::SortKey.new("count", :desc)
125
+ # key.name # => "count"
126
+ # key.order # => Arrow::SortOrder::DESCENDING
127
+ #
128
+ # @example Order by String
129
+ # key = Arrow::SortKey.new("count", "descending")
130
+ # key.name # => "count"
131
+ # key.order # => Arrow::SortOrder::DESCENDING
132
+ #
133
+ # @example Order by Arrow::SortOrder
134
+ # key = Arrow::SortKey.new("count", Arrow::SortOrder::DESCENDING)
135
+ # key.name # => "count"
136
+ # key.order # => Arrow::SortOrder::DESCENDING
137
+ #
138
+ # @since 4.0.0
139
+ def initialize(name, order=nil)
140
+ name, order = normalize_name(name, order)
141
+ order = normalize_order(order) || :ascending
142
+ initialize_raw(name, order)
143
+ end
144
+
145
+ # @return [String] The string representation of this sort key. You
146
+ # can use recreate {Arrow::SortKey} by
147
+ # `Arrow::SortKey.new(key.to_s)`.
148
+ #
149
+ # @example Recreate Arrow::SortKey
150
+ # key = Arrow::SortKey.new("-count")
151
+ # key.to_s # => "-count"
152
+ # key == Arrow::SortKey.new(key.to_s) # => true
153
+ #
154
+ # @since 4.0.0
155
+ def to_s
156
+ if order == SortOrder::ASCENDING
157
+ "+#{name}"
158
+ else
159
+ "-#{name}"
160
+ end
161
+ end
162
+
163
+ private
164
+ def normalize_name(name, order)
165
+ case name
166
+ when Symbol
167
+ return name.to_s, order
168
+ when String
169
+ return name, order if order
170
+ if name.start_with?("-")
171
+ return name[1..-1], order || :descending
172
+ elsif name.start_with?("+")
173
+ return name[1..-1], order || :ascending
174
+ else
175
+ return name, order
176
+ end
177
+ else
178
+ return name, order
179
+ end
180
+ end
181
+
182
+ def normalize_order(order)
183
+ case order
184
+ when :asc, "asc"
185
+ :ascending
186
+ when :desc, "desc"
187
+ :descending
188
+ else
189
+ order
190
+ end
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,109 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Arrow
19
+ class SortOptions
20
+ class << self
21
+ # @api private
22
+ def try_convert(value)
23
+ case value
24
+ when Symbol, String
25
+ new(value)
26
+ when ::Array
27
+ new(*value)
28
+ else
29
+ nil
30
+ end
31
+ end
32
+ end
33
+
34
+ alias_method :initialize_raw, :initialize
35
+ private :initialize_raw
36
+ # @param sort_keys [::Array<String, Symbol, Arrow::SortKey>] The
37
+ # sort keys to be used. See {Arrow::SortKey.resolve} how to
38
+ # resolve each sort key in `sort_keys`.
39
+ #
40
+ # You can add more sort keys by {#add_sort_key} later.
41
+ #
42
+ # @example No initial sort keys
43
+ # options = Arrow::SortOptions.new
44
+ # options.sort_keys # => []
45
+ #
46
+ # @example String sort keys
47
+ # options = Arrow::SortOptions.new("count", "-age")
48
+ # options.sort_keys.collect(&:to_s) # => ["+count", "-age"]
49
+ #
50
+ # @example Symbol sort keys
51
+ # options = Arrow::SortOptions.new(:count, :age)
52
+ # options.sort_keys.collect(&:to_s) # => ["+count", "+age"]
53
+ #
54
+ # @example Mixed sort keys
55
+ # options = Arrow::SortOptions.new(:count, "-age")
56
+ # options.sort_keys.collect(&:to_s) # => ["+count", "-age"]
57
+ #
58
+ # @since 4.0.0
59
+ def initialize(*sort_keys)
60
+ initialize_raw
61
+ sort_keys.each do |sort_key|
62
+ add_sort_key(sort_key)
63
+ end
64
+ end
65
+
66
+ # @api private
67
+ alias_method :add_sort_key_raw, :add_sort_key
68
+ # Add a sort key.
69
+ #
70
+ # @return [void]
71
+ #
72
+ # @overload add_sort_key(key)
73
+ #
74
+ # @param key [Arrow::SortKey] The sort key to be added.
75
+ #
76
+ # @example Add a key to sort by "price" column in descending order
77
+ # options = Arrow::SortOptions.new
78
+ # options.add_sort_key(Arrow::SortKey.new(:price, :descending))
79
+ # options.sort_keys.collect(&:to_s) # => ["-price"]
80
+ #
81
+ # @overload add_sort_key(name)
82
+ #
83
+ # @param name [Symbol, String] The sort key name to be
84
+ # added. See also {Arrow::SortKey#initialize} for the leading
85
+ # order mark for String name.
86
+ #
87
+ # @example Add a key to sort by "price" column in descending order
88
+ # options = Arrow::SortOptions.new
89
+ # options.add_sort_key("-price")
90
+ # options.sort_keys.collect(&:to_s) # => ["-price"]
91
+ #
92
+ # @overload add_sort_key(name, order)
93
+ #
94
+ # @param name [Symbol, String] The sort key name.
95
+ #
96
+ # @param order [Symbol, String, Arrow::SortOrder] The sort
97
+ # order. See {Arrow::SortKey#initialize} for details.
98
+ #
99
+ # @example Add a key to sort by "price" column in descending order
100
+ # options = Arrow::SortOptions.new
101
+ # options.add_sort_key("price", :desc)
102
+ # options.sort_keys.collect(&:to_s) # => ["-price"]
103
+ #
104
+ # @since 4.0.0
105
+ def add_sort_key(name, order=nil)
106
+ add_sort_key_raw(SortKey.resolve(name, order))
107
+ end
108
+ end
109
+ end