Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support TPC-H Q9 #761

Merged
merged 10 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add function extract
Signed-off-by: Runji Wang <wangrunji0408@163.com>
  • Loading branch information
wangrunji0408 committed Jan 7, 2023
commit 5392ac8324424ba7c73632ce30560e88abb8e321
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
smallvec = { version = "1", features = ["serde"] }
sqllogictest = "0.9"
sqlparser = { version = "0.27", features = ["serde"] }
sqlparser = { version = "0.30", features = ["serde"] }
thiserror = "1"
tikv-jemallocator = { version = "0.5", optional = true, features=["disable_initial_exec_tls"] }
tokio = { version = "1", features = ["full"] }
Expand Down
7 changes: 7 additions & 0 deletions src/binder_v2/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Binder {
leading_field,
..
} => self.bind_interval(*value, leading_field),
Expr::Extract { field, expr } => self.bind_extract(field, *expr),
_ => todo!("bind expression: {:?}", expr),
}?;
self.check_type(id)?;
Expand Down Expand Up @@ -182,6 +183,12 @@ impl Binder {
Ok(self.egraph.add(Node::Constant(value)))
}

fn bind_extract(&mut self, field: DateTimeField, expr: Expr) -> Result {
let expr = self.bind_expr(expr)?;
let field = self.egraph.add(Node::Field(field.into()));
Ok(self.egraph.add(Node::Extract([field, expr])))
}

fn bind_function(&mut self, func: Function) -> Result {
// TODO: Support scalar function
let mut args = vec![];
Expand Down
5 changes: 3 additions & 2 deletions src/binder_v2/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Binder {
.insert(alias.value, ref_id);
select_list.push(id);
}
SelectItem::Wildcard => {
SelectItem::Wildcard(_) => {
select_list.append(&mut self.schema(from));
}
_ => todo!("bind select list"),
Expand Down Expand Up @@ -135,7 +135,8 @@ impl Binder {
}

/// Binds the VALUES clause. Returns a [`Values`](Node::Values) plan.
fn bind_values(&mut self, Values(values): Values) -> Result {
fn bind_values(&mut self, values: Values) -> Result {
let values = values.rows;
let mut bound_values = Vec::with_capacity(values.len());
if values.is_empty() {
return Ok(self.egraph.add(Node::Values([].into())));
Expand Down
5 changes: 5 additions & 0 deletions src/planner/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl Display for Explain<'_> {
self.expr(else_)
),

// functions
Extract([field, e]) => write!(f, "extract({} from {})", self.expr(field), self.expr(e)),
Field(field) => write!(f, "{field}"),

// aggregations
RowCount => write!(f, "rowcount"),
Max(a) | Min(a) | Sum(a) | Avg(a) | Count(a) | First(a) | Last(a) => {
write!(f, "{}({})", enode, self.expr(a))
Expand Down
8 changes: 6 additions & 2 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::binder_v2::copy::ExtSource;
use crate::binder_v2::{BoundDrop, CreateTable};
use crate::catalog::{ColumnRefId, TableRefId};
use crate::parser::{BinaryOperator, UnaryOperator};
use crate::types::{ColumnIndex, DataTypeKind, DataValue};
use crate::types::{ColumnIndex, DataTypeKind, DataValue, DateTimeField};

mod cost;
mod explain;
Expand Down Expand Up @@ -60,7 +60,11 @@ define_language! {

"if" = If([Id; 3]), // (if cond then else)

// aggregates
// functions
"extract" = Extract([Id; 2]), // (extract field expr)
Field(DateTimeField),

// aggregations
"max" = Max(Id),
"min" = Min(Id),
"sum" = Sum(Id),
Expand Down
5 changes: 5 additions & 0 deletions src/planner/rules/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ pub fn analyze_type(enode: &Expr, x: impl Fn(&Id) -> Type, catalog: &RootCatalog
// null ops
IsNull(_) => Ok(Kind::Bool.not_null()),

// functions
Extract([_, a]) => merge(enode, [x(a)?], |[a]| {
matches!(a, Kind::Date | Kind::Interval).then_some(Kind::Int32)
}),

// number agg
Max(a) | Min(a) => x(a),
Sum(a) => check(enode, x(a)?, |a| a.is_number()),
Expand Down
54 changes: 54 additions & 0 deletions src/types/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Date {
pub fn get_inner(&self) -> i32 {
self.0
}

pub fn extract(&self, field: DateTimeField) -> i32 {
todo!()
}
}

pub type ParseDateError = chrono::ParseError;
Expand Down Expand Up @@ -130,3 +134,53 @@ impl Display for Date {
)
}
}

#[derive(Debug, parse_display::Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[display("{0}")]
pub struct DateTimeField(sqlparser::ast::DateTimeField);

impl From<sqlparser::ast::DateTimeField> for DateTimeField {
fn from(field: sqlparser::ast::DateTimeField) -> Self {
DateTimeField(field)
}
}

impl FromStr for DateTimeField {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
use sqlparser::ast::DateTimeField::*;
Ok(Self(match s {
"YEAR" => Year,
"MONTH" => Month,
"WEEK" => Week,
"DAY" => Day,
"DATE" => Date,
"HOUR" => Hour,
"MINUTE" => Minute,
"SECOND" => Second,
"CENTURY" => Century,
"DECADE" => Decade,
"DOW" => Dow,
"DOY" => Doy,
"EPOCH" => Epoch,
"ISODOW" => Isodow,
"ISOYEAR" => Isoyear,
"JULIAN" => Julian,
"MICROSECOND" => Microsecond,
"MICROSECONDS" => Microseconds,
"MILLENIUM" => Millenium,
"MILLENNIUM" => Millennium,
"MILLISECOND" => Millisecond,
"MILLISECONDS" => Milliseconds,
"NANOSECOND" => Nanosecond,
"NANOSECONDS" => Nanoseconds,
"QUARTER" => Quarter,
"TIMEZONE" => Timezone,
"TIMEZONE_HOUR" => TimezoneHour,
"TIMEZONE_MINUTE" => TimezoneMinute,
"NODATETIME" => NoDateTime,
_ => return Err(()),
}))
}
}
2 changes: 1 addition & 1 deletion src/v1/binder/statement/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Binder {
column_ids,
column_types,
column_descs,
&values.0,
&values.rows,
),
_ => todo!("handle insert ???"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/v1/binder/statement/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Binder {
let expr = self.bind_alias(expr, alias.clone());
select_list.push(expr);
}
SelectItem::Wildcard => {
SelectItem::Wildcard(_) => {
select_list.extend_from_slice(self.bind_all_column_refs()?.as_slice())
}
_ => todo!("bind select list"),
Expand Down