Skip to content

Add Support for Dynamic SQL Macros for Flexible Column Selection #14512

@berkaysynnada

Description

@berkaysynnada

Is your feature request related to a problem or challenge?

People may need to write flexible SQL queries that adapt to different table schemas without hardcoding column names.

Query reusability is very limited currently, and creating dynamic queries requires external templating. A built-in mechanism to define and use dynamic SQL macros would significantly enhance the expressiveness and reusability

Describe the solution you'd like

Introduce support for dynamic SQL macros that allow users to parameterize column selection. For example, a macro could accept lists of columns to include or exclude and then automatically select the appropriate columns from a given table or CTE. An example usage might look like this:

CREATE OR REPLACE MACRO dynamic_select(
    include_cols, 
    exclude_cols 
) AS TABLE (
    FROM source_table
    SELECT
        COLUMNS(col -> 
            (list_contains(include_cols, col))
            AND
            (NOT list_contains(exclude_cols, col))
        )
);

WITH source_table AS (
    SELECT * FROM employees
)
FROM dynamic_select(
    ['id', 'name', 'department'], 
    ['salary']                 
);

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions