#61514function format_bytes(bytes) to format byte sizes into human-readable strings

murphyatworkmurphyatwork
opened 3 months ago
Author

Description:

StarRocks currently lacks a native function to transform raw byte counts into human-readable formats with units such as B, KB, MB, GB, etc.

This functionality is widely needed for tasks like examining table sizes, monitoring disk usage, and reviewing metrics. Comparable features are available in other databases, including:

  • PostgreSQL: pg_size_pretty()
  • MySQL 8.0+: FORMAT_BYTES()
  • ClickHouse: formatReadableSize()

Proposal:

Introduce a new scalar function:

format_bytes(bytes BIGINT) → STRING

Behavior:

  • Input: A BIGINT representing the number of bytes.
  • Output: A string displaying the size with an appropriate unit, rounded to two decimal places.
  • Examples:
    • format_bytes(123) → 123 B
    • format_bytes(4096) → 4 KB
    • format_bytes(123456789) → 117.74 MB
    • format_bytes(10737418240) → 10 GB

Rationale:

  • Enhances usability for system queries and monitoring dashboards.
  • Aligns with user expectations based on features available in other databases.

Additional Notes:

  • Unit thresholds follow a 1024-based system (KiB, MiB, GiB), but units are displayed as KB/MB/GB for simplicity.
  • Supports sizes up to TB/PB.