Skip to content

Commit

Permalink
Refactor codebase to optimize execution of logical plans and improve …
Browse files Browse the repository at this point in the history
…aggregation functionality
  • Loading branch information
holicc committed Sep 6, 2024
1 parent ee957e7 commit e94bae0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion qurious/src/execution/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ impl ExecuteSession {

pub fn execute_logical_plan(&self, plan: &LogicalPlan) -> Result<Vec<RecordBatch>> {
// let plan = self.optimizer.optimize(plan)?;

match &plan {
LogicalPlan::Ddl(ddl) => self.execute_ddl(ddl),
LogicalPlan::Dml(DmlStatement {
Expand Down Expand Up @@ -228,6 +227,7 @@ mod tests {

let batch = session.sql("select 1+0.1")?;


print_batches(&batch)?;

Ok(())
Expand Down
8 changes: 8 additions & 0 deletions qurious/src/logical/expr/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ impl BinaryExpr {
self.left,
Box::new(LogicalExpr::Cast(CastExpr::new(*self.right, DataType::LargeUtf8))),
),
(DataType::Float64, _) => (
Box::new(LogicalExpr::Cast(CastExpr::new(*self.left, DataType::Float64))),
self.right,
),
(_, DataType::Float64) => (
self.left,
Box::new(LogicalExpr::Cast(CastExpr::new(*self.right, DataType::Float64))),
),
_ => (self.left, self.right),
};

Expand Down
4 changes: 4 additions & 0 deletions sqlparser/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ impl Display for DataType {
}
}
}

pub struct Number{

}
5 changes: 5 additions & 0 deletions sqlparser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ macro_rules! generate_error_enum {
pub enum Error {
$($variant(crate::token::Token)),*,
ParseIntError(std::num::ParseIntError,crate::token::Token),
ParseFloatError(std::num::ParseFloatError,crate::token::Token),
DuplicateColumn(String),
UnKnownInfixOperator(String),
ParserError(String),
Expand All @@ -18,6 +19,9 @@ macro_rules! generate_error_enum {
Error::ParseIntError(e, token) => {
write!(f, "error: {} line: {} column: {}", e, token.location.line, token.location.column)
}
Error::ParseFloatError(e, token) => {
write!(f, "error: {} line: {} column: {}", e, token.location.line, token.location.column)
}
Error::DuplicateColumn(column) => {
write!(f, "error: duplicate column: {}", column)
}
Expand Down Expand Up @@ -45,6 +49,7 @@ impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::ParseIntError(e, _) => Some(e),
Error::ParseFloatError(e, _) => Some(e),
_ => None,
}
}
Expand Down
26 changes: 17 additions & 9 deletions sqlparser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ impl<'a> Lexer<'a> {
'\'' => {
break;
}
EMPTY_CHAR => {
return Token::new(TokenType::ILLIGAL, literal, self.location())
}
EMPTY_CHAR => return Token::new(TokenType::ILLIGAL, literal, self.location()),
_ => {
s.push(char::from(self.cur_ch));
}
Expand All @@ -108,7 +106,11 @@ impl<'a> Lexer<'a> {
return Token::new(token_type, literal, self.location());
}
b if b.is_ascii_digit() => {
return Token::new(TokenType::Int, self.read_number(), self.location());
let number = self.read_number();
if number.contains('.') {
return Token::new(TokenType::Float, number, self.location());
}
return Token::new(TokenType::Int, number, self.location());
}
_ => Token::new(TokenType::ILLIGAL, literal, self.location()),
};
Expand Down Expand Up @@ -142,10 +144,7 @@ impl<'a> Lexer<'a> {

fn read_literal(&mut self) -> String {
let mut literal = String::new();
while self.cur_ch.is_ascii_alphabetic()
|| self.cur_ch.is_ascii_alphanumeric()
|| self.cur_ch == '_'
{
while self.cur_ch.is_ascii_alphabetic() || self.cur_ch.is_ascii_alphanumeric() || self.cur_ch == '_' {
literal.push(self.cur_ch);
self.read_char();
}
Expand All @@ -155,7 +154,7 @@ impl<'a> Lexer<'a> {

fn read_number(&mut self) -> String {
let mut number = String::new();
while self.cur_ch.is_ascii_digit() {
while self.cur_ch.is_ascii_digit() || self.cur_ch == '.' {
number.push(self.cur_ch);
self.read_char();
}
Expand All @@ -178,6 +177,15 @@ mod tests {
use super::*;
use crate::token::{Keyword, TokenType};

#[test]
fn test_float() {
let input = "1.23";
let mut l = Lexer::new(input);
let tok = l.next();
assert_eq!(tok.token_type, TokenType::Float);
assert_eq!(tok.literal, "1.23");
}

#[test]
fn test_single_char_token() {
let input = "=-+(){},;*/<>!?";
Expand Down
11 changes: 11 additions & 0 deletions sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ impl<'a> Parser<'a> {
}
}
TokenType::Asterisk => Ok(ast::Expression::Identifier("*".into())),
TokenType::Float=> literal
.parse()
.map(|f| ast::Expression::Literal(ast::Literal::Float(f)))
.map_err(|e| Error::ParseFloatError(e, token)),
TokenType::Int => literal
.parse()
.map(|i| ast::Expression::Literal(ast::Literal::Int(i)))
Expand Down Expand Up @@ -3063,6 +3067,13 @@ mod tests {
);
}

#[test]
fn test_parse_float() {
let stmt = parse_expr("1.0").unwrap();

assert_eq!(stmt, Expression::Literal(ast::Literal::Float(1.0)));
}

#[test]
fn test_parse_integer() {
let stmt = parse_expr("123").unwrap();
Expand Down
1 change: 1 addition & 0 deletions sqlparser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum TokenType {
Ident,
String,
Int,
Float,

// Operators
Assign,
Expand Down

0 comments on commit e94bae0

Please sign in to comment.