Hive/Spark/Databricks to dbt Model Conversion
Purpose
Transform Hive/Spark/Databricks DDL (views, tables, UDFs) into production-quality dbt models
compatible with Snowflake, maintaining the same business logic and data transformation steps while
following dbt best practices.
When to Use This Skill
Activate this skill when users ask about:
- Converting Hive/Spark/Databricks views or tables to dbt models
- Migrating HiveQL UDFs to dbt
- Translating HiveQL syntax to Snowflake
- Generating schema.yml files with tests and documentation
- Handling Hive-specific syntax conversions (external tables, PARTITIONED BY, LATERAL VIEW, file
formats)
Task Description
You are a database engineer working for a hospital system. You need to convert Hive/Spark/Databricks
DDL to equivalent dbt code compatible with Snowflake, maintaining the same business logic and data
transformation steps while following dbt best practices.
Input Requirements
I will provide you the HiveQL DDL to convert.
Audience
The code will be executed by data engineers who are learning Snowflake and dbt.
Output Requirements
Generate the following:
- One or more dbt models with complete SQL for every column
- A corresponding schema.yml file with appropriate tests and documentation
- A config block with materialization strategy
- Explanation of key changes and architectural decisions
- Inline comments highlighting any syntax that was converted
Conversion Guidelines
General Principles
- Replace procedural logic with declarative SQL where possible
- Break down complex procedures into multiple modular dbt models
- Implement appropriate incremental processing strategies
- Maintain data quality checks through dbt tests
- Use Snowflake SQL functions rather than macros whenever possible
Sample Response Format
-- dbt model: models/[domain]/[target_schema_name]/model_name.sql
{{ config(materialized='view') }}
/* Original Object: [database].[object_name]
Source Platform: Hive/Spark/Databricks
Purpose: [brief description]
Conversion Notes: [key changes]
Description: [SQL logic description] */
WITH source_data AS (
SELECT
-- Hive BIGINT/INT converted to INTEGER
customer_id::INTEGER AS customer_id,
-- STRING converted to VARCHAR
customer_name::VARCHAR(100) AS customer_name,
-- DECIMAL converted to NUMBER
account_balance::NUMBER(18,2) AS account_balance,
-- TIMESTAMP converted to TIMESTAMP_NTZ
created_date::TIMESTAMP_NTZ AS created_date
FROM {{ ref('upstream_model') }}
),
transformed_data AS (
SELECT
customer_id,
UPPER(customer_name)::VARCHAR(100) AS customer_name_upper,
account_balance,
created_date,
CURRENT_TIMESTAMP()::TIMESTAMP_NTZ AS loaded_at
FROM source_data
)
SELECT
customer_id,
customer_name_upper,
account_balance,
created_date,
loaded_at
FROM transformed_data
## models/[domain]/[target_schema_name]/_models.yml
version: 2
models:
- name: model_name
description: "Table description; converted from Hive/Spark/Databricks [Original object name]"
columns:
- name: customer_id
description: "Primary key - unique customer identifier"
tests:
- unique
- not_null
- name: customer_name_upper
description: "Customer name in uppercase"
- name: account_balance
description: "Current account balance; Foreign key to OTHER_TABLE"
tests:
- relationships:
to: ref('OTHER_TABLE')
field: OTHER_TABLE_KEY
- name: created_date
description: "Date the customer record was created"
- name: loaded_at
description: "Timestamp when the record was loaded by dbt"
## dbt_project.yml (excerpt)
models:
my_project:
+materialized: view
domain_name:
+schema: target_schema_name
Specific Translation Rules
dbt Specific Requirements
- If the source is a view, use a view materialization in dbt
- Include appropriate dbt model configuration (materialization type)
- Add documentation blocks for a schema.yml
- Add descriptions for tables and columns
- Include relevant tests
- Define primary keys and relationships
- Assume that upstream objects are models
- Comprehensively provide all the columns in the output
- Break complex procedures into multiple models if needed
- Implement appropriate incremental strategies for large tables
- Use Snowflake SQL functions rather than macros whenever possible
- Always cast columns with explicit precision/scale using
::TYPE syntax (e.g.,
column_name::VARCHAR(100), amount::NUMBER(18,2)) to ensure output matches expected data types
- Always provide explicit column aliases for clarity and documentation
Performance Optimization
- Suggest clustering keys if needed
- Recommend materialization strategy (view vs table)
- Identify potential performance improvements
Hive/Spark to Snowflake Syntax Conversion
- Convert LATERAL VIEW to LATERAL FLATTEN
- Replace PARTITIONED BY with clustering keys
- Convert file format specifications (PARQUET, ORC) to Snowflake staging
- Handle EXTERNAL TABLE references
- Convert Hive UDFs to Snowflake equivalents
- Replace DISTRIBUTE BY/SORT BY with clustering
- Convert ARRAY/MAP/STRUCT types to VARIANT
Key Data Type Mappings
| Hive/Spark |
Snowflake |
Notes |
| TINYINT/SMALLINT/INT/BIGINT |
Same |
|
| FLOAT/DOUBLE |
FLOAT |
|
| DECIMAL |
DECIMAL |
|
| STRING |
VARCHAR |
|
| CHAR/VARCHAR |
Same |
|
| BOOLEAN |
BOOLEAN |
|
| BINARY |
BINARY |
|
| DATE |
DATE |
|
| TIMESTAMP |
TIMESTAMP_NTZ |
|
| ARRAY |
ARRAY |
|
| MAP<K,V> |
VARIANT |
Use OBJECT_CONSTRUCT |
| STRUCT |
VARIANT |
|
Key Syntax Conversions
-- LATERAL VIEW -> LATERAL FLATTEN
SELECT * FROM table LATERAL VIEW EXPLODE(array_col) t AS elem ->
SELECT * FROM table, LATERAL FLATTEN(input => array_col) AS f
-- PARTITIONED BY -> Clustering
CREATE TABLE t (...) PARTITIONED BY (dt STRING) ->
CREATE TABLE t (...) CLUSTER BY (dt)
-- External tables
CREATE EXTERNAL TABLE t LOCATION 's3://...' ->
CREATE EXTERNAL TABLE t WITH LOCATION = @stage/path
-- collect_list/collect_set
collect_list(col) -> ARRAY_AGG(col)
collect_set(col) -> ARRAY_AGG(DISTINCT col)
-- size() -> ARRAY_SIZE()
size(array_col) -> ARRAY_SIZE(array_col)
Common Function Mappings
| Hive/Spark |
Snowflake |
Notes |
collect_list(col) |
ARRAY_AGG(col) |
|
collect_set(col) |
ARRAY_AGG(DISTINCT col) |
|
size(arr) |
ARRAY_SIZE(arr) |
|
explode(arr) |
LATERAL FLATTEN(input => arr) |
|
posexplode(arr) |
LATERAL FLATTEN(input => arr) |
Use f.index |
concat_ws(sep, ...) |
CONCAT_WS(sep, ...) |
Same |
nvl(a, b) |
NVL(a, b) or COALESCE(a, b) |
Same |
coalesce(...) |
COALESCE(...) |
Same |
if(cond, a, b) |
IFF(cond, a, b) |
|
unix_timestamp() |
DATE_PART(epoch_second, CURRENT_TIMESTAMP()) |
|
from_unixtime(ts) |
TO_TIMESTAMP(ts) |
|
to_date(str, fmt) |
TO_DATE(str, fmt) |
Same |
date_format(d, fmt) |
TO_CHAR(d, fmt) |
Format codes differ |
datediff(d1, d2) |
DATEDIFF('day', d2, d1) |
Arg order differs |
regexp_replace(...) |
REGEXP_REPLACE(...) |
Same |
regexp_extract(...) |
REGEXP_SUBSTR(...) |
|
split(str, delim) |
SPLIT(str, delim) |
Same |
get_json_object(j, p) |
GET_PATH(PARSE_JSON(j), p) |
|
Dependencies
- List any upstream dependencies
- Suggest model organization in dbt project
Validation Checklist
- [] Every DDL statement has been accounted for in the dbt models
- [] SQL in models is compatible with Snowflake
- [] Hive-specific syntax converted (external tables, PARTITIONED BY, file formats, LATERAL VIEW)
- [] All business logic preserved
- [] All columns included in output
- [] Data types correctly mapped
- [] Functions translated to Snowflake equivalents
- [] Materialization strategy selected
- [] Tests added
- [] SQL logic description complete
- [] Table descriptions added
- [] Column descriptions added
- [] Dependencies correctly mapped
- [] Incremental logic (if applicable) verified
- [] Inline comments added for converted syntax
Related Skills
- $dbt-migration - For the complete migration workflow (discovery, planning, placeholder models,
testing, deployment)
- $dbt-modeling - For CTE patterns and SQL structure guidance
- $dbt-testing - For implementing comprehensive dbt tests
- $dbt-architecture - For project organization and folder structure
- $dbt-materializations - For choosing materialization strategies (view, table, incremental,
snapshots)
- $dbt-performance - For clustering keys, warehouse sizing, and query optimization
- $dbt-commands - For running dbt commands and model selection syntax
- $dbt-core - For dbt installation, configuration, and package management
- $snowflake-cli - For executing SQL and managing Snowflake objects
Supported Source Database
| Database |
Key Considerations |
| Hive / Spark / Databricks |
External tables, PARTITIONED BY, LATERAL VIEW, file formats (PARQUET, ORC), UDFs |
Translation References
Detailed syntax translation guides are available in the translation-references/ folder.
Copyright Notice: The translation reference documentation in this repository is derived from
Snowflake SnowConvert Documentation
and is © Copyright Snowflake Inc. All rights reserved. Used for reference purposes only.
Reference Index
- Built In Functions
- Data Types
- Ddls Create External Table
- Ddls Create View
- Ddls Readme
- Ddls Select
- Ddls Tables
- Overview (README)
- Subqueries
1---2name: dbt-migration-hive3description: Convert Hive/Spark/Databricks DDL to dbt models compatible with Snowflake. This skill should be used when converting views, tables, or UDFs from Hive, Spark, or Databricks to dbt code, generating schema.yml files with tests and documentation, or migrating HiveQL to follow dbt best practices.4---5
6# Hive/Spark/Databricks to dbt Model Conversion
7
8## Purpose
9
10Transform Hive/Spark/Databricks DDL (views, tables, UDFs) into production-quality dbt models
11compatible with Snowflake, maintaining the same business logic and data transformation steps while
12following dbt best practices.
13
14## When to Use This Skill
15
16Activate this skill when users ask about:
17
18- Converting Hive/Spark/Databricks views or tables to dbt models
19- Migrating HiveQL UDFs to dbt
20- Translating HiveQL syntax to Snowflake
21- Generating schema.yml files with tests and documentation
22- Handling Hive-specific syntax conversions (external tables, PARTITIONED BY, LATERAL VIEW, file
23 formats)
24
25---
26
27## Task Description
28
29You are a database engineer working for a hospital system. You need to convert Hive/Spark/Databricks
30DDL to equivalent dbt code compatible with Snowflake, maintaining the same business logic and data
31transformation steps while following dbt best practices.
32
33## Input Requirements
34
35I will provide you the HiveQL DDL to convert.
36
37## Audience
38
39The code will be executed by data engineers who are learning Snowflake and dbt.
40
41## Output Requirements
42
43Generate the following:
44
451. One or more dbt models with complete SQL for every column
462. A corresponding schema.yml file with appropriate tests and documentation
473. A config block with materialization strategy
484. Explanation of key changes and architectural decisions
495. Inline comments highlighting any syntax that was converted
50
51## Conversion Guidelines
52
53### General Principles
54
55- Replace procedural logic with declarative SQL where possible
56- Break down complex procedures into multiple modular dbt models
57- Implement appropriate incremental processing strategies
58- Maintain data quality checks through dbt tests
59- Use Snowflake SQL functions rather than macros whenever possible
60
61### Sample Response Format
62
63```sql
64-- dbt model: models/[domain]/[target_schema_name]/model_name.sql
65{{ config(materialized='view') }}
66
67/* Original Object: [database].[object_name]
68 Source Platform: Hive/Spark/Databricks
69 Purpose: [brief description]
70 Conversion Notes: [key changes]
71 Description: [SQL logic description] */
72
73WITH source_data AS (
74 SELECT
75 -- Hive BIGINT/INT converted to INTEGER
76 customer_id::INTEGER AS customer_id,
77 -- STRING converted to VARCHAR
78 customer_name::VARCHAR(100) AS customer_name,
79 -- DECIMAL converted to NUMBER
80 account_balance::NUMBER(18,2) AS account_balance,
81 -- TIMESTAMP converted to TIMESTAMP_NTZ
82 created_date::TIMESTAMP_NTZ AS created_date
83 FROM {{ ref('upstream_model') }}
84),
85
86transformed_data AS (
87 SELECT
88 customer_id,
89 UPPER(customer_name)::VARCHAR(100) AS customer_name_upper,
90 account_balance,
91 created_date,
92 CURRENT_TIMESTAMP()::TIMESTAMP_NTZ AS loaded_at
93 FROM source_data
94)
95
96SELECT
97 customer_id,
98 customer_name_upper,
99 account_balance,
100 created_date,
101 loaded_at
102FROM transformed_data
103```
104
105```yaml
106## models/[domain]/[target_schema_name]/_models.yml
107version: 2
108
109models:
110 - name: model_name
111 description: "Table description; converted from Hive/Spark/Databricks [Original object name]"
112 columns:
113 - name: customer_id
114 description: "Primary key - unique customer identifier"
115 tests:
116 - unique
117 - not_null
118 - name: customer_name_upper
119 description: "Customer name in uppercase"
120 - name: account_balance
121 description: "Current account balance; Foreign key to OTHER_TABLE"
122 tests:
123 - relationships:
124 to: ref('OTHER_TABLE')
125 field: OTHER_TABLE_KEY
126 - name: created_date
127 description: "Date the customer record was created"
128 - name: loaded_at
129 description: "Timestamp when the record was loaded by dbt"
130```
131
132```yaml
133## dbt_project.yml (excerpt)
134models:
135 my_project:
136 +materialized: view
137 domain_name:
138 +schema: target_schema_name
139```
140
141### Specific Translation Rules
142
143#### dbt Specific Requirements
144
145- If the source is a view, use a view materialization in dbt
146- Include appropriate dbt model configuration (materialization type)
147- Add documentation blocks for a schema.yml
148- Add descriptions for tables and columns
149- Include relevant tests
150- Define primary keys and relationships
151- Assume that upstream objects are models
152- Comprehensively provide all the columns in the output
153- Break complex procedures into multiple models if needed
154- Implement appropriate incremental strategies for large tables
155- Use Snowflake SQL functions rather than macros whenever possible
156- **Always cast columns with explicit precision/scale** using `::TYPE` syntax (e.g.,
157 `column_name::VARCHAR(100)`, `amount::NUMBER(18,2)`) to ensure output matches expected data types
158- **Always provide explicit column aliases** for clarity and documentation
159
160#### Performance Optimization
161
162- Suggest clustering keys if needed
163- Recommend materialization strategy (view vs table)
164- Identify potential performance improvements
165
166#### Hive/Spark to Snowflake Syntax Conversion
167
168- Convert LATERAL VIEW to LATERAL FLATTEN
169- Replace PARTITIONED BY with clustering keys
170- Convert file format specifications (PARQUET, ORC) to Snowflake staging
171- Handle EXTERNAL TABLE references
172- Convert Hive UDFs to Snowflake equivalents
173- Replace DISTRIBUTE BY/SORT BY with clustering
174- Convert ARRAY/MAP/STRUCT types to VARIANT
175
176#### Key Data Type Mappings
177
178| Hive/Spark | Snowflake | Notes |
179| --------------------------- | ------------- | -------------------- |
180| TINYINT/SMALLINT/INT/BIGINT | Same | |
181| FLOAT/DOUBLE | FLOAT | |
182| DECIMAL | DECIMAL | |
183| STRING | VARCHAR | |
184| CHAR/VARCHAR | Same | |
185| BOOLEAN | BOOLEAN | |
186| BINARY | BINARY | |
187| DATE | DATE | |
188| TIMESTAMP | TIMESTAMP_NTZ | |
189| ARRAY<T> | ARRAY | |
190| MAP<K,V> | VARIANT | Use OBJECT_CONSTRUCT |
191| STRUCT | VARIANT | |
192
193#### Key Syntax Conversions
194
195```sql
196-- LATERAL VIEW -> LATERAL FLATTEN
197SELECT * FROM table LATERAL VIEW EXPLODE(array_col) t AS elem ->
198SELECT * FROM table, LATERAL FLATTEN(input => array_col) AS f
199
200-- PARTITIONED BY -> Clustering
201CREATE TABLE t (...) PARTITIONED BY (dt STRING) ->
202CREATE TABLE t (...) CLUSTER BY (dt)
203
204-- External tables
205CREATE EXTERNAL TABLE t LOCATION 's3://...' ->
206CREATE EXTERNAL TABLE t WITH LOCATION = @stage/path
207
208-- collect_list/collect_set
209collect_list(col) -> ARRAY_AGG(col)
210collect_set(col) -> ARRAY_AGG(DISTINCT col)
211
212-- size() -> ARRAY_SIZE()
213size(array_col) -> ARRAY_SIZE(array_col)
214```
215
216#### Common Function Mappings
217
218| Hive/Spark | Snowflake | Notes |
219| ----------------------- | ---------------------------------------------- | ------------------- |
220| `collect_list(col)` | `ARRAY_AGG(col)` | |
221| `collect_set(col)` | `ARRAY_AGG(DISTINCT col)` | |
222| `size(arr)` | `ARRAY_SIZE(arr)` | |
223| `explode(arr)` | `LATERAL FLATTEN(input => arr)` | |
224| `posexplode(arr)` | `LATERAL FLATTEN(input => arr)` | Use `f.index` |
225| `concat_ws(sep, ...)` | `CONCAT_WS(sep, ...)` | Same |
226| `nvl(a, b)` | `NVL(a, b)` or `COALESCE(a, b)` | Same |
227| `coalesce(...)` | `COALESCE(...)` | Same |
228| `if(cond, a, b)` | `IFF(cond, a, b)` | |
229| `unix_timestamp()` | `DATE_PART(epoch_second, CURRENT_TIMESTAMP())` | |
230| `from_unixtime(ts)` | `TO_TIMESTAMP(ts)` | |
231| `to_date(str, fmt)` | `TO_DATE(str, fmt)` | Same |
232| `date_format(d, fmt)` | `TO_CHAR(d, fmt)` | Format codes differ |
233| `datediff(d1, d2)` | `DATEDIFF('day', d2, d1)` | Arg order differs |
234| `regexp_replace(...)` | `REGEXP_REPLACE(...)` | Same |
235| `regexp_extract(...)` | `REGEXP_SUBSTR(...)` | |
236| `split(str, delim)` | `SPLIT(str, delim)` | Same |
237| `get_json_object(j, p)` | `GET_PATH(PARSE_JSON(j), p)` | |
238
239#### Dependencies
240
241- List any upstream dependencies
242- Suggest model organization in dbt project
243
244---
245
246## Validation Checklist
247
248- [] Every DDL statement has been accounted for in the dbt models
249- [] SQL in models is compatible with Snowflake
250- [] Hive-specific syntax converted (external tables, PARTITIONED BY, file formats, LATERAL VIEW)
251- [] All business logic preserved
252- [] All columns included in output
253- [] Data types correctly mapped
254- [] Functions translated to Snowflake equivalents
255- [] Materialization strategy selected
256- [] Tests added
257- [] SQL logic description complete
258- [] Table descriptions added
259- [] Column descriptions added
260- [] Dependencies correctly mapped
261- [] Incremental logic (if applicable) verified
262- [] Inline comments added for converted syntax
263
264---
265
266## Related Skills
267
268- $dbt-migration - For the complete migration workflow (discovery, planning, placeholder models,
269 testing, deployment)
270- $dbt-modeling - For CTE patterns and SQL structure guidance
271- $dbt-testing - For implementing comprehensive dbt tests
272- $dbt-architecture - For project organization and folder structure
273- $dbt-materializations - For choosing materialization strategies (view, table, incremental,
274 snapshots)
275- $dbt-performance - For clustering keys, warehouse sizing, and query optimization
276- $dbt-commands - For running dbt commands and model selection syntax
277- $dbt-core - For dbt installation, configuration, and package management
278- $snowflake-cli - For executing SQL and managing Snowflake objects
279
280---
281
282## Supported Source Database
283
284| Database | Key Considerations |
285| ----------------------------- | -------------------------------------------------------------------------------- |
286| **Hive / Spark / Databricks** | External tables, PARTITIONED BY, LATERAL VIEW, file formats (PARQUET, ORC), UDFs |
287
288## Translation References
289
290Detailed syntax translation guides are available in the `translation-references/` folder.
291
292> **Copyright Notice:** The translation reference documentation in this repository is derived from
293> [Snowflake SnowConvert Documentation](https://docs.snowflake.com/en/migrations/snowconvert-docs)
294> and is © Copyright Snowflake Inc. All rights reserved. Used for reference purposes only.
295
296### Reference Index
297
298- [Built In Functions](translation-references/hive-built-in-functions.md)
299- [Data Types](translation-references/hive-data-types.md)
300- [Ddls Create External Table](translation-references/hive-ddls-create-external-table.md)
301- [Ddls Create View](translation-references/hive-ddls-create-view.md)
302- [Ddls Readme](translation-references/hive-ddls-readme.md)
303- [Ddls Select](translation-references/hive-ddls-select.md)
304- [Ddls Tables](translation-references/hive-ddls-tables.md)
305- [Overview (README)](translation-references/hive-readme.md)
306- [Subqueries](translation-references/hive-subqueries.md)