MCPcopy Create free account
hub / github.com/FirebirdSQL/firebird / checkSorts

Method checkSorts

src/jrd/optimizer/Optimizer.cpp:1862–2106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1860//
1861
1862void Optimizer::checkSorts()
1863{
1864 SortNode* sort = rse->rse_sorted;
1865 const auto sortCount = sort ? sort->expressions.getCount() : 0;
1866
1867 SortNode* project = rse->rse_projection;
1868 const auto projectCount = project ? project->expressions.getCount() : 0;
1869
1870 // Check if a GROUP BY exists using the same fields as the project or sort:
1871 // if so, the projection can be eliminated; if no projection exists, then
1872 // the sort can be eliminated
1873
1874 RecordSourceNode* subRse;
1875 AggregateSourceNode* aggregate;
1876 SortNode* group;
1877
1878 if ((project || sort) &&
1879 rse->rse_relations.getCount() == 1 &&
1880 (subRse = rse->rse_relations[0]) &&
1881 (aggregate = nodeAs<AggregateSourceNode>(subRse)) &&
1882 (group = aggregate->group))
1883 {
1884 const auto map = aggregate->map;
1885 const auto groupCount = group->expressions.getCount();
1886
1887 // If all the fields of the project are the same as all the fields
1888 // of the group by, get rid of the project
1889
1890 if (project && projectCount == groupCount)
1891 {
1892 bool equal = true;
1893 for (unsigned i = 0; i < groupCount; i++)
1894 {
1895 const auto groupNode = group->expressions[i];
1896 const auto projectNode = project->expressions[i];
1897
1898 if (!mapEqual(groupNode, projectNode, map))
1899 {
1900 equal = false;
1901 break;
1902 }
1903 }
1904
1905 // We can now ignore the project, but in case the project is being done
1906 // in descending order because of an order by, do the group by the same way.
1907 if (equal)
1908 {
1909 setDirection(project, group);
1910 project = rse->rse_projection = nullptr;
1911 }
1912 }
1913
1914 // If there is no projection, then we can make a similar optimization
1915 // for sort, except that sort may have fewer fields than group by
1916
1917 if (!project && sort && sortCount <= groupCount)
1918 {
1919 bool equal = true;

Callers

nothing calls this directly

Calls 9

mapEqualFunction · 0.85
setDirectionFunction · 0.85
setPositionFunction · 0.85
fieldEqualFunction · 0.85
getCountMethod · 0.45
collectStreamsMethod · 0.45
isInnerJoinMethod · 0.45
getStreamMethod · 0.45
isLeftJoinMethod · 0.45

Tested by

no test coverage detected