MaxGekk commented on a change in pull request #32056:
URL: https://github.com/apache/spark/pull/32056#discussion_r607605794



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -834,4 +834,21 @@ object IntervalUtils {
    * @return The period of months, not null
    */
   def monthsToPeriod(months: Int): Period = 
Period.ofMonths(months).normalized()
+
+  /**
+   * Converts an year-month interval as a number of months to its textual 
representation
+   * which conforms to the ANSI SQL standard.
+   *
+   * @param months The number of months, positive or negative
+   * @return Year-month interval string
+   */
+  def toYMIntervalString(months: Int): String = {
+    var sign = ""
+    var absMonths: Long = months
+    if (months < 0) {
+      sign = "-"
+      absMonths = -absMonths
+    }
+    s"interval '$sign${absMonths / MONTHS_PER_YEAR}-${absMonths % 
MONTHS_PER_YEAR}' year to month"

Review comment:
       > The last part "year to month" looks a bit confusing to me
   
   This part conforms to the SQL standard, see:
   ```
   <interval literal> ::=
     INTERVAL [ <sign> ] <interval string> <interval qualifier>
   
   <interval qualifier> ::=
       <start field> TO <end field>
     | <single datetime field>
   <start field> ::=
   <non-second primary datetime field>
         [ <left paren> <interval leading field precision> <right paren> ]
   <end field> ::=
   <non-second primary datetime field>
     | SECOND [ <left paren> <interval fractional seconds precision> <right 
paren> ]
   <single datetime field> ::= <non-second primary datetime field>
           [ <left paren> <interval leading field precision> <right paren> ]
     | SECOND [ <left paren> <interval leading field precision>
         [ <comma> <interval fractional seconds precision> ] <right paren> ]
   <primary datetime field> ::= <non-second primary datetime field>
   | SECOND
   <non-second primary datetime field> ::= YEAR
     | MONTH
     | DAY
     | HOUR
     | MINUTE
   
   <interval string> ::=
     <quote> <unquoted interval string> <quote>
   <unquoted interval string> ::=
   [ <sign> ] { <year-month literal> | <day-time literal> }
   ```
   
   > What does other DBMS systems do?
   
   For instance, see Oracle 
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Literals.html#GUID-4C258D8F-3DF2-4D45-BE3E-14864DD77100




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to