feat: cast numeric literals to decimal type (#805)

## What

Add casting of numeric literals (`int`, `long`, `float`, `double`) to a decimal target type in `Literal::CastTo`, so a numeric value can be used as a default for a decimal column.

Previously `CastFromInt` / `CastFromLong` / `CastFromFloat` / `CastFromDouble` had no `kDecimal` case and fell through to `NotSupported`, so a default like `Literal::Int(12)` or `Literal::Double(9.99)` for a `decimal(9, 2)` column was rejected. Java allows these (`IntegerLiteral.to`, `DoubleLiteral.to`, etc. scale the value to the target scale), so this brings the C++ literal cast layer to parity for numeric sources.

## How

- **Integer → decimal**: `CastIntegerToDecimal` treats the integer as scale 0 and rescales it to the target scale via `RescaleHalfUp` (exact when increasing scale, HALF_UP when decreasing it), then verifies the result fits the target precision (`FitsInPrecision`). Example: `12` → `decimal(9,2)` yields unscaled `1200` (`12.00`).
- **Float/double → decimal**: `CastRealToDecimal` parses the value's shortest round-tripping decimal representation (matching Java's `BigDecimal.valueOf(double)` via `Double.toString`) into an integer coefficient and exponent-derived scale without expanding scientific notation, then rounds to the target scale with **HALF_UP** rounding (round half away from zero, as Java does — `2.5` → `3`, `-2.5` → `-3`) and checks precision. Non-finite values are rejected.
- Both paths reject an out-of-range decimal scale before indexing the powers-of-ten table (`DecimalType` does not bound its scale on construction, so `decimal(9, 40)` would otherwise read past the table).

## Scope

Follow-up split out from the v3 default-value work (see the `CastDefaultToType` discussion on #793). It only extends the shared `Literal::CastTo` layer for numeric sources.

## Testing

`LiteralTest.IntegerCastToDecimal` (int/long scaling, out-of-precision rejection, out-of-range scale rejection) and `LiteralTest.RealCastToDecimal` (float/double scaling, HALF_UP rounding incl. negative, round-down, out-of-precision and non-finite rejection, scientific-notation overflow rejection and negative-scale acceptance), all verified fail-without / pass-with. Full `expression_test` passes (495 tests).
2 files changed
tree: f3e1b7149725e516a4f7b5444d91a16f56e6cf8a
  1. .devcontainer/
  2. .github/
  3. ci/
  4. cmake_modules/
  5. dev/
  6. example/
  7. mkdocs/
  8. src/
  9. subprojects/
  10. thirdparty/
  11. .asf.yaml
  12. .clang-format
  13. .clang-tidy
  14. .gitignore
  15. .pre-commit-config.yaml
  16. AGENTS.md
  17. cmake-format.py
  18. CMakeLists.txt
  19. LICENSE
  20. Makefile
  21. meson.build
  22. meson.options
  23. NOTICE
  24. README.md
  25. requirements.txt
  26. SECURITY-THREAT-MODEL.md
README.md

Iceberg

Slack Ask DeepWiki License

Apache Iceberg™ C++

C++ implementation of Apache Iceberg™.

Requirements

Required:

  • C++23 compliant compiler (GCC 14+, Clang 18+, MSVC 2022+)
  • CMake 3.25+ or Meson 1.5+
  • Ninja (recommended build backend)

Optional:

Quick Start

git clone https://github.com/apache/iceberg-cpp.git
cd iceberg-cpp
cmake -S . -B build -G Ninja
cmake --build build
ctest --test-dir build --output-on-failure

Build with CMake

Build, Run Tests and Install Core Libraries

cmake -S . -B build -G Ninja -DCMAKE_INSTALL_PREFIX=/path/to/install -DICEBERG_BUILD_STATIC=ON -DICEBERG_BUILD_SHARED=ON
cmake --build build
ctest --test-dir build --output-on-failure
cmake --install build

To run a specific test suite:

ctest --test-dir build -R schema_test --output-on-failure

Build and Install Iceberg Bundle Library

Vendored Apache Arrow (default)

cmake -S . -B build -G Ninja -DCMAKE_INSTALL_PREFIX=/path/to/install -DICEBERG_BUILD_BUNDLE=ON
cmake --build build
ctest --test-dir build --output-on-failure
cmake --install build

Provided Apache Arrow

cmake -S . -B build -G Ninja -DCMAKE_INSTALL_PREFIX=/path/to/install -DCMAKE_PREFIX_PATH=/path/to/arrow -DICEBERG_BUILD_BUNDLE=ON
cmake --build build
ctest --test-dir build --output-on-failure
cmake --install build

CMake Build Options

OptionDefaultDescription
ICEBERG_BUILD_STATICONBuild static library
ICEBERG_BUILD_SHAREDOFFBuild shared library
ICEBERG_BUILD_TESTSONBuild tests
ICEBERG_BUILD_BUNDLEONBuild the battery-included library
ICEBERG_BUILD_RESTONBuild REST catalog client
ICEBERG_BUILD_REST_INTEGRATION_TESTSOFFBuild REST catalog integration tests
ICEBERG_BUILD_HIVEOFFBuild Hive (HMS) catalog client
ICEBERG_BUILD_SQL_CATALOGOFFBuild SQL catalog client
ICEBERG_SQL_SQLITEOFFBuild the SQLite connector for the SQL catalog
ICEBERG_SQL_POSTGRESQLOFFBuild the PostgreSQL connector for the SQL catalog
ICEBERG_SQL_MYSQLOFFBuild the MySQL connector for the SQL catalog
ICEBERG_ENABLE_ASANOFFEnable Address Sanitizer
ICEBERG_ENABLE_UBSANOFFEnable Undefined Behavior Sanitizer

Build with Meson

meson setup builddir
meson compile -C builddir
meson test -C builddir --timeout-multiplier 0

Meson provides built-in equivalents for several CMake options:

  • --default-library=<shared|static|both> instead of ICEBERG_BUILD_STATIC / ICEBERG_BUILD_SHARED
  • -Db_sanitize=address,undefined instead of ICEBERG_ENABLE_ASAN / ICEBERG_ENABLE_UBSAN
  • --libdir, --bindir, --includedir for install directories

Meson-specific options (configured via -D<option>=<value>):

OptionDefaultDescription
restenabledBuild REST catalog client
rest_integration_testdisabledBuild integration test for REST catalog
testsenabledBuild tests

Build Examples

After installing the core libraries, you can build the examples:

cd example
cmake -S . -B build -G Ninja -DCMAKE_PREFIX_PATH=/path/to/install
cmake --build build

If you are using provided Apache Arrow, include /path/to/arrow in CMAKE_PREFIX_PATH:

cmake -S . -B build -G Ninja -DCMAKE_PREFIX_PATH="/path/to/install;/path/to/arrow"

Customizing Dependency URLs

If you experience network issues when downloading dependencies, you can customize the download URLs using environment variables:

  • ICEBERG_ARROW_URL: Apache Arrow tarball URL
  • ICEBERG_AVRO_URL: Apache Avro tarball URL
  • ICEBERG_AVRO_GIT_URL: Apache Avro git repository URL
  • ICEBERG_NANOARROW_URL: Nanoarrow tarball URL
  • ICEBERG_CROARING_URL: CRoaring tarball URL
  • ICEBERG_UTF8PROC_URL: utf8proc tarball URL
  • ICEBERG_NLOHMANN_JSON_URL: nlohmann-json tarball URL
  • ICEBERG_SPDLOG_URL: spdlog tarball URL
  • ICEBERG_CPR_URL: cpr tarball URL

Example:

export ICEBERG_ARROW_URL="https://your-mirror.com/apache-arrow-22.0.0.tar.gz"
cmake -S . -B build

Contribute

Apache Iceberg is an active open-source project, governed under the Apache Software Foundation (ASF). Iceberg-cpp is open to people who want to contribute to it. Here are some ways to get involved:

The Apache Iceberg community is built on the principles described in the Apache Way and all who engage with the community are expected to be respectful, open, come with the best interests of the community in mind, and abide by the Apache Foundation Code of Conduct.

In addition, contributors using AI-assisted tools must follow the documented guidelines for AI-assisted contributions available on the Iceberg website: https://iceberg.apache.org/contribute/#guidelines-for-ai-assisted-contributions.

Linting

Install the python package pre-commit and run once pre-commit install.

pip install pre-commit
pre-commit install

This will setup a git pre-commit-hook that is executed on each commit and will report the linting problems. To run all hooks on all files use pre-commit run -a.

Dev Containers

We provide Dev Container configuration file templates.

To use a Dev Container as your development environment, follow the steps below, then select Dev Containers: Reopen in Container from VS Code's Command Palette.

cd .devcontainer
cp Dockerfile.template Dockerfile
cp devcontainer.json.template devcontainer.json

If you make improvements that could benefit all developers, please update the template files and submit a pull request.

License

Licensed under the Apache License, Version 2.0