{"record":{"id":"ffa8289bd5c7c988","repo":"mikefarah/yq","slug":"arrays-not-yet-supported-for-comparison","errorCode":null,"errorMessage":"arrays not yet supported for comparison","messagePattern":"arrays not yet supported for comparison","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_compare.go","lineNumber":38,"sourceCode":"func compare(prefs compareTypePref) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {\n\treturn func(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {\n\t\tlog.Debugf(\"compare cross function\")\n\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","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_compare.go#L20-L56","documentation":"Same limitation as the map case: yq's comparison operators (<, <=, >, >=) do not implement ordering for SequenceNode (array) operands. When the left-hand side of a comparison is an array, yq returns this error. Arrays are not naturally ordered in the comparator, so yq refuses rather than producing a bogus result.","triggerScenarios":"Any yq expression with <, <=, >, >= where the lhs evaluates to a sequence, e.g. `yq '.tags > [\"a\"]' file.yaml`, `yq '.items < 5'` where `.items` is an array, or min/max/sort_by over elements that are themselves arrays.","commonSituations":"Forgetting to index into an array (comparing `.ports` instead of `.ports[0]`); sorting an array of arrays; scripts where a field changed from a scalar to a list after a schema update; using length comparisons intended as `(.x | length) > 3` but written as `.x > 3`.","solutions":["Index into the array or compare its length: `(.items | length) > 3` instead of `.items > 3`.","Sort by an inner key/element: `sort_by(.[0])` if ordering arrays is the goal.","Use equality (== or contains) for array checks instead of ordering operators.","Reshape the data so the operand is a scalar before comparing."],"exampleFix":"# before\nyq '.servers > 2' file.yaml   # servers is an array\n# after\nyq '(.servers | length) > 2' file.yaml","handlingStrategy":"type-guard","validationCode":"# ensure the operand is not an array before comparing:\nyq -e '(.items | type) != \"!!seq\"' file.yaml || echo \"operand is an array\"","typeGuard":"// yq expression guard: only compare scalars\n// yq 'select(tag != \"!!seq\" and tag != \"!!map\") | . > 3' file.yaml","tryCatchPattern":null,"preventionTips":["Use (expr | length) when you mean to compare sizes of arrays","Index arrays (.items[0]) before ordering comparisons","Use contains/any for membership checks instead of < or >","Watch for schema drift turning scalars into lists"],"tags":["yq","comparison-operator","unsupported-type","array"],"backgroundTag":"comparison-not-supported-for-type","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"}