kou commented on a change in pull request #11167:
URL: https://github.com/apache/arrow/pull/11167#discussion_r710835738
##########
File path: ruby/red-arrow/lib/arrow/table.rb
##########
@@ -261,12 +261,35 @@ def slice(*args)
expected_n_args = nil
case args.size
when 1
- if args[0].is_a?(Integer)
+ case args[0]
+ when Integer
index = args[0]
index += n_rows if index < 0
return nil if index < 0
return nil if index >= n_rows
return Record.new(self, index)
+ when Hash
+ hash = args[0]
+ if hash.size != 1
+ message = "wrong number of hash size (given #{hash.size},
expected 1)"
+ raise ArgumentError, message
+ end
+
+ key = hash.keys[0]
+ value = hash.values[0]
+ case value
+ when Integer, String, TrueClass, FalseClass
+ slicers << (Slicer.new(self).send(key) == value)
+ when Range
+ if value.end == nil
+ slicers << (Slicer.new(self).send(key) >= value.first)
+ else
+ slicers << (Slicer.new(self).send(key) <= value.last)
+ end
+ else
+ message = "hash value must be Integer, String, TrueClass,
FalseClass, Range"
+ raise ArgumentError, message
+ end
Review comment:
How about using `Hash#each`?
```suggestion
condition_pairs = args[0]
slicer = Slicer.new(self)
conditions = []
condition_pairs.each do |key, value|
case value
when Range
# TODO: Optimize "begin <= key <= end" case by missing
"between" kernel
# https://issues.apache.org/jira/browse/ARROW-9843
unless value.begin.nil?
conditions = (slicer[key] >= value.begin)
end
unless value.end.nil?
if value.exclude_end?
conditions << (slicer[key] < value.end)
else
conditions << (slicer[key] <= value.end)
end
end
else
conditions = (slicer[key] == value)
end
end
slicers << conditions.inject(:&)
```
##########
File path: ruby/red-arrow/test/test-table.rb
##########
@@ -147,6 +147,34 @@ def setup
TABLE
end
+ test("Key Filter: equal number") do
+ assert_equal(<<-TABLE, @table.slice(count: 16).to_s)
+ count visible
+0 16 true
+ TABLE
+ end
+
+ test("Key Filter Range: include end") do
+ assert_equal(<<-TABLE, @table.slice(count: 16..).to_s)
+ count visible
+0 16 true
+1 32 false
+2 64 (null)
+3 128 (null)
+ TABLE
+ end
+
+ test("Key Filter Range: exclude end") do
Review comment:
```suggestion
test("{key: Range}: include end") do
```
##########
File path: ruby/red-arrow/test/test-table.rb
##########
@@ -147,6 +147,34 @@ def setup
TABLE
end
+ test("Key Filter: equal number") do
Review comment:
```suggestion
test("{key: value}") do
```
##########
File path: ruby/red-arrow/test/test-table.rb
##########
@@ -147,6 +147,34 @@ def setup
TABLE
end
+ test("Key Filter: equal number") do
+ assert_equal(<<-TABLE, @table.slice(count: 16).to_s)
+ count visible
+0 16 true
+ TABLE
+ end
+
+ test("Key Filter Range: include end") do
Review comment:
```suggestion
test("{key: Range}: endless") do
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]