Rational points and dimension of a variety Notice that $|GL_n(\mathbb{F}_p)|$ is a polynomial of degree $n^2$ in $p$, and that $GL_n(k)$ is a variety of dimension $n^2$.  Similarly, $|SL_n(\mathbb{F}_p)|$ is a polynomial of degree $n^2-1$ in $p$, and $SL_n(k)$ is a variety of dimension $n^2-1$.
If $V\subset\mathbb{A}^2$ is the one-dimensional $k$-variety defined by the equation $xy-1=0$, then $\#V(\mathbb{F}_p)=p-1$.  As a final example, the $2$-dimensional variety defined by the equation $x^2+y^2+z^2=0$ has $p^2$ $\mathbb{F}_p$-points.
All of these examples cause me to ask a question that I doubt is true in general, but perhaps is true with some conditions.

If $V$ is an $n$-dimensional variety defined over $\mathbb{Z}$, and $P$ is the set of primes, we can define a function $f:P\to\mathbb{N}$ by $f(p)=\#V_p(\mathbb{F}_p)$ where $V_p$ is the variety over $\mathbb{F}_p$ given by reducing mod $p$ the equations defining $V$.  Is $f$ a polynomial of degree $n$?

I'm sure a counterexample exists, and I'd love to see one.  Also, if this is not true in general, what conditions can we add to $V$ that might make the claim true?
Edit: PVAL's counterexample shows that $f$ might not be a strict polynomial, but that $f$ might be a "piecewise" polynomial, such that each piece has the appropriate degree:
$$f=\begin{cases}2&p\equiv 1\;\mathrm{mod}\; 4\\0&p\equiv 3\;\mathrm{mod}\;4\\1&p=2\end{cases}$$
Notice that the dimension of $V(x^2+1)\subset\mathbb{A}^1$ is $0$.
 A: $\#V(x^2+1,  \Bbb{F}_p) = 2$ when $p=1 \operatorname{mod} 4$, $1$ when $p=2$ and $0$ otherwise. In particular since there are infinite number of primes equivalent to $1$ and $3$ mod $4$, we have no such polynomial defining $\#V$ (such a polynomial is nonzero and has infinitely many zeroes).
A: The concept you're talking about is usually called polynomial countability. In general this won't hold as PVAL's answer has shown. In fact, I think you can construct examples where the counting function can get pretty far from polynomial, not even quasipolynomial as PVAL's example is. I think you can find such an example here.
However, when the counting function is a polynomial, then your question has (at least conjecturally) an affirmative answer. 
Consider the free abelian group generated by isomorphism classes $[V]$ of varieties over $\mathbb{Z}$. We impose the following relation: if $Z \subset V$ is a closed subvariety and $U = V \setminus Z$, then $[V] = [U] + [Z]$. This can be seen as a sort of cutting and pasting relation. Finally, we define multiplication by $[X][Y] = [X \times_\mathbb{Z} Y]$. This gives a ring denoted $K_0(\mathcal{V}_\mathbb{Z})$ and called the Grothendieck ring of varieties. 
If $\chi$ is any invariant of varieties valued in some ring $S$ that satisfies $\chi(X) = \chi(Y)$ if $X$ and $Y$ are isomorphic, $\chi(V) = \chi(U) + \chi(Z)$ with $U$ and $Z$ as above and $\chi(X \times_\mathbb{Z} Y) = \chi(X) \chi(Y)$, then $\chi$ is called an additive invariant. It is not hard to see that any additive invariant is equivalent to a unique ring homomorphism $\chi:K_0(\mathcal{V}_\mathbb{Z}) \to S$. Examples include the topological Euler characteristc $\chi_{top}(X_\mathbb{C})$ and the counting function. 
Let's denote the class $[\mathbb{A}^1]$ by $\mathbb{L}$. Here are some examples of classes of varieties in the Grothendieck ring:
$$[\mathbb{A}^n] = \mathbb{L}^n, \enspace \enspace \enspace \mathbb{P}^n = \mathbb{L}^n + \mathbb{L}^{n-1} + \ldots + 1,
$$
$$
[\operatorname{GL}_n] = (\mathbb{L}^n - 1)(\mathbb{L}^n - \mathbb{L})\ldots(\mathbb{L}^n - \mathbb{L}^{n-1})
$$
Let's denote by $N(V)$ the counting function of $V$, that is, $N(V)(q) = \#V_{\mathbb{F}_q}(\mathbb{F}_q)$ where $q = p^r$ for some prime $r$. Then $N$ is an additive invariant so it only depends on the class in $K_0(\mathcal{V}_\mathbb{Z})$. 
Then what we are really interested in is the question of when is $N(V)$ a polynomial, and in this case, how is the degree of $N(V)$ related to the dimension of $V$?
Knowing that $N$ is additive and knowing the trivial identity $N(\mathbb{L}) = q$, we see that in all the examples I gave above, $N(V)$ is a polynomial. More generally, any time $[V] = f(\mathbb{L})$ where $f$ is a polynomial, $N(V)$ is a polynomial in $q$, namely, the polynomial $f(q)$. In this case, the degree of $N(V)$ is the dimension of the largest affine space appearing in the expression for $[V]$. 
If $V$ actually has a decomposition into such affine spaces, as is the case in every example I gave, then the dimension of $V$ will is the dimension of the largest affine space, and thus the degree of $N(V)$. What if $[V] = f(\mathbb{L})$ formally in the Grothendieck ring but $V$ doesn't necessarily have an explicit cut and paste decomposition into affine spaces? Then we're still okay because there are additive invariants that can pick out the dimension of a variety, for example, the Hodge-Deligne polynomial. Therefore, the dimension of $V$ still has to be equal to the dimension of the largest affine space in the expression for $[V]$, i.e., the degree of $f$ and so the degree of the counting function. This is the reason for your examples above. You can actually explicitly cut and paste those varieties from affine spaces, giving you polynomial in $\mathbb{L}$ classes in the Grothendieck ring whose degree is the dimension of the largest affine spaces and therefore the dimension of your varieties. 
Now, how about if we know that $N(V)$ is a polynomial in $q$ but not necessarily that $[V]$ is a polynomial in $\mathbb{L}$? This is the part that is only conjectural. I'm not sure how to relate a polynomial counting function directly to the dimension without having a polynomial in $\mathbb{L}$. However, assuming the Tate conjecture, then we actually have that $N(V)$ is polynomial in $q$ if and only if $[V]$ is polynomial in $\mathbb{L}$. So assuming the Tate conjecture, then for any variety $V$ such that $N(V)$ is a polynomial, the degree of $V$ agrees with the degree of $N(V)$. 
