Skip to content
Open
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
17 changes: 17 additions & 0 deletions crates/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,23 @@ impl Expr {
}
}

/// A literal unit value
pub fn unit() -> Self {
Self::Value(AlgebraicValue::unit(), AlgebraicType::unit())
}

/// A literal option none value
#[allow(non_snake_case)]
pub fn OptionNone(ty: AlgebraicType) -> Self {
Self::Value(AlgebraicValue::OptionNone(), AlgebraicType::option(ty))
}

/// A literal option none value
#[allow(non_snake_case)]
pub fn OptionSome(ty: AlgebraicType, v: AlgebraicValue) -> Self {
Self::Value(AlgebraicValue::OptionSome(v), AlgebraicType::option(ty))
}

/// A literal boolean value
pub const fn bool(v: bool) -> Self {
Self::Value(AlgebraicValue::Bool(v), AlgebraicType::Bool)
Expand Down
4 changes: 4 additions & 0 deletions crates/expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ fn _type_expr(vars: &Relvars, expr: SqlExpr, expected: Option<&AlgebraicType>, d
recursion::guard(depth, recursion::MAX_RECURSION_TYP_EXPR, "expr::type_expr")?;

match (expr, expected) {
(SqlExpr::Lit(SqlLiteral::Null), None) => Ok(Expr::unit()),
(SqlExpr::Lit(SqlLiteral::Null), Some(ty)) if ty.is_unit() => Ok(Expr::unit()),
(SqlExpr::Lit(SqlLiteral::Null), Some(ty)) if ty.is_option() => Ok(Expr::OptionNone(ty.clone())),
(SqlExpr::Lit(SqlLiteral::Null), Some(ty)) => Err(UnexpectedType::new(&AlgebraicType::unit(), ty).into()),
(SqlExpr::Lit(SqlLiteral::Bool(v)), None | Some(AlgebraicType::Bool)) => Ok(Expr::bool(v)),
(SqlExpr::Lit(SqlLiteral::Bool(_)), Some(ty)) => Err(UnexpectedType::new(&AlgebraicType::Bool, ty).into()),
(SqlExpr::Lit(SqlLiteral::Str(_) | SqlLiteral::Num(_) | SqlLiteral::Hex(_)), None) => {
Expand Down
19 changes: 19 additions & 0 deletions crates/expr/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ pub fn type_insert(insert: SqlInsert, tx: &impl SchemaView) -> TypingResult<Tabl
.map(|ColumnSchema { col_type, .. }| col_type),
) {
match (value, ty) {
(SqlLiteral::Null, _) if ty.is_unit() => {
values.push(AlgebraicValue::unit())
}
(SqlLiteral::Null, _) if ty.is_option() => {
values.push(AlgebraicValue::OptionNone())
}
(SqlLiteral::Null, _) => {
return Err(UnexpectedType::new(ty, &AlgebraicType::unit()).into());
}
(SqlLiteral::Bool(v), AlgebraicType::Bool) => {
values.push(AlgebraicValue::Bool(v));
}
Expand Down Expand Up @@ -212,6 +221,15 @@ pub fn type_update(update: SqlUpdate, tx: &impl SchemaView) -> TypingResult<Tabl
.get_column_by_name(&field)
.ok_or_else(|| Unresolved::field(&table_name, &field))?;
match (lit, ty) {
(SqlLiteral::Null, _) if ty.is_unit() => {
values.push((*col_id, AlgebraicValue::unit()));
}
(SqlLiteral::Null, _) if ty.is_option() => {
values.push((*col_id, AlgebraicValue::OptionNone()));
}
(SqlLiteral::Null, _) => {
return Err(UnexpectedType::new(ty, &AlgebraicType::unit()).into());
}
(SqlLiteral::Bool(v), AlgebraicType::Bool) => {
values.push((*col_id, AlgebraicValue::Bool(v)));
}
Expand Down Expand Up @@ -286,6 +304,7 @@ pub fn type_and_rewrite_set(set: SqlSet, tx: &impl SchemaView) -> TypingResult<T
}

match lit {
SqlLiteral::Null => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::unit()).into()),
SqlLiteral::Bool(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::Bool).into()),
SqlLiteral::Str(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::String).into()),
SqlLiteral::Hex(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::bytes()).into()),
Expand Down
2 changes: 2 additions & 0 deletions crates/sql-parser/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ impl From<Ident> for SqlIdent {
/// A SQL constant expression
#[derive(Debug)]
pub enum SqlLiteral {
/// Null literal
Null,
/// A boolean constant
Bool(bool),
/// A hex value like 0xFF or x'FF'
Expand Down
1 change: 1 addition & 0 deletions crates/sql-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub(crate) fn parse_binop(op: BinaryOperator) -> SqlParseResult<BinOp> {
/// Parse a literal expression
pub(crate) fn parse_literal(value: Value) -> SqlParseResult<SqlLiteral> {
match value {
Value::Null => Ok(SqlLiteral::Null),
Value::Boolean(v) => Ok(SqlLiteral::Bool(v)),
Value::Number(v, _) => Ok(SqlLiteral::Num(v.into_boxed_str())),
Value::SingleQuotedString(s) => Ok(SqlLiteral::Str(s.into_boxed_str())),
Expand Down