1: I’m not sure what you mean by “only retrieves resources”. The “state” here refers to the state of the data source, i.e. the value you are going to return. You set it so that it can be transmitted back to Terraform.
2: Yeah, the early return is obviously not necessary, the example was probably truncated from a larger one where an early return would avoid later logic in the function body.
Here is what I mean by “only retrieves resources.” I notice that whatever is fetched by the datasource is saved in the state file. What is the use case of this ?
That is, does terraform ever look at the state file for datasources ? If yes then why ?
How terraform stores the data is a separate implementation detail from the plugin protocol you are working with when implementing a provider.
Terraform does currently store the state returned by data resources, but only due to legacy constraints. That stored data is not used for evaluation by terraform, and can probably be dropped entirely in a future major release.