-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathdatatype_inference.py
More file actions
130 lines (104 loc) · 3.64 KB
/
datatype_inference.py
File metadata and controls
130 lines (104 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pytype: skip-file
import array
from collections import OrderedDict
import numpy as np
from fastavro import parse_schema
from apache_beam.typehints import trivial_inference
from apache_beam.typehints import typehints
try:
import pyarrow as pa
except ImportError:
pa = None
def infer_element_type(elements):
"""For internal use only; no backwards-compatibility guarantees.
Infer a Beam type for a list of elements.
Args:
elements (List[Any]): A list of elements for which the type should be
inferred.
Returns:
A Beam type encompassing all elements.
"""
element_type = typehints.Union[[
trivial_inference.instance_to_type(e) for e in elements
]]
return element_type
def infer_typehints_schema(data):
"""For internal use only; no backwards-compatibility guarantees.
Infer Beam types for tabular data.
Args:
data (List[dict]): A list of dictionaries representing rows in a table.
Returns:
An OrderedDict mapping column names to Beam types.
"""
column_data = OrderedDict()
for row in data:
for key, value in row.items():
column_data.setdefault(key, []).append(value)
column_types = OrderedDict([(key, infer_element_type(values))
for key, values in column_data.items()])
return column_types
def infer_avro_schema(data):
"""For internal use only; no backwards-compatibility guarantees.
Infer avro schema for tabular data.
Args:
data (List[dict]): A list of dictionaries representing rows in a table.
Returns:
An avro schema object.
"""
_typehint_to_avro_type = {
type(None): "null",
int: "int",
float: "double",
str: "string",
bytes: "bytes",
np.ndarray: "bytes",
array.array: "bytes",
}
def typehint_to_avro_type(value):
if isinstance(value, typehints.UnionConstraint):
return sorted(
typehint_to_avro_type(union_type) for union_type in value.union_types)
else:
return _typehint_to_avro_type[value]
column_types = infer_typehints_schema(data)
avro_fields = [{
"name": str(key), "type": typehint_to_avro_type(value)
} for key, value in column_types.items()]
schema_dict = {
"namespace": "example.avro",
"name": "User",
"type": "record",
"fields": avro_fields
}
return parse_schema(schema_dict)
def infer_pyarrow_schema(data):
"""For internal use only; no backwards-compatibility guarantees.
Infer PyArrow schema for tabular data.
Args:
data (List[dict]): A list of dictionaries representing rows in a table.
Returns:
A PyArrow schema object.
"""
column_data = OrderedDict()
for row in data:
for key, value in row.items():
column_data.setdefault(key, []).append(value)
column_types = OrderedDict([(key, pa.array(value).type)
for key, value in column_data.items()])
return pa.schema(list(column_types.items()))