prestodb/presto · error · PrestoException
INVALID_FUNCTION_ARGUMENT
INVALID_FUNCTION_ARGUMENT
Error message
Not a valid base-%d number: %s
What it means
The from_base(varchar, radix) function parses a string as a number in the given base using Long.parseLong. If the string contains characters invalid for that radix or overflows a signed 64-bit long, it throws INVALID_FUNCTION_ARGUMENT 'Not a valid base-%d number: %s'.
Source
Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java:1616
@SqlType("varchar(64)")
public static Slice toBase(@SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.BIGINT) long radix)
{
checkRadix(radix);
return utf8Slice(Long.toString(value, (int) radix));
}
@Description("convert a string in the given base to a number")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long fromBase(@SqlType("varchar(x)") Slice value, @SqlType(StandardTypes.BIGINT) long radix)
{
checkRadix(radix);
try {
return Long.parseLong(value.toStringUtf8(), (int) radix);
}
catch (NumberFormatException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Not a valid base-%d number: %s", radix, value.toStringUtf8()), e);
}
}
private static void checkRadix(long radix)
{
checkCondition(radix >= MIN_RADIX && radix <= MAX_RADIX,
INVALID_FUNCTION_ARGUMENT, "Radix must be between %d and %d", MIN_RADIX, MAX_RADIX);
}
@Description("The bucket number of a value given a lower and upper bound and the number of buckets")
@ScalarFunction("width_bucket")
@SqlType(StandardTypes.BIGINT)
public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @SqlType(StandardTypes.DOUBLE) double bound1, @SqlType(StandardTypes.DOUBLE) double bound2, @SqlType(StandardTypes.BIGINT) long bucketCount)
{
checkCondition(bucketCount > 0, INVALID_FUNCTION_ARGUMENT, "bucketCount must be greater than 0");
checkCondition(!isNaN(operand), INVALID_FUNCTION_ARGUMENT, "operand must not be NaN");
checkCondition(isFinite(bound1), INVALID_FUNCTION_ARGUMENT, "first bound must be finite");
checkCondition(isFinite(bound2), INVALID_FUNCTION_ARGUMENT, "second bound must be finite");View on GitHub (pinned to 55bb57d202)
Solutions
- Validate/clean the string before parsing: trim whitespace, ensure only digits valid for the radix (regex '^[0-9a-fA-F]+$' style for the radix).
- Wrap with TRY: SELECT TRY(from_base(value, 16)) to return NULL for invalid rows instead of failing the query.
- Confirm the radix matches the actual encoding of the data.
- Check magnitude fits in BIGINT; use from_base on halves or handle larger values differently.
Example fix
-- before SELECT from_base(value, 16) FROM t; -- value='1Z3' fails -- after SELECT TRY(from_base(trim(value), 16)) AS parsed FROM t WHERE regexp_like(value, '^[0-9A-Fa-f]+$');
Defensive patterns
Strategy: validation
Validate before calling
-- radix 16 example: validate characters and length before from_base SELECT * FROM t WHERE value IS NULL OR NOT regexp_like(trim(value), '^[0-9A-Fa-f]+$') OR length(trim(value)) > 15;
Try / catch
SELECT TRY(from_base(value, 16)) AS parsed FROM t; -- NULL for invalid strings
Prevention
- Match the radix to the actual data encoding; confirm upstream format docs.
- Trim inputs and reject empty/whitespace strings at ingestion.
- Validate with a regex per radix before parsing; cap length to stay within BIGINT range.
When it happens
Trigger: SELECT from_base('12g', 10); from_base('z', 10); from_base('9223372036854775808', 10) (BIGINT overflow); whitespace or sign-only strings; wrong radix/data pairing (binary string parsed as base 10).
Common situations: Parsing user-supplied encoded IDs (hex/base36) with mixed-case or stray characters; decoding columns where some rows hold decimal values while the query uses base 16; upstream systems writing unpadded/malformed numeric strings.
Related errors
- INVALID_TABLE_PROPERTY
- INVALID_FUNCTION_ARGUMENT
- QualifiedObjectName should have exactly 3 parts, found %s: %
- Invalid decimal value '{stringValue}'
- Bad type signature: '%s'
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/8767d728b530dd14.
Report an issue: GitHub.