Skip to content

Commit 52241ae

Browse files
committed
More tests, minor adjustements
1 parent 3641b55 commit 52241ae

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.util.TimeZone;
1919

20-
public abstract class BaseDateTimeFunction extends UnaryScalarFunction {
20+
abstract class BaseDateTimeFunction extends UnaryScalarFunction {
2121

2222
private final TimeZone timeZone;
2323
private final String name;
@@ -53,7 +53,6 @@ public TimeZone timeZone() {
5353
return timeZone;
5454
}
5555

56-
// add tz along the rest of the params
5756
@Override
5857
public String name() {
5958
return name;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeFunction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
2525
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
2626

27-
public abstract class NamedDateTimeFunction extends BaseDateTimeFunction {
27+
/*
28+
* Base class for the two "naming" date/time functions: month_name and day_name
29+
*/
30+
abstract class NamedDateTimeFunction extends BaseDateTimeFunction {
2831

2932
NamedDateTimeFunction(Location location, Expression field, TimeZone timeZone) {
3033
super(location, field, timeZone);
@@ -55,6 +58,7 @@ protected ScriptTemplate asScriptFrom(FieldAttribute field) {
5558
}
5659

5760
private String formatMethodName(String template) {
61+
// the Painless method name will be the enum's lower camelcase name
5862
return template.replace("{method_name}", StringUtils.underscoreToLowerCamelCase(nameExtractor().toString()));
5963
}
6064

x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScriptUtils {
1010

1111
Integer dateTimeChrono(long, String, String)
12-
String dayName(long,String)
13-
String monthName(long,String)
14-
Integer quarter(long,String)
12+
String dayName(long, String)
13+
String monthName(long, String)
14+
Integer quarter(long, String)
1515
Integer ascii(String)
1616
Integer bitLength(String)
1717
String character(Number)

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/QuarterProcessorTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class QuarterProcessorTests extends ESTestCase {
1616

1717
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
1818

19-
public void testQuarter() {
19+
public void testQuarterWithUTCTimezone() {
2020
QuarterProcessor proc = new QuarterProcessor(UTC);
2121

2222
assertEquals(1, proc.process(new DateTime(0L, DateTimeZone.UTC)));
@@ -28,4 +28,19 @@ public void testQuarter() {
2828
assertEquals(3, proc.process("-64164233612338"));
2929
assertEquals(2, proc.process("64164233612338"));
3030
}
31+
32+
public void testValidDayNamesWithNonUTCTimeZone() {
33+
QuarterProcessor proc = new QuarterProcessor(TimeZone.getTimeZone("GMT-10:00"));
34+
assertEquals(4, proc.process(new DateTime(0L, DateTimeZone.UTC)));
35+
assertEquals(4, proc.process(new DateTime(-5400, 1, 1, 5, 0, DateTimeZone.UTC)));
36+
assertEquals(1, proc.process(new DateTime(30, 4, 1, 9, 59, DateTimeZone.UTC)));
37+
38+
proc = new QuarterProcessor(TimeZone.getTimeZone("GMT+10:00"));
39+
assertEquals(4, proc.process(new DateTime(10902, 9, 30, 14, 1, DateTimeZone.UTC)));
40+
assertEquals(3, proc.process(new DateTime(10902, 9, 30, 13, 59, DateTimeZone.UTC)));
41+
42+
assertEquals(1, proc.process("0"));
43+
assertEquals(3, proc.process("-64164233612338"));
44+
assertEquals(2, proc.process("64164233612338"));
45+
}
3146
}

x-pack/qa/sql/src/main/resources/datetime.sql-spec

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212

1313
dateTimeDay
1414
SELECT DAY(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
15+
1516
dateTimeDayOfMonth
1617
SELECT DAY_OF_MONTH(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
18+
1719
dateTimeMonth
1820
SELECT MONTH(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
21+
1922
dateTimeYear
2023
SELECT YEAR(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
2124

@@ -31,10 +34,13 @@ SELECT QUARTER(hire_date) q, hire_date FROM test_emp ORDER BY hire_date LIMIT 15
3134
//
3235
// Filter
3336
//
37+
3438
dateTimeFilterDayOfMonth
3539
SELECT DAY_OF_MONTH(birth_date) AS d, last_name l FROM "test_emp" WHERE DAY_OF_MONTH(birth_date) <= 10 ORDER BY emp_no LIMIT 5;
40+
3641
dateTimeFilterMonth
3742
SELECT MONTH(birth_date) AS d, last_name l FROM "test_emp" WHERE MONTH(birth_date) <= 5 ORDER BY emp_no LIMIT 5;
43+
3844
dateTimeFilterYear
3945
SELECT YEAR(birth_date) AS d, last_name l FROM "test_emp" WHERE YEAR(birth_date) <= 1960 ORDER BY emp_no LIMIT 5;
4046

@@ -60,20 +66,30 @@ SELECT QUARTER(hire_date) quarter, hire_date FROM test_emp WHERE QUARTER(hire_da
6066
dateTimeAggByYear
6167
SELECT YEAR(birth_date) AS d, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY YEAR(birth_date) ORDER BY YEAR(birth_date) LIMIT 13;
6268

63-
dateTimeAggByMonth
69+
dateTimeAggByMonthWithOrderBy
6470
SELECT MONTH(birth_date) AS d, COUNT(*) AS c, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY MONTH(birth_date) ORDER BY MONTH(birth_date) DESC;
6571

66-
dateTimeAggByDayOfMonth
72+
dateTimeAggByDayOfMonthWithOrderBy
6773
SELECT DAY_OF_MONTH(birth_date) AS d, COUNT(*) AS c, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY DAY_OF_MONTH(birth_date) ORDER BY DAY_OF_MONTH(birth_date) DESC;
6874

6975
monthNameWithGroupBy
70-
select MONTHNAME("hire_date") AS month, COUNT(*) AS count FROM "test_emp" GROUP BY MONTHNAME("hire_date"), MONTH("hire_date") ORDER BY MONTH("hire_date");
76+
SELECT MONTHNAME("hire_date") AS month, COUNT(*) AS count FROM "test_emp" GROUP BY MONTHNAME("hire_date"), MONTH("hire_date") ORDER BY MONTH("hire_date");
77+
78+
dayNameWithHaving
79+
SELECT DAYNAME("hire_date") FROM "test_emp" GROUP BY DAYNAME("hire_date") HAVING MAX("emp_no") > ASCII(DAYNAME("hire_date"));
7180

72-
monthNameWithGroupByAndOrderBy
73-
select MONTHNAME("hire_date") AS month, COUNT(*) AS count FROM "test_emp" GROUP BY MONTHNAME("hire_date"), MONTH("hire_date") ORDER BY MONTHNAME("hire_date") DESC;
81+
monthNameWithDoubleGroupByAndOrderBy
82+
SELECT MONTHNAME("hire_date") AS month, COUNT(*) AS count FROM "test_emp" GROUP BY MONTHNAME("hire_date"), MONTH("hire_date") ORDER BY MONTHNAME("hire_date") DESC;
7483

75-
dayNameWithGroupByAndOrderBy
84+
// AwaitsFix https://github.com/elastic/elasticsearch/issues/33519
85+
// monthNameWithGroupByOrderByAndHaving
86+
// SELECT CAST(MAX("salary") AS DOUBLE) max_salary, MONTHNAME("hire_date") month_name FROM "test_emp" GROUP BY MONTHNAME("hire_date") HAVING MAX("salary") > 50000 ORDER BY MONTHNAME(hire_date);
87+
88+
dayNameWithDoubleGroupByAndOrderBy
7689
SELECT COUNT(*) c, DAYNAME(hire_date) day_name, DAY(hire_date) day FROM test_emp WHERE MONTHNAME(hire_date) = 'August' GROUP BY DAYNAME(hire_date), DAY(hire_date) ORDER BY DAYNAME(hire_date), DAY(hire_date);
7790

91+
dayNameWithGroupByOrderByAndHaving
92+
SELECT CAST(MAX(salary) AS DOUBLE) max_salary, DAYNAME(hire_date) day_name FROM test_emp GROUP BY DAYNAME(hire_date) HAVING MAX(salary) > 50000 ORDER BY DAYNAME("hire_date");
93+
7894
quarterWithGroupByAndOrderBy
7995
SELECT QUARTER(hire_date) quarter, COUNT(*) hires FROM test_emp GROUP BY QUARTER(hire_date) ORDER BY QUARTER(hire_date);

0 commit comments

Comments
 (0)