Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/lykia-rs/lykiadb into featu…
Browse files Browse the repository at this point in the history
…re/planner
  • Loading branch information
can-keklik committed Dec 16, 2024
2 parents 2265529 + 8339ad8 commit 6355a06
Show file tree
Hide file tree
Showing 21 changed files with 466 additions and 506 deletions.
18 changes: 13 additions & 5 deletions lykiadb-lang/benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lykiadb_lang::{parser::{program::Program, resolver::Resolver, Parser}, tokenizer::scanner::Scanner, Locals, Scopes};
use lykiadb_lang::{
parser::{program::Program, resolver::Resolver, Parser},
tokenizer::scanner::Scanner,
Locals, Scopes,
};
use rustc_hash::FxHashMap;

pub struct ParserBenchmark {
scopes: Scopes,
locals: Locals,
}

impl Default for ParserBenchmark {
fn default() -> Self {
Self::new()
}
}

impl ParserBenchmark {
pub fn new() -> ParserBenchmark {
Expand Down Expand Up @@ -49,16 +58,15 @@ fn runtime() {
EXCEPT
SELECT * FROM books;
".to_string();
"
.to_string();
let mut parser = black_box(ParserBenchmark::new());
black_box(parser.process(black_box(&content)));
}

fn bench(c: &mut Criterion) {
let mut group = c.benchmark_group("sample-size-example");
group.bench_function("2-way join", |b| {
b.iter(|| runtime())
});
group.bench_function("2-way join", |b| b.iter(runtime));
group.finish();
}

Expand Down
17 changes: 3 additions & 14 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ use serde::{Deserialize, Serialize};

use std::{fmt::Display, sync::Arc};

use crate::{Identifier, Span, Spanned};

use super::{
sql::{SqlDelete, SqlInsert, SqlSelect, SqlUpdate},
stmt::Stmt,
AstNode,
AstNode, Identifier, Literal, Span, Spanned,
};

use crate::Literal;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(tag = "@type")]
pub enum Operation {
Expand Down Expand Up @@ -497,7 +493,7 @@ impl Expr {
pub mod test {
use std::collections::HashSet;

use crate::{ast::expr::Expr, Literal, Span};
use crate::ast::expr::Expr;

use super::*;

Expand Down Expand Up @@ -703,12 +699,10 @@ pub mod test {
});
assert_eq!(visited, vec![3, 1, 2]);
}

}

#[test]
fn test_expr_get_id() {

// Test Variable
let var_expr = Expr::Variable {
name: Identifier::new("test_var", false),
Expand Down Expand Up @@ -822,7 +816,6 @@ pub mod test {
fn test_expr_get_span() {
let test_span = Span::default();


// Test Variable
let var_expr = Expr::Variable {
name: Identifier::new("test_var", false),
Expand Down Expand Up @@ -1073,10 +1066,7 @@ pub mod test {
fn test_function_display() {
let func = Expr::Function {
name: Some(Identifier::new("test_func", false)),
parameters: vec![
Identifier::new("a", false),
Identifier::new("b", false),
],
parameters: vec![Identifier::new("a", false), Identifier::new("b", false)],
body: Arc::new(vec![]),
span: Span::default(),
id: 1,
Expand Down Expand Up @@ -1139,7 +1129,6 @@ pub mod test {
assert_eq!(call.to_string(), "test_func(Num(1.0), Num(2.0))");
}


pub fn create_simple_add_expr(id: usize, left: f64, right: f64) -> Expr {
Expr::Binary {
left: Box::new(Expr::Literal {
Expand Down
107 changes: 106 additions & 1 deletion lykiadb-lang/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use crate::Spanned;
use std::{
fmt::{Display, Formatter},
hash::Hash,
sync::Arc,
};

use derivative::Derivative;
use expr::Expr;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

pub mod expr;
pub mod sql;
Expand All @@ -8,3 +17,99 @@ pub mod visitor;
pub trait AstNode: Spanned {
fn get_id(&self) -> usize;
}

#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: u32,
pub line_end: u32,
}

pub trait Spanned {
fn get_span(&self) -> Span;
}

impl Spanned for Span {
fn get_span(&self) -> Span {
*self
}
}

impl Span {
pub fn merge(&self, other: &Span) -> Span {
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
line: self.line.min(other.line),
line_end: self.line_end.min(other.line_end),
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Str(Arc<String>),
Num(f64),
Bool(bool),
Undefined,
Object(FxHashMap<String, Box<Expr>>),
Array(Vec<Expr>),
NaN,
Null,
}

impl Literal {
pub fn as_str(&self) -> Option<&str> {
match self {
Literal::Str(s) => Some(s),
_ => None,
}
}
}

impl Hash for Literal {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Literal::Str(s) => s.hash(state),
Literal::Num(n) => n.to_bits().hash(state),
Literal::Bool(b) => b.hash(state),
Literal::Object(o) => (o as *const _ as usize).hash(state),
Literal::Array(a) => a.hash(state),
//
Literal::Undefined => "undefined".hash(state),
Literal::NaN => "NaN".hash(state),
Literal::Null => "null".hash(state),
}
}
}

impl Eq for Literal {}

#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
#[serde(tag = "@type")]
#[derivative(Eq, PartialEq, Hash)]
pub struct Identifier {
pub name: String,
pub dollar: bool,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
pub span: Span,
}

impl Identifier {
pub fn new(name: &str, dollar: bool) -> Self {
Identifier {
name: name.to_string(),
dollar,
span: Span::default(),
}
}
}

impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
3 changes: 1 addition & 2 deletions lykiadb-lang/src/ast/sql.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::Identifier;
use serde::{Deserialize, Serialize};

use super::expr::Expr;
use super::{expr::Expr, Identifier};

// Enums

Expand Down
12 changes: 4 additions & 8 deletions lykiadb-lang/src/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use derivative::Derivative;
use serde::{Deserialize, Serialize};

use crate::{Identifier, Span, Spanned};

use super::expr::Expr;
use super::{expr::Expr, Identifier, Span, Spanned};

#[derive(Debug, Serialize, Deserialize, Clone, Derivative)]
#[serde(tag = "@type")]
Expand Down Expand Up @@ -106,11 +104,9 @@ impl Spanned for Stmt {
mod test {
use std::collections::HashSet;

use crate::{
ast::{
expr::{test::create_simple_add_expr, Expr},
stmt::Stmt,
},
use crate::ast::{
expr::{test::create_simple_add_expr, Expr},
stmt::Stmt,
Span,
};

Expand Down
Loading

0 comments on commit 6355a06

Please sign in to comment.