Michele Martucci
11/08/2025, 6:28 PMkapa.ai
11/08/2025, 6:28 PMkapa.ai
11/08/2025, 6:28 PMSQL compilation error: inconsistent data type for result columns for set operator input branches, expected BINARY(16), got VARCHAR(16777216) is caused by a mismatch in the data types of the id column when performing a UNION or similar set operation in Snowflake. Specifically, one branch is producing a BINARY(16) (from md5_binary), while another is producing a VARCHAR. This issue is known to occur in Elementary when the macro or function used to generate the id column differs in type between different parts of the query. For example, if one part uses md5_binary() (which returns a binary type) and another uses md5() or a string-based hash (which returns a varchar), Snowflake will throw this error during a union. A similar issue was reported in the Elementary community, where a hash macro was overridden by another package, causing the type to change from varchar to binary. The solution in that case was to ensure that the correct macro is used and, if necessary, adjust the dispatch order so that Elementary's version takes precedence. Here’s the relevant discussion: > "Another package is overriding the hash() macro, making it binary. I will try to solve it with dispatch order." [Snowflake error: union the ID column, once as binary and once as varchar](https://slack.com/archives/C02CTC89LAX/p1735815549097269) How to resolve: - Ensure that all branches of your union produce the id column with the same data type. For Elementary, this is typically VARCHAR. - Check if any other dbt packages in your project override the hash() or generate_surrogate_key() macros, and if so, use dbt's [dispatch mechanism](https://docs.getdbt.com/docs/build/custom-macros#dispatch) to ensure Elementary's implementation is used. - If you are using md5_binary, consider switching to md5 or casting the result to VARCHAR to match the expected type. Example fix: Replace sql md5_binary(...) with sql md5(...) or sql cast(md5_binary(...) as varchar) in all relevant places, so the type is consistent. If you continue to have issues, review your dbt project for any custom or overridden macros that might affect hashing or surrogate key generation, and ensure consistency across all test and model definitions. If you need more specific guidance, please provide the full SQL for all branches of the union, and check for any custom macro overrides in your dbt project or dependencies.