@@ -1784,4 +1784,134 @@ private boolean conditionExist(
17841784 }
17851785 return false ;
17861786 }
1787+
1788+ /** JOIN Aggregation Validation Tests */
1789+ private void expectJoinAggregationException () {
1790+ thrown .expect (SqlParseException .class );
1791+ thrown .expectMessage ("JOIN queries do not support aggregations on the joined result." );
1792+ }
1793+
1794+ @ Test
1795+ public void joinWithGroupByShouldThrowException () throws SqlParseException {
1796+ expectJoinAggregationException ();
1797+
1798+ String query =
1799+ String .format (
1800+ Locale .ROOT ,
1801+ "SELECT a.firstname FROM %s/account a "
1802+ + "JOIN %s/account b ON a.account_number = b.account_number "
1803+ + "GROUP BY a.firstname" ,
1804+ TestsConstants .TEST_INDEX_ACCOUNT ,
1805+ TestsConstants .TEST_INDEX_ACCOUNT );
1806+
1807+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1808+ }
1809+
1810+ @ Test
1811+ public void joinWithCountFunctionShouldThrowException () throws SqlParseException {
1812+ expectJoinAggregationException ();
1813+
1814+ String query =
1815+ String .format (
1816+ Locale .ROOT ,
1817+ "SELECT COUNT(*) FROM %s/account a "
1818+ + "JOIN %s/account b ON a.account_number = b.account_number" ,
1819+ TestsConstants .TEST_INDEX_ACCOUNT ,
1820+ TestsConstants .TEST_INDEX_ACCOUNT );
1821+
1822+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1823+ }
1824+
1825+ @ Test
1826+ public void joinWithSumFunctionShouldThrowException () throws SqlParseException {
1827+ expectJoinAggregationException ();
1828+
1829+ String query =
1830+ String .format (
1831+ Locale .ROOT ,
1832+ "SELECT SUM(a.balance) FROM %s/account a "
1833+ + "JOIN %s/account b ON a.account_number = b.account_number" ,
1834+ TestsConstants .TEST_INDEX_ACCOUNT ,
1835+ TestsConstants .TEST_INDEX_ACCOUNT );
1836+
1837+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1838+ }
1839+
1840+ @ Test
1841+ public void joinWithAvgFunctionShouldThrowException () throws SqlParseException {
1842+ expectJoinAggregationException ();
1843+
1844+ String query =
1845+ String .format (
1846+ Locale .ROOT ,
1847+ "SELECT AVG(a.age) FROM %s/account a "
1848+ + "JOIN %s/account b ON a.account_number = b.account_number" ,
1849+ TestsConstants .TEST_INDEX_ACCOUNT ,
1850+ TestsConstants .TEST_INDEX_ACCOUNT );
1851+
1852+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1853+ }
1854+
1855+ @ Test
1856+ public void joinWithMaxFunctionShouldThrowException () throws SqlParseException {
1857+ expectJoinAggregationException ();
1858+
1859+ String query =
1860+ String .format (
1861+ Locale .ROOT ,
1862+ "SELECT MAX(a.balance) FROM %s/account a "
1863+ + "JOIN %s/account b ON a.account_number = b.account_number" ,
1864+ TestsConstants .TEST_INDEX_ACCOUNT ,
1865+ TestsConstants .TEST_INDEX_ACCOUNT );
1866+
1867+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1868+ }
1869+
1870+ @ Test
1871+ public void joinWithMinFunctionShouldThrowException () throws SqlParseException {
1872+ expectJoinAggregationException ();
1873+
1874+ String query =
1875+ String .format (
1876+ Locale .ROOT ,
1877+ "SELECT MIN(a.age) FROM %s/account a "
1878+ + "JOIN %s/account b ON a.account_number = b.account_number" ,
1879+ TestsConstants .TEST_INDEX_ACCOUNT ,
1880+ TestsConstants .TEST_INDEX_ACCOUNT );
1881+
1882+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1883+ }
1884+
1885+ @ Test
1886+ public void joinWithBothGroupByAndAggregateShouldThrowException () throws SqlParseException {
1887+ expectJoinAggregationException ();
1888+
1889+ String query =
1890+ String .format (
1891+ Locale .ROOT ,
1892+ "SELECT a.gender, COUNT(*) FROM %s/account a "
1893+ + "JOIN %s/account b ON a.account_number = b.account_number "
1894+ + "GROUP BY a.gender" ,
1895+ TestsConstants .TEST_INDEX_ACCOUNT ,
1896+ TestsConstants .TEST_INDEX_ACCOUNT );
1897+
1898+ parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1899+ }
1900+
1901+ @ Test
1902+ public void regularJoinWithoutAggregationShouldWork () throws SqlParseException {
1903+ String query =
1904+ String .format (
1905+ Locale .ROOT ,
1906+ "SELECT a.firstname, b.lastname FROM %s/account a "
1907+ + "JOIN %s/account b ON a.account_number = b.account_number "
1908+ + "WHERE a.age > 20 LIMIT 10" ,
1909+ TestsConstants .TEST_INDEX_ACCOUNT ,
1910+ TestsConstants .TEST_INDEX_ACCOUNT );
1911+
1912+ JoinSelect joinSelect = parser .parseJoinSelect ((SQLQueryExpr ) queryToExpr (query ));
1913+ Assert .assertNotNull ("Join select should be parsed successfully" , joinSelect );
1914+ Assert .assertNotNull ("First table should not be null" , joinSelect .getFirstTable ());
1915+ Assert .assertNotNull ("Second table should not be null" , joinSelect .getSecondTable ());
1916+ }
17871917}
0 commit comments