drizzle-team/drizzle-orm · error · Error

Cannot pass undefined values to any set operator

Error message

Cannot pass undefined values to any set operator

What it means

Thrown at the top of SingleStoreDialect.buildSetOperations (dialect.ts:453) when the destructured first element of the setOperators array is undefined/falsy. This indicates the query's setOperators config contains an undefined entry, which Drizzle treats as an invalid set operation rather than silently producing malformed SQL.

Source

Thrown at drizzle-orm/src/singlestore-core/dialect.ts:453

		const finalQuery =
			sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`;

		if (setOperators.length > 0) {
			return this.buildSetOperations(finalQuery, setOperators);
		}

		return finalQuery;
	}

	buildSetOperations(
		leftSelect: SQL,
		setOperators: SingleStoreSelectConfig['setOperators'],
	): SQL {
		const [setOperator, ...rest] = setOperators;

		if (!setOperator) {
			throw new Error('Cannot pass undefined values to any set operator');
		}

		if (rest.length === 0) {
			return this.buildSetOperationQuery({ leftSelect, setOperator });
		}

		// Some recursive magic here
		return this.buildSetOperations(
			this.buildSetOperationQuery({ leftSelect, setOperator }),
			rest,
		);
	}

	buildSetOperationQuery({
		leftSelect,
		setOperator: { type, isAll, rightSelect, limit, orderBy, offset },
	}: {
		leftSelect: SQL;

View on GitHub (pinned to b7862528fd)

Solutions

  1. Filter out undefined entries before adding set operators: `operators.filter(Boolean)`.
  2. Ensure every union/intersect/except argument is a concrete select builder, not a possibly-undefined expression.
  3. If a set operator is optional, build the operator list conditionally and only call addSetOperators when at least one defined operator exists.

Example fix

// before
const extra = cond ? db.select({a: t.a}).from(t) : undefined;
await base.union(extra as any);
// after
const extra = cond ? db.select({a: t.a}).from(t) : null;
const q = base;
if (extra) q.union(extra);
Defensive patterns

Strategy: validation

Validate before calling

function buildOperators(ops) {
  return ops.filter((o) => o !== undefined && o !== null);
}
// only call addSetOperators / union when result is non-empty

Type guard

function hasNoUndefinedOperators(ops) { return ops.every((o) => o !== undefined && o !== null); }

Prevention

When it happens

Trigger: Calling union/unionAll/intersect/except with an undefined argument that gets pushed into setOperators; programmatic query assembly that spreads a sparse array (e.g. `[undefined]`) into the chain; internal addSetOperators receiving a filtered/mapped array that yields undefined.

Common situations: Conditionally building a union where a branch returns undefined (`...cond ? qb2 : undefined`); refactoring a set-operator pipeline and accidentally leaving a placeholder; passing a subquery variable that was never assigned.

Related errors


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/b5ced02e79c3c4a2.json. Report an issue: GitHub.