Skip to content

[Bug] format_string prints the unscaled value for %s of a DECIMAL and strips trailing zeros for %g #26337

Open

Description

@comphead

Describe the bug

Spark's format_string hands its arguments to Java's java.util.Formatter (stringExpressions.scala#L2045). Two behaviors of datafusion-spark differ from that:

  1. %s with a DECIMAL argument prints the unscaled integer. Spark prints the decimal with its scale.
  2. %g and %G drop trailing zeros like C does. Java keeps the full precision, which is six significant digits by default.

To Reproduce

Spark 4.0.0:

SELECT format_string('String: %s', CAST(123.456 AS DECIMAL(10,3)))
-- String: 123.456
SELECT format_string('%s', CAST(0.5 AS DECIMAL(10,2)))
-- 0.50
SELECT format_string('Compact: %g', 1234.5D)
-- Compact: 1234.50
SELECT format_string('%g', 0.0001D)
-- 0.000100000

A literal such as 1234.5 is a DECIMAL in Spark and makes %g throw (see below), so the examples use 1234.5D.

DataFusion, existing tests:

The 0.0001 example was not run on DataFusion.

Expected behavior

Print a DECIMAL argument of %s with its scale, keep trailing zeros for %g and %G like Formatter, and update the expectations above.

Related

Additional context

Found by running the queries in datafusion/sqllogictest/test_files/spark on a local Spark and comparing the results with the expected values in the files, which is the kind of check proposed in #17045. Spark results come from Apple's build of Spark 4.0.0 (4.0.0.65-apple) with a UTC session and spark.sql.ansi.enabled=false unless stated. The Spark source links point at the v4.0.0 tag. DataFusion results are the sqllogictest expectations at 9dec775, which CI enforces. Anything marked as not run was derived from reading the code.

Activity

  1. added
    bugSomething isn't working
    sparkChanges to datafusion-spark crate
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsparkChanges to datafusion-spark crate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions