You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "suibianwanwank (via GitHub)" <gi...@apache.org> on 2024/08/28 16:09:23 UTC

[PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

suibianwanwank opened a new pull request, #3936:
URL: https://github.com/apache/calcite/pull/3936

   Document: https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-format


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738104077


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   I tested this example in mysql.
   `SELECT DATE_FORMAT('0000-01-01', '%X %V');`
   return 
   0000-01
   but
   `SELECT DATE_FORMAT(timestamp '0000-01-01', '%X %V');`
   return error: Incorrect DATETIME value: '0000-01-01'



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "caicancai (via GitHub)" <gi...@apache.org>.
caicancai commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739558445


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   > > SELECT DATE_FORMAT(timestamp '0000-01-01', '%X %V');
   > 
   > This failed because '0000-01-01' is not a timestamp. You can try:
   > 
   > ```
   > SELECT DATE_FORMAT(data '0000-01-01', '%X %V');
   > ```
   
   0000-01-01 is not even a valid date according to the SQL standard. 0000-01-01 is not even a valid date according to the SQL standard. It is difficult to implement the date_format standard in all dialects. Maybe we need to make some decisions



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739787327


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,60 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",

Review Comment:
   > We check the results by CalciteSqlOperatorTest. In SqlOperatorTest we check the return type.
   It looks like CalciteSqlOperatorTest is better suited for this test, do I need to modify this test into CalciteSqlOperatorTest as this looks like it needs result checking too



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,60 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",

Review Comment:
   > We check the results by CalciteSqlOperatorTest. In SqlOperatorTest we check the return type.
   
   It looks like CalciteSqlOperatorTest is better suited for this test, do I need to modify this test into CalciteSqlOperatorTest as this looks like it needs result checking too



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "asolimando (via GitHub)" <gi...@apache.org>.
asolimando commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2316118165

   > If you want deterministic tests I think you should not use the local timezone.
   
   @mihaibudiu is right, tests should be timezone agnostic, see [Cassandra tests](https://github.com/apache/calcite/blob/main/cassandra/src/test/java/org/apache/calcite/test/CassandraAdapterDataTypesTest.java#L196) for an example.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2316846023

   > You can see a solution in e.g., #3805, where the timezone is set before the tests and reset afterwards. It's sufficient to make your own tests pass.
   
   I've fixed my test by keeping the data consistent with other tests. But I think in some time zones, some tests may not pass if we don't specify the time zone.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738350543


##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -399,6 +480,27 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(work.yyyyFormat.format(date));
     }
   },
+  WFY("", "The year for the week where Sunday is the first day of the week") {

Review Comment:
   The result returned by this symbol is the number of years, "the week of the year" might not be appropriate



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "caicancai (via GitHub)" <gi...@apache.org>.
caicancai commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738725475


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   Yes, Calcite has its own date standard.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739201090


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);

Review Comment:
   There are three distinct data types in Calcite:
   TIMESTAMP
   TIMESTAMP WITH LOCAL TIME ZONE
   TIMESTAMP WITH TIME ZONE
   
   You have to specify the type these functions operate on. I think you probably want functions for each data type. The ones for TIMESTAMP and TIMESTAMP WITH TIMEZONE should be deterministic. And the ones for LOCAL TIME ZONE should be implemented in terms of the other ones by setting the timezone first. But you should not rely on the Java system timezone for this.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "caicancai (via GitHub)" <gi...@apache.org>.
caicancai commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1737663750


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   Does 0000-01-01 or BC date support format?



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738134180


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");

Review Comment:
   It throws an exception:Illegal TIMESTAMP literal, In the timestamp scenario, this is consistent with mysql's result.
   Yes, I do these tests in mysql . In fact, I write these test cases in mysql and get the results, and then add them to test to validate 



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739203430


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%l %p %U %u')",
+          "10 PM 39 40",
+          "VARCHAR NOT NULL");
+      f.checkQueryFails(StringAndPos.of("date_format(^timestamp '2006-06-00'^, '%d')"),
+          "Non-query expression encountered in illegal context");
+      f.checkNull("to_char(timestamp '2022-06-03 12:15:48.678', NULL)");
+//      f.checkString("date_format('1999-13-01', '%X %V')",
+//          null,

Review Comment:
   I see, the date is incorrect.
   In this case indeed you can probably not match exactly the behavior of mysql.
   But I think you should still make it a negative test and leave the comment in.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2315872231

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [5 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936)
   
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2315960288

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [5 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936)
   
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738113091


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);

Review Comment:
   For datetime functions, the implementation should be based on the user's time zone, just as other arbitrary databases support setting time zones. And other test do it.
   And other tests have done the same



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2316531148

   @mihaibudiu @asolimando I found this problem with all of this test classes, if the time zone is changed arbitrarily. For Calender's test, we need to set the time zone at this time like SqlOperatorTest. I'm not sure if this should be fixed in this PR .


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2321832851

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [3 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936)
   
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739805659


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);

Review Comment:
   Yes, this require more testing, including for BC dates.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2519022087

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 90 days if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@calcite.apache.org list. Thank you for your contributions.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "NobiGo (via GitHub)" <gi...@apache.org>.
NobiGo commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739524596


##########
site/_docs/reference.md:
##########
@@ -2944,6 +2944,7 @@ In the following:
 | b | TIME_SUB(time, interval)                       | Returns the TIME value that is *interval* before *time*
 | b | TIME_TRUNC(time, timeUnit)                     | Truncates *time* to the granularity of *timeUnit*, rounding to the beginning of the unit
 | m o p r | TO_CHAR(timestamp, format)               | Converts *timestamp* to a string using the format *format*
+| m | DATE_FORMAT(timestamp, format)                 | Converts *timestamp* to a string using the format *format*

Review Comment:
   Do DATE-FORMAT and TO_CHAR have the same functional description? Are their functions confirmed to be consistent?



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,60 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",

Review Comment:
   In MySQL,This will throw exception `ERROR 1525 (HY000) at line 1: Incorrect DATETIME value: '1994-01-01'`



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   > SELECT DATE_FORMAT(timestamp '0000-01-01', '%X %V');
   
   This failed because '0000-01-01' is not a timestamp. You can try:
   ```
   SELECT DATE_FORMAT(data '0000-01-01', '%X %V');
   ```



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -4919,7 +4920,8 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
   }
 
   @Test void testToCharPg() {
-    final SqlOperatorFixture f = fixture().withLibrary(SqlLibrary.POSTGRESQL);

Review Comment:
   We check the results by CalciteSqlOperatorTest. In SqlOperatorTest we check the return type.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2315955845

   If you want deterministic tests I think you should not use the local timezone.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738347582


##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -221,6 +245,33 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(String.format(Locale.ROOT, "%02d", calendar.get(Calendar.WEEK_OF_YEAR)));
     }
   },
+  V("", "The number of the year (Monday as the first day of the week),"

Review Comment:
   +1, i will fix it



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739780579


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,60 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",

Review Comment:
   Yes, I removed time to simplify testing Case, and in fact, Mysql doesn't support doing that. I'll add time to this test, but for this current test, I'm not sure we can be consistent with Mysql in Calcite



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739201965


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");

Review Comment:
   The Gregorian Calendar starts in 1582: https://en.wikipedia.org/wiki/Gregorian_calendar
   I don't expect that mysql fails in this case.
   Perhaps it fails for dates BC.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2316538622

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [5 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=3936&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=3936&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=3936&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=3936)
   
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2316532958

   You can see a solution in e.g., https://github.com/apache/calcite/pull/3805, where the timezone is set before the tests and reset afterwards. It's sufficient to make your own tests pass.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1736831132


##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -221,6 +245,33 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(String.format(Locale.ROOT, "%02d", calendar.get(Calendar.WEEK_OF_YEAR)));
     }
   },
+  V("", "The number of the year (Monday as the first day of the week),"

Review Comment:
   the comment seems wrong, it is about the week number



##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -326,7 +377,16 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(String.format(Locale.ROOT, "%03d", calendar.get(Calendar.MILLISECOND)));
     }
   },
-  SS("s", "The second as a decimal number (00-60)") {
+  MCS("", "The millisecond as a decimal number (000000-999999)") {
+    @Override public void format(StringBuilder sb, Date date) {
+      final Calendar calendar = Work.get().calendar;
+      calendar.setTime(date);
+      // It exceeds the precision of Calendar, we fill it with 0.

Review Comment:
   Milliseconds are usually between 0 and 999.
   



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");

Review Comment:
   can you add some tests with dates before the Gregorian calendar?
   I found that some of these fail.
   Also, can you say how you validated the outputs?
   Are these tested in MySQL?



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%l %p %U %u')",
+          "10 PM 39 40",
+          "VARCHAR NOT NULL");
+      f.checkQueryFails(StringAndPos.of("date_format(^timestamp '2006-06-00'^, '%d')"),
+          "Non-query expression encountered in illegal context");
+      f.checkNull("to_char(timestamp '2022-06-03 12:15:48.678', NULL)");
+//      f.checkString("date_format('1999-13-01', '%X %V')",
+//          null,

Review Comment:
   what you need is to use an implicit cast to timestamp if the input is a CHAR data type.
   you should be able to support that.



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);

Review Comment:
   You should not need to set the locale in the test if these functions are supposed to be deterministic.
   Perhaps the function implementation should set the locale.



##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -399,6 +480,27 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(work.yyyyFormat.format(date));
     }
   },
+  WFY("", "The year for the week where Sunday is the first day of the week") {
+    @Override public void format(StringBuilder sb, Date date) {
+      final Calendar calendar = Work.get().calendar;
+      calendar.setTime(date);
+      calendar.setFirstDayOfWeek(Calendar.SUNDAY);
+      // Day of year of the first Sunday of the year
+      int minimalDaysInFirstWeek =
+          getFirstWeekdayOfYear(DayOfWeek.MONDAY, calendar.get(Calendar.YEAR));
+      calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
+      sb.append(String.format(Locale.ROOT, "%04d", calendar.getWeekYear()));
+    }
+  },
+  WFY0("", "The year for the week where Monday is the first day of the week") {

Review Comment:
   same problem



##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -399,6 +480,27 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(work.yyyyFormat.format(date));
     }
   },
+  WFY("", "The year for the week where Sunday is the first day of the week") {

Review Comment:
   I think this should say "the week of the year"



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738414575


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%l %p %U %u')",
+          "10 PM 39 40",
+          "VARCHAR NOT NULL");
+      f.checkQueryFails(StringAndPos.of("date_format(^timestamp '2006-06-00'^, '%d')"),
+          "Non-query expression encountered in illegal context");
+      f.checkNull("to_char(timestamp '2022-06-03 12:15:48.678', NULL)");
+//      f.checkString("date_format('1999-13-01', '%X %V')",
+//          null,

Review Comment:
   Yes, implicit cast to timestamp this can be done in calcite, but if this cast fails, calcite will throw an exception, but for mysql, it will return null. 
   This is something that I don't think is easy to implement. unless we add a Config that adds a Config in the checksum phase and returns NULL if the implicit cast fails. WDYT



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-4275173023

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 90 days if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@calcite.apache.org list. Thank you for your contributions.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "caicancai (via GitHub)" <gi...@apache.org>.
caicancai commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739558445


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   > > SELECT DATE_FORMAT(timestamp '0000-01-01', '%X %V');
   > 
   > This failed because '0000-01-01' is not a timestamp. You can try:
   > 
   > ```
   > SELECT DATE_FORMAT(data '0000-01-01', '%X %V');
   > ```
   
   0000-01-01 is not even a valid date according to the SQL standard.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738357182


##########
core/src/main/java/org/apache/calcite/util/format/FormatElementEnum.java:
##########
@@ -326,7 +377,16 @@ public enum FormatElementEnum implements FormatElement {
       sb.append(String.format(Locale.ROOT, "%03d", calendar.get(Calendar.MILLISECOND)));
     }
   },
-  SS("s", "The second as a decimal number (00-60)") {
+  MCS("", "The millisecond as a decimal number (000000-999999)") {
+    @Override public void format(StringBuilder sb, Date date) {
+      final Calendar calendar = Work.get().calendar;
+      calendar.setTime(date);
+      // It exceeds the precision of Calendar, we fill it with 0.

Review Comment:
   This is an incorrect function description, it means microsecond here, I'll fix it



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1739793258


##########
site/_docs/reference.md:
##########
@@ -2944,6 +2944,7 @@ In the following:
 | b | TIME_SUB(time, interval)                       | Returns the TIME value that is *interval* before *time*
 | b | TIME_TRUNC(time, timeUnit)                     | Truncates *time* to the granularity of *timeUnit*, rounding to the beginning of the unit
 | m o p r | TO_CHAR(timestamp, format)               | Converts *timestamp* to a string using the format *format*
+| m | DATE_FORMAT(timestamp, format)                 | Converts *timestamp* to a string using the format *format*

Review Comment:
   Yeah, I think they're the same. In fact, Mysql does not have a to_char function.
   > {@code TO_CHAR} is not supported in MySQL, but it is supported in * MariaDB, a variant of MySQL covered by {@link SqlLibrary#MYSQL}.
   



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1738439735


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",

Review Comment:
   > It throws an exception:Illegal TIMESTAMP literal, In the timestamp scenario, this is consistent with mysql's result.
   
   In the Calcite test, this throws an exception because it can't be converted to a timestamp



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on code in PR #3936:
URL: https://github.com/apache/calcite/pull/3936#discussion_r1734965576


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",
+          "6 00 01 52 01 1996 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-05 22:23:00', '%w %U %u %V %v %X %x')",
+          "0 01 01 01 01 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-06 22:23:00', '%w %U %u %V %v %X %x')",
+          "1 01 02 01 02 1997 1997",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%V %v %X %x %%')",
+          "39 40 1997 1997 %",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%l %p %U %u')",
+          "10 PM 39 40",
+          "VARCHAR NOT NULL");
+      f.checkQueryFails(StringAndPos.of("date_format(^timestamp '2006-06-00'^, '%d')"),
+          "Non-query expression encountered in illegal context");
+      f.checkNull("to_char(timestamp '2022-06-03 12:15:48.678', NULL)");
+//      f.checkString("date_format('1999-13-01', '%X %V')",
+//          null,

Review Comment:
   MySQL supports this writing and will return null if the timestamp type is not declared and the parameter does not satisfy the timestamp format, which seems to be difficult to do in calcite



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5185,6 +5187,61 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
     }
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-6551">[CALCITE-6551]
+   * Add DATE_FORMAT function (enabled in MySQL library)</a>. */
+  @Test void testDateFormat() {
+    final SqlOperatorFixture f = fixture()
+        .withTester(t -> TESTER).withLibrary(SqlLibrary.MYSQL);
+    f.setFor(SqlLibraryOperators.DATE_FORMAT);
+    final Locale originalLocale = Locale.getDefault();
+
+    try {
+      Locale.setDefault(Locale.US);
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%W %M %Y')",
+          "Sunday October 2009",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '2009-10-04 22:23:00', '%H:%i:%s')",
+          "22:23:00",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1900-10-04 22:23:00', '%D %y %a %d %m %b %j')",
+          "4th 00 Thu 04 10 Oct 277",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00', '%H %k %I %r %T %S %w')",
+          "22 22 10 10:23:00 PM 22:23:00 00 6",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1999-01-01', '%X %V')",
+          "1998 52",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-10-04 22:23:00.000000', '%c %D %e %f %h')",
+          "10 4th 4 000000 10",
+          "VARCHAR NOT NULL");
+      f.checkString("date_format(timestamp '1997-01-04 22:23:00', '%w %U %u %V %v %X %x')",

Review Comment:
   Here are 4 cases testing mysql's weekForYear in two modes.
   https://stackoverflow.com/questions/30364141/mysql-convert-yearweek-to-date



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -4919,7 +4920,8 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
   }
 
   @Test void testToCharPg() {
-    final SqlOperatorFixture f = fixture().withLibrary(SqlLibrary.POSTGRESQL);

Review Comment:
   I noticed that the default tester doesn't check the results, so I added this.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-6551] Add DATE_FORMAT function (enabled in MySQL library) [calcite]

Posted by "suibianwanwank (via GitHub)" <gi...@apache.org>.
suibianwanwank commented on PR #3936:
URL: https://github.com/apache/calcite/pull/3936#issuecomment-2315952891

   The local time zone causes incorrect results and I need to check how to fix it.


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org