Templates
Overview
Sling uses database templates to generate SQL statements for operations like creating tables, inserting data, querying metadata, and type mappings. These templates are built-in for each database connector (PostgreSQL, Snowflake, MySQL, SQL Server, etc.).
Starting in v1.5.3, you can customize these templates by creating user override files in ~/.sling/templates/ directory (or env var $SLING_HOME_DIR/templates). This allows you to:
Customize table creation with database-specific options (e.g., compression, partitioning)
Modify metadata queries for special schema configurations
Override type mappings for your data types
Add database-specific optimizations
File Location and Naming
User template override files must be placed in:
~/.sling/templates/{database_type}.yamlWhere {database_type} matches your database connection type exactly.
Examples
PostgreSQL:
~/.sling/templates/postgres.yamlSnowflake:
~/.sling/templates/snowflake.yamlMySQL:
~/.sling/templates/mysql.yamlSQL Server:
~/.sling/templates/sqlserver.yamlBigQuery:
~/.sling/templates/bigquery.yamlRedshift:
~/.sling/templates/redshift.yaml
To find your database type, run:
Template Structure
Each template file contains up to 7 sections. You only need to include the sections you want to override:
1. core - SQL DDL/DML Templates
core - SQL DDL/DML TemplatesDefines SQL statements for basic operations:
2. metadata - Schema Information Queries
metadata - Schema Information QueriesQueries to retrieve database metadata:
3. analysis - Analytical Queries
analysis - Analytical QueriesUsed for data profiling and validation:
4. function - Database-Specific Functions
function - Database-Specific FunctionsCustom functions available in templates:
5. general_type_map - Generic to Database Type Mapping
general_type_map - Generic to Database Type MappingMaps generic data types to database-specific types:
6. native_type_map - Database Type to Generic Mapping
native_type_map - Database Type to Generic MappingMaps database-specific types back to generic types:
7. variable - Database Variables
variable - Database VariablesDatabase-specific configuration variables:
Examples
Example 1: Custom PostgreSQL Table Creation with Compression
Add compression and optimization to PostgreSQL table creation:
File: ~/.sling/templates/postgres.yaml
This overrides only the create_table template. All other PostgreSQL templates remain unchanged.
Example 2: Custom Snowflake Type Mappings
Override type mappings for Snowflake to use specific precision:
File: ~/.sling/templates/snowflake.yaml
Example 3: Modified Metadata Query for Custom Schemas
Customize the columns query for a specific schema configuration:
File: ~/.sling/templates/snowflake.yaml
Example 4: MySQL with Specific Collation
File: ~/.sling/templates/mysql.yaml
How It Works
Templates are loaded in a layered hierarchy, with each layer overwriting nested keys from the previous:
Base Template: Sling first loads
base.yaml, which contains common SQL templates shared across all databasesDatabase-Specific Template: The database-specific template (e.g.,
postgres.yaml,snowflake.yaml) is loaded next, overwriting any nested keys from the base template with database-specific implementationsUser Override: If a user template exists at
~/.sling/templates/{database_type}.yaml, it is merged last, overwriting any nested keys from the previous layersPartial Overrides: Only the keys you define override the built-in templates - all other values remain unchanged
Caching: The merged template is cached for performance
Debug Logging: With
--debugflag, you can see when overrides are loaded
Last updated
Was this helpful?