red-arrow 0.17.1 → 4.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.
- checksums.yaml +4 -4
- data/ext/arrow/converters.hpp +75 -32
- data/ext/arrow/extconf.rb +14 -3
- data/ext/arrow/raw-records.cpp +3 -1
- data/ext/arrow/values.cpp +3 -1
- data/lib/arrow/array-builder.rb +11 -6
- data/lib/arrow/array.rb +118 -0
- data/lib/arrow/bigdecimal-extension.rb +5 -1
- data/lib/arrow/buffer.rb +28 -0
- data/lib/arrow/data-type.rb +14 -5
- data/lib/arrow/decimal128-array-builder.rb +21 -25
- data/lib/arrow/decimal128-data-type.rb +2 -0
- data/lib/arrow/decimal128.rb +18 -0
- data/lib/arrow/decimal256-array-builder.rb +61 -0
- data/lib/arrow/decimal256-array.rb +25 -0
- data/lib/arrow/decimal256-data-type.rb +73 -0
- data/lib/arrow/decimal256.rb +60 -0
- data/lib/arrow/dense-union-data-type.rb +2 -2
- data/lib/arrow/dictionary-array.rb +24 -0
- data/lib/arrow/dictionary-data-type.rb +2 -2
- data/lib/arrow/fixed-size-binary-array-builder.rb +38 -0
- data/lib/arrow/fixed-size-binary-array.rb +26 -0
- data/lib/arrow/loader.rb +18 -0
- data/lib/arrow/raw-table-converter.rb +47 -0
- data/lib/arrow/record-batch-iterator.rb +22 -0
- data/lib/arrow/record-batch.rb +9 -1
- data/lib/arrow/sort-key.rb +193 -0
- data/lib/arrow/sort-options.rb +109 -0
- data/lib/arrow/sparse-union-data-type.rb +2 -2
- data/lib/arrow/struct-array-builder.rb +13 -7
- data/lib/arrow/table-saver.rb +6 -6
- data/lib/arrow/table.rb +5 -24
- data/lib/arrow/time32-data-type.rb +2 -2
- data/lib/arrow/time64-data-type.rb +2 -2
- data/lib/arrow/timestamp-data-type.rb +2 -2
- data/lib/arrow/version.rb +1 -1
- data/red-arrow.gemspec +1 -0
- data/test/raw-records/test-basic-arrays.rb +17 -0
- data/test/raw-records/test-dense-union-array.rb +15 -34
- data/test/raw-records/test-list-array.rb +20 -0
- data/test/raw-records/test-sparse-union-array.rb +15 -33
- data/test/raw-records/test-struct-array.rb +15 -0
- data/test/test-array.rb +122 -2
- data/test/test-bigdecimal.rb +20 -3
- data/test/test-buffer.rb +11 -0
- data/test/test-decimal128-array-builder.rb +18 -1
- data/test/test-decimal128-data-type.rb +2 -2
- data/test/test-decimal128.rb +38 -0
- data/test/test-decimal256-array-builder.rb +112 -0
- data/test/test-decimal256-array.rb +38 -0
- data/test/test-decimal256-data-type.rb +31 -0
- data/test/test-decimal256.rb +102 -0
- data/test/test-dense-union-data-type.rb +2 -2
- data/test/test-dictionary-array.rb +41 -0
- data/test/test-feather.rb +1 -1
- data/test/test-fixed-size-binary-array-builder.rb +92 -0
- data/test/test-fixed-size-binary-array.rb +36 -0
- data/test/test-orc.rb +19 -23
- data/test/test-record-batch-iterator.rb +37 -0
- data/test/test-record-batch.rb +14 -0
- data/test/test-sort-indices.rb +40 -0
- data/test/test-sort-key.rb +81 -0
- data/test/test-sort-options.rb +58 -0
- data/test/test-sparse-union-data-type.rb +2 -2
- data/test/test-struct-array-builder.rb +16 -12
- data/test/test-struct-array.rb +2 -2
- data/test/values/test-basic-arrays.rb +11 -0
- data/test/values/test-dense-union-array.rb +15 -34
- data/test/values/test-list-array.rb +18 -0
- data/test/values/test-sparse-union-array.rb +15 -33
- data/test/values/test-struct-array.rb +15 -0
- metadata +107 -59
@@ -50,7 +50,7 @@ module Arrow
|
|
50
50
|
# @param ordered [Boolean] Whether dictionary contents are
|
51
51
|
# ordered or not.
|
52
52
|
#
|
53
|
-
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
|
53
|
+
# @example Create a dictionary data type for `{0: "Hello", 1: "World"}`
|
54
54
|
# index_data_type = :int8
|
55
55
|
# value_data_type = :string
|
56
56
|
# ordered = true
|
@@ -91,7 +91,7 @@ module Arrow
|
|
91
91
|
# @option description [Boolean] :ordered Whether dictionary
|
92
92
|
# contents are ordered or not.
|
93
93
|
#
|
94
|
-
# @example Create a dictionary data type for {0: "Hello", 1: "World"}
|
94
|
+
# @example Create a dictionary data type for `{0: "Hello", 1: "World"}`
|
95
95
|
# Arrow::DictionaryDataType.new(index_data_type: :int8,
|
96
96
|
# value_data_type: :string,
|
97
97
|
# ordered: true)
|
@@ -0,0 +1,38 @@
|
|
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 FixedSizeBinaryArrayBuilder
|
20
|
+
class << self
|
21
|
+
# @since 3.0.0
|
22
|
+
def build(data_type, values)
|
23
|
+
builder = new(data_type)
|
24
|
+
builder.build(values)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :append_values_raw, :append_values
|
29
|
+
# @since 3.0.0
|
30
|
+
def append_values(values, is_valids=nil)
|
31
|
+
if values.is_a?(::Array)
|
32
|
+
append_values_raw(values, is_valids)
|
33
|
+
else
|
34
|
+
append_values_packed(values, is_valids)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
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 FixedSizeBinaryArray
|
20
|
+
alias_method :get_value_raw, :get_value
|
21
|
+
# @since 3.0.0
|
22
|
+
def get_value(i)
|
23
|
+
get_value_raw(i).to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/arrow/loader.rb
CHANGED
@@ -41,6 +41,7 @@ module Arrow
|
|
41
41
|
require "arrow/array"
|
42
42
|
require "arrow/array-builder"
|
43
43
|
require "arrow/bigdecimal-extension"
|
44
|
+
require "arrow/buffer"
|
44
45
|
require "arrow/chunked-array"
|
45
46
|
require "arrow/column"
|
46
47
|
require "arrow/compression-type"
|
@@ -55,10 +56,17 @@ module Arrow
|
|
55
56
|
require "arrow/decimal128-array"
|
56
57
|
require "arrow/decimal128-array-builder"
|
57
58
|
require "arrow/decimal128-data-type"
|
59
|
+
require "arrow/decimal256"
|
60
|
+
require "arrow/decimal256-array"
|
61
|
+
require "arrow/decimal256-array-builder"
|
62
|
+
require "arrow/decimal256-data-type"
|
58
63
|
require "arrow/dense-union-data-type"
|
64
|
+
require "arrow/dictionary-array"
|
59
65
|
require "arrow/dictionary-data-type"
|
60
66
|
require "arrow/field"
|
61
67
|
require "arrow/file-output-stream"
|
68
|
+
require "arrow/fixed-size-binary-array"
|
69
|
+
require "arrow/fixed-size-binary-array-builder"
|
62
70
|
require "arrow/group"
|
63
71
|
require "arrow/list-array-builder"
|
64
72
|
require "arrow/list-data-type"
|
@@ -69,10 +77,13 @@ module Arrow
|
|
69
77
|
require "arrow/record-batch"
|
70
78
|
require "arrow/record-batch-builder"
|
71
79
|
require "arrow/record-batch-file-reader"
|
80
|
+
require "arrow/record-batch-iterator"
|
72
81
|
require "arrow/record-batch-stream-reader"
|
73
82
|
require "arrow/rolling-window"
|
74
83
|
require "arrow/schema"
|
75
84
|
require "arrow/slicer"
|
85
|
+
require "arrow/sort-key"
|
86
|
+
require "arrow/sort-options"
|
76
87
|
require "arrow/sparse-union-data-type"
|
77
88
|
require "arrow/struct-array"
|
78
89
|
require "arrow/struct-array-builder"
|
@@ -138,6 +149,7 @@ module Arrow
|
|
138
149
|
when "Arrow::Date32Array",
|
139
150
|
"Arrow::Date64Array",
|
140
151
|
"Arrow::Decimal128Array",
|
152
|
+
"Arrow::Decimal256Array",
|
141
153
|
"Arrow::Time32Array",
|
142
154
|
"Arrow::Time64Array",
|
143
155
|
"Arrow::TimestampArray"
|
@@ -146,6 +158,12 @@ module Arrow
|
|
146
158
|
method_name = "get_raw_value"
|
147
159
|
end
|
148
160
|
super(info, klass, method_name)
|
161
|
+
when "Arrow::Decimal128", "Arrow::Decimal256"
|
162
|
+
case method_name
|
163
|
+
when "copy"
|
164
|
+
method_name = "dup"
|
165
|
+
end
|
166
|
+
super(info, klass, method_name)
|
149
167
|
else
|
150
168
|
super
|
151
169
|
end
|
@@ -0,0 +1,47 @@
|
|
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 RawTableConverter
|
20
|
+
attr_reader :n_rows
|
21
|
+
attr_reader :schema
|
22
|
+
attr_reader :values
|
23
|
+
def initialize(raw_table)
|
24
|
+
@raw_table = raw_table
|
25
|
+
convert
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def convert
|
30
|
+
if @raw_table.is_a?(::Array) and @raw_table[0].is_a?(Column)
|
31
|
+
fields = @raw_table.collect(&:field)
|
32
|
+
@schema = Schema.new(fields)
|
33
|
+
@values = @raw_table.collect(&:data)
|
34
|
+
else
|
35
|
+
fields = []
|
36
|
+
@values = []
|
37
|
+
@raw_table.each do |name, array|
|
38
|
+
array = ArrayBuilder.build(array) if array.is_a?(::Array)
|
39
|
+
fields << Field.new(name.to_s, array.value_data_type)
|
40
|
+
@values << array
|
41
|
+
end
|
42
|
+
@schema = Schema.new(fields)
|
43
|
+
end
|
44
|
+
@n_rows = @values[0].length
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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 RecordBatchIterator
|
20
|
+
alias_method :to_a, :to_list
|
21
|
+
end
|
22
|
+
end
|
data/lib/arrow/record-batch.rb
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
# specific language governing permissions and limitations
|
16
16
|
# under the License.
|
17
17
|
|
18
|
+
require "arrow/raw-table-converter"
|
19
|
+
|
18
20
|
module Arrow
|
19
21
|
class RecordBatch
|
20
22
|
include ColumnContainable
|
@@ -25,13 +27,19 @@ module Arrow
|
|
25
27
|
def new(*args)
|
26
28
|
n_args = args.size
|
27
29
|
case n_args
|
30
|
+
when 1
|
31
|
+
raw_table_converter = RawTableConverter.new(args[0])
|
32
|
+
n_rows = raw_table_converter.n_rows
|
33
|
+
schema = raw_table_converter.schema
|
34
|
+
values = raw_table_converter.values
|
35
|
+
super(schema, n_rows, values)
|
28
36
|
when 2
|
29
37
|
schema, data = args
|
30
38
|
RecordBatchBuilder.build(schema, data)
|
31
39
|
when 3
|
32
40
|
super
|
33
41
|
else
|
34
|
-
message = "wrong number of arguments (given #{n_args}, expected
|
42
|
+
message = "wrong number of arguments (given #{n_args}, expected 1..3)"
|
35
43
|
raise ArgumentError, message
|
36
44
|
end
|
37
45
|
end
|
@@ -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
|