OpenAICloudflare Developer PlatformMar 23, 2026, 12:00 AM

R2 SQL - R2 SQL now supports over 190 new functions, expressions, and complex types

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

R2 SQL now supports over 190 new functions, expressions, and complex types

Key Points

  • Adds 163 scalar and 33 aggregate functions
  • Supports CASE, CTEs, EXPLAIN, and full expression support
  • Struct/array/map access with 46 array functions

Summary

R2 SQL (Cloudflare's serverless, distributed analytics engine for querying Apache Iceberg tables in R2 Data Catalog) now supports an expanded SQL grammar so you can run richer analytical queries in-place. This release adds CASE expressions, column aliases in all clauses, arithmetic and casting across clauses, 163 scalar functions, 33 aggregate functions, EXPLAIN, Common Table Expressions (CTEs), and full struct/array/map access including 46 array functions.

Key Points

  • New functions: 163 scalar functions (math, string, datetime, regex, crypto, encoding, type-inspection) and 33 aggregate functions (variance, stddev, correlation, regression, bitwise, boolean, positional, etc.).
  • Expressions & syntax: CASE (searched and simple), column aliases (AS) in all clauses, arithmetic in SELECT, WHERE, GROUP BY, HAVING, and ORDER BY, plus CAST/TRY_CAST/:: shorthand and EXTRACT.
  • Complex types: bracket notation for struct fields, map key/value extraction, and 46 array functions (e.g., array_has, array_to_string).
  • CTEs: WITH ... AS and chained CTEs are supported. Limitation: all CTEs must reference the same single table.
  • Tools & workflow: EXPLAIN is available for query-plan inspection; queries run directly on Iceberg tables in R2 Data Catalog so exporting data is not required.
  • Recommended action: review the SQL reference for the full function list and the Limitations and best practices guide before authoring or migrating complex queries.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

R2 SQL — 190以上の新しい関数、式、および複合型をサポート

R2 SQL — 190以上の新しい関数、式、複合型をサポート

公開日: 2026-03-23

R2 SQL は拡張された SQL 文法をサポートするようになり、データをエクスポートせずによりリッチな分析クエリを記述できるようになりました。本リリースでは、CASE 式、列エイリアス、句内での算術演算、163 のスカラー関数、33 の集約関数、EXPLAIN、Common Table Expressions (CTEs)、および構造体/配列/マップの完全なアクセスが追加されました。

R2 SQL は、R2 Data Catalog に格納された Apache Iceberg ↗ テーブルを照会するための、Cloudflare のサーバーレス分散型分析クエリエンジンです。本ページではサポートされている SQL 構文を文書化します。

ハイライト

  • 列エイリアス: SELECT col AS alias がすべての句で使用可能になりました。
  • CASE 式: SQL 内での条件ロジック(searched と simple の両方)をサポートします。
  • スカラー関数: math, string, datetime, regex, crypto, encoding, type inspection の各カテゴリを含む 163 の新しい関数。
  • 集約関数: 分散・標準偏差・相関・回帰などの統計関数、ビット演算、ブール、位置ベースの集約が既存の基本的・近似関数に加わりました(合計 33 関数)。
  • 複合型: ブラケット表記で struct フィールドを照会可能、46 の配列関数、マップのキー/値抽出。
  • Common Table Expressions (CTEs): WITH ... AS で名前付きの一時結果セットを定義できます。チェーンされた CTE をサポートします。すべての CTE は同一の単一テーブルを参照する必要があります。
  • 完全な式サポート: 算術、型変換(CAST, TRY_CAST, :: ショートハンド)、および EXTRACTSELECT, WHERE, GROUP BY, HAVING, ORDER BY で使用可能。
  • EXPLAIN: クエリプランの説明を取得できます。

CASE 式と統計集約の例

SELECT
  source,
  CASE
    WHEN AVG(price) > 30 THEN 'premium'
    WHEN AVG(price) > 10 THEN 'mid-tier'
    ELSE 'budget'
  END AS tier,
  round(stddev(price), 2) AS price_volatility,
  approx_percentile_cont(price, 0.95) AS p95_price
FROM my_namespace.sales_data
GROUP BY source

構造体と配列のアクセス

SELECT
  product_name,
  pricing['price'] AS price,
  array_to_string(tags, ', ') AS tag_list
FROM my_namespace.products
WHERE array_has(tags, 'Action')
ORDER BY pricing['price'] DESC
LIMIT 10

チェーンされた CTE を用いた時系列分析

WITH monthly AS (
  SELECT
    date_trunc('month', sale_timestamp) AS month,
    department,
    COUNT(*) AS transactions,
    round(AVG(total_amount), 2) AS avg_amount
  FROM my_namespace.sales_data
  WHERE sale_timestamp BETWEEN '2025-01-01T00:00:00Z' AND '2025-12-31T23:59:59Z'
  GROUP BY date_trunc('month', sale_timestamp), department
),
ranked AS (
  SELECT
    month,
    department,
    transactions,
    avg_amount,
    CASE
      WHEN avg_amount > 1000 THEN 'high-value'
      WHEN avg_amount > 500 THEN 'mid-value'
      ELSE 'standard'
    END AS tier
  FROM monthly
  WHERE transactions > 100
)
SELECT * FROM ranked
ORDER BY month, avg_amount DESC

詳細な関数リファレンスと構文の詳細は SQL reference を参照してください。制限事項とベストプラクティスについては Limitations and best practices を参照してください。