Limitations, troubleshooting, and common pitfalls for R2 SQL.
Cannot call R2 SQL from Workers/Pages code - no binding exists.
// ❌ This doesn't exist
export default {
async fetch(request, env) {
const result = await env.R2_SQL.query("SELECT * FROM table"); // Not possible
return Response.json(result);
}
};
Solutions:
Can only order by:
Cannot order by regular non-partition columns.
-- ✅ Valid: ORDER BY partition key
SELECT * FROM logs.requests ORDER BY timestamp DESC LIMIT 100;
-- ✅ Valid: ORDER BY aggregation
SELECT region, SUM(amount) FROM sales.transactions
GROUP BY region ORDER BY SUM(amount) DESC;
-- ❌ Invalid: ORDER BY non-partition column
SELECT * FROM logs.requests ORDER BY user_id;
-- ❌ Invalid: ORDER BY alias (must repeat function)
SELECT region, SUM(amount) as total FROM sales.transactions
GROUP BY region ORDER BY total; -- Use ORDER BY SUM(amount)
Check partition spec: DESCRIBE namespace.table_name
| Feature | Supported | Notes |
|---|---|---|
| SELECT, WHERE, GROUP BY, HAVING | ✅ | Standard support |
| COUNT, SUM, AVG, MIN, MAX | ✅ | Standard aggregations |
| ORDER BY partition/aggregation | ✅ | See above |
| LIMIT | ✅ | Max 10,000 |
| Column aliases | ❌ | No AS alias |
| Expressions in SELECT | ❌ | No col1 + col2 |
| ORDER BY non-partition | ❌ | Fails at runtime |
| JOINs, subqueries, CTEs | ❌ | Denormalize at write time |
| Window functions, UNION | ❌ | Use external engines |
| INSERT/UPDATE/DELETE | ❌ | Use PyIceberg/Pipelines |
| Nested columns, arrays, JSON | ❌ | Flatten at write time |
Workarounds:
Cause: Typo, column doesn't exist, or case mismatch
Solution: DESCRIBE namespace.table_name to check schema
-- ❌ Wrong types
WHERE status = '200' -- string instead of integer
WHERE timestamp > '2025-01-01' -- missing time/timezone
-- ✅ Correct types
WHERE status = 200
WHERE timestamp > '2025-01-01T00:00:00Z'
Cause: Ordering by non-partition column
Solution: Use partition key, aggregation, or remove ORDER BY. Check: DESCRIBE table
# Check/set token
echo $WRANGLER_R2_SQL_AUTH_TOKEN
export WRANGLER_R2_SQL_AUTH_TOKEN=<your-token>
# Or .env file
echo "WRANGLER_R2_SQL_AUTH_TOKEN=<your-token>" > .env
-- Verify catalog and tables
SHOW DATABASES;
SHOW TABLES IN namespace_name;
Enable catalog: npx wrangler r2 bucket catalog enable <bucket>
Max LIMIT is 10,000. For pagination, use WHERE filters with partition keys.
Debug steps:
SELECT COUNT(*) FROM table - verify data existsSELECT * FROM table LIMIT 10 - inspect actual data/typesCauses: Too many partitions, large LIMIT, no filters, small files
-- ❌ Slow: No filters
SELECT * FROM logs.requests LIMIT 10000;
-- ✅ Fast: Filter on partition key
SELECT * FROM logs.requests
WHERE timestamp >= '2025-01-15T00:00:00Z' AND timestamp < '2025-01-16T00:00:00Z'
LIMIT 1000;
-- ✅ Faster: Multiple filters
SELECT * FROM logs.requests
WHERE timestamp >= '2025-01-15T00:00:00Z' AND status = 404 AND method = 'GET'
LIMIT 1000;
File optimization:
Solution: Add restrictive WHERE filters, reduce time range, query smaller intervals
-- ❌ Times out: Year-long aggregation
SELECT status, COUNT(*) FROM logs.requests
WHERE timestamp >= '2024-01-01T00:00:00Z' GROUP BY status;
-- ✅ Faster: Month-long aggregation
SELECT status, COUNT(*) FROM logs.requests
WHERE timestamp >= '2025-01-01T00:00:00Z' AND timestamp < '2025-02-01T00:00:00Z'
GROUP BY status;
Avoid: High-cardinality keys (user_id), >10,000 partitions
from pyiceberg.partitioning import PartitionSpec, PartitionField
from pyiceberg.transforms import DayTransform
PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=DayTransform(), name="day"))
Combine filters with AND for more pruning
-- Good
WHERE timestamp >= '2025-01-15T00:00:00Z' AND status = 404 AND method = 'GET' LIMIT 100
'GET' not GET'2025-01-01T00:00:00Z' not '2025-01-01''2025-01-15' not '01/15/2025'roll_file_time: 10, Prod roll_file_time: 300+zstdnpx wrangler r2 bucket catalog enable <bucket> - Verify catalogecho $WRANGLER_R2_SQL_AUTH_TOKEN - Check tokenSHOW DATABASES - List namespacesSHOW TABLES IN namespace - List tablesDESCRIBE namespace.table - Check schemaSELECT COUNT(*) FROM namespace.table - Verify dataSELECT * FROM namespace.table LIMIT 10 - Test simple query