pipeforge 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE +21 -0
- data/README.md +168 -0
- data/guides/matching-columns.md +126 -0
- data/guides/measuring-values.md +121 -0
- data/guides/running-the-migration.md +145 -0
- data/lib/pipeforge/adapter_registry.rb +37 -0
- data/lib/pipeforge/adapters/activerecord/adapter.rb +7 -0
- data/lib/pipeforge/adapters/activerecord/model_record.rb +202 -0
- data/lib/pipeforge/adapters/activerecord/schema_reader.rb +142 -0
- data/lib/pipeforge/adapters/activerecord/target.rb +277 -0
- data/lib/pipeforge/cast_result.rb +13 -0
- data/lib/pipeforge/column_limits_checker.rb +152 -0
- data/lib/pipeforge/column_value_analyzer/column_summary_builder.rb +163 -0
- data/lib/pipeforge/column_value_analyzer/distinct_values.rb +61 -0
- data/lib/pipeforge/column_value_analyzer/listed_values.rb +26 -0
- data/lib/pipeforge/column_value_analyzer.rb +96 -0
- data/lib/pipeforge/configuration.rb +209 -0
- data/lib/pipeforge/error_catalog.rb +184 -0
- data/lib/pipeforge/errors.rb +229 -0
- data/lib/pipeforge/header_analyzer/matcher.rb +170 -0
- data/lib/pipeforge/header_analyzer/pair_scorer.rb +116 -0
- data/lib/pipeforge/header_analyzer/tokenizer.rb +43 -0
- data/lib/pipeforge/header_analyzer.rb +129 -0
- data/lib/pipeforge/mapped_columns.rb +208 -0
- data/lib/pipeforge/migration.rb +189 -0
- data/lib/pipeforge/resolved_mapped_column.rb +13 -0
- data/lib/pipeforge/run_processor/chunk_processor.rb +141 -0
- data/lib/pipeforge/run_processor/row_transformer.rb +35 -0
- data/lib/pipeforge/run_processor/schema_checker.rb +67 -0
- data/lib/pipeforge/run_processor.rb +99 -0
- data/lib/pipeforge/source/csv_reader.rb +212 -0
- data/lib/pipeforge/source/reusable_io.rb +71 -0
- data/lib/pipeforge/target_schema.rb +47 -0
- data/lib/pipeforge/utilities.rb +58 -0
- data/lib/pipeforge/version.rb +5 -0
- data/lib/pipeforge.rb +22 -0
- metadata +97 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pipeforge
|
|
4
|
+
# Whether a value is within the limits of one target column: the size of an integer, the digits of a
|
|
5
|
+
# decimal, the range of a float, the length of a string and the labels of an enum. The value is the one
|
|
6
|
+
# the adapter cast (an Integer, a Float or a decimal for a number).
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# limits = ColumnLimitsChecker.new(target_column_name: "age", type: "integer", limit: 2)
|
|
10
|
+
# limits.violation_of(value: 40_000) # => ErrorCatalog::ERRORS[:value_out_of_range_bytes].call(target_column_name: "age", bytes: 2)
|
|
11
|
+
# limits.violation_of(value: 100) # => nil
|
|
12
|
+
class ColumnLimitsChecker
|
|
13
|
+
# The types that have a limit, each with the check that applies to its values. A value of any other
|
|
14
|
+
# type (a boolean, a date, a uuid, json, and so on) has no limit.
|
|
15
|
+
CHECKS = {
|
|
16
|
+
"string" => :length_violation,
|
|
17
|
+
"text" => :length_violation,
|
|
18
|
+
"integer" => :integer_violation,
|
|
19
|
+
"decimal" => :decimal_violation,
|
|
20
|
+
"float" => :float_violation
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# The size in bytes of an integer column that has no limit.
|
|
24
|
+
DEFAULT_INTEGER_LIMIT = 4
|
|
25
|
+
|
|
26
|
+
# Builds the checker of a target column from its type and limits.
|
|
27
|
+
#
|
|
28
|
+
# @param target_column [TargetColumn]
|
|
29
|
+
# @return [ColumnLimitsChecker]
|
|
30
|
+
def self.for(target_column:)
|
|
31
|
+
new(
|
|
32
|
+
target_column_name: target_column.name,
|
|
33
|
+
type: target_column.type,
|
|
34
|
+
limit: target_column.limit,
|
|
35
|
+
is_signed: target_column.is_signed,
|
|
36
|
+
max_length: target_column.max_length,
|
|
37
|
+
precision: target_column.precision,
|
|
38
|
+
scale: target_column.scale,
|
|
39
|
+
enum_labels: target_column.enum_labels
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @param target_column_name [String] the column the checks are made for, which the message of a violation names
|
|
44
|
+
# @param type [String, Symbol, nil] the type of the target column; a type with no limit, or nil, has none
|
|
45
|
+
# @param limit [Integer, nil] integer: the size in bytes, 4 when not given; float: the significand bits, 24 or
|
|
46
|
+
# less for single precision and anything else for double
|
|
47
|
+
# @param is_signed [Boolean, nil] false for an integer column that holds no negative number; true, or nil when
|
|
48
|
+
# not given, for one that does
|
|
49
|
+
# @param max_length [Integer, nil] the length of a string or text column
|
|
50
|
+
# @param precision [Integer, nil] the digits of a decimal column
|
|
51
|
+
# @param scale [Integer, nil] the digits of a decimal column after the point; 0 when not given
|
|
52
|
+
# @param enum_labels [Array<String>, nil] the labels of an enum column, which accepts these and nothing else
|
|
53
|
+
def initialize(
|
|
54
|
+
target_column_name:, type:, limit: nil, is_signed: nil, max_length: nil, precision: nil, scale: nil,
|
|
55
|
+
enum_labels: nil
|
|
56
|
+
)
|
|
57
|
+
@target_column_name = target_column_name
|
|
58
|
+
@type = type.to_s
|
|
59
|
+
@enum_labels = enum_labels
|
|
60
|
+
@limit = limit
|
|
61
|
+
@is_signed = is_signed
|
|
62
|
+
@max_length = max_length
|
|
63
|
+
@precision = precision
|
|
64
|
+
# A decimal declared with no scale keeps no digits after the point (DECIMAL(10) is DECIMAL(10, 0)), and the
|
|
65
|
+
# checks below do arithmetic on it.
|
|
66
|
+
@scale = scale || 0
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @param value [Object] the value the adapter cast for the column
|
|
70
|
+
# @return [ErrorCatalog::ColumnError, nil] the error that the {ErrorCatalog} gives for the mistake, with +origin: :schema+
|
|
71
|
+
# and the value in JSON types; nil when the value is within the limits, or when the type has no limit
|
|
72
|
+
def violation_of(value:)
|
|
73
|
+
return enum_violation(value: value) unless @enum_labels.nil?
|
|
74
|
+
|
|
75
|
+
check = CHECKS[@type]
|
|
76
|
+
return nil if check.nil?
|
|
77
|
+
|
|
78
|
+
send(check, value: value)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
# An enum column accepts its labels. A value the database stores for a label is not a label.
|
|
84
|
+
def enum_violation(value:)
|
|
85
|
+
return nil if @enum_labels.include?(value.to_s)
|
|
86
|
+
|
|
87
|
+
ErrorCatalog::ERRORS.fetch(:value_not_allowed).call(
|
|
88
|
+
origin: :schema, target_column_name: @target_column_name, value: value.as_json, labels: @enum_labels
|
|
89
|
+
)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Only the length is limited, and only when the column has a limit; it is counted in characters.
|
|
93
|
+
def length_violation(value:)
|
|
94
|
+
length = value.to_s.length
|
|
95
|
+
return nil if @max_length.nil? || length <= @max_length
|
|
96
|
+
|
|
97
|
+
ErrorCatalog::ERRORS.fetch(:value_too_long).call(
|
|
98
|
+
origin: :schema, target_column_name: @target_column_name, value: value.as_json, length: length, max_length: @max_length
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def integer_violation(value:)
|
|
103
|
+
bytes = @limit || DEFAULT_INTEGER_LIMIT
|
|
104
|
+
return nil if value.between?(*integer_range(bytes: bytes))
|
|
105
|
+
|
|
106
|
+
ErrorCatalog::ERRORS.fetch(:value_out_of_range_bytes).call(
|
|
107
|
+
origin: :schema, target_column_name: @target_column_name, value: value.as_json, bytes: bytes
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# The smallest and the largest whole number of a column of that many bytes: an unsigned column
|
|
112
|
+
# starts at 0 and reaches twice as high.
|
|
113
|
+
def integer_range(bytes:)
|
|
114
|
+
bits = 8 * bytes
|
|
115
|
+
# nil (not given) is a signed column; only false means unsigned.
|
|
116
|
+
return [0, 2**bits - 1] if @is_signed == false
|
|
117
|
+
|
|
118
|
+
[-(2**(bits - 1)), 2**(bits - 1) - 1]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# The digits before the point, once the number is rounded to the scale, must fit in precision - scale.
|
|
122
|
+
# A number that is not finite is left alone.
|
|
123
|
+
def decimal_violation(value:)
|
|
124
|
+
return nil if @precision.nil? || !value.finite?
|
|
125
|
+
|
|
126
|
+
allowed_digits = @precision - @scale
|
|
127
|
+
return nil if digits_before_point(number: value.to_r) <= allowed_digits
|
|
128
|
+
|
|
129
|
+
ErrorCatalog::ERRORS.fetch(:value_out_of_range_digits).call(
|
|
130
|
+
origin: :schema, target_column_name: @target_column_name, value: value.as_json, allowed_digits: allowed_digits
|
|
131
|
+
)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def digits_before_point(number:)
|
|
135
|
+
# Rounded first because the database stores the rounded number, which can gain a digit before the
|
|
136
|
+
# point: 999.999 in a column of scale 2 is stored as 1000.00.
|
|
137
|
+
number.round(@scale).truncate.abs.to_s.sub(/\A0+/, "").length
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A float past the largest the column holds, or infinite. NaN is left alone.
|
|
141
|
+
def float_violation(value:)
|
|
142
|
+
number = value.to_f
|
|
143
|
+
# 1e9999 is cast to Infinity, which is out of range; NaN is not.
|
|
144
|
+
return nil if number.nan?
|
|
145
|
+
return nil if number.finite? && number.abs <= Utilities.largest_float(limit: @limit)
|
|
146
|
+
|
|
147
|
+
ErrorCatalog::ERRORS.fetch(:value_out_of_range_float).call(
|
|
148
|
+
origin: :schema, target_column_name: @target_column_name, value: value.as_json
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "distinct_values"
|
|
4
|
+
require_relative "listed_values"
|
|
5
|
+
|
|
6
|
+
module Pipeforge
|
|
7
|
+
module ColumnValueAnalyzer
|
|
8
|
+
# The distinct texts of one column, split by whether the column accepts them, with how many values each list
|
|
9
|
+
# stands for. Each list holds every different text, most frequent first, and the texts with the same count in
|
|
10
|
+
# the order they first appeared.
|
|
11
|
+
# compatible Array<CompatibleValue> the texts the column accepts
|
|
12
|
+
# incompatible Array<IncompatibleValue> the texts the column does not accept: +reason+ says why,
|
|
13
|
+
# either +:value_not_castable+ (the adapter could not cast
|
|
14
|
+
# the text) or the code of the limit the cast value breaks
|
|
15
|
+
# (+:value_too_long+, +:value_out_of_range+,
|
|
16
|
+
# +:value_not_allowed+)
|
|
17
|
+
# compatible_count Integer how many values the column accepts: the sum of the counts
|
|
18
|
+
# of +compatible+
|
|
19
|
+
# incompatible_count Integer how many values the column does not accept: the sum of the
|
|
20
|
+
# counts of +incompatible+
|
|
21
|
+
DistinctValueLists = Data.define(:compatible, :incompatible, :compatible_count, :incompatible_count)
|
|
22
|
+
|
|
23
|
+
# What the scan found out about one mapped target column. A Result holds it under the name of the
|
|
24
|
+
# target column.
|
|
25
|
+
# source_column_name String the source column it is read from
|
|
26
|
+
# position Integer the position of the source column in the file, starting at 1
|
|
27
|
+
# empty Integer how many cells are empty
|
|
28
|
+
# distinct_values DistinctValueLists the texts that fit and the texts that do not, with the number of
|
|
29
|
+
# values in each; the non-empty values are the two counts added up
|
|
30
|
+
# value_mapping Hash the consumer's mapping of source values to the values to load; the
|
|
31
|
+
# counts and the lists above describe the values after applying it,
|
|
32
|
+
# that is, what would be loaded
|
|
33
|
+
ColumnSummary = Data.define(
|
|
34
|
+
:source_column_name, :position, :empty, :distinct_values, :value_mapping
|
|
35
|
+
) do
|
|
36
|
+
# Writes the summary in plain JSON types, to store it.
|
|
37
|
+
#
|
|
38
|
+
# @return [Hash] plain JSON types, with each list as an array of hashes with the fields of its listed values
|
|
39
|
+
def to_h
|
|
40
|
+
{
|
|
41
|
+
source_column_name: source_column_name,
|
|
42
|
+
position: position,
|
|
43
|
+
empty: empty,
|
|
44
|
+
distinct_values: {
|
|
45
|
+
compatible: distinct_values.compatible.map(&:to_h),
|
|
46
|
+
incompatible: distinct_values.incompatible.map(&:to_h),
|
|
47
|
+
compatible_count: distinct_values.compatible_count,
|
|
48
|
+
incompatible_count: distinct_values.incompatible_count
|
|
49
|
+
},
|
|
50
|
+
value_mapping: value_mapping
|
|
51
|
+
}
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The running measurements of one mapped target column while the file is read, one row at a time: how
|
|
56
|
+
# many cells are empty and the distinct values that do and do not fit, each with its number of values. Every
|
|
57
|
+
# distinct value is kept. It is mutable: {#add_cell} is the only method that changes it, and {#summary}
|
|
58
|
+
# gives the finished {ColumnSummary}.
|
|
59
|
+
#
|
|
60
|
+
# A value with an entry in the value mapping of the column is measured as the value it maps to: that is what
|
|
61
|
+
# would be loaded. It is listed under its text from the file, with the value it maps to next to it. A value that
|
|
62
|
+
# maps to nil or to a blank counts as empty and is not listed.
|
|
63
|
+
class ColumnSummaryBuilder
|
|
64
|
+
# @param target_column_name [String] the target column the values are measured for
|
|
65
|
+
# @param source_column_name [String] the source column the values are read from
|
|
66
|
+
# @param position [Integer] the position of the source column in the file, starting at 1
|
|
67
|
+
# @param limits_checker [ColumnLimitsChecker] the checker of the target column
|
|
68
|
+
# @param target [#cast_value] the target of the target model
|
|
69
|
+
# @param value_mapping [Hash{String => Object}] the consumer's mapping of source values to the values to
|
|
70
|
+
# load; an empty Hash for no mapping
|
|
71
|
+
def initialize(target_column_name:, source_column_name:, position:, limits_checker:, target:, value_mapping:)
|
|
72
|
+
@target_column_name = target_column_name
|
|
73
|
+
@source_column_name = source_column_name
|
|
74
|
+
@position = position
|
|
75
|
+
@limits_checker = limits_checker
|
|
76
|
+
@value_mapping = value_mapping
|
|
77
|
+
@target = target
|
|
78
|
+
@empty = 0
|
|
79
|
+
@compatible_values = DistinctValues.new(listed_value_class: CompatibleValue)
|
|
80
|
+
@incompatible_values = DistinctValues.new(listed_value_class: IncompatibleValue)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @return [String] the source column the cells are read from
|
|
84
|
+
attr_reader :source_column_name
|
|
85
|
+
|
|
86
|
+
# Records the cell of one row.
|
|
87
|
+
#
|
|
88
|
+
# @param cell [String, nil] the value of the source column in one row; nil for an empty cell
|
|
89
|
+
# @return [void]
|
|
90
|
+
def add_cell(cell:)
|
|
91
|
+
if cell.nil?
|
|
92
|
+
@empty += 1
|
|
93
|
+
return
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
mapped_value = @value_mapping.fetch(cell, cell)
|
|
97
|
+
|
|
98
|
+
if Utilities.blank?(value: mapped_value)
|
|
99
|
+
@empty += 1
|
|
100
|
+
return
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
record_value(value: cell, mapped_value: mapped_value)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @return [ColumnSummary] what the summary_builder holds now
|
|
107
|
+
def summary
|
|
108
|
+
ColumnSummary.new(
|
|
109
|
+
source_column_name: @source_column_name,
|
|
110
|
+
position: @position,
|
|
111
|
+
empty: @empty,
|
|
112
|
+
distinct_values: DistinctValueLists.new(
|
|
113
|
+
compatible: @compatible_values.values,
|
|
114
|
+
incompatible: @incompatible_values.values,
|
|
115
|
+
compatible_count: @compatible_values.total_count,
|
|
116
|
+
incompatible_count: @incompatible_values.total_count
|
|
117
|
+
),
|
|
118
|
+
value_mapping: @value_mapping
|
|
119
|
+
)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
# A value that is already listed is only counted, because the list it is in says whether it fits. Only a
|
|
125
|
+
# new value is cast and checked.
|
|
126
|
+
def record_value(value:, mapped_value:)
|
|
127
|
+
if @compatible_values.listed?(value: value)
|
|
128
|
+
@compatible_values.record(value: value)
|
|
129
|
+
elsif @incompatible_values.listed?(value: value)
|
|
130
|
+
@incompatible_values.record(value: value)
|
|
131
|
+
else
|
|
132
|
+
record_new_value(value: value, mapped_value: mapped_value)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def record_new_value(value:, mapped_value:)
|
|
137
|
+
cast_result = @target.cast_value(column_name: @target_column_name, value: mapped_value)
|
|
138
|
+
mapping_applied = @value_mapping.key?(value)
|
|
139
|
+
|
|
140
|
+
unless cast_result.castable
|
|
141
|
+
@incompatible_values.record(
|
|
142
|
+
value: value, mapping_applied: mapping_applied, mapped_value: mapped_value, casted_to: nil,
|
|
143
|
+
reason: :value_not_castable
|
|
144
|
+
)
|
|
145
|
+
return
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
violation = @limits_checker.violation_of(value: cast_result.cast_value)
|
|
149
|
+
|
|
150
|
+
if violation.nil?
|
|
151
|
+
@compatible_values.record(
|
|
152
|
+
value: value, mapping_applied: mapping_applied, mapped_value: mapped_value, casted_to: cast_result.json_value
|
|
153
|
+
)
|
|
154
|
+
else
|
|
155
|
+
@incompatible_values.record(
|
|
156
|
+
value: value, mapping_applied: mapping_applied, mapped_value: mapped_value,
|
|
157
|
+
casted_to: cast_result.json_value, reason: violation.code
|
|
158
|
+
)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "listed_values"
|
|
4
|
+
|
|
5
|
+
module Pipeforge
|
|
6
|
+
module ColumnValueAnalyzer
|
|
7
|
+
# The different values of one list, with how often each occurs. Every value is kept. Mutable:
|
|
8
|
+
# {#record} is the only method that changes it.
|
|
9
|
+
class DistinctValues
|
|
10
|
+
# @param listed_value_class [Class] {CompatibleValue} or {IncompatibleValue}: the shape of every value the
|
|
11
|
+
# list gives
|
|
12
|
+
def initialize(listed_value_class:)
|
|
13
|
+
@listed_value_class = listed_value_class
|
|
14
|
+
@listed_values = {}
|
|
15
|
+
@total_count = 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @param value [String]
|
|
19
|
+
# @return [Boolean] whether the value is listed
|
|
20
|
+
def listed?(value:)
|
|
21
|
+
@listed_values.key?(value)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Counts one occurrence of a value, listing it first when it is new.
|
|
25
|
+
#
|
|
26
|
+
# @param value [String]
|
|
27
|
+
# @param listed_value_fields [Hash] the other fields of the listed value class (+mapping_applied:+,
|
|
28
|
+
# +mapped_value:+, +casted_to:+, and +reason:+ for an {IncompatibleValue}); only the ones given with the
|
|
29
|
+
# first occurrence are kept
|
|
30
|
+
# @return [void]
|
|
31
|
+
# @raise [ArgumentError] when a new value is recorded without a field the listed value class needs
|
|
32
|
+
def record(value:, **listed_value_fields)
|
|
33
|
+
listed_value = @listed_values[value]
|
|
34
|
+
|
|
35
|
+
if listed_value.nil?
|
|
36
|
+
# They depend only on the value, so the ones given the first time are kept.
|
|
37
|
+
@listed_values[value] = @listed_value_class.new(value: value, count: 1, **listed_value_fields)
|
|
38
|
+
@total_count += 1
|
|
39
|
+
return
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# A listed value cannot change, so the counted copy takes its place.
|
|
43
|
+
@listed_values[value] = listed_value.with(count: listed_value.count + 1)
|
|
44
|
+
@total_count += 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @return [Integer] how many times values were recorded: the sum of the counts of the listed values
|
|
48
|
+
attr_reader :total_count
|
|
49
|
+
|
|
50
|
+
# The listed values, most frequent first. Values with the same count keep the order they
|
|
51
|
+
# first appeared in, which keeps the result the same on every run.
|
|
52
|
+
#
|
|
53
|
+
# @return [Array<CompatibleValue, IncompatibleValue>] one value of the list's listed value class for each
|
|
54
|
+
# different value
|
|
55
|
+
def values
|
|
56
|
+
# The position breaks ties: sort_by does not keep equal items in the order they were listed.
|
|
57
|
+
@listed_values.values.sort_by.with_index { |listed_value, appearance| [-listed_value.count, appearance] }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pipeforge
|
|
4
|
+
module ColumnValueAnalyzer
|
|
5
|
+
# A distinct text of the file that the column accepts, with how many times it appears.
|
|
6
|
+
# value the text as the file has it
|
|
7
|
+
# count how many times the text appears
|
|
8
|
+
# mapping_applied whether the value mapping has an entry for the text, and it was applied
|
|
9
|
+
# mapped_value what is loaded for the text: what the value mapping gives, in its own shape, or the text
|
|
10
|
+
# itself when the mapping has no entry for it
|
|
11
|
+
# casted_to the mapped value read as the column's type, in plain JSON types; nil for the JSON null
|
|
12
|
+
CompatibleValue = Data.define(:value, :count, :mapping_applied, :mapped_value, :casted_to)
|
|
13
|
+
|
|
14
|
+
# A distinct text of the file that the column does not accept, with how many times it appears.
|
|
15
|
+
# value the text as the file has it
|
|
16
|
+
# count how many times the text appears
|
|
17
|
+
# mapping_applied whether the value mapping has an entry for the text, and it was applied
|
|
18
|
+
# mapped_value what is loaded for the text: what the value mapping gives, in its own shape, or the text
|
|
19
|
+
# itself when the mapping has no entry for it
|
|
20
|
+
# casted_to the mapped value read as the column's type, in plain JSON types; nil when the type cannot
|
|
21
|
+
# read it
|
|
22
|
+
# reason why the column does not accept it: +:value_not_castable+, +:value_too_long+,
|
|
23
|
+
# +:value_out_of_range+ or +:value_not_allowed+
|
|
24
|
+
IncompatibleValue = Data.define(:value, :count, :mapping_applied, :mapped_value, :casted_to, :reason)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "column_limits_checker"
|
|
4
|
+
require_relative "column_value_analyzer/column_summary_builder"
|
|
5
|
+
require_relative "source/csv_reader"
|
|
6
|
+
|
|
7
|
+
module Pipeforge
|
|
8
|
+
# Measures the mapped columns of a CSV source: for each one, how many values are empty, how
|
|
9
|
+
# many fit the type of its target column, and which distinct values do and do not fit.
|
|
10
|
+
#
|
|
11
|
+
# It is a service: {.call} is given everything it needs and does the whole job, once. It
|
|
12
|
+
# keeps nothing between calls, so any number of engines can use it at the same time.
|
|
13
|
+
#
|
|
14
|
+
# The file is read once, one row at a time, and each value is added to the summary builder of
|
|
15
|
+
# its column. Every distinct value of every mapped column is kept, because the result is
|
|
16
|
+
# complete only after the last row, so memory grows with the number of distinct values. The
|
|
17
|
+
# analyzer reads source values only and never touches the target.
|
|
18
|
+
#
|
|
19
|
+
# The {Migration} is the way in: {Migration#analyze_column_values} checks the mapped columns, makes a
|
|
20
|
+
# {ResolvedMappedColumn} of each one and then calls this service.
|
|
21
|
+
#
|
|
22
|
+
# @api private
|
|
23
|
+
#
|
|
24
|
+
# @example through a migration
|
|
25
|
+
# result = migration.analyze_column_values(mapped_columns: {email: {source_column_name: "Mail"}})
|
|
26
|
+
# result.column_summaries["email"].distinct_values.compatible_count # => 1170
|
|
27
|
+
module ColumnValueAnalyzer
|
|
28
|
+
# The outcome of one analysis.
|
|
29
|
+
# row_count data rows read; blank and all-empty rows are not counted
|
|
30
|
+
# column_summaries a ColumnSummary per mapped target column, keyed by the target column
|
|
31
|
+
# and in the order of the mapped columns
|
|
32
|
+
Result = Data.define(:row_count, :column_summaries) do
|
|
33
|
+
# @return [Hash] the result as plain JSON types, ready to store or send
|
|
34
|
+
def to_h
|
|
35
|
+
column_summaries_as_hashes = {}
|
|
36
|
+
|
|
37
|
+
column_summaries.each do |target_column_name, column_summary|
|
|
38
|
+
column_summaries_as_hashes[target_column_name] = column_summary.to_h
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
{
|
|
42
|
+
row_count: row_count,
|
|
43
|
+
column_summaries: column_summaries_as_hashes
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class << self
|
|
49
|
+
# Reads the file and measures every mapped column.
|
|
50
|
+
#
|
|
51
|
+
# A mapped column may carry a +value_mapping+. Each of its values is then measured as the
|
|
52
|
+
# value it maps to, which is what would be loaded, and the mapping comes back in the result.
|
|
53
|
+
#
|
|
54
|
+
# The mapped columns must already be checked against the file header and the target schema
|
|
55
|
+
# ({MappedColumns}); the {Migration} does that. The position of each source column is read from the
|
|
56
|
+
# header, and a {ColumnLimitsChecker} is built for each target column from its type and limits. A file
|
|
57
|
+
# with no data rows is not an error: its mapped columns come back with zero counts. A source column mapped
|
|
58
|
+
# to several target columns is measured once for each of them. Nothing the caller gave is changed.
|
|
59
|
+
#
|
|
60
|
+
# @param source_reader [Source::CsvReader] the reader of the CSV source
|
|
61
|
+
# @param target [#cast_value] the target of the target model, which casts a value
|
|
62
|
+
# for a column so that it is checked as the value it would load
|
|
63
|
+
# @param resolved_mapped_columns [Hash{String => ResolvedMappedColumn}] target column => its source column, value mapping
|
|
64
|
+
# and column of the target table, which gives the type and the limits. Only these columns are measured.
|
|
65
|
+
# @return [Result]
|
|
66
|
+
#
|
|
67
|
+
# Errors raised by +source_reader+ and +target+ are not rescued.
|
|
68
|
+
def call(source_reader:, resolved_mapped_columns:, target:)
|
|
69
|
+
header_names = source_reader.headers
|
|
70
|
+
|
|
71
|
+
summary_builders = {}
|
|
72
|
+
resolved_mapped_columns.each do |target_column_name, resolved_mapped_column|
|
|
73
|
+
summary_builders[target_column_name] = ColumnSummaryBuilder.new(
|
|
74
|
+
target_column_name: target_column_name,
|
|
75
|
+
source_column_name: resolved_mapped_column.source_column_name,
|
|
76
|
+
position: header_names.index(resolved_mapped_column.source_column_name) + 1,
|
|
77
|
+
limits_checker: ColumnLimitsChecker.for(target_column: resolved_mapped_column.target_column),
|
|
78
|
+
value_mapping: resolved_mapped_column.value_mapping,
|
|
79
|
+
target: target
|
|
80
|
+
)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
row_count = source_reader.each_row do |row|
|
|
84
|
+
summary_builders.each_value do |summary_builder|
|
|
85
|
+
summary_builder.add_cell(cell: row[summary_builder.source_column_name])
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
column_summaries = {}
|
|
90
|
+
summary_builders.each { |target_column_name, summary_builder| column_summaries[target_column_name] = summary_builder.summary }
|
|
91
|
+
|
|
92
|
+
Result.new(row_count: row_count, column_summaries: column_summaries)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|