BAD_ARGUMENTS error

This error usually means you're passing invalid arguments to a function. Check the function documentation to see what arguments it expects and what types they should be.

The BAD_ARGUMENTS error in ClickHouse (and Tinybird) happens when you pass invalid arguments to a function. This can occur due to wrong argument types, invalid values, or incorrect argument order. Unlike NUMBER_OF_ARGUMENTS_DOESNT_MATCH, this error indicates that the arguments are present but invalid.

What causes this error

You'll typically see it when:

  • Passing arguments of the wrong type to a function
  • Using invalid values for function parameters
  • Providing arguments in the wrong order
  • Using unsupported argument combinations
  • Passing NULL values where not allowed

Example errors

Fails: wrong argument type
SELECT substring(123, 1, 5)
Fails: invalid argument value
SELECT toDate('invalid-date')
Fails: wrong argument order
SELECT round(3.14159, 2, 'decimal')
Fails: unsupported argument combination
SELECT toDateTime('2023-01-01', 'UTC', 'invalid_format')

How to fix it

Check argument types

Ensure arguments match the expected types:

Fix: correct argument type
SELECT substring('hello world', 1, 5)

Use valid argument values

Provide valid values for function parameters:

Fix: valid argument value
SELECT toDate('2023-01-01')

Check argument order

Verify arguments are in the correct order:

Fix: correct argument order
SELECT round(3.14159, 2)

Use proper argument combinations

Follow function documentation for valid combinations:

Fix: valid argument combination
SELECT toDateTime('2023-01-01 12:00:00')

Common patterns and solutions

ClickHouse functions rarely accept optional named parameters. You must provide all required arguments in the correct order.

Function argument constraints

FunctionArgumentsConstraints
substring(str, start, length)string, start, lengthstart ≥ 1, length ≥ 0
round(number, scale)number, scalescale ≥ 0
toDate(string)date stringISO format (YYYY-MM-DD)
toDateTime(string)datetime stringISO format (YYYY-MM-DD HH:MM:SS)
formatDateTime(datetime, format)datetime, formatValid format string
addDays(date, days)date, daysdays can be negative

String functions

Common string function argument patterns:

String function arguments
SELECT substring('hello world', 1, 5)  -- string, start, length
SELECT replace('hello world', 'world', 'clickhouse')  -- string, old, new
SELECT concat('hello', ' ', 'world')  -- variable arguments
SELECT trim('  hello  ')  -- single argument

Date and time functions

Date function argument patterns:

Date function arguments
SELECT toDate('2023-01-01')  -- date string
SELECT toDateTime('2023-01-01 12:00:00')  -- datetime string
SELECT formatDateTime(now(), '%Y-%m-%d')  -- datetime, format
SELECT addDays(toDate('2023-01-01'), 7)  -- date, days

Mathematical functions

Math function argument patterns:

Math function arguments
SELECT abs(-10)  -- single numeric argument
SELECT round(3.14159, 2)  -- number, precision
SELECT power(2, 3)  -- base, exponent
SELECT sqrt(16)  -- single numeric argument

Aggregate functions

Aggregate function argument patterns:

Aggregate function arguments
SELECT count(*)  -- expression or *
SELECT sum(value)  -- numeric expression
SELECT avg(value)  -- numeric expression
SELECT max(timestamp)  -- any expression

Advanced solutions

Using try functions for error handling

Use try functions to handle invalid arguments gracefully:

Try functions for error handling
SELECT try(toInt32(text_column)) as safe_int FROM events
SELECT try(toDate(date_string)) as safe_date FROM events
SELECT try(toBool(bool_string)) as safe_bool FROM events

Validating arguments before use

Check argument validity before passing to functions:

Argument validation
SELECT CASE 
    WHEN text_column REGEXP '^[0-9]+$' THEN toInt32(text_column)
    ELSE NULL
END as validated_int FROM events

Using default values

Provide default values for potentially invalid arguments:

Default values
SELECT toInt32OrZero(text_column) as safe_int FROM events
SELECT toDateOrZero(date_string) as safe_date FROM events
SELECT toBoolOrFalse(bool_string) as safe_bool FROM events

Function-specific solutions

String manipulation functions

String function patterns
-- substring(string, start, length)
SELECT substring('hello world', 1, 5)  -- 'hello'

-- replace(string, old, new)
SELECT replace('hello world', 'world', 'clickhouse')  -- 'hello clickhouse'

-- concat(string1, string2, ...)
SELECT concat('hello', ' ', 'world')  -- 'hello world'

Date and time functions

Date function patterns
-- toDate(string)
SELECT toDate('2023-01-01')  -- 2023-01-01

-- toDateTime(string)
SELECT toDateTime('2023-01-01 12:00:00')  -- 2023-01-01 12:00:00

-- formatDateTime(datetime, format)
SELECT formatDateTime(now(), '%Y-%m-%d')  -- Current date formatted

Type conversion functions

Type conversion patterns
-- toInt32(string)
SELECT toInt32('123')  -- 123

-- toFloat64(string)
SELECT toFloat64('123.45')  -- 123.45

-- toString(number)
SELECT toString(123)  -- '123'

Tinybird-specific notes

In Tinybird, this error often occurs when:

  • Using functions in Pipes with invalid arguments
  • Working with Materialized Views that use functions incorrectly
  • Processing data with inconsistent types
  • Using functions from other database systems

To debug in Tinybird:

  1. Check function documentation for correct argument patterns
  2. Test functions in the Query Builder first
  3. Validate data types before using functions
  4. Use try functions for robust error handling

Best practices

Function usage

  • Always check function documentation before use
  • Test functions with simple examples first
  • Use try functions for potentially problematic operations
  • Validate arguments before passing to functions

Error handling

  • Implement proper error handling for function calls
  • Use try functions for robust operations
  • Provide fallback values for invalid arguments
  • Log function errors for debugging

Data validation

  • Validate data types before function calls
  • Use appropriate type conversion functions
  • Handle NULL values properly
  • Test functions with sample data

See also