Store rel expr operands as `LitIR` reprs

next
Sayan Nandan 2 years ago
parent b918af6d96
commit 4c5e919954
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -32,7 +32,7 @@
use {
super::{
ast::Entity,
lexer::{Lit, Symbol, Token},
lexer::{Lit, LitIR, Symbol, Token},
LangError, LangResult, RawSlice,
},
crate::{
@ -77,13 +77,13 @@ fn process_entity(tok: &[Token], d: &mut MaybeInit<Entity>, i: &mut usize) -> bo
#[derive(Debug, PartialEq)]
pub struct RelationalExpr<'a> {
pub(super) lhs: &'a [u8],
pub(super) rhs: &'a Lit,
pub(super) rhs: LitIR<'a>,
pub(super) opc: u8,
}
impl<'a> RelationalExpr<'a> {
#[inline(always)]
pub(super) fn new(lhs: &'a [u8], rhs: &'a Lit, opc: u8) -> Self {
pub(super) fn new(lhs: &'a [u8], rhs: LitIR<'a>, opc: u8) -> RelationalExpr<'a> {
Self { lhs, rhs, opc }
}
pub(super) const OP_EQ: u8 = 1;
@ -138,11 +138,11 @@ impl<'a> RelationalExpr<'a> {
if compiler::likely(okay) {
Some(unsafe {
// UNSAFE(@ohsayan): tok[0] is checked for being an ident, tok[lit_idx] also checked to be a lit
Self {
lhs: extract!(tok[0], Token::Ident(ref id) => id.as_slice()),
rhs: extract!(tok[lit_idx], Token::Lit(ref l) => l),
opc: operator,
}
Self::new(
extract!(tok[0], Token::Ident(ref id) => id.as_slice()),
extract!(tok[lit_idx], Token::Lit(ref l) => l.as_ir()),
operator,
)
})
} else {
compiler::cold_val(None)

@ -82,6 +82,18 @@ pub enum Lit {
Bin(RawSlice),
}
impl Lit {
pub(super) unsafe fn as_ir<'a>(&'a self) -> LitIR<'a> {
match self {
Self::Str(s) => LitIR::Str(s.as_ref()),
Self::Bool(b) => LitIR::Bool(*b),
Self::UnsignedInt(u) => LitIR::UInt(*u),
Self::SignedInt(s) => LitIR::SInt(*s),
Self::Bin(b) => LitIR::Bin(b.as_slice()),
}
}
}
impl From<&'static str> for Lit {
fn from(s: &'static str) -> Self {
Self::Str(s.into())

@ -321,6 +321,6 @@ macro_rules! statictbl {
($name:ident: $kind:ty => [$($expr:expr),*]) => {{
const LEN: usize = {let mut i = 0;$(let _ = $expr; i += 1;)*i};
static $name: [$kind; LEN] = [$($expr),*];
$name
&'static $name
}};
}

@ -46,6 +46,7 @@ mod schema_tests;
mod structure_syn;
/// Uses the [`InsecureLexer`] to lex the given input
#[inline(always)]
pub(super) fn lex_insecure(src: &[u8]) -> LangResult<Vec<Token>> {
InsecureLexer::lex(src)
}

@ -472,6 +472,7 @@ mod stmt_select {
crate::engine::ql::{
ast::Entity,
dml::{self, SelectStatement},
lexer::LitIR,
},
};
#[test]
@ -483,14 +484,13 @@ mod stmt_select {
)
.unwrap();
let r = dml::parse_select_full(&tok[1..]).unwrap();
let username_where = "sayan".into();
let e = SelectStatement::new_test(
Entity::Single("users".into()),
[].to_vec(),
true,
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(), &username_where, RelationalExpr::OP_EQ
"username".as_bytes(), LitIR::Str("username"), RelationalExpr::OP_EQ
),
},
);
@ -505,14 +505,13 @@ mod stmt_select {
)
.unwrap();
let r = dml::parse_select_full(&tok[1..]).unwrap();
let username_where = "sayan".into();
let e = SelectStatement::new_test(
Entity::Single("users".into()),
["field1".into()].to_vec(),
false,
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(), &username_where, RelationalExpr::OP_EQ
"username".as_bytes(), LitIR::Str("username"), RelationalExpr::OP_EQ
),
},
);
@ -527,14 +526,13 @@ mod stmt_select {
)
.unwrap();
let r = dml::parse_select_full(&tok[1..]).unwrap();
let username_where = "sayan".into();
let e = SelectStatement::new_test(
Entity::Full("twitter".into(), "users".into()),
["field1".into()].to_vec(),
false,
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(), &username_where, RelationalExpr::OP_EQ
"username".as_bytes(), LitIR::Str("username"), RelationalExpr::OP_EQ
),
},
);
@ -549,14 +547,13 @@ mod stmt_select {
)
.unwrap();
let r = dml::parse_select_full(&tok[1..]).unwrap();
let username_where = "sayan".into();
let e = SelectStatement::new_test(
Entity::Full("twitter".into(), "users".into()),
["field1".into(), "field2".into()].to_vec(),
false,
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(), &username_where, RelationalExpr::OP_EQ
"username".as_bytes(), LitIR::Str("sayan"), RelationalExpr::OP_EQ
),
},
);
@ -645,6 +642,7 @@ mod update_statement {
dml::{
self, AssignmentExpression, Operator, RelationalExpr, UpdateStatement, WhereClause,
},
lexer::LitIR,
},
};
#[test]
@ -655,7 +653,6 @@ mod update_statement {
"#,
)
.unwrap();
let where_username = "sayan".into();
let note = "this is my new note".to_string().into();
let r = dml::parse_update_full(&tok[1..]).unwrap();
let e = UpdateStatement {
@ -668,7 +665,7 @@ mod update_statement {
wc: WhereClause::new(dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(),
&where_username,
LitIR::Str("sayan"),
RelationalExpr::OP_EQ
)
}),
@ -690,7 +687,6 @@ mod update_statement {
)
.unwrap();
let r = dml::parse_update_full(&tok[1..]).unwrap();
let where_username = "sayan".into();
let field_note = "this is my new note".into();
let field_email = "sayan@example.com".into();
let e = UpdateStatement {
@ -702,7 +698,7 @@ mod update_statement {
wc: WhereClause::new(dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(),
&where_username,
LitIR::Str("sayan"),
RelationalExpr::OP_EQ
)
}),
@ -717,6 +713,7 @@ mod delete_stmt {
crate::engine::ql::{
ast::Entity,
dml::{self, DeleteStatement, RelationalExpr},
lexer::LitIR,
},
};
@ -728,13 +725,12 @@ mod delete_stmt {
"#,
)
.unwrap();
let primary_key = "sayan".into();
let e = DeleteStatement::new_test(
Entity::Single("users".into()),
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(),
&primary_key,
LitIR::Str("sayan"),
RelationalExpr::OP_EQ
)
},
@ -750,13 +746,12 @@ mod delete_stmt {
"#,
)
.unwrap();
let primary_key = "sayan".into();
let e = DeleteStatement::new_test(
("twitter", "users").into(),
dict! {
"username".as_bytes() => RelationalExpr::new(
"username".as_bytes(),
&primary_key,
LitIR::Str("user"),
RelationalExpr::OP_EQ
)
},
@ -768,7 +763,10 @@ mod delete_stmt {
mod relational_expr {
use {
super::*,
crate::engine::ql::dml::{self, RelationalExpr},
crate::engine::ql::{
dml::{self, RelationalExpr},
lexer::LitIR,
},
};
#[test]
@ -778,7 +776,7 @@ mod relational_expr {
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
rhs: LitIR::UInt(10),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_EQ
}
@ -791,7 +789,7 @@ mod relational_expr {
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
rhs: LitIR::UInt(10),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_NE
}
@ -804,7 +802,7 @@ mod relational_expr {
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
rhs: LitIR::UInt(10),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_GT
}
@ -817,7 +815,7 @@ mod relational_expr {
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
rhs: LitIR::UInt(10),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_GE
}
@ -830,7 +828,7 @@ mod relational_expr {
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
rhs: LitIR::UInt(10),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_LT
}
@ -842,18 +840,21 @@ mod relational_expr {
let r = dml::parse_relexpr_full(&expr).unwrap();
assert_eq!(
r,
RelationalExpr {
rhs: &(10.into()),
lhs: "primary_key".as_bytes(),
opc: RelationalExpr::OP_LE
}
RelationalExpr::new(
"primary_key".as_bytes(),
LitIR::UInt(10),
RelationalExpr::OP_LE
)
);
}
}
mod where_clause {
use {
super::*,
crate::engine::ql::dml::{self, RelationalExpr, WhereClause},
crate::engine::ql::{
dml::{self, RelationalExpr, WhereClause},
lexer::LitIR,
},
};
#[test]
fn where_single() {
@ -863,13 +864,12 @@ mod where_clause {
"#,
)
.unwrap();
let rhs_hundred = 100.into();
let expected = WhereClause::new(dict! {
"x".as_bytes() => RelationalExpr {
rhs: &rhs_hundred,
lhs: "x".as_bytes(),
opc: RelationalExpr::OP_EQ
}
"x".as_bytes() => RelationalExpr::new(
"x".as_bytes(),
LitIR::UInt(100),
RelationalExpr::OP_EQ
)
});
assert_eq!(expected, dml::parse_where_clause_full(&tok).unwrap());
}
@ -881,19 +881,17 @@ mod where_clause {
"#,
)
.unwrap();
let rhs_hundred = 100.into();
let rhs_password = "password".into();
let expected = WhereClause::new(dict! {
"userid".as_bytes() => RelationalExpr {
rhs: &rhs_hundred,
lhs: "userid".as_bytes(),
opc: RelationalExpr::OP_EQ
},
"pass".as_bytes() => RelationalExpr {
rhs: &rhs_password,
lhs: "pass".as_bytes(),
opc: RelationalExpr::OP_EQ
}
"userid".as_bytes() => RelationalExpr::new(
"userid".as_bytes(),
LitIR::UInt(100),
RelationalExpr::OP_EQ
),
"pass".as_bytes() => RelationalExpr::new(
"pass".as_bytes(),
LitIR::Str("password"),
RelationalExpr::OP_EQ
)
});
assert_eq!(expected, dml::parse_where_clause_full(&tok).unwrap());
}

Loading…
Cancel
Save