DrPizza":3hbgjppj said:
Because f(X,Y) might not be unique for every unique pairing of X and Y, but I don't think there's any way to determine that computationally by analyzing the expression.
You didn't have any "f(X, Y)"; you had a simple string expression. I don't know, but it seems plausible to me that it could reason about that string operation and know that it wouldn't be a problem.
I thought it was pretty obvious that I was using a specific example but talking about a general problem. In a paragraph all by itself, I wrote:
This can't be fixed by having the system group by the individual fields instead of the expression. The system can't know if the result will be the same. Given two fields X and Y and an expression f(X,Y), it's possible that GROUP BY X, Y and GROUP BY f(X,Y) will produce different results.
SQL expressions come in so many varieties that I couldn't hope to cover them all with one example.
But, to address the specific example of simple string expressions: yes, you could do some reasoning to determine if the result of the expression produces a unique output for every input combination. Perhaps you'd start by saying that adding a static to a field ('<something>' + F or F + '<something>') is always just as unique as the field. You can concatenate fixed fields with a certain format requirement (e.g. bit or date) to a varchar without a problem (though you couldn't chain them -- varchar + bit + varchar no longer guarantees uniqueness).
You could concatenate an int with a varchar only if the two are separated by a static value that has a character in it that is not allowed in int.
So, for example, given an int i and string s, i + ' - ' + s (the original example), and i + ' ' + s would be unique for every (i,s). i + '-' + s would be too, but you'd have to add the additional reasoning that '-' can only appear at the beginning of an int in string form -- because s + '-' + i is not unique for every pairing.
But even once you'd added all of these rules to optimize performance, it still only improves performance in the broadest cases. The database engine knows nothing about your data, so it has to assume any field could contain anything. But maybe I have business rules in place ensuring a certain varchar field can't contain numbers (making any concatenation with an int field just as unique as the components), or disallowing empty strings, or imposing a certain format (e.g. phone number). All of these will affect the uniqueness of a particular string operation, and I may know as the developer than in a certain case, grouping by X,Y is equivalent to my expression, even though it would not be equivalent when assuming the fields could contain any allowable value.
So even though you could probably do it, I'm not sure it's a useful exercise. My original example was in the context of having the database engine automatically determine grouping, and it was intended to show that there may be situations where you have an expression f(X,Y) in the SELECT statement, which would force an automatic GROUP BY to group on f(X,Y) even in cases where GROUP BY X,Y would perform better and provide the same result. Yes, it would be possible, for some simple expressions, to programmatically determine if grouping by f(X,Y) is the same as grouping by X,Y and choose the latter for performance. But this couldn't be done generically to cover every possible expression, and what you'd end up with would be a bunch of special-case optimizations for the sole purpose of saving someone the trouble of typing a GROUP BY clause. I don't think it's worth it.