masamune 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +54 -0
  4. data/Rakefile +15 -0
  5. data/bin/masamune-elastic-mapreduce +4 -0
  6. data/bin/masamune-hive +4 -0
  7. data/bin/masamune-psql +4 -0
  8. data/bin/masamune-shell +4 -0
  9. data/lib/masamune.rb +56 -0
  10. data/lib/masamune/accumulate.rb +60 -0
  11. data/lib/masamune/actions.rb +38 -0
  12. data/lib/masamune/actions/data_flow.rb +131 -0
  13. data/lib/masamune/actions/date_parse.rb +75 -0
  14. data/lib/masamune/actions/elastic_mapreduce.rb +68 -0
  15. data/lib/masamune/actions/execute.rb +52 -0
  16. data/lib/masamune/actions/filesystem.rb +37 -0
  17. data/lib/masamune/actions/hadoop_filesystem.rb +40 -0
  18. data/lib/masamune/actions/hadoop_streaming.rb +41 -0
  19. data/lib/masamune/actions/hive.rb +74 -0
  20. data/lib/masamune/actions/postgres.rb +76 -0
  21. data/lib/masamune/actions/postgres_admin.rb +34 -0
  22. data/lib/masamune/actions/s3cmd.rb +44 -0
  23. data/lib/masamune/actions/transform.rb +89 -0
  24. data/lib/masamune/after_initialize_callbacks.rb +55 -0
  25. data/lib/masamune/cached_filesystem.rb +110 -0
  26. data/lib/masamune/commands.rb +37 -0
  27. data/lib/masamune/commands/elastic_mapreduce.rb +119 -0
  28. data/lib/masamune/commands/hadoop_filesystem.rb +57 -0
  29. data/lib/masamune/commands/hadoop_streaming.rb +116 -0
  30. data/lib/masamune/commands/hive.rb +178 -0
  31. data/lib/masamune/commands/interactive.rb +37 -0
  32. data/lib/masamune/commands/postgres.rb +128 -0
  33. data/lib/masamune/commands/postgres_admin.rb +72 -0
  34. data/lib/masamune/commands/postgres_common.rb +33 -0
  35. data/lib/masamune/commands/retry_with_backoff.rb +60 -0
  36. data/lib/masamune/commands/s3cmd.rb +70 -0
  37. data/lib/masamune/commands/shell.rb +202 -0
  38. data/lib/masamune/configuration.rb +195 -0
  39. data/lib/masamune/data_plan.rb +31 -0
  40. data/lib/masamune/data_plan/builder.rb +66 -0
  41. data/lib/masamune/data_plan/elem.rb +190 -0
  42. data/lib/masamune/data_plan/engine.rb +162 -0
  43. data/lib/masamune/data_plan/rule.rb +292 -0
  44. data/lib/masamune/data_plan/set.rb +176 -0
  45. data/lib/masamune/environment.rb +164 -0
  46. data/lib/masamune/filesystem.rb +567 -0
  47. data/lib/masamune/has_environment.rb +40 -0
  48. data/lib/masamune/helpers.rb +27 -0
  49. data/lib/masamune/helpers/postgres.rb +84 -0
  50. data/lib/masamune/io.rb +33 -0
  51. data/lib/masamune/last_element.rb +53 -0
  52. data/lib/masamune/method_logger.rb +41 -0
  53. data/lib/masamune/multi_io.rb +39 -0
  54. data/lib/masamune/schema.rb +36 -0
  55. data/lib/masamune/schema/catalog.rb +233 -0
  56. data/lib/masamune/schema/column.rb +527 -0
  57. data/lib/masamune/schema/dimension.rb +133 -0
  58. data/lib/masamune/schema/event.rb +121 -0
  59. data/lib/masamune/schema/fact.rb +133 -0
  60. data/lib/masamune/schema/map.rb +265 -0
  61. data/lib/masamune/schema/row.rb +133 -0
  62. data/lib/masamune/schema/store.rb +115 -0
  63. data/lib/masamune/schema/table.rb +308 -0
  64. data/lib/masamune/schema/table_reference.rb +76 -0
  65. data/lib/masamune/spec_helper.rb +23 -0
  66. data/lib/masamune/string_format.rb +34 -0
  67. data/lib/masamune/tasks/elastic_mapreduce_thor.rb +60 -0
  68. data/lib/masamune/tasks/hive_thor.rb +55 -0
  69. data/lib/masamune/tasks/postgres_thor.rb +47 -0
  70. data/lib/masamune/tasks/shell_thor.rb +63 -0
  71. data/lib/masamune/template.rb +77 -0
  72. data/lib/masamune/thor.rb +186 -0
  73. data/lib/masamune/thor_loader.rb +38 -0
  74. data/lib/masamune/topological_hash.rb +34 -0
  75. data/lib/masamune/transform.rb +47 -0
  76. data/lib/masamune/transform/bulk_upsert.psql.erb +64 -0
  77. data/lib/masamune/transform/bulk_upsert.rb +52 -0
  78. data/lib/masamune/transform/consolidate_dimension.rb +54 -0
  79. data/lib/masamune/transform/deduplicate_dimension.psql.erb +52 -0
  80. data/lib/masamune/transform/deduplicate_dimension.rb +53 -0
  81. data/lib/masamune/transform/define_event_view.hql.erb +51 -0
  82. data/lib/masamune/transform/define_event_view.rb +60 -0
  83. data/lib/masamune/transform/define_index.psql.erb +34 -0
  84. data/lib/masamune/transform/define_schema.hql.erb +23 -0
  85. data/lib/masamune/transform/define_schema.psql.erb +79 -0
  86. data/lib/masamune/transform/define_schema.rb +56 -0
  87. data/lib/masamune/transform/define_table.hql.erb +34 -0
  88. data/lib/masamune/transform/define_table.psql.erb +95 -0
  89. data/lib/masamune/transform/define_table.rb +40 -0
  90. data/lib/masamune/transform/define_unique.psql.erb +30 -0
  91. data/lib/masamune/transform/insert_reference_values.psql.erb +43 -0
  92. data/lib/masamune/transform/insert_reference_values.rb +64 -0
  93. data/lib/masamune/transform/load_dimension.rb +47 -0
  94. data/lib/masamune/transform/load_fact.rb +45 -0
  95. data/lib/masamune/transform/operator.rb +96 -0
  96. data/lib/masamune/transform/relabel_dimension.psql.erb +76 -0
  97. data/lib/masamune/transform/relabel_dimension.rb +39 -0
  98. data/lib/masamune/transform/rollup_fact.psql.erb +79 -0
  99. data/lib/masamune/transform/rollup_fact.rb +149 -0
  100. data/lib/masamune/transform/snapshot_dimension.psql.erb +75 -0
  101. data/lib/masamune/transform/snapshot_dimension.rb +74 -0
  102. data/lib/masamune/transform/stage_dimension.psql.erb +39 -0
  103. data/lib/masamune/transform/stage_dimension.rb +83 -0
  104. data/lib/masamune/transform/stage_fact.psql.erb +80 -0
  105. data/lib/masamune/transform/stage_fact.rb +111 -0
  106. data/lib/masamune/version.rb +25 -0
  107. data/spec/fixtures/aggregate.sql.erb +25 -0
  108. data/spec/fixtures/comment.sql.erb +27 -0
  109. data/spec/fixtures/invalid.sql.erb +23 -0
  110. data/spec/fixtures/relative.sql.erb +23 -0
  111. data/spec/fixtures/simple.sql.erb +28 -0
  112. data/spec/fixtures/whitespace.sql.erb +30 -0
  113. data/spec/masamune/actions/elastic_mapreduce_spec.rb +108 -0
  114. data/spec/masamune/actions/execute_spec.rb +50 -0
  115. data/spec/masamune/actions/hadoop_filesystem_spec.rb +44 -0
  116. data/spec/masamune/actions/hadoop_streaming_spec.rb +74 -0
  117. data/spec/masamune/actions/hive_spec.rb +117 -0
  118. data/spec/masamune/actions/postgres_admin_spec.rb +58 -0
  119. data/spec/masamune/actions/postgres_spec.rb +134 -0
  120. data/spec/masamune/actions/s3cmd_spec.rb +44 -0
  121. data/spec/masamune/actions/transform_spec.rb +144 -0
  122. data/spec/masamune/after_initialization_callbacks_spec.rb +61 -0
  123. data/spec/masamune/cached_filesystem_spec.rb +167 -0
  124. data/spec/masamune/commands/hadoop_filesystem_spec.rb +50 -0
  125. data/spec/masamune/commands/hadoop_streaming_spec.rb +106 -0
  126. data/spec/masamune/commands/hive_spec.rb +117 -0
  127. data/spec/masamune/commands/postgres_admin_spec.rb +69 -0
  128. data/spec/masamune/commands/postgres_spec.rb +100 -0
  129. data/spec/masamune/commands/retry_with_backoff_spec.rb +116 -0
  130. data/spec/masamune/commands/s3cmd_spec.rb +50 -0
  131. data/spec/masamune/commands/shell_spec.rb +101 -0
  132. data/spec/masamune/configuration_spec.rb +102 -0
  133. data/spec/masamune/data_plan/builder_spec.rb +91 -0
  134. data/spec/masamune/data_plan/elem_spec.rb +102 -0
  135. data/spec/masamune/data_plan/engine_spec.rb +356 -0
  136. data/spec/masamune/data_plan/rule_spec.rb +407 -0
  137. data/spec/masamune/data_plan/set_spec.rb +517 -0
  138. data/spec/masamune/environment_spec.rb +65 -0
  139. data/spec/masamune/filesystem_spec.rb +1421 -0
  140. data/spec/masamune/helpers/postgres_spec.rb +95 -0
  141. data/spec/masamune/schema/catalog_spec.rb +613 -0
  142. data/spec/masamune/schema/column_spec.rb +696 -0
  143. data/spec/masamune/schema/dimension_spec.rb +137 -0
  144. data/spec/masamune/schema/event_spec.rb +75 -0
  145. data/spec/masamune/schema/fact_spec.rb +117 -0
  146. data/spec/masamune/schema/map_spec.rb +593 -0
  147. data/spec/masamune/schema/row_spec.rb +28 -0
  148. data/spec/masamune/schema/store_spec.rb +49 -0
  149. data/spec/masamune/schema/table_spec.rb +395 -0
  150. data/spec/masamune/string_format_spec.rb +60 -0
  151. data/spec/masamune/tasks/elastic_mapreduce_thor_spec.rb +57 -0
  152. data/spec/masamune/tasks/hive_thor_spec.rb +75 -0
  153. data/spec/masamune/tasks/postgres_thor_spec.rb +42 -0
  154. data/spec/masamune/tasks/shell_thor_spec.rb +51 -0
  155. data/spec/masamune/template_spec.rb +77 -0
  156. data/spec/masamune/thor_spec.rb +238 -0
  157. data/spec/masamune/transform/bulk_upsert.dimension_spec.rb +200 -0
  158. data/spec/masamune/transform/consolidate_dimension_spec.rb +62 -0
  159. data/spec/masamune/transform/deduplicate_dimension_spec.rb +84 -0
  160. data/spec/masamune/transform/define_event_view_spec.rb +84 -0
  161. data/spec/masamune/transform/define_schema_spec.rb +83 -0
  162. data/spec/masamune/transform/define_table.dimension_spec.rb +306 -0
  163. data/spec/masamune/transform/define_table.fact_spec.rb +291 -0
  164. data/spec/masamune/transform/define_table.table_spec.rb +525 -0
  165. data/spec/masamune/transform/insert_reference_values.dimension_spec.rb +111 -0
  166. data/spec/masamune/transform/insert_reference_values.fact_spec.rb +149 -0
  167. data/spec/masamune/transform/load_dimension_spec.rb +76 -0
  168. data/spec/masamune/transform/load_fact_spec.rb +89 -0
  169. data/spec/masamune/transform/relabel_dimension_spec.rb +102 -0
  170. data/spec/masamune/transform/rollup_fact_spec.rb +333 -0
  171. data/spec/masamune/transform/snapshot_dimension_spec.rb +103 -0
  172. data/spec/masamune/transform/stage_dimension_spec.rb +115 -0
  173. data/spec/masamune/transform/stage_fact_spec.rb +204 -0
  174. data/spec/masamune_spec.rb +32 -0
  175. data/spec/spec_helper.rb +41 -0
  176. data/spec/support/masamune/example_group.rb +36 -0
  177. data/spec/support/masamune/mock_command.rb +99 -0
  178. data/spec/support/masamune/mock_delegate.rb +51 -0
  179. data/spec/support/masamune/mock_filesystem.rb +96 -0
  180. data/spec/support/masamune/thor_mute.rb +35 -0
  181. data/spec/support/rspec/example/action_example_group.rb +34 -0
  182. data/spec/support/rspec/example/task_example_group.rb +80 -0
  183. data/spec/support/rspec/example/transform_example_group.rb +36 -0
  184. data/spec/support/shared_examples/postgres_common_examples.rb +53 -0
  185. metadata +462 -0
@@ -0,0 +1,111 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Transform::InsertReferenceValues do
26
+ before do
27
+ catalog.schema :postgres do
28
+ dimension 'department', type: :mini do
29
+ column 'tenant_id', type: :integer, unique: true, natural_key: true
30
+ column 'department_id', type: :integer, unique: true, natural_key: true
31
+ row tenant_id: -1, department_id: -1, attributes: {default: true}
32
+ end
33
+
34
+ dimension 'user', type: :four do
35
+ references :department, insert: true
36
+ column 'tenant_id', index: true, natural_key: true
37
+ column 'user_id', index: true, natural_key: true
38
+ column 'name', type: :string
39
+ end
40
+
41
+ file 'user' do
42
+ column 'tenant_id', type: :integer
43
+ column 'user_id', type: :integer
44
+ column 'department.department_id', type: :integer
45
+ column 'start_at', type: :timestamp
46
+ column 'source_kind', type: :string
47
+ column 'delta', type: :integer
48
+ end
49
+
50
+ file 'misc' do
51
+ end
52
+ end
53
+ end
54
+
55
+ let(:target) { catalog.postgres.user_dimension.ledger_table }
56
+
57
+ context 'for postgres dimension with file containing references' do
58
+ let(:source) { catalog.postgres.user_file.stage_table(table: target) }
59
+ subject(:result) { transform.insert_reference_values(source, target).to_s }
60
+
61
+ it 'should render insert_reference_values template' do
62
+ is_expected.to eq <<-EOS.strip_heredoc
63
+ CREATE TEMPORARY TABLE IF NOT EXISTS department_type_stage (LIKE department_type INCLUDING ALL);
64
+
65
+ INSERT INTO
66
+ department_type_stage (tenant_id, department_id)
67
+ SELECT DISTINCT
68
+ tenant_id,
69
+ department_type_department_id
70
+ FROM
71
+ user_dimension_ledger_stage
72
+ WHERE
73
+ tenant_id IS NOT NULL AND
74
+ department_type_department_id IS NOT NULL
75
+ ;
76
+
77
+ BEGIN;
78
+ LOCK TABLE department_type IN EXCLUSIVE MODE;
79
+
80
+ INSERT INTO
81
+ department_type (tenant_id, department_id)
82
+ SELECT
83
+ department_type_stage.tenant_id,
84
+ department_type_stage.department_id
85
+ FROM
86
+ department_type_stage
87
+ LEFT OUTER JOIN
88
+ department_type
89
+ ON
90
+ department_type.tenant_id = department_type_stage.tenant_id AND
91
+ department_type.department_id = department_type_stage.department_id
92
+ WHERE
93
+ department_type.tenant_id IS NULL AND
94
+ department_type.department_id IS NULL
95
+ ;
96
+
97
+ COMMIT;
98
+ EOS
99
+ end
100
+ end
101
+
102
+ context 'for postgres dimension with file missing references' do
103
+ let(:source) { catalog.postgres.misc_file.stage_table(table: target) }
104
+
105
+ subject(:result) { transform.insert_reference_values(source, target).to_s }
106
+
107
+ it 'should not render insert_reference_values template' do
108
+ is_expected.to eq("\n")
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,149 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Transform::InsertReferenceValues do
26
+ before do
27
+ catalog.schema :postgres do
28
+ dimension 'date', type: :one do
29
+ column 'date_id', type: :integer, unique: true, index: true, natural_key: true
30
+ end
31
+
32
+ dimension 'user_agent', type: :mini do
33
+ column 'name', type: :string, unique: true, index: 'shared'
34
+ column 'version', type: :string, unique: true, index: 'shared', default: 'Unknown'
35
+ column 'description', type: :string, null: true, ignore: true
36
+ end
37
+
38
+ dimension 'feature', type: :mini do
39
+ column 'name', type: :string, unique: true, index: true
40
+ end
41
+
42
+ dimension 'tenant', type: :two do
43
+ column 'tenant_id', type: :integer, index: true, natural_key: true
44
+ end
45
+
46
+ dimension 'user', type: :two do
47
+ column 'tenant_id', type: :integer, index: true, natural_key: true
48
+ column 'user_id', type: :integer, index: true, natural_key: true
49
+ end
50
+
51
+ fact 'visits', partition: 'y%Ym%m' do
52
+ references :date
53
+ references :tenant
54
+ references :user
55
+ references :user_agent, insert: true
56
+ references :feature, insert: true
57
+ measure 'total', type: :integer
58
+ end
59
+
60
+ file 'visits' do
61
+ column 'date.date_id', type: :integer
62
+ column 'tenant.tenant_id', type: :integer
63
+ column 'user.user_id', type: :integer
64
+ column 'user_agent.name', type: :string
65
+ column 'user_agent.version', type: :string
66
+ column 'feature.name', type: :string
67
+ column 'time_key', type: :integer
68
+ column 'total', type: :integer
69
+ end
70
+ end
71
+ end
72
+
73
+ let(:target) { catalog.postgres.visits_fact }
74
+ let(:source) { catalog.postgres.visits_file.stage_table(table: target) }
75
+
76
+ context 'with postgres fact' do
77
+ subject(:result) { transform.insert_reference_values(source, target).to_s }
78
+
79
+ it 'should eq render insert_reference_values template' do
80
+ is_expected.to eq <<-EOS.strip_heredoc
81
+ CREATE TEMPORARY TABLE IF NOT EXISTS user_agent_type_stage (LIKE user_agent_type INCLUDING ALL);
82
+
83
+ INSERT INTO
84
+ user_agent_type_stage (name, version)
85
+ SELECT DISTINCT
86
+ user_agent_type_name,
87
+ COALESCE(user_agent_type_version, 'Unknown')
88
+ FROM
89
+ visits_fact_stage
90
+ WHERE
91
+ user_agent_type_name IS NOT NULL
92
+ ;
93
+
94
+ BEGIN;
95
+ LOCK TABLE user_agent_type IN EXCLUSIVE MODE;
96
+
97
+ INSERT INTO
98
+ user_agent_type (name, version)
99
+ SELECT
100
+ user_agent_type_stage.name,
101
+ user_agent_type_stage.version
102
+ FROM
103
+ user_agent_type_stage
104
+ LEFT OUTER JOIN
105
+ user_agent_type
106
+ ON
107
+ user_agent_type.name = user_agent_type_stage.name AND
108
+ user_agent_type.version = user_agent_type_stage.version
109
+ WHERE
110
+ user_agent_type.name IS NULL AND
111
+ user_agent_type.version IS NULL
112
+ ;
113
+
114
+ COMMIT;
115
+
116
+ CREATE TEMPORARY TABLE IF NOT EXISTS feature_type_stage (LIKE feature_type INCLUDING ALL);
117
+
118
+ INSERT INTO
119
+ feature_type_stage (name)
120
+ SELECT DISTINCT
121
+ feature_type_name
122
+ FROM
123
+ visits_fact_stage
124
+ WHERE
125
+ feature_type_name IS NOT NULL
126
+ ;
127
+
128
+ BEGIN;
129
+ LOCK TABLE feature_type IN EXCLUSIVE MODE;
130
+
131
+ INSERT INTO
132
+ feature_type (name)
133
+ SELECT
134
+ feature_type_stage.name
135
+ FROM
136
+ feature_type_stage
137
+ LEFT OUTER JOIN
138
+ feature_type
139
+ ON
140
+ feature_type.name = feature_type_stage.name
141
+ WHERE
142
+ feature_type.name IS NULL
143
+ ;
144
+
145
+ COMMIT;
146
+ EOS
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,76 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Transform::LoadDimension do
26
+ before do
27
+ catalog.schema :postgres do
28
+ dimension 'user_account_state', type: :mini do
29
+ column 'name', type: :string, unique: true
30
+ column 'description', type: :string
31
+ end
32
+
33
+ dimension 'department', type: :mini do
34
+ column 'tenant_id', type: :integer, unique: true, natural_key: true
35
+ column 'department_id', type: :integer, unique: true, natural_key: true
36
+ row tenant_id: -1, department_id: -1, attributes: {default: true}
37
+ end
38
+
39
+ dimension 'user', type: :four do
40
+ references :department, insert: true
41
+ references :user_account_state
42
+ column 'tenant_id', index: true, natural_key: true
43
+ column 'user_id', index: true, natural_key: true
44
+ column 'preferences', type: :key_value, null: true
45
+ end
46
+
47
+ file 'user' do
48
+ column 'tenant_id', type: :integer
49
+ column 'user_id', type: :integer
50
+ column 'user_account_state.name', type: :string
51
+ column 'preferences_now', type: :json
52
+ column 'start_at', type: :timestamp
53
+ column 'source_kind', type: :string
54
+ column 'delta', type: :integer
55
+ end
56
+ end
57
+ end
58
+
59
+ let(:data) { double(path: 'output.csv') }
60
+ let(:target) { catalog.postgres.user_dimension }
61
+ let(:source) { catalog.postgres.user_file }
62
+ let(:target_ledger) { target.ledger_table }
63
+ let(:source_table) { source.stage_table(suffix: 'file', table: target_ledger, inherit: false) }
64
+
65
+ context 'with postgres dimension' do
66
+ subject(:result) { transform.load_dimension(data, source, target).to_s }
67
+
68
+ it 'should render combined template' do
69
+ is_expected.to eq Masamune::Template.combine \
70
+ transform.define_table(source_table, data),
71
+ transform.insert_reference_values(source_table, target_ledger),
72
+ transform.stage_dimension(source_table, target_ledger),
73
+ transform.bulk_upsert(target_ledger.stage_table, target_ledger)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,89 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Transform::LoadFact do
26
+ before do
27
+ catalog.schema :postgres do
28
+ dimension 'date', type: :date do
29
+ column 'date_id', type: :integer, unique: true, index: true, natural_key: true
30
+ end
31
+
32
+ dimension 'user_agent', type: :mini do
33
+ column 'name', type: :string, unique: true, index: 'shared'
34
+ column 'version', type: :string, unique: true, index: 'shared', default: 'Unknown'
35
+ column 'description', type: :string, null: true, ignore: true
36
+ end
37
+
38
+ dimension 'feature', type: :mini do
39
+ column 'name', type: :string, unique: true, index: true
40
+ end
41
+
42
+ dimension 'tenant', type: :two do
43
+ column 'tenant_id', type: :integer, index: true, natural_key: true
44
+ end
45
+
46
+ dimension 'user', type: :two do
47
+ column 'tenant_id', type: :integer, index: true, natural_key: true
48
+ column 'user_id', type: :integer, index: true, natural_key: true
49
+ end
50
+
51
+ fact 'visits', partition: 'y%Ym%m' do
52
+ references :date
53
+ references :tenant
54
+ references :user
55
+ references :user_agent, insert: true
56
+ references :feature, insert: true
57
+ measure 'total', type: :integer
58
+ end
59
+
60
+ file 'visits' do
61
+ column 'date.date_id', type: :integer
62
+ column 'tenant.tenant_id', type: :integer
63
+ column 'user.user_id', type: :integer
64
+ column 'user_agent.name', type: :string
65
+ column 'user_agent.version', type: :string
66
+ column 'feature.name', type: :string
67
+ column 'time_key', type: :integer
68
+ column 'total', type: :integer
69
+ end
70
+ end
71
+ end
72
+
73
+ let(:files) { (1..3).map { |i| double(path: "output_#{i}.csv") } }
74
+ let(:date) { DateTime.civil(2014,8) }
75
+ let(:target) { catalog.postgres.visits_fact }
76
+ let(:source) { catalog.postgres.visits_file }
77
+ let(:source_table) { source.stage_table(suffix: 'file', table: target, inherit: false) }
78
+
79
+ context 'for postgres fact' do
80
+ subject(:result) { transform.load_fact(files, source, target, date).to_s }
81
+
82
+ it 'should render combined template' do
83
+ is_expected.to eq Masamune::Template.combine \
84
+ transform.define_table(source_table, files),
85
+ transform.insert_reference_values(source_table, target),
86
+ transform.stage_fact(source_table, target, date)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,102 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Transform::RelabelDimension do
26
+ before do
27
+ catalog.schema :postgres do
28
+ dimension 'user_account_state', type: :mini do
29
+ column 'name', type: :string, unique: true
30
+ column 'description', type: :string
31
+ end
32
+
33
+ dimension 'user', type: :four do
34
+ references :user_account_state
35
+ column 'tenant_id', index: true, natural_key: true
36
+ column 'user_id', index: true, natural_key: true
37
+ column 'preferences', type: :key_value, null: true
38
+ end
39
+ end
40
+ end
41
+
42
+ let(:target) { catalog.postgres.user_dimension }
43
+
44
+ context 'for postgres dimension' do
45
+ subject(:result) { transform.relabel_dimension(target).to_s }
46
+
47
+ it 'should eq render relabel_dimension template' do
48
+ is_expected.to eq <<-EOS.strip_heredoc
49
+ BEGIN;
50
+ LOCK TABLE user_dimension IN EXCLUSIVE MODE;
51
+
52
+ UPDATE user_dimension SET version = NULL;
53
+
54
+ UPDATE
55
+ user_dimension
56
+ SET
57
+ version = tmp.version
58
+ FROM
59
+ (
60
+ SELECT
61
+ id,
62
+ tenant_id,
63
+ user_id,
64
+ start_at,
65
+ rank() OVER (PARTITION BY tenant_id, user_id ORDER BY start_at) AS version
66
+ FROM
67
+ user_dimension
68
+ GROUP BY
69
+ id, tenant_id, user_id, start_at
70
+ ) AS tmp
71
+ WHERE
72
+ user_dimension.id = tmp.id
73
+ ;
74
+
75
+ UPDATE user_dimension SET end_at = NULL;
76
+
77
+ UPDATE
78
+ user_dimension
79
+ SET
80
+ end_at = tmp.end_at
81
+ FROM
82
+ (
83
+ SELECT
84
+ id,
85
+ start_at,
86
+ tenant_id,
87
+ user_id,
88
+ LEAD(start_at, 1) OVER (PARTITION BY tenant_id, user_id ORDER BY start_at) AS end_at
89
+ FROM
90
+ user_dimension
91
+ GROUP BY
92
+ id, tenant_id, user_id, start_at
93
+ ) AS tmp
94
+ WHERE
95
+ user_dimension.id = tmp.id
96
+ ;
97
+
98
+ COMMIT;
99
+ EOS
100
+ end
101
+ end
102
+ end