Skip to content

Commit 13d35d0

Browse files
authored
refactor: binding struct values when they are parsed as row (#2508)
Delete scalarImpl to datatype function since we can get the data type from literal.
1 parent 21e3edf commit 13d35d0

File tree

3 files changed

+12
-57
lines changed

3 files changed

+12
-57
lines changed

src/common/src/types/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bytes::{Buf, BufMut};
1919
use risingwave_pb::data::DataType as ProstDataType;
2020
use serde::{Deserialize, Serialize};
2121

22-
use crate::error::{internal_error, ErrorCode, Result, RwError};
22+
use crate::error::{ErrorCode, Result, RwError};
2323
mod native_type;
2424

2525
mod scalar_impl;
@@ -326,16 +326,6 @@ for_all_scalar_variants! { scalar_impl_enum }
326326
pub type Datum = Option<ScalarImpl>;
327327
pub type DatumRef<'a> = Option<ScalarRefImpl<'a>>;
328328

329-
pub fn get_data_type_from_datum(datum: &Datum) -> Result<DataType> {
330-
match datum {
331-
// TODO: Predicate data type from None Datum
332-
None => Err(internal_error(
333-
"cannot get data type from None Datum".to_string(),
334-
)),
335-
Some(scalar) => scalar.data_type(),
336-
}
337-
}
338-
339329
/// Convert a [`Datum`] to a [`DatumRef`].
340330
pub fn to_datum_ref(datum: &Datum) -> DatumRef<'_> {
341331
datum.as_ref().map(|d| d.as_scalar_ref_impl())

src/common/src/types/scalar_impl.rs

-34
Original file line numberDiff line numberDiff line change
@@ -300,40 +300,6 @@ impl ScalarImpl {
300300
}
301301
for_all_scalar_variants! { impl_all_get_ident, self }
302302
}
303-
304-
pub(crate) fn data_type(&self) -> Result<DataType> {
305-
let data_type = match self {
306-
ScalarImpl::Int16(_) => DataType::Int16,
307-
ScalarImpl::Int32(_) => DataType::Int32,
308-
ScalarImpl::Int64(_) => DataType::Int64,
309-
ScalarImpl::Float32(_) => DataType::Float32,
310-
ScalarImpl::Float64(_) => DataType::Float64,
311-
ScalarImpl::Utf8(_) => DataType::Varchar,
312-
ScalarImpl::Bool(_) => DataType::Boolean,
313-
ScalarImpl::Decimal(_) => DataType::Decimal,
314-
ScalarImpl::Interval(_) => DataType::Interval,
315-
ScalarImpl::NaiveDate(_) => DataType::Date,
316-
ScalarImpl::NaiveDateTime(_) => DataType::Timestamp,
317-
ScalarImpl::NaiveTime(_) => DataType::Time,
318-
ScalarImpl::Struct(data) => {
319-
let types = data
320-
.fields()
321-
.iter()
322-
.map(get_data_type_from_datum)
323-
.collect::<Result<Vec<_>>>()?;
324-
DataType::Struct {
325-
fields: types.into(),
326-
}
327-
}
328-
ScalarImpl::List(data) => {
329-
let data = data.values().get(0).ok_or_else(|| {
330-
internal_error("cannot get data type from empty list".to_string())
331-
})?;
332-
get_data_type_from_datum(data)?
333-
}
334-
};
335-
Ok(data_type)
336-
}
337303
}
338304

339305
impl<'scalar> ScalarRefImpl<'scalar> {

src/frontend/src/binder/values.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use itertools::Itertools;
1616
use risingwave_common::array::StructValue;
1717
use risingwave_common::catalog::{Field, Schema};
1818
use risingwave_common::error::{ErrorCode, Result, TrackingIssue};
19-
use risingwave_common::types::{get_data_type_from_datum, DataType, Datum, Scalar};
19+
use risingwave_common::types::{DataType, Scalar};
2020
use risingwave_sqlparser::ast::{Expr, Values};
2121

2222
use super::bind_context::Clause;
2323
use crate::binder::Binder;
24-
use crate::expr::{align_types, ExprImpl, Literal};
24+
use crate::expr::{align_types, Expr as _, ExprImpl, Literal};
2525

2626
#[derive(Debug)]
2727
pub struct BoundValues {
@@ -86,27 +86,27 @@ impl Binder {
8686
/// e.g. Row(1,2,(1,2,3)).
8787
/// Only accept value and row expr in row.
8888
pub fn bind_row(&mut self, exprs: &[Expr]) -> Result<Literal> {
89-
let datums = exprs
89+
let literals = exprs
9090
.iter()
9191
.map(|e| match e {
92-
Expr::Value(value) => Ok(self.bind_value(value.clone())?.get_data().clone()),
93-
Expr::Row(expr) => Ok(self.bind_row(expr)?.get_data().clone()),
92+
Expr::Value(value) => Ok(self.bind_value(value.clone())?),
93+
Expr::Row(expr) => Ok(self.bind_row(expr)?),
9494
_ => Err(ErrorCode::NotImplemented(
9595
format!("unsupported expression {:?}", e),
9696
TrackingIssue::none(),
9797
)
9898
.into()),
9999
})
100-
.collect::<Result<Vec<Datum>>>()?;
101-
let value = StructValue::new(datums);
100+
.collect::<Result<Vec<_>>>()?;
102101
let data_type = DataType::Struct {
103-
fields: value
104-
.fields()
102+
fields: literals
105103
.iter()
106-
.map(get_data_type_from_datum)
107-
.collect::<Result<Vec<_>>>()?
104+
.map(|l| l.return_type())
105+
.collect_vec()
108106
.into(),
109107
};
108+
let value = StructValue::new(literals.iter().map(|f| f.get_data().clone()).collect_vec());
109+
110110
Ok(Literal::new(Some(value.to_scalar_value()), data_type))
111111
}
112112
}
@@ -119,7 +119,6 @@ mod tests {
119119

120120
use super::*;
121121
use crate::binder::test_utils::mock_binder;
122-
use crate::expr::Expr as _;
123122

124123
#[test]
125124
fn test_bind_values() {

0 commit comments

Comments
 (0)