int sqlite3_vtab_distinct(sqlite3_index_info*);
This API may only be used from within an xBestIndex method of a virtual table implementation. The result of calling this interface from outside of xBestIndex() is undefined and probably harmful.
R-59232-02458:[The sqlite3_vtab_distinct() interface returns an integer between 0 and 3. ] The integer returned by sqlite3_vtab_distinct() gives the virtual table additional information about how the query planner wants the output to be ordered. As long as the virtual table can meet the ordering requirements of the query planner, it may set the "orderByConsumed" flag.
R-57761-09663:[If the sqlite3_vtab_distinct() interface returns 0, that means that the query planner needs the virtual table to return all rows in the sort order defined by the "nOrderBy" and "aOrderBy" fields of the sqlite3_index_info object. ] This is the default expectation. If the virtual table outputs all rows in sorted order, then it is always safe for the xBestIndex method to set the "orderByConsumed" flag, regardless of the return value from sqlite3_vtab_distinct().
R-08010-38213:[If the sqlite3_vtab_distinct() interface returns 1, that means that the query planner does not need the rows to be returned in sorted order as long as all rows with the same values in all columns identified by the "aOrderBy" field are adjacent. ] This mode is used when the query planner is doing a GROUP BY.
R-08996-44767:[If the sqlite3_vtab_distinct() interface returns 2, that means that the query planner does not need the rows returned in any particular order, as long as rows with the same values in all "aOrderBy" columns are adjacent. ] R-58848-61508:[Furthermore, only a single row for each particular combination of values in the columns identified by the "aOrderBy" field needs to be returned. ] R-57474-41961:[It is always ok for two or more rows with the same values in all "aOrderBy" columns to be returned, as long as all such rows are adjacent. ] R-11582-07116:[The virtual table may, if it chooses, omit extra rows that have the same value for all columns identified by "aOrderBy". ] R-08214-18010:[However omitting the extra rows is optional. ] This mode is used for a DISTINCT query.
R-52675-02101:[If the sqlite3_vtab_distinct() interface returns 3, that means that the query planner needs only distinct rows but it does need the rows to be sorted. ] R-00041-06536:[The virtual table implementation is free to omit rows that are identical in all aOrderBy columns, if it wants to, but it is not required to omit any rows. ] This mode is used for queries that have both DISTINCT and ORDER BY clauses.
R-32161-57285:[For the purposes of comparing virtual table output values to see if the values are same value for sorting purposes, two NULL values are considered to be the same. ] In other words, the comparison operator is "IS" (or "IS NOT DISTINCT FROM") and not "==".
If a virtual table implementation is unable to meet the requirements specified above, then it must not set the "orderByConsumed" flag in the sqlite3_index_info object or an incorrect answer may result.
R-55082-03288:[A virtual table implementation is always free to return rows in any order it wants, as long as the "orderByConsumed" flag is not set. ] R-34581-10147:[When the the "orderByConsumed" flag is unset, the query planner will add extra bytecode to ensure that the final results returned by the SQL query are ordered correctly. ] The use of the "orderByConsumed" flag and the sqlite3_vtab_distinct() interface is merely an optimization. R-50007-39580:[Careful use of the sqlite3_vtab_distinct() interface and the "orderByConsumed" flag might help queries against a virtual table to run faster. ] Being overly aggressive and setting the "orderByConsumed" flag when it is not valid to do so, on the other hand, might cause SQLite to return incorrect results.
See also lists of Objects, Constants, and Functions.
*** DRAFT ***