Lakehouse Data Type Conversion Guide
Overview
During data processing, it is often necessary to convert data from one type to another. Singdata Lakehouse provides multiple type conversion syntaxes, including standard CAST, shorthand ::, safe conversion TRY_CAST, and automatic implicit conversion. This guide categorizes usage by common business scenarios to help you quickly master efficient and safe type conversion methods.
Quick Navigation
- Basic Type Conversion -- Use CAST and :: for explicit conversion
- Safe Type Conversion -- Use TRY_CAST to handle dirty data
- Date/Time Conversion -- Convert between strings and date/timestamp
- JSON Data Conversion -- Parse strings into JSON type
- Implicit Conversion Rules -- Understand the boundaries of automatic conversion
SQL Commands Covered
| Command/Function | Purpose | Applicable Scenario |
|---|---|---|
CAST(expr AS type) | Standard type conversion | General type conversion, standard SQL compatible |
expr::type | Shorthand type conversion | Quick conversion, more concise code |
TRY_CAST(expr AS type) | Safe type conversion | Handle data that may fail conversion, returns NULL instead of error |
PARSE_JSON(str) | String to JSON | Parse JSON strings into JSON type |
TO_DATE(str, fmt) | Formatted date conversion | Convert non-standard format strings to date |
Prerequisites
The following examples use a simulated mixed-type table mixed_data:
Basic Type Conversion
Use CAST or :: for explicit type conversion. Both are functionally equivalent; the :: syntax is more concise.
Result:
| id | str_val | cast_int | shorthand_int |
|---|---|---|---|
| 1 | 100 | 100 | 100 |
| 2 | 200 | 200 | 200 |
| 4 | 300 | 300 | 300 |
Safe Type Conversion
When data may contain unconvertible values, use TRY_CAST to avoid query errors. Failed conversions return NULL.
Result:
| id | str_val | safe_int |
|---|---|---|
| 1 | 100 | 100 |
| 2 | 200 | 200 |
| 3 | abc | NULL |
| 4 | 300 | 300 |
Handling TRY_CAST Results
TRY_CAST returns NULL on failure, which can be filtered directly with IS NULL:
Result:
| id | str_val | safe_int |
|---|---|---|
| 1 | 100 | 100 |
| 2 | 200 | 200 |
| 4 | 300 | 300 |
Date/Time Conversion
Convert strings to date or timestamp types, supporting standard and custom formats.
Result:
| id | date_val | std_date |
|---|---|---|
| 1 | 2024-06-01 | 2024-06-01 |
| 4 | 2024-06-04 | 2024-06-04 |
Custom Format Conversion
For non-standard formats, use TO_DATE with a format template:
JSON Data Conversion
Parse JSON strings into Lakehouse's native JSON type for querying with JSON functions.
Result:
| id | json_data | name |
|---|---|---|
| 1 | {"name":"Alice","age":30} | Alice |
Implicit Conversion Rules
Lakehouse performs automatic implicit conversion in certain scenarios, but explicit conversion is recommended for code readability and stability.
Supported Implicit Conversions
- Between numeric types:
INTtoDOUBLE(widening precision) - String to date: In
WHEREconditions,date_col = '2024-06-01'is automatically converted
Unsupported Implicit Conversions
- Write operations: In
INSERT INTO, strings cannot be implicitly converted toDATE,TIMESTAMP,JSON, orBINARY; explicit conversion is required. - Complex types:
STRINGcannot be implicitly converted toJSONorARRAY.
Clean Up Test Data
After completing type conversion verification, it is recommended to clean up test tables:
Important Notes
- TRY_CAST Return Value: Returns
NULLon conversion failure; can be filtered directly withIS NULL. - Strict Date Format:
CASTfor date conversion requires standard format (yyyy-MM-dd); non-standard formats needTO_DATE(str, fmt). - Precision Loss:
DOUBLEtoINTtruncates the decimal part;DECIMALtoINTrounds. - JSON Field Name Case:
FROM_JSONforce-lowercases field names when parsing schemas. UsePARSE_JSONto preserve case. - Date Literals: Prefer
DATE '2024-06-01'orTIMESTAMP '2024-06-01 10:00:00'syntax, which is more concise thanCAST.
