Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: binding struct values when they are parsed as row #2508

Merged
merged 4 commits into from
May 14, 2022
Merged
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
12 changes: 1 addition & 11 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use bytes::{Buf, BufMut};
use risingwave_pb::data::DataType as ProstDataType;
use serde::{Deserialize, Serialize};

use crate::error::{internal_error, ErrorCode, Result, RwError};
use crate::error::{ErrorCode, Result, RwError};
mod native_type;

mod scalar_impl;
Expand Down Expand Up @@ -326,16 +326,6 @@ for_all_scalar_variants! { scalar_impl_enum }
pub type Datum = Option<ScalarImpl>;
pub type DatumRef<'a> = Option<ScalarRefImpl<'a>>;

pub fn get_data_type_from_datum(datum: &Datum) -> Result<DataType> {
match datum {
// TODO: Predicate data type from None Datum
None => Err(internal_error(
"cannot get data type from None Datum".to_string(),
)),
Some(scalar) => scalar.data_type(),
}
}

/// Convert a [`Datum`] to a [`DatumRef`].
pub fn to_datum_ref(datum: &Datum) -> DatumRef<'_> {
datum.as_ref().map(|d| d.as_scalar_ref_impl())
Expand Down
34 changes: 0 additions & 34 deletions src/common/src/types/scalar_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,40 +300,6 @@ impl ScalarImpl {
}
for_all_scalar_variants! { impl_all_get_ident, self }
}

pub(crate) fn data_type(&self) -> Result<DataType> {
let data_type = match self {
ScalarImpl::Int16(_) => DataType::Int16,
ScalarImpl::Int32(_) => DataType::Int32,
ScalarImpl::Int64(_) => DataType::Int64,
ScalarImpl::Float32(_) => DataType::Float32,
ScalarImpl::Float64(_) => DataType::Float64,
ScalarImpl::Utf8(_) => DataType::Varchar,
ScalarImpl::Bool(_) => DataType::Boolean,
ScalarImpl::Decimal(_) => DataType::Decimal,
ScalarImpl::Interval(_) => DataType::Interval,
ScalarImpl::NaiveDate(_) => DataType::Date,
ScalarImpl::NaiveDateTime(_) => DataType::Timestamp,
ScalarImpl::NaiveTime(_) => DataType::Time,
ScalarImpl::Struct(data) => {
let types = data
.fields()
.iter()
.map(get_data_type_from_datum)
.collect::<Result<Vec<_>>>()?;
DataType::Struct {
fields: types.into(),
}
}
ScalarImpl::List(data) => {
let data = data.values().get(0).ok_or_else(|| {
internal_error("cannot get data type from empty list".to_string())
})?;
get_data_type_from_datum(data)?
}
};
Ok(data_type)
}
}

impl<'scalar> ScalarRefImpl<'scalar> {
Expand Down
23 changes: 11 additions & 12 deletions src/frontend/src/binder/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ use itertools::Itertools;
use risingwave_common::array::StructValue;
use risingwave_common::catalog::{Field, Schema};
use risingwave_common::error::{ErrorCode, Result, TrackingIssue};
use risingwave_common::types::{get_data_type_from_datum, DataType, Datum, Scalar};
use risingwave_common::types::{DataType, Scalar};
use risingwave_sqlparser::ast::{Expr, Values};

use super::bind_context::Clause;
use crate::binder::Binder;
use crate::expr::{align_types, ExprImpl, Literal};
use crate::expr::{align_types, Expr as _, ExprImpl, Literal};

#[derive(Debug)]
pub struct BoundValues {
Expand Down Expand Up @@ -86,27 +86,27 @@ impl Binder {
/// e.g. Row(1,2,(1,2,3)).
/// Only accept value and row expr in row.
pub fn bind_row(&mut self, exprs: &[Expr]) -> Result<Literal> {
let datums = exprs
let literals = exprs
.iter()
.map(|e| match e {
Expr::Value(value) => Ok(self.bind_value(value.clone())?.get_data().clone()),
Expr::Row(expr) => Ok(self.bind_row(expr)?.get_data().clone()),
Expr::Value(value) => Ok(self.bind_value(value.clone())?),
Expr::Row(expr) => Ok(self.bind_row(expr)?),
_ => Err(ErrorCode::NotImplemented(
format!("unsupported expression {:?}", e),
TrackingIssue::none(),
)
.into()),
})
.collect::<Result<Vec<Datum>>>()?;
let value = StructValue::new(datums);
.collect::<Result<Vec<_>>>()?;
let data_type = DataType::Struct {
fields: value
.fields()
fields: literals
.iter()
.map(get_data_type_from_datum)
.collect::<Result<Vec<_>>>()?
.map(|l| l.return_type())
.collect_vec()
.into(),
};
let value = StructValue::new(literals.iter().map(|f| f.get_data().clone()).collect_vec());

Ok(Literal::new(Some(value.to_scalar_value()), data_type))
}
}
Expand All @@ -119,7 +119,6 @@ mod tests {

use super::*;
use crate::binder::test_utils::mock_binder;
use crate::expr::Expr as _;

#[test]
fn test_bind_values() {
Expand Down