Let $X$ be an affine algebraic set in $\mathbb C^n$, i.e., the vanishing set of a family of polynomials. Let's call a function $f:X\to \mathbb C$ locally rational if for every $x\in X$ there exists a Zariski open $U\subseteq \mathbb C^n$ such that the restriction of $f$ to $U\cap X$ is a rational function (i.e., given by a ratio of two polynomials).
If $X$ is irreducible, then there is a theorem in Shafarevich's book which says that any locally rational map $f:X\to \mathbb C$ is in fact a polynomial. Does anyone know of a counterexample to this statement for the case where $X$ is reducible? And in the latter case, what is the optimal refinement of the theorem?