mikefarah/yq · error

%v (%v) cannot be divided by %v (%v)

Error message

%v (%v) cannot be divided by %v (%v)

What it means

The divide operator short-circuits when the left-hand tag is !!null: dividing null by anything is undefined, so it errors naming both sides and their paths. This fires for expressions like null / 2 or when the LHS expression matched a missing/null field.

Source

Thrown at pkg/yqlib/operator_divide.go:17

package yqlib

import (
	"fmt"
	"strconv"
	"strings"
)

func divideOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	log.Debugf("Divide operator")

	return crossFunction(d, context.ReadOnlyClone(), expressionNode, divide, false)
}

func divide(_ *dataTreeNavigator, _ Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
	if lhs.Tag == "!!null" {
		return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhs.Tag, lhs.GetNicePath(), rhs.Tag, rhs.GetNicePath())
	}

	target := lhs.CopyWithoutContent()

	if lhs.Kind == ScalarNode && rhs.Kind == ScalarNode {
		if err := divideScalars(target, lhs, rhs); err != nil {
			return nil, err
		}
	} else {
		return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhs.Tag, lhs.GetNicePath(), rhs.Tag, rhs.GetNicePath())
	}

	return target, nil
}

func divideScalars(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
	lhsTag := lhs.Tag
	rhsTag := rhs.guessTagFromCustomType()

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Guard for nulls: select(. != null) / 2 or use // alternative
  2. Ensure the LHS field exists in the document before dividing
  3. Provide a default: (.count // 0) / 2
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/yqlib/operator_divide.go:17 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/5f02a491c2838c45. Report an issue: GitHub.