danny0405 commented on a change in pull request #1271: [CALCITE-3112] Support
Window in RelToSqlConverter
URL: https://github.com/apache/calcite/pull/1271#discussion_r321970002
##########
File path:
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
##########
@@ -1826,6 +1827,88 @@ private void checkLiteral2(String expression, String
expected) {
sql(query).ok(expected);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-3112">[CALCITE-3112]
+ * Support Window in RelToSqlConverter</a>. */
+ @Test public void testConvertWinodwToSql() {
+ String query0 = "SELECT row_number() over (order by \"hire_date\") FROM
\"employee\"";
+ String expected0 = "SELECT ROW_NUMBER() OVER (ORDER BY \"hire_date\") AS
\"$0\"\n"
+ + "FROM \"foodmart\".\"employee\"";
+
+ String query1 = "SELECT rank() over (order by \"hire_date\") FROM
\"employee\"";
+ String expected1 = "SELECT RANK() OVER (ORDER BY \"hire_date\") AS
\"$0\"\n"
+ + "FROM \"foodmart\".\"employee\"";
+
+ String query2 = "SELECT lead(\"employee_id\",1,'NA') over "
+ + "(partition by \"hire_date\" order by \"employee_id\") FROM
\"employee\"";
+ String expected2 = "SELECT LEAD(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"hire_date\" "
+ + "ORDER BY \"employee_id\") AS \"$0\"\n"
+ + "FROM \"foodmart\".\"employee\"";
+
+ String query3 = "SELECT lag(\"employee_id\",1,'NA') over "
+ + "(partition by \"hire_date\" order by \"employee_id\") FROM
\"employee\"";
+ String expected3 = "SELECT LAG(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"hire_date\" ORDER BY \"employee_id\") AS
\"$0\"\n"
+ + "FROM \"foodmart\".\"employee\"";
+
+ String query4 = "SELECT lag(\"employee_id\",1,'NA') "
+ + "over (partition by \"hire_date\" order by \"employee_id\") as
lag1, "
+ + "lag(\"employee_id\",1,'NA') "
+ + "over (partition by \"birth_date\" order by \"employee_id\") as
lag2, "
+ + "count(*) over (partition by \"hire_date\" order by
\"employee_id\") as count1, "
+ + "count(*) over (partition by \"birth_date\" order by
\"employee_id\") as count2 "
+ + "FROM \"employee\"";
+ String expected4 = "SELECT LAG(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"hire_date\" ORDER BY \"employee_id\") AS
\"$0\", "
+ + "LAG(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"birth_date\" ORDER BY \"employee_id\") AS
\"$1\", "
+ + "COUNT(*) OVER (PARTITION BY \"hire_date\" ORDER BY
\"employee_id\" "
+ + "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"$2\", "
+ + "COUNT(*) OVER (PARTITION BY \"birth_date\" ORDER BY
\"employee_id\" "
+ + "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"$3\"\n"
+ + "FROM \"foodmart\".\"employee\"";
+
+ String query5 = "SELECT lag(\"employee_id\",1,'NA') "
+ + "over (partition by \"hire_date\" order by \"employee_id\") as
lag1, "
+ + "lag(\"employee_id\",1,'NA') "
+ + "over (partition by \"birth_date\" order by \"employee_id\") as
lag2, "
+ + "max(sum(\"employee_id\")) over (partition by \"hire_date\"
order by \"employee_id\") as count1, "
+ + "max(sum(\"employee_id\")) over (partition by \"birth_date\"
order by \"employee_id\") as count2 "
+ + "FROM \"employee\" group by \"employee_id\", \"hire_date\",
\"birth_date\"";
+ String expected5 = "SELECT LAG(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"hire_date\" ORDER BY \"employee_id\") AS
\"$0\", "
+ + "LAG(\"employee_id\", 1, 'NA') OVER "
+ + "(PARTITION BY \"birth_date\" ORDER BY \"employee_id\") AS
\"$1\", "
+ + "MAX(SUM(\"employee_id\")) OVER (PARTITION BY \"hire_date\"
ORDER BY \"employee_id\" "
+ + "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"$2\", "
+ + "MAX(SUM(\"employee_id\")) OVER (PARTITION BY \"birth_date\"
ORDER BY \"employee_id\" "
+ + "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"$3\"\n"
+ + "FROM \"foodmart\".\"employee\"\n"
+ + "GROUP BY \"employee_id\", \"hire_date\", \"birth_date\"";
+
+ String query6 = "SELECT lag(\"employee_id\",1,'NA') over "
+ + "(partition by \"hire_date\" order by \"employee_id\"), "
+ + "\"hire_date\" FROM \"employee\" group by \"hire_date\",
\"employee_id\"";
+ String expected6 = "SELECT LAG(\"employee_id\", 1, 'NA') "
Review comment:
This PR looks basically good, a code style suggestion: usually we put a \n
in each end of the sql statement line so they looks just the same as it shows
when we move it to other places.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services