Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

large numbers rendered incorrectly in plan output #34686

Closed
heldersepu opened this issue Feb 15, 2024 · 6 comments · Fixed by #34702
Closed

large numbers rendered incorrectly in plan output #34686

heldersepu opened this issue Feb 15, 2024 · 6 comments · Fixed by #34702
Assignees
Labels
bug cli confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code

Comments

@heldersepu
Copy link

heldersepu commented Feb 15, 2024

Terraform Version

Terraform v1.6.2

Terraform Configuration Files

locals {
  ids = [
    9223372036854773256,
    9223372036854773263,
    9223372036854773263,
    9223372036854773266
  ]
  sorted = sort(local.ids)
  unique = distinct(local.ids)
}

output "sorted" {
  value = local.sorted
}

output "unique" {
  value = local.unique
}

Debug Output

Changes to Outputs:
  + sorted = [
      + "9223372036854773256",
      + "9223372036854773263",
      + "9223372036854773263",
      + "9223372036854773266",
    ]
  + unique = [
      + 9223372036854774000,
      + 9223372036854774000,
      + 9223372036854774000,
    ]

Expected Behavior

the distinct function should output full values just like sort does

Actual Behavior

distinct is rounding the values to 9223372036854774000

Steps to Reproduce

just do a TF plan on the sample provided

@heldersepu heldersepu added bug new new issue not yet triaged labels Feb 15, 2024
@jbardin
Copy link
Member

jbardin commented Feb 15, 2024

Thanks for filing the issue @heldersepu!

I don't think this is actually a problem with distinct, something appears to be going wrong with the rendered output. If you format the numbers within Terraform, you can see the full value:

output "unique" {
  value = [for v in local.unique : format("%d", v)]
}
  + unique = [
      + "9223372036854773256",
      + "9223372036854773263",
      + "9223372036854773266",
    ]

Note that sort here works for the same reason, the sort function operates on strings, so the numbers are implicitly converted before being sorted, which is why your sorted output is in quotes.

@jbardin jbardin added config confirmed a Terraform Core team member has reproduced this issue and removed new new issue not yet triaged labels Feb 15, 2024
@jbardin
Copy link
Member

jbardin commented Feb 15, 2024

Also checked that the plan has stored the correct list(number) value, and can be verified in the -json output.

@jbardin jbardin added cli and removed config labels Feb 15, 2024
@jbardin jbardin changed the title distinct function rounding large integers large numbers rendered incorrectly in plan output Feb 15, 2024
@heldersepu
Copy link
Author

100% not just the distinct I got the same just outputting the list:

locals {
  ids = [
    9223372036854773256,
    9223372036854773263,
    9223372036854773263,
    9223372036854773266
  ]
}

output "ids" {
  value = local.ids
}

output "sorted" {
  value = sort(local.ids)
}

Changes to Outputs:
  + ids    = [
      + 9223372036854774000,
      + 9223372036854774000,
      + 9223372036854774000,
      + 9223372036854774000,
    ]
  + sorted = [
      + "9223372036854773256",
      + "9223372036854773263",
      + "9223372036854773263",
      + "9223372036854773266",
    ]

@heldersepu
Copy link
Author

... and yes the terraform.tfstate has the correct values

@liamcervante liamcervante self-assigned this Feb 19, 2024
@liamcervante
Copy link
Member

This happens when we read the changes back from the json.RawMessage format they are stored in the json plan file. This is here: https://github.com/hashicorp/terraform/blob/main/internal/command/jsonformat/structured/change.go#L277

Essentially, the numbers are bigger than 2^63, and the json package by default attempts to read them into float64 types, which overflows and then we see the number being truncated.

The fix is relatively simple, we just need to tell the json package to use json.Number to represent numbers instead of float64. We have a lot of tests that need to be updated though to support the new internal representation.

Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug cli confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants