Skip to content

Commit

Permalink
Add support for DataType Serial
Browse files Browse the repository at this point in the history
  • Loading branch information
shanicky committed Mar 7, 2023
1 parent d7d0307 commit 2f358ab
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions proto/data.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ message DataType {
LIST = 16;
BYTEA = 17;
JSONB = 18;
SERIAL = 19;
}
TypeName type_name = 1;
// Data length for char.
Expand Down
2 changes: 2 additions & 0 deletions src/common/src/hash/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use super::HashKey;
use crate::array::serial_array::Serial;
use crate::hash;
use crate::types::DataType;

Expand Down Expand Up @@ -90,6 +91,7 @@ fn hash_key_size(data_type: &DataType) -> HashKeySize {
DataType::Int16 => HashKeySize::Fixed(size_of::<i16>()),
DataType::Int32 => HashKeySize::Fixed(size_of::<i32>()),
DataType::Int64 => HashKeySize::Fixed(size_of::<i64>()),
DataType::Serial => HashKeySize::Fixed(size_of::<Serial>()),
DataType::Float32 => HashKeySize::Fixed(size_of::<OrderedF32>()),
DataType::Float64 => HashKeySize::Fixed(size_of::<OrderedF64>()),
DataType::Decimal => HashKeySize::Fixed(size_of::<Decimal>()),
Expand Down
14 changes: 14 additions & 0 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ pub enum DataType {
#[display("jsonb")]
#[from_str(regex = "(?i)^jsonb$")]
Jsonb,
#[display("serial")]
#[from_str(regex = "(?i)^serial$")]
Serial,
}

impl std::str::FromStr for Box<DataType> {
Expand All @@ -149,6 +152,7 @@ impl DataTypeName {
| DataTypeName::Int16
| DataTypeName::Int32
| DataTypeName::Int64
| DataTypeName::Serial
| DataTypeName::Decimal
| DataTypeName::Float32
| DataTypeName::Float64
Expand All @@ -171,6 +175,7 @@ impl DataTypeName {
DataTypeName::Int16 => DataType::Int16,
DataTypeName::Int32 => DataType::Int32,
DataTypeName::Int64 => DataType::Int64,
DataTypeName::Serial => DataType::Serial,
DataTypeName::Decimal => DataType::Decimal,
DataTypeName::Float32 => DataType::Float32,
DataTypeName::Float64 => DataType::Float64,
Expand Down Expand Up @@ -209,6 +214,7 @@ impl From<&ProstDataType> for DataType {
TypeName::Int16 => DataType::Int16,
TypeName::Int32 => DataType::Int32,
TypeName::Int64 => DataType::Int64,
TypeName::Serial => DataType::Serial,
TypeName::Float => DataType::Float32,
TypeName::Double => DataType::Float64,
TypeName::Boolean => DataType::Boolean,
Expand Down Expand Up @@ -243,6 +249,7 @@ impl DataType {
DataType::Int16 => PrimitiveArrayBuilder::<i16>::new(capacity).into(),
DataType::Int32 => PrimitiveArrayBuilder::<i32>::new(capacity).into(),
DataType::Int64 => PrimitiveArrayBuilder::<i64>::new(capacity).into(),
DataType::Serial => PrimitiveArrayBuilder::<Serial>::new(capacity).into(),
DataType::Float32 => PrimitiveArrayBuilder::<OrderedF32>::new(capacity).into(),
DataType::Float64 => PrimitiveArrayBuilder::<OrderedF64>::new(capacity).into(),
DataType::Decimal => DecimalArrayBuilder::new(capacity).into(),
Expand Down Expand Up @@ -272,6 +279,7 @@ impl DataType {
DataType::Int16 => TypeName::Int16,
DataType::Int32 => TypeName::Int32,
DataType::Int64 => TypeName::Int64,
DataType::Serial => TypeName::Serial,
DataType::Float32 => TypeName::Float,
DataType::Float64 => TypeName::Double,
DataType::Boolean => TypeName::Boolean,
Expand Down Expand Up @@ -314,6 +322,7 @@ impl DataType {
DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::Serial
| DataType::Float32
| DataType::Float64
| DataType::Decimal
Expand Down Expand Up @@ -354,6 +363,7 @@ impl DataType {
DataType::Int16 => ScalarImpl::Int16(i16::MIN),
DataType::Int32 => ScalarImpl::Int32(i32::MIN),
DataType::Int64 => ScalarImpl::Int64(i64::MIN),
DataType::Serial => ScalarImpl::Serial(Serial::from(i64::MIN)),
DataType::Float32 => ScalarImpl::Float32(OrderedF32::neg_infinity()),
DataType::Float64 => ScalarImpl::Float64(OrderedF64::neg_infinity()),
DataType::Boolean => ScalarImpl::Bool(false),
Expand Down Expand Up @@ -883,6 +893,7 @@ impl ScalarImpl {
Ty::Int16 => Self::Int16(i16::deserialize(de)?),
Ty::Int32 => Self::Int32(i32::deserialize(de)?),
Ty::Int64 => Self::Int64(i64::deserialize(de)?),
Ty::Serial => todo!("SERIAL"),
Ty::Float32 => Self::Float32(f32::deserialize(de)?.into()),
Ty::Float64 => Self::Float64(f64::deserialize(de)?.into()),
Ty::Varchar => Self::Utf8(Box::<str>::deserialize(de)?),
Expand Down Expand Up @@ -933,6 +944,7 @@ impl ScalarImpl {
DataType::Int16 => size_of::<i16>(),
DataType::Int32 => size_of::<i32>(),
DataType::Int64 => size_of::<i64>(),
DataType::Serial => size_of::<Serial>(),
DataType::Float32 => size_of::<OrderedF32>(),
DataType::Float64 => size_of::<OrderedF64>(),
DataType::Date => size_of::<NaiveDateWrapper>(),
Expand Down Expand Up @@ -993,6 +1005,7 @@ pub fn literal_type_match(data_type: &DataType, literal: Option<&ScalarImpl>) ->
| (DataType::Int16, ScalarImpl::Int16(_))
| (DataType::Int32, ScalarImpl::Int32(_))
| (DataType::Int64, ScalarImpl::Int64(_))
| (DataType::Serial, ScalarImpl::Serial(_))
| (DataType::Float32, ScalarImpl::Float32(_))
| (DataType::Float64, ScalarImpl::Float64(_))
| (DataType::Varchar, ScalarImpl::Utf8(_))
Expand Down Expand Up @@ -1172,6 +1185,7 @@ mod tests {
DataTypeName::Int16 => (ScalarImpl::Int16(233), DataType::Int16),
DataTypeName::Int32 => (ScalarImpl::Int32(233333), DataType::Int32),
DataTypeName::Int64 => (ScalarImpl::Int64(233333333333), DataType::Int64),
DataTypeName::Serial => (ScalarImpl::Serial(233333333333.into()), DataType::Serial),
DataTypeName::Float32 => (ScalarImpl::Float32(23.33.into()), DataType::Float32),
DataTypeName::Float64 => (
ScalarImpl::Float64(23.333333333333.into()),
Expand Down
3 changes: 3 additions & 0 deletions src/common/src/types/postgres_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl DataType {
DataType::Boolean => 1,
DataType::Int16 => 2,
DataType::Int32 | DataType::Float32 | DataType::Date => 4,
DataType::Serial => todo!(),
DataType::Int64
| DataType::Float64
| DataType::Timestamp
Expand Down Expand Up @@ -114,6 +115,7 @@ impl DataType {
DataType::Int16 => 21,
DataType::Int32 => 23,
DataType::Int64 => 20,
DataType::Serial => todo!("SERIAL"),
DataType::Float32 => 700,
DataType::Float64 => 701,
DataType::Decimal => 1700,
Expand All @@ -133,6 +135,7 @@ impl DataType {
DataType::Int16 => 1005,
DataType::Int32 => 1007,
DataType::Int64 => 1016,
DataType::Serial => todo!("SERIAL"),
DataType::Float32 => 1021,
DataType::Float64 => 1022,
DataType::Decimal => 1231,
Expand Down
5 changes: 4 additions & 1 deletion src/common/src/util/value_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bytes::{Buf, BufMut};
use chrono::{Datelike, Timelike};
use itertools::Itertools;

use crate::array::{JsonbVal, ListRef, ListValue, StructRef, StructValue};
use crate::array::{serial_array, JsonbVal, ListRef, ListValue, StructRef, StructValue};
use crate::catalog::ColumnId;
use crate::row::{Row, RowDeserializer as BasicDeserializer};
use crate::types::struct_type::StructType;
Expand All @@ -33,6 +33,8 @@ use crate::types::{

pub mod error;
use error::ValueEncodingError;
use serial_array::Serial;

pub mod column_aware_row_encoding;

pub type Result<T> = std::result::Result<T, ValueEncodingError>;
Expand Down Expand Up @@ -223,6 +225,7 @@ fn deserialize_value(ty: &DataType, data: &mut impl Buf) -> Result<ScalarImpl> {
DataType::Int16 => ScalarImpl::Int16(data.get_i16_le()),
DataType::Int32 => ScalarImpl::Int32(data.get_i32_le()),
DataType::Int64 => ScalarImpl::Int64(data.get_i64_le()),
DataType::Serial => ScalarImpl::Serial(Serial::from(data.get_i64_le())),
DataType::Float32 => ScalarImpl::Float32(OrderedF32::from(data.get_f32_le())),
DataType::Float64 => ScalarImpl::Float64(OrderedF64::from(data.get_f64_le())),
DataType::Varchar => ScalarImpl::Utf8(deserialize_str(data)?),
Expand Down
1 change: 1 addition & 0 deletions src/connector/src/parser/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn do_parse_simd_json_value(dtype: &DataType, v: &BorrowedValue<'_>) -> Result<S
.map_err(|e| anyhow!("expect i32: {}", e))?,
),
DataType::Int64 => ensure_int!(v, i64).into(),
DataType::Serial => ensure_int!(v, i64).into(),
DataType::Float32 => ScalarImpl::Float32((simd_json_ensure_float!(v, f32) as f32).into()),
DataType::Float64 => ScalarImpl::Float64((simd_json_ensure_float!(v, f64)).into()),
// FIXME: decimal should have more precision than f64
Expand Down
2 changes: 2 additions & 0 deletions src/expr/src/expr/expr_binary_nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use std::sync::Arc;

use risingwave_common::array::serial_array::SerialArray;
use risingwave_common::array::*;
use risingwave_common::buffer::Bitmap;
use risingwave_common::row::OwnedRow;
Expand Down Expand Up @@ -201,6 +202,7 @@ fn build_array_access_expr(
DataType::Int16 => array_access_expression!(I16Array),
DataType::Int32 => array_access_expression!(I32Array),
DataType::Int64 => array_access_expression!(I64Array),
DataType::Serial => array_access_expression!(SerialArray),
DataType::Float32 => array_access_expression!(F32Array),
DataType::Float64 => array_access_expression!(F64Array),
DataType::Decimal => array_access_expression!(DecimalArray),
Expand Down
1 change: 1 addition & 0 deletions src/expr/src/vector_op/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ pub fn literal_parsing(
DataType::Int16 => str_parse::<i16>(s)?.into(),
DataType::Int32 => str_parse::<i32>(s)?.into(),
DataType::Int64 => str_parse::<i64>(s)?.into(),
DataType::Serial => str_parse::<i64>(s)?.into(),
DataType::Decimal => str_parse::<Decimal>(s)?.into(),
DataType::Float32 => str_parse::<OrderedF32>(s)?.into(),
DataType::Float64 => str_parse::<OrderedF64>(s)?.into(),
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/expr/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl std::fmt::Debug for Literal {
DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::Serial
| DataType::Decimal
| DataType::Float32
| DataType::Float64 => write!(f, "{}", v.as_scalar_ref_impl().to_text()),
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/optimizer/rule/index_selection_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::rc::Rc;

use itertools::Itertools;
use risingwave_common::array::serial_array::Serial;
use risingwave_common::catalog::Schema;
use risingwave_common::types::{
DataType, Decimal, IntervalUnit, NaiveDateTimeWrapper, NaiveDateWrapper, NaiveTimeWrapper,
Expand Down Expand Up @@ -702,6 +703,7 @@ impl<'a> TableScanIoEstimator<'a> {
DataType::Int16 => size_of::<i16>(),
DataType::Int32 => size_of::<i32>(),
DataType::Int64 => size_of::<i64>(),
DataType::Serial => size_of::<Serial>(),
DataType::Float32 => size_of::<f32>(),
DataType::Float64 => size_of::<f64>(),
DataType::Decimal => size_of::<Decimal>(),
Expand Down
1 change: 1 addition & 0 deletions src/tests/sqlsmith/src/sql_gen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(super) fn data_type_to_ast_data_type(data_type: &DataType) -> AstDataType {
DataType::Int16 => AstDataType::SmallInt,
DataType::Int32 => AstDataType::Int,
DataType::Int64 => AstDataType::BigInt,
DataType::Serial => AstDataType::Custom(vec!["SERIAL".into()].into()),
DataType::Decimal => AstDataType::Decimal(None, None),
DataType::Float32 => AstDataType::Real,
DataType::Float64 => AstDataType::Double,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/pgwire/src/pg_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ impl PreparedStatement {
};
format!("{}::INT8", tmp)
}
DataType::Serial => todo!("SERIAL"),
DataType::Int16 => {
let tmp = match param_format {
Format::Binary => {
Expand Down Expand Up @@ -542,6 +543,7 @@ impl PreparedStatement {
match oid {
DataType::Boolean => params.push("false".to_string()),
DataType::Int64 => params.push("0::BIGINT".to_string()),
DataType::Serial => todo!("SERIAL"),
DataType::Int16 => params.push("0::SMALLINT".to_string()),
DataType::Int32 => params.push("0::INT".to_string()),
DataType::Float32 => params.push("0::FLOAT4".to_string()),
Expand Down

0 comments on commit 2f358ab

Please sign in to comment.