-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcodegen.py
More file actions
408 lines (347 loc) · 12.8 KB
/
codegen.py
File metadata and controls
408 lines (347 loc) · 12.8 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
Code generator for dbt to Feast imports.
This module generates Python code files containing Feast object definitions
(Entity, DataSource, FeatureView) from dbt model metadata.
"""
import logging
from typing import Any, List, Optional, Set
from jinja2 import BaseLoader, Environment
from feast.dbt.mapper import map_dbt_type_to_feast_type
from feast.dbt.parser import DbtModel
from feast.types import (
Array,
Bool,
Bytes,
Float32,
Float64,
Int32,
Int64,
String,
UnixTimestamp,
)
logger = logging.getLogger(__name__)
# Template for generating a complete Feast definitions file
FEAST_FILE_TEMPLATE = '''"""
Feast feature definitions generated from dbt models.
Source: {{ manifest_path }}
Project: {{ project_name }}
Generated by: feast dbt import
"""
from datetime import timedelta
from feast import Entity, FeatureView, Field
{% if type_imports %}
from feast.types import {{ type_imports | join(', ') }}
{% endif %}
{% if data_source_type == 'bigquery' %}
from feast.infra.offline_stores.bigquery_source import BigQuerySource
{% elif data_source_type == 'snowflake' %}
from feast.infra.offline_stores.snowflake_source import SnowflakeSource
{% elif data_source_type == 'file' %}
from feast.infra.offline_stores.file_source import FileSource
{% endif %}
# =============================================================================
# Entities
# =============================================================================
{% for entity in entities %}
{{ entity.var_name }} = Entity(
name="{{ entity.name }}",
join_keys=["{{ entity.join_key }}"],
description="{{ entity.description }}",
tags={{ entity.tags }},
)
{% endfor %}
# =============================================================================
# Data Sources
# =============================================================================
{% for source in data_sources %}
{% if data_source_type == 'bigquery' %}
{{ source.var_name }} = BigQuerySource(
name="{{ source.name }}",
table="{{ source.table }}",
timestamp_field="{{ source.timestamp_field }}",
description="{{ source.description }}",
tags={{ source.tags }},
)
{% elif data_source_type == 'snowflake' %}
{{ source.var_name }} = SnowflakeSource(
name="{{ source.name }}",
database="{{ source.database }}",
schema="{{ source.schema }}",
table="{{ source.table }}",
timestamp_field="{{ source.timestamp_field }}",
description="{{ source.description }}",
tags={{ source.tags }},
)
{% elif data_source_type == 'file' %}
{{ source.var_name }} = FileSource(
name="{{ source.name }}",
path="{{ source.path }}",
timestamp_field="{{ source.timestamp_field }}",
description="{{ source.description }}",
tags={{ source.tags }},
)
{% endif %}
{% endfor %}
# =============================================================================
# Feature Views
# =============================================================================
{% for fv in feature_views %}
{{ fv.var_name }} = FeatureView(
name="{{ fv.name }}",
entities=[{{ fv.entity_var }}],
ttl=timedelta(days={{ fv.ttl_days }}),
schema=[
{% for field in fv.fields %}
Field(name="{{ field.name }}", dtype={{ field.dtype }}{% if field.description %}, description="{{ field.description }}"{% endif %}),
{% endfor %}
],
online={{ fv.online }},
source={{ fv.source_var }},
description="{{ fv.description }}",
tags={{ fv.tags }},
)
{% endfor %}
'''
def _get_feast_type_name(feast_type: Any) -> str:
"""Get the string name of a Feast type for code generation."""
if isinstance(feast_type, Array):
# Safely get base_type. Should always exist since Array.__init__ sets it.
# Example: Array(String) -> base_type = String
base_type = getattr(feast_type, "base_type", None)
if base_type is None:
logger.warning(
"Array type missing 'base_type' attribute. "
"This indicates a bug in Array initialization. Falling back to String."
)
base_type = String
base_type_name = _get_feast_type_name(base_type)
return f"Array({base_type_name})"
# Map type objects to their names.
# Note: ImageBytes and PdfBytes are excluded since dbt manifests only expose
# generic BYTES type without semantic information about binary content.
type_map = {
String: "String",
Int32: "Int32",
Int64: "Int64",
Float32: "Float32",
Float64: "Float64",
Bool: "Bool",
UnixTimestamp: "UnixTimestamp",
Bytes: "Bytes",
}
return type_map.get(feast_type, "String")
def _make_var_name(name: str) -> str:
"""Convert a name to a valid Python variable name."""
# Replace hyphens and spaces with underscores
var_name = name.replace("-", "_").replace(" ", "_")
# Ensure it starts with a letter or underscore
if var_name and var_name[0].isdigit():
var_name = f"_{var_name}"
return var_name
def _escape_description(desc: Optional[str]) -> str:
"""Escape a description string for use in Python code."""
if not desc:
return ""
# Escape quotes and newlines
return desc.replace("\\", "\\\\").replace('"', '\\"').replace("\n", " ")
class DbtCodeGenerator:
"""
Generates Python code for Feast objects from dbt models.
This class creates complete, importable Python files containing
Entity, DataSource, and FeatureView definitions.
Example::
generator = DbtCodeGenerator(
data_source_type="bigquery",
timestamp_field="event_timestamp",
ttl_days=7
)
code = generator.generate(
models=models,
entity_column="user_id",
manifest_path="target/manifest.json",
project_name="my_project"
)
with open("features.py", "w") as f:
f.write(code)
"""
def __init__(
self,
data_source_type: str = "bigquery",
timestamp_field: str = "event_timestamp",
ttl_days: int = 1,
):
self.data_source_type = data_source_type.lower()
self.timestamp_field = timestamp_field
self.ttl_days = ttl_days
# Set up Jinja2 environment
self.env = Environment(
loader=BaseLoader(),
trim_blocks=True,
lstrip_blocks=True,
)
self.template = self.env.from_string(FEAST_FILE_TEMPLATE)
def generate(
self,
models: List[DbtModel],
entity_column: str,
manifest_path: str = "",
project_name: str = "",
exclude_columns: Optional[List[str]] = None,
online: bool = True,
) -> str:
"""
Generate Python code for Feast objects from dbt models.
Args:
models: List of DbtModel objects to generate code for
entity_column: The entity/primary key column name
manifest_path: Path to the dbt manifest (for documentation)
project_name: dbt project name (for documentation)
exclude_columns: Columns to exclude from features
online: Whether to enable online serving
Returns:
Generated Python code as a string
"""
excluded = {entity_column, self.timestamp_field}
if exclude_columns:
excluded.update(exclude_columns)
# Collect all Feast types used for imports
type_imports: Set[str] = set()
# Prepare entity data
entities = []
entity_var = _make_var_name(entity_column)
entities.append(
{
"var_name": entity_var,
"name": entity_column,
"join_key": entity_column,
"description": "Entity key for dbt models",
"tags": {"source": "dbt"},
}
)
# Prepare data sources and feature views
data_sources = []
feature_views = []
for model in models:
# Check required columns exist
column_names = [c.name for c in model.columns]
if self.timestamp_field not in column_names:
continue
if entity_column not in column_names:
continue
# Build tags
tags = {"dbt.model": model.name}
for tag in model.tags:
tags[f"dbt.tag.{tag}"] = "true"
# Data source
source_var = _make_var_name(f"{model.name}_source")
source_data = {
"var_name": source_var,
"name": f"{model.name}_source",
"timestamp_field": self.timestamp_field,
"description": _escape_description(model.description),
"tags": tags,
}
if self.data_source_type == "bigquery":
source_data["table"] = model.full_table_name
elif self.data_source_type == "snowflake":
source_data["database"] = model.database
source_data["schema"] = model.schema
source_data["table"] = model.alias
elif self.data_source_type == "file":
source_data["path"] = f"/data/{model.name}.parquet"
data_sources.append(source_data)
# Feature view fields
fields = []
for column in model.columns:
if column.name in excluded:
continue
feast_type = map_dbt_type_to_feast_type(column.data_type)
type_name = _get_feast_type_name(feast_type)
# Track base type for imports. For Array types, import both Array and base type.
# Example: Array(Int64) requires imports: Array, Int64
if isinstance(feast_type, Array):
type_imports.add("Array")
base_type = getattr(feast_type, "base_type", None)
if base_type is None:
logger.warning(
"Array type missing 'base_type' attribute while generating imports. "
"This indicates a bug in Array initialization. Falling back to String."
)
base_type = String
base_type_name = _get_feast_type_name(base_type)
type_imports.add(base_type_name)
else:
type_imports.add(type_name)
fields.append(
{
"name": column.name,
"dtype": type_name,
"description": _escape_description(column.description),
}
)
# Feature view
fv_var = _make_var_name(f"{model.name}_fv")
feature_views.append(
{
"var_name": fv_var,
"name": model.name,
"entity_var": entity_var,
"source_var": source_var,
"ttl_days": self.ttl_days,
"fields": fields,
"online": online,
"description": _escape_description(model.description),
"tags": tags,
}
)
# Sort type imports for consistent output
sorted_types = sorted(type_imports)
# Render template
return self.template.render(
manifest_path=manifest_path,
project_name=project_name,
data_source_type=self.data_source_type,
type_imports=sorted_types,
entities=entities,
data_sources=data_sources,
feature_views=feature_views,
)
def generate_feast_code(
models: List[DbtModel],
entity_column: str,
data_source_type: str = "bigquery",
timestamp_field: str = "event_timestamp",
ttl_days: int = 1,
manifest_path: str = "",
project_name: str = "",
exclude_columns: Optional[List[str]] = None,
online: bool = True,
) -> str:
"""
Convenience function to generate Feast code from dbt models.
Args:
models: List of DbtModel objects
entity_column: Primary key column name
data_source_type: Type of data source (bigquery, snowflake, file)
timestamp_field: Timestamp column name
ttl_days: TTL in days for feature views
manifest_path: Path to manifest for documentation
project_name: Project name for documentation
exclude_columns: Columns to exclude from features
online: Whether to enable online serving
Returns:
Generated Python code as a string
"""
generator = DbtCodeGenerator(
data_source_type=data_source_type,
timestamp_field=timestamp_field,
ttl_days=ttl_days,
)
return generator.generate(
models=models,
entity_column=entity_column,
manifest_path=manifest_path,
project_name=project_name,
exclude_columns=exclude_columns,
online=online,
)