We have been working on SQLX for the data warehousing use case, allowing you to embed JavaScript into your SQL queries and making things like code re-use much easier while fitting in with your existing SQL dialect, might be of interest - https://docs.dataform.co/guides/sqlx. Some of the concepts in there are specific to our framework, some are not.
For data warehousing, create more views! They are IMO the right level of abstraction for encapsulating common business logic, data definitions, joins etc, making composing downstream queries much easier. Managing lots of views like this requires some investment in a data modelling tooling tool however (Dataform, DBT etc).
For generating queries used against production databases for building user applications, none of this applies.