red-gandiva 0.15.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -14
- data/Rakefile +5 -3
- data/lib/gandiva/arrow-schema.rb +25 -0
- data/lib/gandiva/expression-builder.rb +45 -0
- data/lib/gandiva/expression-builder/add.rb +40 -0
- data/lib/gandiva/expression-builder/binary-operation.rb +38 -0
- data/lib/gandiva/expression-builder/context.rb +26 -0
- data/lib/gandiva/expression-builder/divide.rb +34 -0
- data/lib/gandiva/expression-builder/elsif.rb +36 -0
- data/lib/gandiva/expression-builder/equal.rb +33 -0
- data/lib/gandiva/expression-builder/field.rb +32 -0
- data/lib/gandiva/expression-builder/greater-than.rb +33 -0
- data/lib/gandiva/expression-builder/if.rb +75 -0
- data/lib/gandiva/expression-builder/less-than.rb +33 -0
- data/lib/gandiva/expression-builder/literal.rb +65 -0
- data/lib/gandiva/expression-builder/multiply.rb +34 -0
- data/lib/gandiva/expression-builder/record.rb +45 -0
- data/lib/gandiva/expression-builder/subtract.rb +34 -0
- data/lib/gandiva/expression-builder/value.rb +55 -0
- data/lib/gandiva/loader.rb +9 -0
- data/lib/gandiva/version.rb +1 -1
- data/test/expression-builder/test-add.rb +54 -0
- data/test/expression-builder/test-record.rb +45 -0
- data/test/run-test.rb +8 -1
- data/test/test-projector.rb +20 -21
- metadata +29 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d0162ea6bd0a7ec7c35fff0d19cd071b2f5090ea1962d634e7077f09b5a335b
|
4
|
+
data.tar.gz: 1fd3fb46dc4804053d8a303d2a2849dea6475720220064303f60ca43a9b52551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4ef5fa824e20f2ee4fd9918854aeb8c7cb9df47c84e6b90108509305ef21c58c0a1d6477025ebd38f0f971ca7fa9c887bf78dd115e0bfb6c58b6fed4bc29d0a
|
7
|
+
data.tar.gz: 652526b57edff7c5d20b1aa2545afcf9b42ac0801a06f000565f661ea893092fc018c79ddc5362c7db604f0c64ee622f466c8949fedefb7d89640a48fa86dd01
|
data/README.md
CHANGED
@@ -46,18 +46,23 @@ Install Red Gandiva after you install Gandiva GLib:
|
|
46
46
|
```ruby
|
47
47
|
require "gandiva"
|
48
48
|
|
49
|
-
|
50
|
-
field2
|
51
|
-
schema =
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
49
|
+
table = Arrow::Table.new(:field1 => Arrow::Int32Array.new([1, 2, 3, 4]),
|
50
|
+
:field2 => Arrow::Int32Array.new([11, 13, 15, 17]))
|
51
|
+
schema = table.schema
|
52
|
+
|
53
|
+
expression1 = schema.build_expression do |record|
|
54
|
+
record.field1 + record.field2
|
55
|
+
end
|
56
|
+
|
57
|
+
expression2 = schema.build_expression do |record, context|
|
58
|
+
context.if(record.field1 > record.field2)
|
59
|
+
.then(record.field1 / record.field2)
|
60
|
+
.else(record.field1)
|
61
|
+
end
|
62
|
+
|
63
|
+
projector = Gandiva::Projector.new(schema, [expression1, expression2])
|
64
|
+
table.each_record_batch do |record_batch|
|
65
|
+
outputs = projector.evaluate(record_batch)
|
66
|
+
puts outputs.collect(&:values))
|
67
|
+
end
|
63
68
|
```
|
data/Rakefile
CHANGED
@@ -30,10 +30,12 @@ release_task.prerequisites.replace(["build", "release:rubygem_push"])
|
|
30
30
|
|
31
31
|
desc "Run tests"
|
32
32
|
task :test do
|
33
|
-
cd(
|
34
|
-
|
33
|
+
cd(base_dir) do
|
34
|
+
cd("dependency-check") do
|
35
|
+
ruby("-S", "rake")
|
36
|
+
end
|
37
|
+
ruby("test/run-test.rb")
|
35
38
|
end
|
36
|
-
ruby("test/run-test.rb")
|
37
39
|
end
|
38
40
|
|
39
41
|
task default: :test
|
@@ -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 Schema
|
20
|
+
def build_expression(&block)
|
21
|
+
builder = Gandiva::ExpressionBuilder.new(self)
|
22
|
+
builder.build(&block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
def initialize(schema)
|
21
|
+
@schema = schema
|
22
|
+
end
|
23
|
+
|
24
|
+
def build
|
25
|
+
builder = yield(Record.new(@schema), Context.new)
|
26
|
+
node = builder.build
|
27
|
+
Expression.new(node,
|
28
|
+
Arrow::Field.new("result", node.return_type))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
require "gandiva/expression-builder/add"
|
34
|
+
require "gandiva/expression-builder/context"
|
35
|
+
require "gandiva/expression-builder/divide"
|
36
|
+
require "gandiva/expression-builder/elsif"
|
37
|
+
require "gandiva/expression-builder/equal"
|
38
|
+
require "gandiva/expression-builder/field"
|
39
|
+
require "gandiva/expression-builder/greater-than"
|
40
|
+
require "gandiva/expression-builder/if"
|
41
|
+
require "gandiva/expression-builder/literal"
|
42
|
+
require "gandiva/expression-builder/less-than"
|
43
|
+
require "gandiva/expression-builder/multiply"
|
44
|
+
require "gandiva/expression-builder/record"
|
45
|
+
require "gandiva/expression-builder/subtract"
|
@@ -0,0 +1,40 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Add < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("add", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
# TODO: More clever implementation. e.g. (int64, float) -> float
|
30
|
+
left_return_type = left_node.return_type
|
31
|
+
right_return_type = right_node.return_type
|
32
|
+
if left_return_type.bit_width > right_return_type.bit_width
|
33
|
+
left_return_type
|
34
|
+
else
|
35
|
+
right_return_type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -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
|
+
require "gandiva/expression-builder/value"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class BinaryOperation < Value
|
23
|
+
def initialize(operator, left, right)
|
24
|
+
@operator = operator
|
25
|
+
@left = left
|
26
|
+
@right = right
|
27
|
+
end
|
28
|
+
|
29
|
+
def build
|
30
|
+
left_node = @left.build
|
31
|
+
right_node = @right.build
|
32
|
+
FunctionNode.new(@operator,
|
33
|
+
[left_node, right_node],
|
34
|
+
return_type(left_node, right_node))
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
class Context
|
21
|
+
def if(condition)
|
22
|
+
If.new(condition)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Divide < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("divide", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
# TODO: Use float if left or right is float
|
30
|
+
left_node.return_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
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
|
+
require "gandiva/expression-builder/if"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Elsif < If
|
23
|
+
def initialize(parent, condition)
|
24
|
+
@parent = parent
|
25
|
+
super(condition)
|
26
|
+
end
|
27
|
+
|
28
|
+
def build
|
29
|
+
elsif_node = super
|
30
|
+
build_if_node(@parent.condition_node,
|
31
|
+
@parent.then_node,
|
32
|
+
elsif_node)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Equal < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("equal", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
Arrow::BooleanDataType.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -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
|
+
require "gandiva/expression-builder/value"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Field < Value
|
23
|
+
def initialize(field)
|
24
|
+
@field = field
|
25
|
+
end
|
26
|
+
|
27
|
+
def build
|
28
|
+
FieldNode.new(@field)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class GreaterThan < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("greater_than", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
Arrow::BooleanDataType.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,75 @@
|
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
class If
|
21
|
+
def initialize(condition)
|
22
|
+
@condition = condition
|
23
|
+
@then = nil
|
24
|
+
@else = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def then(clause)
|
28
|
+
@then = clause
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def else(clause)
|
33
|
+
@else = clause
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def elsif(condition)
|
38
|
+
Elsif.new(self, condition)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build
|
42
|
+
build_if_node(condition_node,
|
43
|
+
then_node,
|
44
|
+
else_node)
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
def condition_node
|
49
|
+
@condition.build
|
50
|
+
end
|
51
|
+
|
52
|
+
def then_node
|
53
|
+
@then&.build
|
54
|
+
end
|
55
|
+
|
56
|
+
def else_node
|
57
|
+
@else&.build
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def build_if_node(condition_node, then_node, else_node)
|
62
|
+
if then_node and else_node
|
63
|
+
# TODO: Validate then_node.return_type == else_node.return_type
|
64
|
+
return_type = then_node.return_type
|
65
|
+
else
|
66
|
+
return_type = (then_node || else_node).return_type
|
67
|
+
end
|
68
|
+
IfNode.new(condition_node,
|
69
|
+
then_node,
|
70
|
+
else_node,
|
71
|
+
return_type)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class LessThan < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("less_than", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
Arrow::BooleanDataType.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,65 @@
|
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
class Literal
|
21
|
+
class << self
|
22
|
+
def resolve(value)
|
23
|
+
case value
|
24
|
+
when true, false
|
25
|
+
new(BooleanLiteralNode, value)
|
26
|
+
when Integer
|
27
|
+
if value < -(2 ** 31)
|
28
|
+
new(Int64LiteralNode, value)
|
29
|
+
elsif value < -(2 ** 15)
|
30
|
+
new(Int32LiteralNode, value)
|
31
|
+
elsif value < -(2 ** 7)
|
32
|
+
new(Int16LiteralNode, value)
|
33
|
+
elsif value < 0
|
34
|
+
new(Int8LiteralNode, value)
|
35
|
+
elsif value < (2 ** 8 - 1)
|
36
|
+
new(UInt8LiteralNode, value)
|
37
|
+
elsif value < (2 ** 16 - 1)
|
38
|
+
new(UInt16LiteralNode, value)
|
39
|
+
elsif value < (2 ** 32 - 1)
|
40
|
+
new(UInt32LiteralNode, value)
|
41
|
+
else
|
42
|
+
new(UInt64LiteralNode, value)
|
43
|
+
end
|
44
|
+
when Float
|
45
|
+
new(DoubleLiteralNode, value)
|
46
|
+
when String
|
47
|
+
new(StringLiteralNode, value)
|
48
|
+
else
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :value
|
55
|
+
def initialize(node_class, value)
|
56
|
+
@node_class = node_class
|
57
|
+
@value = value
|
58
|
+
end
|
59
|
+
|
60
|
+
def build
|
61
|
+
@node_class.new(value)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Multiply < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("multiply", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
# TODO: Use larger type
|
30
|
+
right_node.return_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
class Record
|
21
|
+
def initialize(schema)
|
22
|
+
@schema = schema
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to_missing?(name, include_private)
|
26
|
+
return true if @schema[name]
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(name, *args)
|
31
|
+
return super unless args.empty?
|
32
|
+
self[name] || super
|
33
|
+
end
|
34
|
+
|
35
|
+
def [](name)
|
36
|
+
field = @schema[name]
|
37
|
+
if field
|
38
|
+
Field.new(field)
|
39
|
+
else
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
+
require "gandiva/expression-builder/binary-operation"
|
19
|
+
|
20
|
+
module Gandiva
|
21
|
+
class ExpressionBuilder
|
22
|
+
class Subtract < BinaryOperation
|
23
|
+
def initialize(left, right)
|
24
|
+
super("subtract", left, right)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def return_type(left_node, right_node)
|
29
|
+
# TODO: Use larger type
|
30
|
+
right_node.return_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,55 @@
|
|
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 Gandiva
|
19
|
+
class ExpressionBuilder
|
20
|
+
class Value
|
21
|
+
def +(right)
|
22
|
+
Add.new(self, resolve(right))
|
23
|
+
end
|
24
|
+
|
25
|
+
def -(right)
|
26
|
+
Subtract.new(self, resolve(right))
|
27
|
+
end
|
28
|
+
|
29
|
+
def *(right)
|
30
|
+
Multiply.new(self, resolve(right))
|
31
|
+
end
|
32
|
+
|
33
|
+
def /(right)
|
34
|
+
Divide.new(self, resolve(right))
|
35
|
+
end
|
36
|
+
|
37
|
+
def >(right)
|
38
|
+
GreaterThan.new(self, resolve(right))
|
39
|
+
end
|
40
|
+
|
41
|
+
def <(right)
|
42
|
+
LessThan.new(self, resolve(right))
|
43
|
+
end
|
44
|
+
|
45
|
+
def ==(right)
|
46
|
+
Equal.new(self, resolve(right))
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def resolve(value)
|
51
|
+
Literal.resolve(value) or value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/gandiva/loader.rb
CHANGED
data/lib/gandiva/version.rb
CHANGED
@@ -0,0 +1,54 @@
|
|
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
|
+
class TestExpressionBuilderAdd < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@table = Arrow::Table.new(int32_field: Arrow::Int32Array.new([1]),
|
21
|
+
int64_field: Arrow::Int64Array.new([2]))
|
22
|
+
@schema = @table.schema
|
23
|
+
end
|
24
|
+
|
25
|
+
def build
|
26
|
+
record = Gandiva::ExpressionBuilder::Record.new(@schema)
|
27
|
+
builder = yield(record)
|
28
|
+
builder.build
|
29
|
+
end
|
30
|
+
|
31
|
+
test("literal") do
|
32
|
+
node = build do |record|
|
33
|
+
record.int32_field + (2 ** 63)
|
34
|
+
end
|
35
|
+
assert_equal("uint64 add((int32) int32_field, (const uint64) #{2 ** 63})",
|
36
|
+
node.to_s)
|
37
|
+
end
|
38
|
+
|
39
|
+
test("int32 + int64") do
|
40
|
+
node = build do |record|
|
41
|
+
record.int32_field + record.int64_field
|
42
|
+
end
|
43
|
+
assert_equal("int64 add((int32) int32_field, (int64) int64_field)",
|
44
|
+
node.to_s)
|
45
|
+
end
|
46
|
+
|
47
|
+
test("int64 + int32") do
|
48
|
+
node = build do |record|
|
49
|
+
record.int64_field + record.int32_field
|
50
|
+
end
|
51
|
+
assert_equal("int64 add((int64) int64_field, (int32) int32_field)",
|
52
|
+
node.to_s)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,45 @@
|
|
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
|
+
class TestExpressionBuilderRecord < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@table = Arrow::Table.new(field: Arrow::Int32Array.new([1, 13, 3, 17]))
|
21
|
+
@schema = @table.schema
|
22
|
+
end
|
23
|
+
|
24
|
+
def build
|
25
|
+
record = Gandiva::ExpressionBuilder::Record.new(@schema)
|
26
|
+
builder = yield(record)
|
27
|
+
builder.build
|
28
|
+
end
|
29
|
+
|
30
|
+
test("name") do
|
31
|
+
node = build do |record|
|
32
|
+
record.field
|
33
|
+
end
|
34
|
+
assert_equal("(int32) field",
|
35
|
+
node.to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
test("#[]") do
|
39
|
+
node = build do |record|
|
40
|
+
record[:field]
|
41
|
+
end
|
42
|
+
assert_equal("(int32) field",
|
43
|
+
node.to_s)
|
44
|
+
end
|
45
|
+
end
|
data/test/run-test.rb
CHANGED
@@ -34,7 +34,14 @@ test_dir = base_dir + "test"
|
|
34
34
|
arrow_lib_dir = arrow_base_dir + "lib"
|
35
35
|
arrow_ext_dir = arrow_base_dir + "ext" + "arrow"
|
36
36
|
|
37
|
-
|
37
|
+
build_dir = ENV["BUILD_DIR"]
|
38
|
+
if build_dir
|
39
|
+
arrow_build_dir = Pathname.new(build_dir) + "red-arrow"
|
40
|
+
else
|
41
|
+
arrow_build_dir = arrow_ext_dir
|
42
|
+
end
|
43
|
+
|
44
|
+
$LOAD_PATH.unshift(arrow_build_dir.to_s)
|
38
45
|
$LOAD_PATH.unshift(arrow_lib_dir.to_s)
|
39
46
|
$LOAD_PATH.unshift(lib_dir.to_s)
|
40
47
|
|
data/test/test-projector.rb
CHANGED
@@ -17,32 +17,31 @@
|
|
17
17
|
|
18
18
|
class TestProjector < Test::Unit::TestCase
|
19
19
|
def test_evaluate
|
20
|
-
table = Arrow::Table.new(:field1 => Arrow::Int32Array.new([1,
|
21
|
-
:field2 => Arrow::Int32Array.new([11,
|
20
|
+
table = Arrow::Table.new(:field1 => Arrow::Int32Array.new([1, 13, 3, 17]),
|
21
|
+
:field2 => Arrow::Int32Array.new([11, 2, 15, 17]),
|
22
|
+
:field3 => Arrow::Int32Array.new([1, 10, 2, 2]))
|
22
23
|
schema = table.schema
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
subtract_result = Arrow::Field.new("subtract_result", :int32)
|
37
|
-
subtract_expression = Gandiva::Expression.new(subtract_function_node,
|
38
|
-
subtract_result)
|
24
|
+
|
25
|
+
expression1 = schema.build_expression do |record|
|
26
|
+
record.field1 + record.field2
|
27
|
+
end
|
28
|
+
|
29
|
+
expression2 = schema.build_expression do |record, context|
|
30
|
+
context.if(record.field1 > record.field2)
|
31
|
+
.then(record.field1 + record.field2 * record.field3)
|
32
|
+
.elsif(record.field1 == record.field2)
|
33
|
+
.then(record.field1 - record.field2 / record.field3)
|
34
|
+
.else(record.field2)
|
35
|
+
end
|
36
|
+
|
39
37
|
projector = Gandiva::Projector.new(schema,
|
40
|
-
[
|
38
|
+
[expression1, expression2])
|
39
|
+
|
41
40
|
table.each_record_batch do |record_batch|
|
42
41
|
outputs = projector.evaluate(record_batch)
|
43
42
|
assert_equal([
|
44
|
-
[12, 15, 18,
|
45
|
-
[
|
43
|
+
[12, 15, 18, 34],
|
44
|
+
[11, 33, 15, 9]
|
46
45
|
],
|
47
46
|
outputs.collect(&:values))
|
48
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: red-gandiva
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Apache Arrow Developers
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: red-arrow
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 1.0.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 1.0.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,9 +82,28 @@ files:
|
|
82
82
|
- Rakefile
|
83
83
|
- dependency-check/Rakefile
|
84
84
|
- lib/gandiva.rb
|
85
|
+
- lib/gandiva/arrow-schema.rb
|
86
|
+
- lib/gandiva/expression-builder.rb
|
87
|
+
- lib/gandiva/expression-builder/add.rb
|
88
|
+
- lib/gandiva/expression-builder/binary-operation.rb
|
89
|
+
- lib/gandiva/expression-builder/context.rb
|
90
|
+
- lib/gandiva/expression-builder/divide.rb
|
91
|
+
- lib/gandiva/expression-builder/elsif.rb
|
92
|
+
- lib/gandiva/expression-builder/equal.rb
|
93
|
+
- lib/gandiva/expression-builder/field.rb
|
94
|
+
- lib/gandiva/expression-builder/greater-than.rb
|
95
|
+
- lib/gandiva/expression-builder/if.rb
|
96
|
+
- lib/gandiva/expression-builder/less-than.rb
|
97
|
+
- lib/gandiva/expression-builder/literal.rb
|
98
|
+
- lib/gandiva/expression-builder/multiply.rb
|
99
|
+
- lib/gandiva/expression-builder/record.rb
|
100
|
+
- lib/gandiva/expression-builder/subtract.rb
|
101
|
+
- lib/gandiva/expression-builder/value.rb
|
85
102
|
- lib/gandiva/loader.rb
|
86
103
|
- lib/gandiva/version.rb
|
87
104
|
- red-gandiva.gemspec
|
105
|
+
- test/expression-builder/test-add.rb
|
106
|
+
- test/expression-builder/test-record.rb
|
88
107
|
- test/helper.rb
|
89
108
|
- test/run-test.rb
|
90
109
|
- test/test-boolean-literal-node.rb
|
@@ -93,7 +112,7 @@ homepage: https://arrow.apache.org/
|
|
93
112
|
licenses:
|
94
113
|
- Apache-2.0
|
95
114
|
metadata: {}
|
96
|
-
post_install_message:
|
115
|
+
post_install_message:
|
97
116
|
rdoc_options: []
|
98
117
|
require_paths:
|
99
118
|
- lib
|
@@ -108,12 +127,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
127
|
- !ruby/object:Gem::Version
|
109
128
|
version: '0'
|
110
129
|
requirements: []
|
111
|
-
rubygems_version: 3.
|
112
|
-
signing_key:
|
130
|
+
rubygems_version: 3.1.2
|
131
|
+
signing_key:
|
113
132
|
specification_version: 4
|
114
133
|
summary: Red Gandiva is the Ruby bindings of Gandiva
|
115
134
|
test_files:
|
116
135
|
- test/test-projector.rb
|
136
|
+
- test/expression-builder/test-add.rb
|
137
|
+
- test/expression-builder/test-record.rb
|
117
138
|
- test/run-test.rb
|
118
139
|
- test/helper.rb
|
119
140
|
- test/test-boolean-literal-node.rb
|