Skip to content

Commit

Permalink
dataflow-expression: Add JsonSubtract operator
Browse files Browse the repository at this point in the history
This new operator represents the `-` operator as applied to JSONB. As
such, we now branch on the left operand's type during lowering time to
determine whether to convert the `Subtract` AST node to the existing
`Subtract` Dataflow operator or the new `JsonSubtract` Dataflow
operator.

For now, nothing is implemented to evaluate this operator; that will be
handled in a future commit.

Change-Id: Ib4327554af43f0edcff5560e0e066b3cac7f2f7d
Reviewed-on: https://gerrit.readyset.name/c/readyset/+/3692
Tested-by: Buildkite CI
Reviewed-by: Griffin Smith <griffin@readyset.io>
  • Loading branch information
nickelization committed Nov 11, 2022
1 parent e5bb034 commit 3753cb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
29 changes: 26 additions & 3 deletions dataflow-expression/src/binary_operator.rs
Expand Up @@ -104,14 +104,17 @@ pub enum BinaryOperator {

/// `<@`
JsonContainedIn,

/// `-` applied to the PostreSQL JSONB type
JsonSubtract,
}

impl BinaryOperator {
/// Converts from a [`nom_sql::BinaryOperator`] within the context of a SQL [`Dialect`].
pub fn from_sql_op(
op: SqlBinaryOperator,
dialect: Dialect,
_left_type: &DfType,
left_type: &DfType,
_right_type: &DfType,
) -> ReadySetResult<Self> {
use SqlBinaryOperator::*;
Expand All @@ -123,7 +126,13 @@ impl BinaryOperator {
Less => Self::Less,
LessOrEqual => Self::LessOrEqual,
Add => Self::Add,
Subtract => Self::Subtract,
Subtract => {
if left_type.is_jsonb() {
Self::JsonSubtract
} else {
Self::Subtract
}
}
Multiply => Self::Multiply,
Divide => Self::Divide,
Like => Self::Like,
Expand Down Expand Up @@ -289,7 +298,7 @@ impl fmt::Display for BinaryOperator {
Self::Is => "IS",
Self::IsNot => "IS NOT",
Self::Add => "+",
Self::Subtract => "-",
Self::Subtract | Self::JsonSubtract => "-",
Self::Multiply => "*",
Self::Divide => "/",
Self::JsonExists => "?",
Expand All @@ -309,6 +318,20 @@ impl fmt::Display for BinaryOperator {
mod tests {
use super::*;

#[test]
fn json_subtract_lowering() {
assert_eq!(
BinaryOperator::from_sql_op(
SqlBinaryOperator::Subtract,
Dialect::DEFAULT_POSTGRESQL,
&DfType::Jsonb,
&DfType::Int
)
.unwrap(),
BinaryOperator::JsonSubtract
);
}

mod output_type {
use super::*;

Expand Down
1 change: 1 addition & 0 deletions dataflow-expression/src/eval.rs
Expand Up @@ -193,6 +193,7 @@ impl Expr {
Ok(serde_json::to_string(&res)?.into())
}
}
JsonSubtract => unsupported!("'-' operator applied to JSONB not yet supported")
}
}
Expr::Cast { expr, ty, .. } => {
Expand Down

0 comments on commit 3753cb7

Please sign in to comment.