-
Notifications
You must be signed in to change notification settings - Fork 4
/
test-session-context.rb
125 lines (117 loc) · 4.34 KB
/
test-session-context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# Copyright 2022-2023 Sutou Kouhei <kou@clear-code.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class SessionContextTest < Test::Unit::TestCase
def setup
@context = DataFusion::SessionContext.new
end
def test_sql
schema = Arrow::Schema.new([Arrow::Field.new("Int64(1)", :int64, false)])
assert_equal(Arrow::Table.new(schema, [Arrow::Int64Array.new([1])]),
@context.sql("SELECT 1").to_table)
end
def test_deregister
record_batch = Arrow::RecordBatch.new(boolean: [true, false, nil],
integer: [1, nil, 3])
assert do
@context.register_record_batch("data", record_batch)
end
data_frame = @context.sql("SELECT * FROM data")
assert_equal(record_batch.to_table, data_frame.to_table)
assert do
@context.deregister("data")
end
message = "[session-context][sql] Error during planning: " +
"table 'datafusion.public.data' not found"
assert_raise(DataFusion::Error::Plan.new(message)) do
@context.sql("SELECT * FROM data")
end
end
def test_register_record_batch
record_batch = Arrow::RecordBatch.new(boolean: [true, false, nil],
integer: [1, nil, 3])
assert do
@context.register_record_batch("data", record_batch)
end
data_frame = @context.sql("SELECT * FROM data")
assert_equal(record_batch.to_table, data_frame.to_table)
end
def test_register_table
boolean_chunks = [
Arrow::Array.new([true]),
Arrow::Array.new([false, nil]),
]
integer_chunks = [
Arrow::Array.new([1, nil]),
Arrow::Array.new([3]),
]
table = Arrow::Table.new(boolean: Arrow::ChunkedArray.new(boolean_chunks),
integer: Arrow::ChunkedArray.new(integer_chunks))
@context.register_table("data", table)
data_frame = @context.sql("SELECT * FROM data")
assert_equal(table, data_frame.to_table)
end
sub_test_case("#register_csv") do
def setup
super
Tempfile.open(["datafusion", ".csv"]) do |csv_file|
@csv_file = csv_file
@csv_file.puts(<<-CSV)
a,b,c
1,2,3
10,20,30
CSV
@csv_file.close
schema = Arrow::Schema.new([
Arrow::Field.new("a", :int64),
Arrow::Field.new("b", :int64),
Arrow::Field.new("c", :int64),
])
@table = Arrow::Table.new(schema,
[
Arrow::Int64Array.new([1, 10]),
Arrow::Int64Array.new([2, 20]),
Arrow::Int64Array.new([3, 30]),
])
yield
end
end
def test_no_options
assert do
@context.register_csv("data", @csv_file.path)
end
data_frame = @context.sql("SELECT * FROM data")
assert_equal(@table, data_frame.to_table)
end
def test_options
options = DataFusion::CSVReadOptions.new
schema = Arrow::Schema.new([
Arrow::Field.new("a", :int8, false),
Arrow::Field.new("b", :int8, false),
Arrow::Field.new("c", :int8, false),
])
table = Arrow::Table.new(schema,
[
Arrow::Int8Array.new([1, 10]),
Arrow::Int8Array.new([2, 20]),
Arrow::Int8Array.new([3, 30]),
])
options.schema = schema
assert do
@context.register_csv("data", @csv_file.path, options)
end
data_frame = @context.sql("SELECT * FROM data")
assert_equal(table, data_frame.to_table)
end
end
end