red-arrow 1.0.0 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/arrow/converters.hpp +15 -2
- data/ext/arrow/extconf.rb +14 -3
- data/ext/arrow/raw-records.cpp +1 -0
- data/ext/arrow/values.cpp +1 -0
- 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/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-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 +15 -0
- 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/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 +14 -0
- data/test/raw-records/test-list-array.rb +20 -0
- data/test/raw-records/test-sparse-union-array.rb +14 -0
- 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-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-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-sort-indices.rb +40 -0
- data/test/test-sort-key.rb +81 -0
- data/test/test-sort-options.rb +58 -0
- data/test/test-struct-array-builder.rb +8 -8
- 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 +14 -0
- data/test/values/test-list-array.rb +18 -0
- data/test/values/test-sparse-union-array.rb +14 -0
- data/test/values/test-struct-array.rb +15 -0
- metadata +101 -61
@@ -0,0 +1,61 @@
|
|
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 Decimal256ArrayBuilder
|
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_value_raw, :append_value
|
29
|
+
# @since 3.0.0
|
30
|
+
def append_value(value)
|
31
|
+
append_value_raw(normalize_value(value))
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method :append_values_raw, :append_values
|
35
|
+
# @since 3.0.0
|
36
|
+
def append_values(values, is_valids=nil)
|
37
|
+
if values.is_a?(::Array)
|
38
|
+
values = values.collect do |value|
|
39
|
+
normalize_value(value)
|
40
|
+
end
|
41
|
+
append_values_raw(values, is_valids)
|
42
|
+
else
|
43
|
+
append_values_packed(values, is_valids)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def normalize_value(value)
|
49
|
+
case value
|
50
|
+
when String
|
51
|
+
Decimal256.new(value)
|
52
|
+
when Float
|
53
|
+
Decimal256.new(value.to_s)
|
54
|
+
when BigDecimal
|
55
|
+
Decimal256.new(value.to_s)
|
56
|
+
else
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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 Decimal256Array
|
20
|
+
# @since 3.0.0
|
21
|
+
def get_value(i)
|
22
|
+
BigDecimal(format_value(i))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,73 @@
|
|
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 Decimal256DataType
|
20
|
+
MAX_PRECISION = max_precision
|
21
|
+
|
22
|
+
alias_method :initialize_raw, :initialize
|
23
|
+
private :initialize_raw
|
24
|
+
|
25
|
+
# Creates a new {Arrow::Decimal256DataType}.
|
26
|
+
#
|
27
|
+
# @overload initialize(precision, scale)
|
28
|
+
#
|
29
|
+
# @param precision [Integer] The precision of the decimal data
|
30
|
+
# type. It's the number of digits including the number of
|
31
|
+
# digits after the decimal point.
|
32
|
+
#
|
33
|
+
# @param scale [Integer] The scale of the decimal data
|
34
|
+
# type. It's the number of digits after the decimal point.
|
35
|
+
#
|
36
|
+
# @example Create a decimal data type for "XXXXXX.YY" decimal
|
37
|
+
# Arrow::Decimal256DataType.new(8, 2)
|
38
|
+
#
|
39
|
+
# @overload initialize(description)
|
40
|
+
#
|
41
|
+
# @param description [Hash] The description of the decimal data
|
42
|
+
# type. It must have `:precision` and `:scale` values.
|
43
|
+
#
|
44
|
+
# @option description [Integer] :precision The precision of the
|
45
|
+
# decimal data type. It's the number of digits including the
|
46
|
+
# number of digits after the decimal point.
|
47
|
+
#
|
48
|
+
# @option description [Integer] :scale The scale of the decimal
|
49
|
+
# data type. It's the number of digits after the decimal
|
50
|
+
# point.
|
51
|
+
#
|
52
|
+
# @example Create a decimal data type for "XXXXXX.YY" decimal
|
53
|
+
# Arrow::Decimal256DataType.new(precision: 8,
|
54
|
+
# scale: 2)
|
55
|
+
#
|
56
|
+
# @since 3.0.0
|
57
|
+
def initialize(*args)
|
58
|
+
n_args = args.size
|
59
|
+
case n_args
|
60
|
+
when 1
|
61
|
+
description = args[0]
|
62
|
+
precision = description[:precision]
|
63
|
+
scale = description[:scale]
|
64
|
+
when 2
|
65
|
+
precision, scale = args
|
66
|
+
else
|
67
|
+
message = "wrong number of arguments (given, #{n_args}, expected 1..2)"
|
68
|
+
raise ArgumentError, message
|
69
|
+
end
|
70
|
+
initialize_raw(precision, scale)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,60 @@
|
|
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 Decimal256
|
20
|
+
alias_method :to_s_raw, :to_s
|
21
|
+
|
22
|
+
# @overload to_s
|
23
|
+
#
|
24
|
+
# @return [String]
|
25
|
+
# The string representation of the decimal.
|
26
|
+
#
|
27
|
+
# @overload to_s(scale)
|
28
|
+
#
|
29
|
+
# @param scale [Integer] The scale of the decimal.
|
30
|
+
# @return [String]
|
31
|
+
# The string representation of the decimal including the scale.
|
32
|
+
#
|
33
|
+
# @since 3.0.0
|
34
|
+
def to_s(scale=nil)
|
35
|
+
if scale
|
36
|
+
to_string_scale(scale)
|
37
|
+
else
|
38
|
+
to_s_raw
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :abs!, :abs
|
43
|
+
|
44
|
+
# @since 3.0.0
|
45
|
+
def abs
|
46
|
+
copied = dup
|
47
|
+
copied.abs!
|
48
|
+
copied
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :negate!, :negate
|
52
|
+
|
53
|
+
# @since 3.0.0
|
54
|
+
def negate
|
55
|
+
copied = dup
|
56
|
+
copied.negate!
|
57
|
+
copied
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -33,7 +33,7 @@ module Arrow
|
|
33
33
|
# @param type_codes [::Array<Integer>] The IDs that indicates
|
34
34
|
# corresponding fields.
|
35
35
|
#
|
36
|
-
# @example Create a dense union data type for {2: visible, 9: count}
|
36
|
+
# @example Create a dense union data type for `{2: visible, 9: count}`
|
37
37
|
# fields = [
|
38
38
|
# Arrow::Field.new("visible", :boolean),
|
39
39
|
# {
|
@@ -57,7 +57,7 @@ module Arrow
|
|
57
57
|
# @option description [::Array<Integer>] :type_codes The IDs
|
58
58
|
# that indicates corresponding fields.
|
59
59
|
#
|
60
|
-
# @example Create a dense union data type for {2: visible, 9: count}
|
60
|
+
# @example Create a dense union data type for `{2: visible, 9: count}`
|
61
61
|
# fields = [
|
62
62
|
# Arrow::Field.new("visible", :boolean),
|
63
63
|
# {
|
@@ -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
@@ -56,11 +56,17 @@ module Arrow
|
|
56
56
|
require "arrow/decimal128-array"
|
57
57
|
require "arrow/decimal128-array-builder"
|
58
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"
|
59
63
|
require "arrow/dense-union-data-type"
|
60
64
|
require "arrow/dictionary-array"
|
61
65
|
require "arrow/dictionary-data-type"
|
62
66
|
require "arrow/field"
|
63
67
|
require "arrow/file-output-stream"
|
68
|
+
require "arrow/fixed-size-binary-array"
|
69
|
+
require "arrow/fixed-size-binary-array-builder"
|
64
70
|
require "arrow/group"
|
65
71
|
require "arrow/list-array-builder"
|
66
72
|
require "arrow/list-data-type"
|
@@ -76,6 +82,8 @@ module Arrow
|
|
76
82
|
require "arrow/rolling-window"
|
77
83
|
require "arrow/schema"
|
78
84
|
require "arrow/slicer"
|
85
|
+
require "arrow/sort-key"
|
86
|
+
require "arrow/sort-options"
|
79
87
|
require "arrow/sparse-union-data-type"
|
80
88
|
require "arrow/struct-array"
|
81
89
|
require "arrow/struct-array-builder"
|
@@ -141,6 +149,7 @@ module Arrow
|
|
141
149
|
when "Arrow::Date32Array",
|
142
150
|
"Arrow::Date64Array",
|
143
151
|
"Arrow::Decimal128Array",
|
152
|
+
"Arrow::Decimal256Array",
|
144
153
|
"Arrow::Time32Array",
|
145
154
|
"Arrow::Time64Array",
|
146
155
|
"Arrow::TimestampArray"
|
@@ -149,6 +158,12 @@ module Arrow
|
|
149
158
|
method_name = "get_raw_value"
|
150
159
|
end
|
151
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)
|
152
167
|
else
|
153
168
|
super
|
154
169
|
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
|