|
| 1 | +# How to Support a New Data Source |
| 2 | + |
| 3 | +This document outlines the steps required to add support for a new data source to the dbt project converter. |
| 4 | +The target data source must be supported by both dbt and the Wren engine: |
| 5 | +- [dbt supported databases](https://docs.getdbt.com/docs/supported-data-platforms) |
| 6 | +- [Wren engine supported data sources](https://docs.getwren.ai/oss/wren_engine_api#tag/AthenaConnectionInfo) |
| 7 | + |
| 8 | +## 1. Implement the DataSource Interface |
| 9 | + |
| 10 | +The first step is to define a new struct for your data source and implement the `DataSource` interface defined in `data_source.go`. |
| 11 | + |
| 12 | +The `DataSource` interface is as follows: |
| 13 | + |
| 14 | +```go |
| 15 | +type DataSource interface { |
| 16 | + GetType() string |
| 17 | + Validate() error |
| 18 | + MapType(sourceType string) string |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### Steps: |
| 23 | + |
| 24 | +1. **Define Your Struct**: Create a new struct that represents the connection properties for your data source. The fields in this struct should correspond to the properties defined in the [Wren engine's API documentation](https://docs.getwren.ai/oss/wren_engine_api#tag/SnowflakeConnectionInfo) for the target data source. |
| 25 | + |
| 26 | + For example, to add support for `Snowflake`, you would define the following struct: |
| 27 | + |
| 28 | + ```go |
| 29 | + type WrenSnowflakeDataSource struct { |
| 30 | + Account string `json:"account"` |
| 31 | + User string `json:"user"` |
| 32 | + Password string `json:"password"` |
| 33 | + Database string `json:"database"` |
| 34 | + Warehouse string `json:"warehouse"` |
| 35 | + // ... other properties |
| 36 | + } |
| 37 | + ``` |
| 38 | + |
| 39 | +2. **Implement `GetType()`**: This method should return a string that identifies your data source type (e.g., `"snowflake"`). |
| 40 | + |
| 41 | +3. **Implement `Validate()`**: This method should check if the essential properties of your data source are set and valid. Return an error if validation fails. |
| 42 | + |
| 43 | +4. **Implement `MapType()`**: This method is crucial for mapping data types from the source system (as defined in `catalog.json`) to Wren's supported data types (e.g., `integer`, `varchar`, `timestamp`). |
| 44 | +
|
| 45 | +## 2. Add Conversion Logic in `data_source.go` |
| 46 | +
|
| 47 | +After implementing the interface, you need to integrate your new data source into the conversion logic. This is done by updating the `convertConnectionToDataSource` function in `data_source.go`. |
| 48 | +
|
| 49 | +Add a new `case` to the `switch` statement that matches the `type` field from the dbt `profiles.yml` file. This new case will be responsible for creating an instance of your new data source struct from the dbt connection details. |
| 50 | +
|
| 51 | +### Example: |
| 52 | +
|
| 53 | +```go |
| 54 | +// in data_source.go |
| 55 | +
|
| 56 | +func convertConnectionToDataSource(conn DbtConnection, dbtHomePath, profileName, outputName string) (DataSource, error) { |
| 57 | + switch strings.ToLower(conn.Type) { |
| 58 | + case "postgres", "postgresql": |
| 59 | + return convertToPostgresDataSource(conn) |
| 60 | + case "duckdb": |
| 61 | + return convertToLocalFileDataSource(conn, dbtHomePath) |
| 62 | + // Add your new case here |
| 63 | + case "snowflake": |
| 64 | + return convertToSnowflakeDataSource(conn) // Implement this function |
| 65 | + default: |
| 66 | + // ... |
| 67 | + } |
| 68 | +} |
| 69 | +
|
| 70 | +// Implement the conversion function |
| 71 | +func convertToSnowflakeDataSource(conn DbtConnection) (*WrenSnowflakeDataSource, error) { |
| 72 | + // Logic to extract snowflake properties from conn |
| 73 | + // and return a new *WrenSnowflakeDataSource |
| 74 | +} |
| 75 | +``` |
| 76 | +
|
| 77 | +## 3. Handle the New Data Source in `ConvertDbtProjectCore` |
| 78 | +
|
| 79 | +The `ConvertDbtProjectCore` function in `converter.go` is responsible for generating the `wren-datasource.json` file. You must add your new data source to the `switch` statement within this function to ensure it is correctly serialized. |
| 80 | +
|
| 81 | +### Steps: |
| 82 | +
|
| 83 | +1. **Locate the `switch` statement**: Find the `switch typedDS := ds.(type)` block inside `ConvertDbtProjectCore`. |
| 84 | +2. **Add a new `case`**: Add a new `case` for your data source struct. Inside this case, construct the `wrenDataSource` map with the correct `type` and `properties`. |
| 85 | +
|
| 86 | +### Example: |
| 87 | +
|
| 88 | +```go |
| 89 | +// in converter.go's ConvertDbtProjectCore function |
| 90 | + |
| 91 | +// ... |
| 92 | + switch typedDS := ds.(type) { |
| 93 | + case *WrenPostgresDataSource: |
| 94 | + // ... |
| 95 | + case *WrenLocalFileDataSource: |
| 96 | + // ... |
| 97 | + // Add your new case here |
| 98 | + case *WrenSnowflakeDataSource: |
| 99 | + wrenDataSource = map[string]interface{}{ |
| 100 | + "type": "snowflake", |
| 101 | + "properties": map[string]interface{}{ |
| 102 | + "account": typedDS.Account, |
| 103 | + "user": typedDS.User, |
| 104 | + "password": typedDS.Password, |
| 105 | + "database": typedDS.Database, |
| 106 | + "warehouse": typedDS.Warehouse, |
| 107 | + // ... other properties |
| 108 | + }, |
| 109 | + } |
| 110 | + default: |
| 111 | + // ... |
| 112 | + } |
| 113 | +// ... |
| 114 | +``` |
| 115 | + |
| 116 | +**Note on File-Based Data Sources**: If your data source is file-based (like `duckdb`), you also need to add logic to set the `localStoragePath` variable correctly within `ConvertDbtProjectCore`. This path tells the Wren engine where to find the data files. |
0 commit comments