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

feat(query): support timezone #4878

Merged
merged 17 commits into from
May 7, 2022
Merged
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions common/datavalues/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::any::Any;
use std::collections::BTreeMap;

use chrono_tz::Tz;
use common_arrow::arrow::datatypes::DataType as ArrowType;
use common_arrow::arrow::datatypes::Field as ArrowField;
use common_exception::Result;
Expand Down Expand Up @@ -125,7 +126,15 @@ pub trait DataType: std::fmt::Debug + Sync + Send + DynClone {

fn create_mutable(&self, capacity: usize) -> Box<dyn MutableColumn>;
fn create_serializer(&self) -> TypeSerializerImpl;
/// work only for timestamp serializer
fn create_serializer_with_tz(&self, _tz: Tz) -> TypeSerializerImpl {
unimplemented!()
}
fn create_deserializer(&self, capacity: usize) -> TypeDeserializerImpl;
/// work only for timestamp deserializer
fn create_deserializer_with_tz(&self, _capacity: usize, _tz: Tz) -> TypeDeserializerImpl {
Veeupup marked this conversation as resolved.
Show resolved Hide resolved
unimplemented!()
}
}

pub fn from_arrow_type(dt: &ArrowType) -> DataTypeImpl {
Expand Down
12 changes: 7 additions & 5 deletions common/datavalues/src/types/serializations/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use common_exception::ErrorCode;
use common_exception::Result;
use common_io::prelude::FormatSettings;
use serde_json::Value;

use crate::prelude::*;
Expand All @@ -25,7 +26,7 @@ pub struct ArraySerializer {
}

impl TypeSerializer for ArraySerializer {
fn serialize_value(&self, value: &DataValue) -> Result<String> {
fn serialize_value(&self, value: &DataValue, format: &FormatSettings) -> Result<String> {
if let DataValue::Array(vals) = value {
let mut res = String::new();
res.push('[');
Expand All @@ -37,7 +38,7 @@ impl TypeSerializer for ArraySerializer {
}
first = false;

let s = self.inner.serialize_value(val)?;
let s = self.inner.serialize_value(val, format)?;
if quoted {
res.push_str(&format!("'{}'", s));
} else {
Expand All @@ -51,24 +52,25 @@ impl TypeSerializer for ArraySerializer {
}
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
fn serialize_column(&self, column: &ColumnRef, format: &FormatSettings) -> Result<Vec<String>> {
let column: &ArrayColumn = Series::check_get(column)?;
let mut result = Vec::with_capacity(column.len());
for i in 0..column.len() {
let val = column.get(i);
let s = self.serialize_value(&val)?;
let s = self.serialize_value(&val, format)?;
result.push(s);
}
Ok(result)
}

fn serialize_json(&self, _column: &ColumnRef) -> Result<Vec<Value>> {
fn serialize_json(&self, _column: &ColumnRef, _format: &FormatSettings) -> Result<Vec<Value>> {
todo!()
}

fn serialize_clickhouse_format(
&self,
_column: &ColumnRef,
_format: &FormatSettings,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
todo!()
}
Expand Down
16 changes: 12 additions & 4 deletions common/datavalues/src/types/serializations/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use common_arrow::arrow::bitmap::Bitmap;
use common_exception::ErrorCode;
use common_exception::Result;
use common_io::prelude::FormatSettings;
use opensrv_clickhouse::types::column::ArcColumnWrapper;
use opensrv_clickhouse::types::column::ColumnFrom;
use serde_json::Value;
Expand All @@ -28,7 +29,7 @@ const TRUE_STR: &str = "1";
const FALSE_STR: &str = "0";

impl TypeSerializer for BooleanSerializer {
fn serialize_value(&self, value: &DataValue) -> Result<String> {
fn serialize_value(&self, value: &DataValue, _format: &FormatSettings) -> Result<String> {
if let DataValue::Boolean(x) = value {
if *x {
Ok(TRUE_STR.to_owned())
Expand All @@ -40,7 +41,11 @@ impl TypeSerializer for BooleanSerializer {
}
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
fn serialize_column(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<Vec<String>> {
let array: &BooleanColumn = Series::check_get(column)?;

let result: Vec<String> = array
Expand All @@ -56,7 +61,7 @@ impl TypeSerializer for BooleanSerializer {
Ok(result)
}

fn serialize_json(&self, column: &ColumnRef) -> Result<Vec<Value>> {
fn serialize_json(&self, column: &ColumnRef, _format: &FormatSettings) -> Result<Vec<Value>> {
let array: &BooleanColumn = Series::check_get(column)?;
let result: Vec<Value> = array
.iter()
Expand All @@ -68,6 +73,7 @@ impl TypeSerializer for BooleanSerializer {
fn serialize_clickhouse_format(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
let col: &BooleanColumn = Series::check_get(column)?;
let values: Vec<u8> = col.iter().map(|c| c as u8).collect();
Expand All @@ -78,13 +84,15 @@ impl TypeSerializer for BooleanSerializer {
&self,
column: &ColumnRef,
_valids: Option<&Bitmap>,
format: &FormatSettings,
) -> Result<Vec<Value>> {
self.serialize_json(column)
self.serialize_json(column, format)
}

fn serialize_json_object_suppress_error(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<Vec<Option<Value>>> {
let column: &BooleanColumn = Series::check_get(column)?;
let result: Vec<Option<Value>> = column
Expand Down
13 changes: 9 additions & 4 deletions common/datavalues/src/types/serializations/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::marker::PhantomData;
use std::ops::AddAssign;

Expand All @@ -20,6 +19,7 @@ use chrono::Duration;
use chrono::NaiveDate;
use chrono_tz::Tz;
use common_exception::*;
use common_io::prelude::FormatSettings;
use num::cast::AsPrimitive;
use opensrv_clickhouse::types::column::ArcColumnWrapper;
use opensrv_clickhouse::types::column::ColumnFrom;
Expand All @@ -43,14 +43,18 @@ impl<T: PrimitiveType + AsPrimitive<i64>> Default for DateSerializer<T> {
const DATE_FMT: &str = "%Y-%m-%d";

impl<T: PrimitiveType + AsPrimitive<i64>> TypeSerializer for DateSerializer<T> {
fn serialize_value(&self, value: &DataValue) -> Result<String> {
fn serialize_value(&self, value: &DataValue, _format: &FormatSettings) -> Result<String> {
let mut date = NaiveDate::from_ymd(1970, 1, 1);
let d = Duration::days(value.as_i64()?);
date.add_assign(d);
Ok(date.format(DATE_FMT).to_string())
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
fn serialize_column(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<Vec<String>> {
let column: &PrimitiveColumn<T> = Series::check_get(column)?;

let result: Vec<String> = column
Expand All @@ -65,7 +69,7 @@ impl<T: PrimitiveType + AsPrimitive<i64>> TypeSerializer for DateSerializer<T> {
Ok(result)
}

fn serialize_json(&self, column: &ColumnRef) -> Result<Vec<Value>> {
fn serialize_json(&self, column: &ColumnRef, _format: &FormatSettings) -> Result<Vec<Value>> {
let array: &PrimitiveColumn<T> = Series::check_get(column)?;
let result: Vec<Value> = array
.iter()
Expand All @@ -83,6 +87,7 @@ impl<T: PrimitiveType + AsPrimitive<i64>> TypeSerializer for DateSerializer<T> {
fn serialize_clickhouse_format(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
let array: &PrimitiveColumn<T> = Series::check_get(column)?;
let tz: Tz = "UTC".parse().unwrap();
Expand Down
15 changes: 11 additions & 4 deletions common/datavalues/src/types/serializations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use common_arrow::arrow::bitmap::Bitmap;
use common_exception::ErrorCode;
use common_exception::Result;
use common_io::prelude::FormatSettings;
use enum_dispatch::enum_dispatch;
use opensrv_clickhouse::types::column::ArcColumnData;
use serde_json::Value;
Expand Down Expand Up @@ -44,15 +45,20 @@ pub use variant::*;

#[enum_dispatch]
pub trait TypeSerializer: Send + Sync {
fn serialize_value(&self, value: &DataValue) -> Result<String>;
fn serialize_json(&self, column: &ColumnRef) -> Result<Vec<Value>>;
fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>>;
fn serialize_clickhouse_format(&self, column: &ColumnRef) -> Result<ArcColumnData>;
fn serialize_value(&self, value: &DataValue, format: &FormatSettings) -> Result<String>;
fn serialize_json(&self, column: &ColumnRef, format: &FormatSettings) -> Result<Vec<Value>>;
fn serialize_column(&self, column: &ColumnRef, format: &FormatSettings) -> Result<Vec<String>>;
fn serialize_clickhouse_format(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<ArcColumnData>;

fn serialize_json_object(
&self,
_column: &ColumnRef,
_valids: Option<&Bitmap>,
_format: &FormatSettings,
) -> Result<Vec<Value>> {
Err(ErrorCode::BadDataValueType(
"Error parsing JSON: unsupported data type",
Expand All @@ -62,6 +68,7 @@ pub trait TypeSerializer: Send + Sync {
fn serialize_json_object_suppress_error(
&self,
_column: &ColumnRef,
_format: &FormatSettings,
) -> Result<Vec<Option<Value>>> {
Err(ErrorCode::BadDataValueType(
"Error parsing JSON: unsupported data type",
Expand Down
12 changes: 9 additions & 3 deletions common/datavalues/src/types/serializations/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::sync::Arc;

use common_exception::Result;
use common_io::prelude::FormatSettings;
use opensrv_clickhouse::types::column::ArcColumnWrapper;
use opensrv_clickhouse::types::column::ColumnFrom;
use opensrv_clickhouse::types::column::NullableColumnData;
Expand All @@ -30,16 +31,20 @@ pub struct NullSerializer {}
const NULL_STR: &str = "NULL";

impl TypeSerializer for NullSerializer {
fn serialize_value(&self, _value: &DataValue) -> Result<String> {
fn serialize_value(&self, _value: &DataValue, _format: &FormatSettings) -> Result<String> {
Ok(NULL_STR.to_owned())
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
fn serialize_column(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<Vec<String>> {
let result: Vec<String> = vec![NULL_STR.to_owned(); column.len()];
Ok(result)
}

fn serialize_json(&self, column: &ColumnRef) -> Result<Vec<Value>> {
fn serialize_json(&self, column: &ColumnRef, _format: &FormatSettings) -> Result<Vec<Value>> {
let null = Value::Null;
let result: Vec<Value> = vec![null; column.len()];
Ok(result)
Expand All @@ -48,6 +53,7 @@ impl TypeSerializer for NullSerializer {
fn serialize_clickhouse_format(
&self,
column: &ColumnRef,
_format: &FormatSettings,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
let nulls = vec![1u8; column.len()];
let inner = Vec::column_from::<ArcColumnWrapper>(vec![1u8; column.len()]);
Expand Down
18 changes: 11 additions & 7 deletions common/datavalues/src/types/serializations/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::sync::Arc;

use common_exception::Result;
use common_io::prelude::FormatSettings;
use opensrv_clickhouse::types::column::NullableColumnData;
use serde_json::Value;

Expand All @@ -32,18 +33,18 @@ pub struct NullableSerializer {
}

impl TypeSerializer for NullableSerializer {
fn serialize_value(&self, value: &DataValue) -> Result<String> {
fn serialize_value(&self, value: &DataValue, format: &FormatSettings) -> Result<String> {
if value.is_null() {
Ok("NULL".to_owned())
} else {
self.inner.serialize_value(value)
self.inner.serialize_value(value, format)
}
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
fn serialize_column(&self, column: &ColumnRef, format: &FormatSettings) -> Result<Vec<String>> {
let column: &NullableColumn = Series::check_get(column)?;
let rows = column.len();
let mut res = self.inner.serialize_column(column.inner())?;
let mut res = self.inner.serialize_column(column.inner(), format)?;

(0..rows).for_each(|row| {
if column.null_at(row) {
Expand All @@ -53,10 +54,10 @@ impl TypeSerializer for NullableSerializer {
Ok(res)
}

fn serialize_json(&self, column: &ColumnRef) -> Result<Vec<Value>> {
fn serialize_json(&self, column: &ColumnRef, format: &FormatSettings) -> Result<Vec<Value>> {
let column: &NullableColumn = Series::check_get(column)?;
let rows = column.len();
let mut res = self.inner.serialize_json(column.inner())?;
let mut res = self.inner.serialize_json(column.inner(), format)?;

(0..rows).for_each(|row| {
if column.null_at(row) {
Expand All @@ -69,9 +70,12 @@ impl TypeSerializer for NullableSerializer {
fn serialize_clickhouse_format(
&self,
column: &ColumnRef,
format: &FormatSettings,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
let column: &NullableColumn = Series::check_get(column)?;
let inner = self.inner.serialize_clickhouse_format(column.inner())?;
let inner = self
.inner
.serialize_clickhouse_format(column.inner(), format)?;
let nulls = column.ensure_validity().iter().map(|v| !v as u8).collect();
let data = NullableColumnData { nulls, inner };

Expand Down
Loading