c77_mvc/grok_validate_order_by_columns-readme.md

64 lines
2.2 KiB
Markdown

# Function: grok_validate_order_by_columns
## Overview
This function validates that specified order-by columns exist in a source table and contain data that can be parsed as timestamps, ensuring they can be used for deterministic ordering.
## Schema
`config.grok_validate_order_by_columns`
## Parameters
- `source_schema` (text): Schema containing the source table
- `source_table` (text): Name of the source table
- `order_by_columns` (text[]): Array of column names to validate
## Return Value
Returns a text array containing warning messages for any issues found:
```
{
"Warning: column_name not found in schema.table",
"Warning: column_name contains unparseable timestamp data: error message"
}
```
## Description
This function validates columns intended for use in ORDER BY clauses, particularly for generating synthetic keys in materialized views. It performs two types of validation:
1. **Existence Check**: Verifies each column exists in the specified table
2. **Timestamp Parsing**: Tests if each column's data can be parsed as a timestamp
For timestamp parsing, the function attempts to convert the column data using:
```sql
TO_TIMESTAMP(SUBSTRING(NULLIF(column, ''), 1, 19), 'YYYY-MM-DD HH24:MI:SS')
```
This validation approach ensures that:
- Columns are valid for the source table
- Timestamp columns can be parsed consistently
- The ORDER BY clause will produce deterministic results
## Timestamp Parsing Details
The timestamp parsing logic:
- Uses NULLIF to handle NULL values
- Takes only the first 19 characters using SUBSTRING
- Uses a fixed format of 'YYYY-MM-DD HH24:MI:SS'
This standardized parsing ensures consistent ordering behavior regardless of the actual format stored in the column.
## Error Handling
The function collects warnings without failing, allowing for a complete validation report:
- Missing columns generate a warning
- Unparseable timestamp data generates a warning with the specific error
- If an unexpected error occurs, it returns a general error message
## Dependencies
This function is likely called by other functions that create materialized views to validate order-by columns before using them.
## Usage Example
```sql
SELECT config.grok_validate_order_by_columns(
'public',
'customers',
ARRAY['created_at', 'updated_at']
);
```