1212
1313#[ cfg( not( feature = "std" ) ) ]
1414use alloc:: string:: String ;
15+
16+ #[ cfg( not( feature = "std" ) ) ]
17+ use alloc:: format;
18+
19+ #[ cfg( not( feature = "std" ) ) ]
20+ use alloc:: string:: ToString ;
21+
1522use core:: fmt;
1623
1724#[ cfg( feature = "bigdecimal" ) ]
@@ -20,6 +27,7 @@ use bigdecimal::BigDecimal;
2027#[ cfg( feature = "serde" ) ]
2128use serde:: { Deserialize , Serialize } ;
2229
30+ use crate :: ast:: Ident ;
2331#[ cfg( feature = "visitor" ) ]
2432use sqlparser_derive:: { Visit , VisitMut } ;
2533
@@ -109,17 +117,25 @@ impl fmt::Display for DollarQuotedString {
109117 }
110118}
111119
112- #[ derive( Debug , Copy , Clone , PartialEq , Eq , Ord , PartialOrd , Hash ) ]
120+ #[ derive( Debug , Clone , PartialEq , Eq , Ord , PartialOrd , Hash ) ]
113121#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
114122#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
115123pub enum DateTimeField {
116124 Year ,
117125 Month ,
118- Week ,
126+ /// Week optionally followed by a WEEKDAY.
127+ ///
128+ /// ```sql
129+ /// WEEK(MONDAY)
130+ /// ```
131+ ///
132+ /// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract)
133+ Week ( Option < Ident > ) ,
119134 Day ,
120135 DayOfWeek ,
121136 DayOfYear ,
122137 Date ,
138+ Datetime ,
123139 Hour ,
124140 Minute ,
125141 Second ,
@@ -148,47 +164,67 @@ pub enum DateTimeField {
148164 TimezoneMinute ,
149165 TimezoneRegion ,
150166 NoDateTime ,
167+ /// Arbitrary abbreviation or custom date-time part.
168+ ///
169+ /// ```sql
170+ /// EXTRACT(q FROM CURRENT_TIMESTAMP)
171+ /// ```
172+ /// [Snowflake](https://docs.snowflake.com/en/sql-reference/functions-date-time#supported-date-and-time-parts)
173+ Custom ( Ident ) ,
151174}
152175
153176impl fmt:: Display for DateTimeField {
154177 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
155- f. write_str ( match self {
156- DateTimeField :: Year => "YEAR" ,
157- DateTimeField :: Month => "MONTH" ,
158- DateTimeField :: Week => "WEEK" ,
159- DateTimeField :: Day => "DAY" ,
160- DateTimeField :: DayOfWeek => "DAYOFWEEK" ,
161- DateTimeField :: DayOfYear => "DAYOFYEAR" ,
162- DateTimeField :: Date => "DATE" ,
163- DateTimeField :: Hour => "HOUR" ,
164- DateTimeField :: Minute => "MINUTE" ,
165- DateTimeField :: Second => "SECOND" ,
166- DateTimeField :: Century => "CENTURY" ,
167- DateTimeField :: Decade => "DECADE" ,
168- DateTimeField :: Dow => "DOW" ,
169- DateTimeField :: Doy => "DOY" ,
170- DateTimeField :: Epoch => "EPOCH" ,
171- DateTimeField :: Isodow => "ISODOW" ,
172- DateTimeField :: Isoyear => "ISOYEAR" ,
173- DateTimeField :: IsoWeek => "ISOWEEK" ,
174- DateTimeField :: Julian => "JULIAN" ,
175- DateTimeField :: Microsecond => "MICROSECOND" ,
176- DateTimeField :: Microseconds => "MICROSECONDS" ,
177- DateTimeField :: Millenium => "MILLENIUM" ,
178- DateTimeField :: Millennium => "MILLENNIUM" ,
179- DateTimeField :: Millisecond => "MILLISECOND" ,
180- DateTimeField :: Milliseconds => "MILLISECONDS" ,
181- DateTimeField :: Nanosecond => "NANOSECOND" ,
182- DateTimeField :: Nanoseconds => "NANOSECONDS" ,
183- DateTimeField :: Quarter => "QUARTER" ,
184- DateTimeField :: Time => "TIME" ,
185- DateTimeField :: Timezone => "TIMEZONE" ,
186- DateTimeField :: TimezoneAbbr => "TIMEZONE_ABBR" ,
187- DateTimeField :: TimezoneHour => "TIMEZONE_HOUR" ,
188- DateTimeField :: TimezoneMinute => "TIMEZONE_MINUTE" ,
189- DateTimeField :: TimezoneRegion => "TIMEZONE_REGION" ,
190- DateTimeField :: NoDateTime => "NODATETIME" ,
191- } )
178+ f. write_str (
179+ match self {
180+ DateTimeField :: Year => "YEAR" . to_string ( ) ,
181+ DateTimeField :: Month => "MONTH" . to_string ( ) ,
182+ DateTimeField :: Week ( week_day) => {
183+ format ! (
184+ "WEEK{}" ,
185+ week_day
186+ . as_ref( )
187+ . map( |w| format!( "({w})" ) )
188+ . unwrap_or_default( )
189+ )
190+ }
191+ DateTimeField :: Day => "DAY" . to_string ( ) ,
192+ DateTimeField :: DayOfWeek => "DAYOFWEEK" . to_string ( ) ,
193+ DateTimeField :: DayOfYear => "DAYOFYEAR" . to_string ( ) ,
194+ DateTimeField :: Date => "DATE" . to_string ( ) ,
195+ DateTimeField :: Datetime => "DATETIME" . to_string ( ) ,
196+ DateTimeField :: Hour => "HOUR" . to_string ( ) ,
197+ DateTimeField :: Minute => "MINUTE" . to_string ( ) ,
198+ DateTimeField :: Second => "SECOND" . to_string ( ) ,
199+ DateTimeField :: Century => "CENTURY" . to_string ( ) ,
200+ DateTimeField :: Decade => "DECADE" . to_string ( ) ,
201+ DateTimeField :: Dow => "DOW" . to_string ( ) ,
202+ DateTimeField :: Doy => "DOY" . to_string ( ) ,
203+ DateTimeField :: Epoch => "EPOCH" . to_string ( ) ,
204+ DateTimeField :: Isodow => "ISODOW" . to_string ( ) ,
205+ DateTimeField :: Isoyear => "ISOYEAR" . to_string ( ) ,
206+ DateTimeField :: IsoWeek => "ISOWEEK" . to_string ( ) ,
207+ DateTimeField :: Julian => "JULIAN" . to_string ( ) ,
208+ DateTimeField :: Microsecond => "MICROSECOND" . to_string ( ) ,
209+ DateTimeField :: Microseconds => "MICROSECONDS" . to_string ( ) ,
210+ DateTimeField :: Millenium => "MILLENIUM" . to_string ( ) ,
211+ DateTimeField :: Millennium => "MILLENNIUM" . to_string ( ) ,
212+ DateTimeField :: Millisecond => "MILLISECOND" . to_string ( ) ,
213+ DateTimeField :: Milliseconds => "MILLISECONDS" . to_string ( ) ,
214+ DateTimeField :: Nanosecond => "NANOSECOND" . to_string ( ) ,
215+ DateTimeField :: Nanoseconds => "NANOSECONDS" . to_string ( ) ,
216+ DateTimeField :: Quarter => "QUARTER" . to_string ( ) ,
217+ DateTimeField :: Time => "TIME" . to_string ( ) ,
218+ DateTimeField :: Timezone => "TIMEZONE" . to_string ( ) ,
219+ DateTimeField :: TimezoneAbbr => "TIMEZONE_ABBR" . to_string ( ) ,
220+ DateTimeField :: TimezoneHour => "TIMEZONE_HOUR" . to_string ( ) ,
221+ DateTimeField :: TimezoneMinute => "TIMEZONE_MINUTE" . to_string ( ) ,
222+ DateTimeField :: TimezoneRegion => "TIMEZONE_REGION" . to_string ( ) ,
223+ DateTimeField :: NoDateTime => "NODATETIME" . to_string ( ) ,
224+ DateTimeField :: Custom ( custom) => format ! ( "{custom}" ) ,
225+ }
226+ . as_str ( ) ,
227+ )
192228 }
193229}
194230
0 commit comments