red-gandiva 0.15.1 → 0.16.0
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/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/test-projector.rb +20 -21
- metadata +30 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58f3d77bc9396b1da9c54c9ce7429ccae42240af4cee7542bd484c6c69b9a05c
|
4
|
+
data.tar.gz: 5c632d1300bb99f30bb54137244858f505202139b59329612f89cbdd872e04d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33a743a8936c77ead7a9ca9a2cb6f5e29a9605f1982e6d2249997b7a85606c5e56a841f53008f7480404521fd0abacbab491c2e60a542020bcb9b4d2f96084a5
|
7
|
+
data.tar.gz: 4f99eee50279cba73d4411d2b07607abdc332a1eca031eb6241490946daa17132b9e652218bf9959489ff02bc30cbecfa50e0bfaad5f93cddffb6f5aa25ddd0a
|
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
|
```
|
@@ -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/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: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Apache Arrow Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-07 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: 0.16.0
|
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: 0.16.0
|
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
|
@@ -108,12 +127,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
127
|
- !ruby/object:Gem::Version
|
109
128
|
version: '0'
|
110
129
|
requirements: []
|
111
|
-
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.7.6.2
|
112
132
|
signing_key:
|
113
133
|
specification_version: 4
|
114
134
|
summary: Red Gandiva is the Ruby bindings of Gandiva
|
115
135
|
test_files:
|
116
|
-
- test/test-projector.rb
|
117
|
-
- test/run-test.rb
|
118
|
-
- test/helper.rb
|
119
136
|
- test/test-boolean-literal-node.rb
|
137
|
+
- test/expression-builder/test-add.rb
|
138
|
+
- test/expression-builder/test-record.rb
|
139
|
+
- test/helper.rb
|
140
|
+
- test/run-test.rb
|
141
|
+
- test/test-projector.rb
|