{"record":{"id":"2e939dea79556991","repo":"mikefarah/yq","slug":"v-v-cannot-be-subtracted-from-v","errorCode":null,"errorMessage":"%v (%v) cannot be subtracted from %v","messagePattern":"(.+?) \\((.+?)\\) cannot be subtracted from (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_compare.go","lineNumber":41,"sourceCode":"\t\tif lhs == nil && rhs == nil {\n\t\t\towner := &CandidateNode{}\n\t\t\treturn createBooleanCandidate(owner, prefs.OrEqual), nil\n\t\t} else if lhs == nil {\n\t\t\tlog.Debugf(\"lhs nil, but rhs is not\")\n\t\t\treturn createBooleanCandidate(rhs, false), nil\n\t\t} else if rhs == nil {\n\t\t\tlog.Debugf(\"rhs nil, but rhs is not\")\n\t\t\treturn createBooleanCandidate(lhs, false), nil\n\t\t}\n\n\t\tswitch lhs.Kind {\n\t\tcase MappingNode:\n\t\t\treturn nil, fmt.Errorf(\"maps not yet supported for comparison\")\n\t\tcase SequenceNode:\n\t\t\treturn nil, fmt.Errorf(\"arrays not yet supported for comparison\")\n\t\tdefault:\n\t\t\tif rhs.Kind != ScalarNode {\n\t\t\t\treturn nil, fmt.Errorf(\"%v (%v) cannot be subtracted from %v\", rhs.Tag, rhs.GetNicePath(), lhs.Tag)\n\t\t\t}\n\t\t\ttarget := lhs.CopyWithoutContent()\n\t\t\tboolV, err := compareScalars(context, prefs, lhs, rhs)\n\n\t\t\treturn createBooleanCandidate(target, boolV), err\n\t\t}\n\t}\n}\n\nfunc compareDateTime(layout string, prefs compareTypePref, lhs *CandidateNode, rhs *CandidateNode) (bool, error) {\n\tlhsTime, err := parseDateTime(layout, lhs.Value)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\n\trhsTime, err := parseDateTime(layout, rhs.Value)\n\tif err != nil {\n\t\treturn false, err","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_compare.go#L23-L59","documentation":"When the lhs of a comparison is a scalar but the RIGHT-hand side is not a scalar (e.g. it is a map or array), yq cannot compare them and reports this error. The message text says 'cannot be subtracted' for historical reasons (the format string is shared), but the operator is actually the comparison (<, <=, >, >=). The error includes the rhs tag and path plus the lhs tag to pinpoint the mismatch.","triggerScenarios":"Expressions like `yq '3 < .tags' file.yaml` or `yq '.count >= .items'` where the rhs path resolves to a non-scalar (MappingNode or SequenceNode) while the lhs is a scalar. Also triggered via crossFunction when one side of a cross-product comparison resolves to a map/array.","commonSituations":"Typos in the rhs path that land on a parent object instead of a leaf field; schema changes where a field became an array; comparing a scalar against a whole list intending 'is it in the list' (should be `any`/`contains`); accidentally comparing across document types.","solutions":["Check what the rhs resolves to: `yq '.items | type' file.yaml`, and navigate to the intended scalar field.","If testing membership, use `contains` or `any`: `(.items | any(. == 3))` instead of `3 < .items`.","If comparing counts, wrap arrays in `length`: `.count >= (.items | length)`.","Verify input schema (e.g. with `yq 'paths(type == \"!!seq\")'`) if the field was expected to be a scalar."],"exampleFix":"# before\nyq '.replicas > .servers' file.yaml   # .servers is an array\n# after\nyq '.replicas > (.servers | length)' file.yaml","handlingStrategy":"validation","validationCode":"# confirm both comparison operands are scalars before running:\nyq -e '((.replicas | type) == \"!!scalar\" and (.servers | type) == \"!!scalar\")' file.yaml || echo \"non-scalar operand\"","typeGuard":"// yq expression guard: assert rhs is scalar\n// yq '. as $l | .servers as $r | select(($r | tag) == \"!!int\" or ($r | tag) == \"!!float\" or ($r | tag) == \"!!str\") | $l.replicas > $r' file.yaml","tryCatchPattern":null,"preventionTips":["Resolve rhs paths to leaf scalars; print them with the expression first","Wrap array/map operands in length or index into them","Use contains/any for 'is it in the list' semantics","Validate input schema when fields may change type"],"tags":["yq","comparison-operator","type-mismatch","scalar"],"backgroundTag":"comparison-type-mismatch","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}