Inconsistent result after apply for Floats

I’m getting weird behaviour when I try to flatten Float List -
I defined a list of floats in the schema -

"buckets": schema.ListAttribute{
       ElementType: types.Float64Type,
       Required:    true,
}

and

Buckets types.List tfsdk:"buckets" //types.Float64

I flatten the results back to the sate the next way -

func float32SliceTypeList(ctx context.Context, arr []float32) (types.List, diag2.Diagnostics) {
	if len(arr) == 0 {
		return types.ListNull(types.Float64Type), nil
	}
	result := make([]attr.Value, 0, len(arr))
	for _, v := range arr {
		log.Printf("[DEBUG] values -  %f, %f", v, float64(v))
		result = append(result, types.Float64Value(float64(v)))
	}
	return types.ListValueFrom(ctx, types.Float64Type, result)
}

and also printed the flatten list to make sure it was flattened correctly -

func flattenE2MHistogramAggregation(ctx context.Context, aggregation *e2m.Aggregation) *HistogramAggregationModel {
	if aggregation == nil {
		return nil
	}

	buckets, diags := float32SliceTypeList(ctx, aggregation.GetHistogram().GetBuckets())
	if diags.HasError() {
		return nil
	}
	log.Printf("[INFO] Flattened histogram buckets: %v", buckets)
	return &HistogramAggregationModel{
		Enable:           types.BoolValue(aggregation.GetEnabled()),
		TargetMetricName: types.StringValue(aggregation.GetTargetMetricName()),
		Buckets:          buckets,
	}
}

I tried to create the next config -

resource "coralogix_events2metric" "xxxxxx"{
   ...
  buckets = [0.1, 0.2, 0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50]
}

and saw the values on the flattened list were right -

[INFO] Flattened histogram buckets: [0.100000,0.200000,0.300000,0.500000,1.000000,2.000000,3.000000,5.000000,10.000000,20.000000,30.000000,50.000000]

but then got the next errors -

Error: Provider produced inconsistent result after apply

│ When applying changes to coralogix_events2metric.xxxxxx, provider “provider["registry.terraform.io/coralogix/coralogix"]” produced an unexpected new value: .metric_fields[“xxxxxx”].aggregations.histogram.buckets[0]: was
│ cty.MustParseNumberVal(“0.1”), but now cty.NumberFloatVal(0.10000000149011612).

│ This is a bug in the provider, which should be reported in the provider’s own issue tracker.


│ Error: Provider produced inconsistent result after apply

│ When applying changes to coralogix_events2metric.xxxxxx, provider “provider["registry.terraform.io/coralogix/coralogix"]” produced an unexpected new value: .metric_fields[“xxxxxx”].aggregations.histogram.buckets[1]: was
│ cty.MustParseNumberVal(“0.2”), but now cty.NumberFloatVal(0.20000000298023224).

│ This is a bug in the provider, which should be reported in the provider’s own issue tracker.


│ Error: Provider produced inconsistent result after apply

│ When applying changes to coralogix_events2metric.xxxxxx, provider “provider["registry.terraform.io/coralogix/coralogix"]” produced an unexpected new value: .metric_fields[“xxxxxx”].aggregations.histogram.buckets[2]: was
│ cty.MustParseNumberVal(“0.3”), but now cty.NumberFloatVal(0.30000001192092896).

Hi @OrNovo :wave:

I believe that this has to do with float64(v) where v is a float32.

Here’s an equivalent example on the Go Playground, and a similar post on Stack Overflow.

In your example, are you able to use float64 throughout? If not, perhaps you can share the relevant parts of the provider code to illustrate where/why float32 is being used.

I’m using float32 because that’s what the API I’m using exposes.
Is there a way to overcome this problem?

The schema you have defined has specified that the buckets attribute is required:

"buckets": schema.ListAttribute{
       ElementType: types.Float64Type,
       Required:    true,
}

As a consequence, the value for buckets must be supplied in the Terraform configuration as you illustrated:

resource "coralogix_events2metric" "xxxxxx"{
   ...
  buckets = [0.1, 0.2, 0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50]
}

Attributes that are specified as Required: true can only be set in practitioner configuration (i.e., Terraform configuration) - https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes/list#configurability. Consequently, you cannot use values retrieved from a provider API call to set values for a required attribute.