locomotive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/lib/locomotive.rb +6 -0
  2. data/lib/locomotive/core_extensions.rb +6 -0
  3. data/lib/locomotive/core_extensions/array.rb +22 -0
  4. data/lib/locomotive/core_extensions/class.rb +47 -0
  5. data/lib/locomotive/core_extensions/hash.rb +7 -0
  6. data/lib/locomotive/core_extensions/inflector.rb +29 -0
  7. data/lib/locomotive/core_extensions/module.rb +77 -0
  8. data/lib/locomotive/core_extensions/symbol.rb +17 -0
  9. data/lib/locomotive/misc.rb +1 -0
  10. data/lib/locomotive/misc/type_check.rb +129 -0
  11. data/lib/locomotive/relational_algebra.rb +8 -0
  12. data/lib/locomotive/relational_algebra/attributes.rb +171 -0
  13. data/lib/locomotive/relational_algebra/operators.rb +15 -0
  14. data/lib/locomotive/relational_algebra/operators/abstraction.rb +2 -0
  15. data/lib/locomotive/relational_algebra/operators/abstraction/lambda.rb +83 -0
  16. data/lib/locomotive/relational_algebra/operators/abstraction/variable.rb +65 -0
  17. data/lib/locomotive/relational_algebra/operators/aggregation.rb +2 -0
  18. data/lib/locomotive/relational_algebra/operators/aggregation/aggr_builtin.rb +26 -0
  19. data/lib/locomotive/relational_algebra/operators/aggregation/aggregation.rb +76 -0
  20. data/lib/locomotive/relational_algebra/operators/basic_operators.rb +144 -0
  21. data/lib/locomotive/relational_algebra/operators/boolean.rb +4 -0
  22. data/lib/locomotive/relational_algebra/operators/boolean/and.rb +9 -0
  23. data/lib/locomotive/relational_algebra/operators/boolean/basic_boolean.rb +66 -0
  24. data/lib/locomotive/relational_algebra/operators/boolean/not.rb +56 -0
  25. data/lib/locomotive/relational_algebra/operators/boolean/or.rb +9 -0
  26. data/lib/locomotive/relational_algebra/operators/builtins.rb +3 -0
  27. data/lib/locomotive/relational_algebra/operators/builtins/arith_builtin.rb +37 -0
  28. data/lib/locomotive/relational_algebra/operators/builtins/basic_builtin.rb +20 -0
  29. data/lib/locomotive/relational_algebra/operators/builtins/function.rb +69 -0
  30. data/lib/locomotive/relational_algebra/operators/comparisons.rb +6 -0
  31. data/lib/locomotive/relational_algebra/operators/comparisons/basic_comparison.rb +65 -0
  32. data/lib/locomotive/relational_algebra/operators/comparisons/equal.rb +13 -0
  33. data/lib/locomotive/relational_algebra/operators/comparisons/greater.rb +13 -0
  34. data/lib/locomotive/relational_algebra/operators/comparisons/greater_equal.rb +14 -0
  35. data/lib/locomotive/relational_algebra/operators/comparisons/less.rb +21 -0
  36. data/lib/locomotive/relational_algebra/operators/comparisons/less_equal.rb +13 -0
  37. data/lib/locomotive/relational_algebra/operators/error.rb +1 -0
  38. data/lib/locomotive/relational_algebra/operators/error/error.rb +52 -0
  39. data/lib/locomotive/relational_algebra/operators/filter.rb +1 -0
  40. data/lib/locomotive/relational_algebra/operators/filter/select.rb +50 -0
  41. data/lib/locomotive/relational_algebra/operators/join.rb +5 -0
  42. data/lib/locomotive/relational_algebra/operators/join/basic_join.rb +9 -0
  43. data/lib/locomotive/relational_algebra/operators/join/cross.rb +28 -0
  44. data/lib/locomotive/relational_algebra/operators/join/equi_join.rb +61 -0
  45. data/lib/locomotive/relational_algebra/operators/join/predicates.rb +95 -0
  46. data/lib/locomotive/relational_algebra/operators/join/theta_join.rb +53 -0
  47. data/lib/locomotive/relational_algebra/operators/projections.rb +2 -0
  48. data/lib/locomotive/relational_algebra/operators/projections/attach.rb +67 -0
  49. data/lib/locomotive/relational_algebra/operators/projections/projection.rb +106 -0
  50. data/lib/locomotive/relational_algebra/operators/ranking.rb +6 -0
  51. data/lib/locomotive/relational_algebra/operators/ranking/basic_ranking.rb +67 -0
  52. data/lib/locomotive/relational_algebra/operators/ranking/rank.rb +9 -0
  53. data/lib/locomotive/relational_algebra/operators/ranking/rank_lists.rb +45 -0
  54. data/lib/locomotive/relational_algebra/operators/ranking/row_id.rb +24 -0
  55. data/lib/locomotive/relational_algebra/operators/ranking/row_number.rb +55 -0
  56. data/lib/locomotive/relational_algebra/operators/ranking/row_rank.rb +9 -0
  57. data/lib/locomotive/relational_algebra/operators/serialization.rb +2 -0
  58. data/lib/locomotive/relational_algebra/operators/serialization/basic_serialize.rb +9 -0
  59. data/lib/locomotive/relational_algebra/operators/serialization/serialize_relation.rb +105 -0
  60. data/lib/locomotive/relational_algebra/operators/set.rb +4 -0
  61. data/lib/locomotive/relational_algebra/operators/set/basic_set.rb +24 -0
  62. data/lib/locomotive/relational_algebra/operators/set/difference.rb +11 -0
  63. data/lib/locomotive/relational_algebra/operators/set/distinct.rb +35 -0
  64. data/lib/locomotive/relational_algebra/operators/set/union.rb +11 -0
  65. data/lib/locomotive/relational_algebra/operators/tables.rb +3 -0
  66. data/lib/locomotive/relational_algebra/operators/tables/literal_table.rb +95 -0
  67. data/lib/locomotive/relational_algebra/operators/tables/nil.rb +13 -0
  68. data/lib/locomotive/relational_algebra/operators/tables/ref_table.rb +75 -0
  69. data/lib/locomotive/relational_algebra/operators/typeing.rb +1 -0
  70. data/lib/locomotive/relational_algebra/operators/typeing/cast.rb +59 -0
  71. data/lib/locomotive/relational_algebra/ordering.rb +30 -0
  72. data/lib/locomotive/relational_algebra/query_information.rb +666 -0
  73. data/lib/locomotive/relational_algebra/rel_alg_ast_node.rb +51 -0
  74. data/lib/locomotive/relational_algebra/rel_alg_exceptions.rb +15 -0
  75. data/lib/locomotive/relational_algebra/schema.rb +87 -0
  76. data/lib/locomotive/relational_algebra/types.rb +86 -0
  77. data/lib/locomotive/tree_helpers.rb +3 -0
  78. data/lib/locomotive/tree_helpers/annotations.rb +58 -0
  79. data/lib/locomotive/tree_helpers/ast.rb +99 -0
  80. data/lib/locomotive/tree_helpers/ast_traversal.rb +43 -0
  81. data/lib/locomotive/utils.rb +2 -0
  82. data/lib/locomotive/utils/relalg2xml.rb +239 -0
  83. data/lib/locomotive/utils/xml.rb +47 -0
  84. metadata +157 -0
@@ -0,0 +1,9 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Rank < Numbering; end
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,45 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class SortList
6
+ include Locomotive::XML
7
+ def_node :column
8
+
9
+ private
10
+
11
+ attr_accessor :sort_list
12
+ def_sig :sort_list=, { ConstAttribute => SortDirection }
13
+
14
+ public
15
+ delegate :[],
16
+ :to_a,
17
+ :to => :sort_list
18
+
19
+ def initialize(hash)
20
+ self.sort_list = hash
21
+ end
22
+
23
+ def attributes
24
+ sort_list.keys
25
+ end
26
+
27
+ def to_xml
28
+ pos = -1
29
+ sort_list.collect do |attr, dir|
30
+ column :name => attr.to_xml,
31
+ :function => :sort,
32
+ :position => pos += 1,
33
+ :direction => dir.to_xml,
34
+ :new => false
35
+ end.join
36
+ end
37
+
38
+ def clone
39
+ SortList.new( sort_list.clone )
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,24 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class RowId < Numbering
6
+ def initialize(op, res)
7
+ super(op,res,{})
8
+ end
9
+
10
+ def clone
11
+ RowId.new(child.clone,
12
+ res.clone)
13
+ end
14
+
15
+ def set(var,plan)
16
+ RowId.new(
17
+ child.set(var,plan),
18
+ res.clone)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,55 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class RowNum < Numbering
6
+ def_node :column
7
+
8
+ attr_accessor :part
9
+ def_sig :part=, [ConstAttribute]
10
+
11
+ def initialize(op, res, part, sortby)
12
+ self.part = part
13
+ super(op,res,sortby)
14
+ end
15
+
16
+ def child=(op)
17
+ unless op.schema.attributes?(part)
18
+ raise CorruptedSchema,
19
+ "Schema #{op.schema.attributes} does not " \
20
+ "contain all attributes of #{part}."
21
+ end
22
+ super(op)
23
+ end
24
+
25
+ def xml_content
26
+ pos = -1
27
+ content do
28
+ [column(:name => res.to_xml, :new => true),
29
+ sort_by.to_xml,
30
+ part.collect do |p|
31
+ column :name => p.to_xml,
32
+ :function => :partition,
33
+ :position => pos += 1,
34
+ :new => false
35
+ end.join].join
36
+ end
37
+ end
38
+
39
+ def clone
40
+ RowNum.new(child.clone,
41
+ res.clone,
42
+ part.clone,
43
+ sort_by.clone)
44
+ end
45
+
46
+ def set(var,plan)
47
+ RowNum.new(
48
+ child.set(var,plan),
49
+ res.clone, part.clone, sort_by.clone)
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,9 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class RowRank < Numbering; end
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,2 @@
1
+ require 'locomotive/relational_algebra/operators/serialization/basic_serialize'
2
+ require 'locomotive/relational_algebra/operators/serialization/serialize_relation'
@@ -0,0 +1,9 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Serialize < Binary; end
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,105 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ # serialize operator
6
+ class SerializeRelation < Serialize
7
+ include Locomotive::XML
8
+ def_node :logical_query_plan
9
+
10
+ attr_accessor :iter,
11
+ :pos,
12
+ :items
13
+ def_sig :iter=, ConstAttribute
14
+ def_sig :pos=, ConstAttribute
15
+ def_sig :items=, [ConstAttribute]
16
+
17
+ def initialize(side,alg,iter,pos,items)
18
+ self.iter,
19
+ self.pos,
20
+ self.items = iter, pos, items
21
+ super(side,alg)
22
+ end
23
+
24
+ def left_and_right(side,alg)
25
+ unless alg.schema.attributes?([self.iter])
26
+ raise CorruptedSchema,
27
+ "Schema #{alg.schema.attributes} does not " \
28
+ "contain all attributes of #{iter}."
29
+ end
30
+ unless alg.schema.attributes?([self.pos])
31
+ raise CorruptedSchema,
32
+ "Schema #{alg.schema.attributes} does not " \
33
+ "contain all attributes of #{pos}."
34
+ end
35
+ unless alg.schema.attributes?(self.items)
36
+ raise CorruptedSchema,
37
+ "Schema #{alg.schema.attributes} does not " \
38
+ "contain all attributes of #{items}."
39
+ end
40
+ self.schema = alg.schema.clone
41
+ super(side,alg)
42
+ end
43
+
44
+ def xml_kind
45
+ "serialize relation".to_sym
46
+ end
47
+
48
+ def xml_content
49
+ pos_ = -1
50
+ content do
51
+ [
52
+ column(:name => iter.to_xml, :new => false, :function => :iter),
53
+ column(:name => pos.to_xml, :new => false, :function => :pos),
54
+ items.collect do |it|
55
+ column :name => it.to_xml,
56
+ :new => false,
57
+ :function => :item,
58
+ :position => pos_ += 1
59
+ end.join
60
+ ].join
61
+ end
62
+ end
63
+
64
+ def serialize
65
+ xml_id = 0
66
+ xml_list = []
67
+
68
+ self.traverse_strategy = Locomotive::AstHelpers::PostOrderTraverse
69
+ self.traverse do |op|
70
+ next if op.ann_vis
71
+ op.ann_xml_id = xml_id += 1
72
+ xml_list << op.to_xml
73
+ op.ann_vis = true
74
+ end
75
+
76
+ self.traverse do |op|
77
+ op.ann_vis = false
78
+ end
79
+
80
+ logical_query_plan :unique_names => true do
81
+ xml_list.join
82
+ end
83
+ end
84
+
85
+ def clone
86
+ Serialization.new(left.clone,
87
+ right.clone,
88
+ iter.clone,
89
+ pos.clone,
90
+ items.clone)
91
+ end
92
+
93
+ def set(var,plan)
94
+ Serialization.new(
95
+ left.set(var,plan),
96
+ right.set(var,plan),
97
+ iter.clone,
98
+ pos.clone,
99
+ items.clone)
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,4 @@
1
+ require 'locomotive/relational_algebra/operators/set/basic_set'
2
+ require 'locomotive/relational_algebra/operators/set/difference'
3
+ require 'locomotive/relational_algebra/operators/set/union'
4
+ require 'locomotive/relational_algebra/operators/set/distinct'
@@ -0,0 +1,24 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Set < Binary
6
+ def left_and_right(op1,op2)
7
+ self.schema = op1.schema.clone
8
+ super(op1,op2)
9
+ end
10
+
11
+ def clone
12
+ self.class.new(left.clone, right.clone)
13
+ end
14
+
15
+ def set(var,plan)
16
+ self.class.new(
17
+ left.set(var,plan),
18
+ right.set(var,plan))
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,11 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ # set operators
6
+ # FIXME: sanity checks (schemas have to be equal)
7
+ class Difference < Set; end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,35 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Distinct < Unary
6
+
7
+ def initialize(op)
8
+ super(op)
9
+ end
10
+
11
+ def child=(op)
12
+ self.schema = op.schema.clone
13
+ super(op)
14
+ end
15
+
16
+ def clone
17
+ Distinct.new(
18
+ child.clone)
19
+ end
20
+
21
+ def set(var,plan)
22
+ Distinct.new(
23
+ child.set(var,plan))
24
+ end
25
+
26
+ def xml_content
27
+ content do
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,11 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ # set operators
6
+ # FIXME: sanity checks (schemas have to be equal)
7
+ class Union < Set; end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'locomotive/relational_algebra/operators/tables/literal_table'
2
+ require 'locomotive/relational_algebra/operators/tables/nil'
3
+ require 'locomotive/relational_algebra/operators/tables/ref_table'
@@ -0,0 +1,95 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class LiteralList
6
+ include Locomotive::XML
7
+ def_node :column
8
+
9
+ protected
10
+
11
+ attr_accessor :literal_list
12
+ def_sig :literal_list=, { ConstAttribute => [RAtomic] }
13
+
14
+
15
+ public
16
+ delegate :[],
17
+ :collect,
18
+ :to_a,
19
+ :to => :literal_list
20
+
21
+ def initialize(hash)
22
+ self.literal_list = hash
23
+ end
24
+
25
+ def attributes
26
+ literal_list.keys
27
+ end
28
+
29
+ def to_xml
30
+ literal_list.collect do |attr,atomary|
31
+ column :name => attr.to_xml, :new => true do
32
+ atomary.collect do |atom|
33
+ atom.to_xml
34
+ end.join
35
+ end
36
+ end.join
37
+ end
38
+
39
+ def clone
40
+ LiteralList.new( literal_list.clone )
41
+ end
42
+ end
43
+
44
+ class LiteralTable < Leaf
45
+ attr_reader :lit_list
46
+
47
+ def lit_list=(llist)
48
+ s = Hash[ *llist.collect do |attr, types|
49
+ [attr,types.collect { |a| a.type }.uniq]
50
+ end.flatten_once ]
51
+ self.schema = Schema.new(s)
52
+ @lit_list = llist
53
+ end
54
+
55
+ def to_literal_list(llist)
56
+ case
57
+ when Hash === llist then
58
+ LiteralList.new(llist)
59
+ when LiteralList === llist then
60
+ llist
61
+ end
62
+ end
63
+ private :to_literal_list
64
+
65
+
66
+ def_sig :lit_list=, LiteralList
67
+ private :lit_list
68
+
69
+
70
+ def initialize(llist)
71
+ self.lit_list = to_literal_list llist
72
+ end
73
+
74
+ def xml_kind
75
+ :table
76
+ end
77
+
78
+ def xml_content
79
+ content do
80
+ lit_list.to_xml
81
+ end
82
+ end
83
+
84
+ def clone
85
+ LiteralTable.new( lit_list.clone )
86
+ end
87
+ end
88
+
89
+ def LiteralTable(values)
90
+ LiteralTable.new(values)
91
+ end
92
+
93
+ end
94
+
95
+ end