Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub trait Dialect {
false
}

fn interval_style(&self) -> IntervalStyle {
IntervalStyle::PostgresVerbose
}
// Does the dialect use DOUBLE PRECISION to represent Float64 rather than DOUBLE?
// E.g. Postgres uses DOUBLE PRECISION instead of DOUBLE
fn use_double_precision_for_float64(&self) -> bool {
Expand All @@ -55,6 +58,22 @@ pub trait Dialect {
false
}
}

/// `IntervalStyle` to use for unparsing
///
/// https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
/// different DBMS follows different standards, popular ones are:
/// postgres_verbose: '2 years 15 months 100 weeks 99 hours 123456789 milliseconds' which is
/// compatible with arrow display format, as well as duckdb
/// sql standard format is '1-2' for year-month, or '1 10:10:10.123456' for day-time
/// https://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
#[derive(Clone, Copy)]
pub enum IntervalStyle {
PostgresVerbose,
SQLStandard,
MySQL,
}

pub struct DefaultDialect {}

impl Dialect for DefaultDialect {
Expand All @@ -77,6 +96,10 @@ impl Dialect for PostgreSqlDialect {
Some('"')
}

fn interval_style(&self) -> IntervalStyle {
IntervalStyle::PostgresVerbose
}

fn use_double_precision_for_float64(&self) -> bool {
true
}
Expand All @@ -93,6 +116,10 @@ impl Dialect for MySqlDialect {
false
}

fn interval_style(&self) -> IntervalStyle {
IntervalStyle::MySQL
}

fn use_char_for_utf8_cast(&self) -> bool {
true
}
Expand All @@ -110,6 +137,7 @@ pub struct CustomDialect {
identifier_quote_style: Option<char>,
supports_nulls_first_in_sort: bool,
use_timestamp_for_date64: bool,
interval_style: IntervalStyle,
use_double_precision_for_float64: bool,
use_char_for_utf8_cast: bool,
}
Expand All @@ -120,6 +148,7 @@ impl Default for CustomDialect {
identifier_quote_style: None,
supports_nulls_first_in_sort: true,
use_timestamp_for_date64: false,
interval_style: IntervalStyle::SQLStandard,
use_double_precision_for_float64: false,
use_char_for_utf8_cast: false,
}
Expand Down Expand Up @@ -149,6 +178,10 @@ impl Dialect for CustomDialect {
self.use_timestamp_for_date64
}

fn interval_style(&self) -> IntervalStyle {
self.interval_style
}

fn use_double_precision_for_float64(&self) -> bool {
self.use_double_precision_for_float64
}
Expand All @@ -163,6 +196,7 @@ pub struct CustomDialectBuilder {
identifier_quote_style: Option<char>,
supports_nulls_first_in_sort: bool,
use_timestamp_for_date64: bool,
interval_style: IntervalStyle,
use_double_precision_for_float64: bool,
use_char_for_utf8_cast: bool,
}
Expand All @@ -173,6 +207,7 @@ impl CustomDialectBuilder {
identifier_quote_style: None,
supports_nulls_first_in_sort: true,
use_timestamp_for_date64: false,
interval_style: IntervalStyle::PostgresVerbose,
use_double_precision_for_float64: false,
use_char_for_utf8_cast: false,
}
Expand All @@ -183,6 +218,7 @@ impl CustomDialectBuilder {
identifier_quote_style: self.identifier_quote_style,
supports_nulls_first_in_sort: self.supports_nulls_first_in_sort,
use_timestamp_for_date64: self.use_timestamp_for_date64,
interval_style: self.interval_style,
use_double_precision_for_float64: self.use_double_precision_for_float64,
use_char_for_utf8_cast: self.use_char_for_utf8_cast,
}
Expand All @@ -209,6 +245,11 @@ impl CustomDialectBuilder {
self
}

pub fn with_interval_style(mut self, interval_style: IntervalStyle) -> Self {
self.interval_style = interval_style;
self
}

pub fn with_use_double_precision_for_float64(
mut self,
use_double_precision_for_float64: bool,
Expand Down
Loading