Skip to content

Error when using a macro within if() (with Python client) #727

Closed

Description

@Hunknown

What happens?

When using a macro within an if() statement (with the Python client), the following error is raised:

BinderException: Binder Error: Macro "if"() does not support the supplied arguments. You might need to add explicit type casts.
Candidate macros:
	"if"(a, b, c)

I've tested with duckdb CLI client and I don't get this error.

To Reproduce

import duckdb

duckdb.sql(
  """
  create macro test() as 'a';
  select 
    a: 1, 
    if(a is null, 'a', test()),
  """
)

Note that if you replace the condition by a constant, e.g. true or 1=1, there's no error.

OS:

Windows

DuckDB Version:

2.0.0

DuckDB Client:

Python

Hardware:

No response

Full Name:

Maxime Nguyen

Affiliation:

CMAM

Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?

  • Yes, I have

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant data sets for reproducing the issue?

Yes

Activity

  1. transferred this issue fromduckdb/duckdbon Oct 28, 2026
  2. evertlammerts commented on Oct 29, 2026

    @evertlammerts
    Member

    Thanks for the report and the repro, @Hunknown.

    I tried this on DuckDB 2.0.0 and on our current 2.0.1 and 2.1 dev builds. It returns (1, 'a') every time. I also tried con.sql(), con.execute(), a prepared statement, Windows line endings, and the relation methods (printing it, .df(), .arrow(), .pl()). None of them raise the error. My runs were on Linux, not Windows, though the binder code involved does not depend on the platform.

    The error means the if macro saw arguments it could not match. That usually happens when one of the arguments carries a name, or when the number of arguments is not three. So I suspect something in your session or your real query differs from the snippet.

    Can you try to:

    1. Run the snippet as a script in a fresh Python process, not in an open notebook or REPL, and tell me whether it still fails.
    2. Paste the output of import duckdb; print(duckdb.version) from that same environment.
    3. Paste the full traceback.
    4. Are you connecting to a database file or using any extensions? If so, which ones?

    Until then, case when a is null then 'a' else test() end is a workaround.

  3. Hunknown commented on Oct 30, 2026

    @Hunknown
    Author

    @evertlammerts Thank you for the investigation!

    Indeed, I should have tried in a new environment without any settings/extensions. Thanks to your advices, I managed to find where the issue is coming from.

    I'm using the community extension mssql to attach my SQL Server database, and it seems that it causes the if macro to not work properly.

    I'll forward the issue to them, thanks again!

    EDIT: After raising the issue on the extension repo, it appears that it's actually coming from core duckdb 2.0, but it should be fixed in 2.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions