Skip to content

Commit 3377efb

Browse files
authored
feat: Configure file paths for dbt imports (#7016)
* test: Cover configurable file paths in dbt imports Signed-off-by: Lucas Pimentel <154371376+luc-pimentel@users.noreply.github.com> * feat: Configure file paths for dbt imports Share path-template validation and expansion between direct imports and generated definitions, preserving the default path and Python literals. Signed-off-by: Lucas Pimentel <154371376+luc-pimentel@users.noreply.github.com> --------- Signed-off-by: Lucas Pimentel <154371376+luc-pimentel@users.noreply.github.com>
1 parent 483dddb commit 3377efb

5 files changed

Lines changed: 484 additions & 8 deletions

File tree

‎docs/how-to-guides/dbt-integration.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ feast dbt import <manifest_path> [OPTIONS]
267267
|--------|-------------|---------|
268268
| `--entity-column`, `-e` | Entity column name (can be specified multiple times) | (required) |
269269
| `--data-source-type`, `-d` | Data source type: `bigquery`, `snowflake`, `file` | `bigquery` |
270+
| `--file-path-template` | File source path; `{model}` expands to the dbt model name | `/data/{model}.parquet` |
270271
| `--tag-filter`, `-t` | Filter models by dbt tag | None |
271272
| `--model`, `-m` | Import specific model(s) only | None |
272273
| `--timestamp-field` | Timestamp column name | `event_timestamp` |
@@ -339,7 +340,7 @@ SnowflakeSource(
339340
### File
340341

341342
```bash
342-
feast dbt import manifest.json -e user_id -d file -o features.py
343+
feast dbt import -m manifest.json -e user_id -d file -o features.py
343344
```
344345

345346
Generates `FileSource` with a placeholder path:
@@ -351,9 +352,41 @@ FileSource(
351352
```
352353

353354
{% hint style="info" %}
354-
For file sources, update the generated path to point to your actual data files.
355+
When `--file-path-template` is omitted, the placeholder path is preserved. Update it
356+
manually or supply a template pointing to your actual files.
355357
{% endhint %}
356358

359+
Use `--file-path-template` to configure the paths for both direct registry imports
360+
and generated Python definitions:
361+
362+
```bash
363+
# Import directly into the Feast registry
364+
feast dbt import -m manifest.json -e user_id -d file \
365+
--file-path-template "/warehouse/{model}.parquet"
366+
367+
# Generate definitions with the same paths
368+
feast dbt import -m manifest.json -e user_id -d file \
369+
--file-path-template "/warehouse/{model}.parquet" -o features.py
370+
```
371+
372+
`{model}` is replaced with the dbt model's name, not its warehouse alias. For a
373+
model named `user_stats`, the example produces `/warehouse/user_stats.parquet`.
374+
A literal path without `{model}` is also accepted; it is used for every selected
375+
model. Repeat `{model}` if needed, and use `{{` and `}}` for literal braces.
376+
377+
Paths are preserved as supplied after substitution, including relative paths and
378+
URIs such as `s3://bucket/{model}.parquet`. The importer does not check whether the
379+
files exist or change how the configured offline store resolves them. For the
380+
File/Dask offline store, relative paths are resolved against the feature repository,
381+
not the manifest's directory or the generated Python file's directory.
382+
383+
The option is only valid with `--data-source-type file`. Empty templates, unknown
384+
placeholders, attribute/index access, conversions, format specifications, and
385+
unmatched braces produce an error before importing or writing definitions.
386+
387+
The Python APIs accept the same optional `file_path_template` argument on
388+
`DbtToFeastMapper`, `DbtCodeGenerator`, and `generate_feast_code`.
389+
357390
## Best Practices
358391

359392
### 1. Use consistent tagging

‎sdk/python/feast/cli/dbt_import.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def dbt_cmd():
4343
show_default=True,
4444
help="Type of data source to create",
4545
)
46+
@click.option(
47+
"--file-path-template",
48+
default=None,
49+
help="Path template for file sources; {model} is the model name. "
50+
"Defaults to /data/{model}.parquet.",
51+
)
4652
@click.option(
4753
"--timestamp-field",
4854
"-t",
@@ -100,6 +106,7 @@ def import_command(
100106
dry_run: bool,
101107
exclude_columns: Optional[str],
102108
output: Optional[str],
109+
file_path_template: Optional[str] = None,
103110
):
104111
"""
105112
Import dbt models as Feast FeatureViews.
@@ -121,10 +128,19 @@ def import_command(
121128
# Generate Python file instead of applying to registry
122129
feast dbt import -m target/manifest.json -e driver_id --tag feast --output features.py
123130
"""
124-
from feast.dbt.mapper import DbtToFeastMapper, map_dbt_type_to_feast_type
131+
from feast.dbt.mapper import (
132+
DbtToFeastMapper,
133+
_validate_file_path_template,
134+
map_dbt_type_to_feast_type,
135+
)
125136
from feast.dbt.parser import DbtManifestParser, DbtModel
126137
from feast.types import Array, Int32, Int64, String
127138

139+
try:
140+
_validate_file_path_template(file_path_template, data_source_type)
141+
except ValueError as e:
142+
raise click.BadParameter(str(e), param_hint="--file-path-template") from e
143+
128144
# Parse manifest
129145
click.echo(f"{Fore.CYAN}Parsing dbt manifest: {manifest_path}{Style.RESET_ALL}")
130146

@@ -192,6 +208,7 @@ def import_command(
192208
data_source_type=data_source_type,
193209
timestamp_field=timestamp_field,
194210
ttl_days=ttl_days,
211+
file_path_template=file_path_template,
195212
)
196213

197214
# Generate Feast objects
@@ -314,6 +331,7 @@ def import_command(
314331
project_name=parser.project_name or "",
315332
exclude_columns=excluded,
316333
online=True,
334+
file_path_template=file_path_template,
317335
)
318336

319337
with open(output, "w") as f:

‎sdk/python/feast/dbt/codegen.py‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
from jinja2 import BaseLoader, Environment
1212

13-
from feast.dbt.mapper import map_dbt_type_to_feast_type
13+
from feast.dbt.mapper import (
14+
_resolve_file_path,
15+
_validate_file_path_template,
16+
map_dbt_type_to_feast_type,
17+
)
1418
from feast.dbt.parser import DbtModel
1519
from feast.types import (
1620
Array,
@@ -90,7 +94,7 @@
9094
{% elif data_source_type == 'file' %}
9195
{{ source.var_name }} = FileSource(
9296
name="{{ source.name }}",
93-
path="{{ source.path }}",
97+
path={{ source.path_literal }},
9498
timestamp_field="{{ source.timestamp_field }}",
9599
description="{{ source.description }}",
96100
tags={{ source.tags }},
@@ -182,6 +186,9 @@ class DbtCodeGenerator:
182186
This class creates complete, importable Python files containing
183187
Entity, DataSource, and FeatureView definitions.
184188
189+
For file sources, ``file_path_template`` optionally replaces the default
190+
``/data/{model}.parquet`` path, using the dbt model name for ``{model}``.
191+
185192
Example::
186193
187194
generator = DbtCodeGenerator(
@@ -204,10 +211,13 @@ def __init__(
204211
data_source_type: str = "bigquery",
205212
timestamp_field: str = "event_timestamp",
206213
ttl_days: int = 1,
214+
file_path_template: Optional[str] = None,
207215
):
208216
self.data_source_type = data_source_type.lower()
209217
self.timestamp_field = timestamp_field
210218
self.ttl_days = ttl_days
219+
_validate_file_path_template(file_path_template, self.data_source_type)
220+
self.file_path_template = file_path_template
211221

212222
# Set up Jinja2 environment
213223
self.env = Environment(
@@ -309,7 +319,9 @@ def generate(
309319
source_data["schema"] = model.schema
310320
source_data["table"] = model.alias
311321
elif self.data_source_type == "file":
312-
source_data["path"] = f"/data/{model.name}.parquet"
322+
source_data["path_literal"] = repr(
323+
_resolve_file_path(self.file_path_template, model.name)
324+
)
313325

314326
data_sources.append(source_data)
315327

@@ -389,6 +401,7 @@ def generate_feast_code(
389401
project_name: str = "",
390402
exclude_columns: Optional[List[str]] = None,
391403
online: bool = True,
404+
file_path_template: Optional[str] = None,
392405
) -> str:
393406
"""
394407
Convenience function to generate Feast code from dbt models.
@@ -403,6 +416,7 @@ def generate_feast_code(
403416
project_name: Project name for documentation
404417
exclude_columns: Columns to exclude from features
405418
online: Whether to enable online serving
419+
file_path_template: Optional file source path, with {model} replaced by the model name
406420
407421
Returns:
408422
Generated Python code as a string
@@ -411,6 +425,7 @@ def generate_feast_code(
411425
data_source_type=data_source_type,
412426
timestamp_field=timestamp_field,
413427
ttl_days=ttl_days,
428+
file_path_template=file_path_template,
414429
)
415430

416431
return generator.generate(

‎sdk/python/feast/dbt/mapper.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
including DataSource, Entity, and FeatureView.
66
"""
77

8+
import re
89
from datetime import timedelta
910
from typing import Any, Dict, List, Optional, Union
1011

@@ -167,6 +168,33 @@ def map_dbt_type_to_feast_type(dbt_type: str) -> FeastType:
167168
return String
168169

169170

171+
def _validate_file_path_template(
172+
file_path_template: Optional[str], data_source_type: str
173+
) -> None:
174+
if file_path_template is None:
175+
return
176+
if data_source_type.lower() != "file":
177+
raise ValueError("file_path_template is only supported for file data sources")
178+
if not file_path_template:
179+
raise ValueError("file_path_template must not be empty")
180+
181+
# Remove the supported tokens; any remaining brace is invalid template syntax.
182+
literal = re.sub(r"\{\{|\}\}|\{model\}", "", file_path_template)
183+
if "{" in literal or "}" in literal:
184+
raise ValueError(
185+
f"Invalid file_path_template {file_path_template!r}. "
186+
"Use only {model}, with {{ and }} for literal braces; "
187+
"other fields and formatting are not supported."
188+
)
189+
190+
191+
def _resolve_file_path(file_path_template: Optional[str], model_name: str) -> str:
192+
template = (
193+
"/data/{model}.parquet" if file_path_template is None else file_path_template
194+
)
195+
return template.format(model=model_name)
196+
197+
170198
class DbtToFeastMapper:
171199
"""
172200
Maps dbt models to Feast objects.
@@ -186,17 +214,21 @@ class DbtToFeastMapper:
186214
data_source_type: Type of data source ('bigquery', 'snowflake', 'file')
187215
timestamp_field: Default timestamp field name
188216
ttl_days: Default TTL in days for feature views
217+
file_path_template: Optional file source path, with {model} replaced by the model name
189218
"""
190219

191220
def __init__(
192221
self,
193222
data_source_type: str = "bigquery",
194223
timestamp_field: str = "event_timestamp",
195224
ttl_days: int = 1,
225+
file_path_template: Optional[str] = None,
196226
):
197227
self.data_source_type = data_source_type.lower()
198228
self.timestamp_field = timestamp_field
199229
self.ttl_days = ttl_days
230+
_validate_file_path_template(file_path_template, self.data_source_type)
231+
self.file_path_template = file_path_template
200232

201233
def _infer_entity_value_type(self, model: DbtModel, entity_col: str) -> ValueType:
202234
"""Infer entity ValueType from dbt model column type."""
@@ -262,10 +294,9 @@ def create_data_source(
262294
elif self.data_source_type == "file":
263295
from feast.infra.offline_stores.file_source import FileSource
264296

265-
# For file sources, use the model name as a placeholder path
266297
return FileSource(
267298
name=f"{model.name}_source",
268-
path=f"/data/{model.name}.parquet",
299+
path=_resolve_file_path(self.file_path_template, model.name),
269300
timestamp_field=ts_field,
270301
created_timestamp_column=created_timestamp_column or "",
271302
description=model.description,

0 commit comments

Comments
 (0)