Repository navigation
Expand file tree
/
Copy pathdata_transformation.sql
More file actions
75 lines (64 loc) · 2.29 KB
/
Copy pathdata_transformation.sql
File metadata and controls
75 lines (64 loc) · 2.29 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
/*
* AI Data Transformation Demo
*
* Scenario:
* Cleaning and standardizing messy data using GenAI.
*
* Requirements:
* - Dremio environment with Generative AI features enabled.
*/
-------------------------------------------------------------------------------
-- 0. SETUP: Create Messy Data
-------------------------------------------------------------------------------
-- PREREQUISITE: Ensure a space named 'RetailDB' exists.
CREATE FOLDER IF NOT EXISTS RetailDB.AI_Lab;
CREATE TABLE IF NOT EXISTS RetailDB.AI_Lab.Messy_Contacts (
ID INT,
Raw_Address VARCHAR,
Raw_Phone VARCHAR
);
INSERT INTO RetailDB.AI_Lab.Messy_Contacts VALUES
(1, '123 main st, springfield, il', '(555) 123-4567'),
(2, '456 Elm Avenue, Apt 4B, New York NY 10001', '555.987.6543'),
(3, 'PO Box 789, austin texas', '5551112222');
-------------------------------------------------------------------------------
-- 1. ADDRESS NORMALIZATION
-- Standardize addresses to a proper format.
-------------------------------------------------------------------------------
SELECT
ID,
Raw_Address,
AI_GENERATE(
'Format the following address into a standardized single-line format (Street, City, State ZIP). Address: ' || Raw_Address
) AS Standardized_Address
FROM RetailDB.AI_Lab.Messy_Contacts;
-------------------------------------------------------------------------------
-- 2. PHONE NUMBER FORMATTING
-- Convert all phone numbers to (XXX) XXX-XXXX.
-------------------------------------------------------------------------------
SELECT
ID,
Raw_Phone,
AI_GENERATE(
'Reformat this phone number to (XXX) XXX-XXXX format. Return only the number. Input: ' || Raw_Phone
) AS Formatted_Phone
FROM RetailDB.AI_Lab.Messy_Contacts;
-------------------------------------------------------------------------------
-- 3. DATA EXTRACTION
-- Extract City and State into separate columns using WITH SCHEMA.
-------------------------------------------------------------------------------
SELECT
ID,
Raw_Address,
t.minc.City,
t.minc.State
FROM (
SELECT
ID,
Raw_Address,
AI_GENERATE(
'Extract the City and State from this address.',
Raw_Address
) WITH SCHEMA (City VARCHAR, State VARCHAR) AS minc
FROM RetailDB.AI_Lab.Messy_Contacts
) t;