Files
signoz/pkg/querybuilder/duration.go
Srikanth Chekuri 9c886be120 chore(querybuilder): compile every signal through one storage contract (#12802)
The semantic convention family as first call citizen revealed that the
current state of the query builder needs a bit refactoring for long term
maintenance.

The `FieldMapper` and `ConditionBuilder` are now one abstraction
`Storage`.

A storage now answers
- what the compiler cannot know i.e one read per field key (the bare
SQL, the membership present or absent, what an absent row reads, and
whether the read keeps its type or filters only).
- the fallback for a key metadata does not report
- its traits
- and one Condition compilation part.

And we introduce a new type to use in the system, `Resolved`

```
// Resolved is what resolution produces for one key: its meanings, and how
// they came to be. It is the only thing the compilers receive. Compile it
// with the operator and value it was resolved with.
type Resolved struct {
	Key    *telemetrytypes.TelemetryFieldKey
	Fields []*telemetrytypes.LogicalField
	// FromFallback: the fields came from the storage's fallback, not from
	// metadata matches.
	FromFallback bool
	// Ambiguous: the matches held several interpretations.
	Ambiguous bool
	// Skipped: the storage contributes nothing for this key.
	Skipped  bool
	Warnings []string
}
```

The prepared SQL has no changes, where it changed, it specifically made
the expression better by removing the redundant part.

- The prepared SQL remains identical with this refactoring
- No changes to integration tests


Assisted-by: Claude Fable 5.1
2026-09-15 11:36:06 +00:00

39 lines
921 B
Go

package querybuilder
import (
"strconv"
"time"
"github.com/SigNoz/signoz/pkg/errors"
)
// CoerceDurationValue accepts duration syntax and numeric strings for a
// duration operand, item by item for a list.
func CoerceDurationValue(value any) (any, error) {
switch v := value.(type) {
case string:
if duration, err := time.ParseDuration(v); err == nil {
return duration.Nanoseconds(), nil
} else if f, err := strconv.ParseFloat(v, 64); err == nil {
return int64(f), nil
} else {
return nil, errors.WrapInvalidInputf(err, errors.CodeInvalidInput, "invalid duration value: %s", v)
}
case float64:
return int64(v), nil
case float32:
return int64(v), nil
case []any:
coerced := make([]any, len(v))
for i, item := range v {
itemValue, err := CoerceDurationValue(item)
if err != nil {
return nil, err
}
coerced[i] = itemValue
}
return coerced, nil
}
return value, nil
}