Skip to content
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
6 changes: 3 additions & 3 deletions clickhouse/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opensrv-clickhouse"
version = "0.2.0"
version = "0.3.0"
authors = ["Databend Authors <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -21,8 +21,8 @@ tls = ["tokio-native-tls"]
async-trait = "0.1.52"
byteorder = "1.4.3"
bytes = "1.1.0"
chrono = { version = "0.4.19", default-features = false, features = ["std"] }
chrono-tz = "0.6.1"
chrono = { version = "0.4.20", default-features = false, features = ["std"] }
chrono-tz = "0.8.0"
combine = "4.6.3"
hostname = "0.3.1"
lz4 = "1.23.2"
Expand Down
1 change: 0 additions & 1 deletion clickhouse/src/types/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ fn put_param<K: ColumnType>(

fn extract_timezone(value: &Value) -> Tz {
match value {
Value::Date(_, tz) => *tz,
Value::DateTime(_, tz) => *tz,
Value::Nullable(Either::Right(d)) => extract_timezone(d),
Value::Array(_, data) => {
Expand Down
2 changes: 1 addition & 1 deletion clickhouse/src/types/column/chrono_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ColumnFrom for Vec<Option<DateTime<Tz>>> {
match time {
None => {
nulls.push(1);
values.push(tz.timestamp(0, 0))
values.push(tz.timestamp_opt(0, 0).unwrap())
}
Some(time) => {
nulls.push(0);
Expand Down
16 changes: 8 additions & 8 deletions clickhouse/src/types/column/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fmt;
use std::sync::Arc;

use chrono::prelude::*;
use chrono::Date;
use chrono::NaiveDate;
use chrono_tz::Tz;
use micromarshal::Marshal;
use micromarshal::Unmarshal;
Expand Down Expand Up @@ -92,7 +92,7 @@ where
}
}

impl ColumnFrom for Vec<Date<Tz>> {
impl ColumnFrom for Vec<NaiveDate> {
fn column_from<W: ColumnWrapper>(source: Self) -> W::Wrapper {
let mut data = List::<u16>::with_capacity(source.len());
for s in source {
Expand All @@ -104,9 +104,9 @@ impl ColumnFrom for Vec<Date<Tz>> {
}
}

impl ColumnFrom for Vec<Vec<Date<Tz>>> {
impl ColumnFrom for Vec<Vec<NaiveDate>> {
fn column_from<W: ColumnWrapper>(source: Self) -> W::Wrapper {
let fake: Vec<Date<Tz>> = Vec::with_capacity(source.len());
let fake: Vec<NaiveDate> = Vec::with_capacity(source.len());
let inner = Vec::column_from::<ArcColumnWrapper>(fake);
let sql_type = inner.sql_type();

Expand All @@ -119,7 +119,7 @@ impl ColumnFrom for Vec<Vec<Date<Tz>>> {
let mut inner = Vec::with_capacity(vs.len());
for v in vs {
let days = u16::get_days(v);
let value: Value = Value::Date(days, v.timezone());
let value: Value = Value::Date(days);
inner.push(value);
}
data.push(Value::Array(sql_type.clone().into(), Arc::new(inner)));
Expand Down Expand Up @@ -153,9 +153,9 @@ impl ColumnFrom for Vec<Vec<DateTime<Tz>>> {
}
}

impl ColumnFrom for Vec<Option<Date<Tz>>> {
impl ColumnFrom for Vec<Option<NaiveDate>> {
fn column_from<W: ColumnWrapper>(source: Self) -> <W as ColumnWrapper>::Wrapper {
let fake: Vec<Date<Tz>> = Vec::with_capacity(source.len());
let fake: Vec<NaiveDate> = Vec::with_capacity(source.len());
let inner = Vec::column_from::<ArcColumnWrapper>(fake);

let mut data = NullableColumnData {
Expand All @@ -168,7 +168,7 @@ impl ColumnFrom for Vec<Option<Date<Tz>>> {
None => data.push(Value::Nullable(Either::Left(SqlType::Date.into()))),
Some(d) => {
let days = u16::get_days(d);
let value = Value::Date(days, d.timezone());
let value = Value::Date(days);
data.push(Value::Nullable(Either::Right(Box::new(value))))
}
}
Expand Down
2 changes: 1 addition & 1 deletion clickhouse/src/types/column/datetime64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ pub fn to_datetime(value: i64, precision: u32, tz: Tz) -> DateTime<Tz> {
let sec = nano / 1_000_000_000;
let nsec = nano - sec * 1_000_000_000;

tz.timestamp(sec, nsec as u32)
tz.timestamp_opt(sec, nsec as u32).unwrap()
}
24 changes: 12 additions & 12 deletions clickhouse/src/types/column/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl IpVersion for Ipv4 {
#[inline(always)]
fn push(inner: &mut Vec<u8>, value: Value) {
if let Value::Ipv4(v) = value {
inner.extend(&v);
inner.extend(v);
} else {
panic!();
}
Expand Down Expand Up @@ -87,7 +87,7 @@ impl IpVersion for Ipv6 {
#[inline(always)]
fn push(inner: &mut Vec<u8>, value: Value) {
if let Value::Ipv6(v) = value {
inner.extend(&v);
inner.extend(v);
} else {
panic!();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl IpVersion for Uuid {
#[inline(always)]
fn push(inner: &mut Vec<u8>, value: Value) {
if let Value::Uuid(v) = value {
inner.extend(&v);
inner.extend(v);
} else {
panic!();
}
Expand All @@ -135,7 +135,7 @@ impl ColumnFrom for Vec<Ipv4Addr> {
for ip in data {
let mut buffer = ip.octets();
buffer.reverse();
inner.extend(&buffer);
inner.extend(buffer);
}

W::wrap(IpColumnData::<Ipv4> {
Expand All @@ -149,7 +149,7 @@ impl ColumnFrom for Vec<Ipv6Addr> {
fn column_from<W: ColumnWrapper>(data: Self) -> W::Wrapper {
let mut inner = Vec::with_capacity(data.len());
for ip in data {
inner.extend(&ip.octets());
inner.extend(ip.octets());
}

W::wrap(IpColumnData::<Ipv6> {
Expand All @@ -166,7 +166,7 @@ impl ColumnFrom for Vec<uuid::Uuid> {
let mut buffer = *uuid.as_bytes();
buffer[..8].reverse();
buffer[8..].reverse();
inner.extend(&buffer);
inner.extend(buffer);
}

W::wrap(IpColumnData::<Uuid> {
Expand All @@ -185,13 +185,13 @@ impl ColumnFrom for Vec<Option<Ipv4Addr>> {
for ip in source {
match ip {
None => {
inner.extend(&[0; 4]);
inner.extend([0; 4]);
nulls.push(1);
}
Some(ip) => {
let mut buffer = ip.octets();
buffer.reverse();
inner.extend(&buffer);
inner.extend(buffer);
nulls.push(0);
}
}
Expand All @@ -217,11 +217,11 @@ impl ColumnFrom for Vec<Option<Ipv6Addr>> {
for ip in source {
match ip {
None => {
inner.extend(&[0; 16]);
inner.extend([0; 16]);
nulls.push(1);
}
Some(ip) => {
inner.extend(&ip.octets());
inner.extend(ip.octets());
nulls.push(0);
}
}
Expand All @@ -247,14 +247,14 @@ impl ColumnFrom for Vec<Option<uuid::Uuid>> {
for uuid in source {
match uuid {
None => {
inner.extend(&[0; 16]);
inner.extend([0; 16]);
nulls.push(1);
}
Some(uuid) => {
let mut buffer = *uuid.as_bytes();
buffer[..8].reverse();
buffer[8..].reverse();
inner.extend(&buffer);
inner.extend(buffer);
nulls.push(0);
}
}
Expand Down
17 changes: 10 additions & 7 deletions clickhouse/src/types/column/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::ptr;
use std::slice;

use chrono::prelude::*;
use chrono::Date;
use chrono::NaiveDate;
use chrono_tz::Tz;

use crate::errors::Error;
Expand Down Expand Up @@ -418,12 +418,15 @@ impl<'a> ExactSizeIterator for UuidIterator<'a> {

impl<'a> DateIterator<'a> {
#[inline(always)]
unsafe fn next_unchecked(&mut self) -> Date<Tz> {
unsafe fn next_unchecked(&mut self) -> NaiveDate {
let current_value = *self.ptr;
self.ptr = self.ptr.offset(1);

let time = self.tz.timestamp(i64::from(current_value) * 24 * 3600, 0);
time.date()
let time = self
.tz
.timestamp_opt(i64::from(current_value) * 24 * 3600, 0)
.unwrap();
time.date_naive()
}

#[inline(always)]
Expand All @@ -439,7 +442,7 @@ impl<'a> DateTimeIterator<'a> {
DateTimeInnerIterator::DateTime32(ptr, _) => {
let current_value = **ptr;
*ptr = ptr.offset(1);
self.tz.timestamp(i64::from(current_value), 0)
self.tz.timestamp_opt(i64::from(current_value), 0).unwrap()
}
DateTimeInnerIterator::DateTime64(ptr, _, precision) => {
let current_value = **ptr;
Expand Down Expand Up @@ -474,7 +477,7 @@ impl ExactSizeIterator for DateTimeIterator<'_> {
}
}

iterator! { DateIterator: Date<Tz> }
iterator! { DateIterator: NaiveDate }

impl<'a> Iterator for DateTimeIterator<'a> {
type Item = DateTime<Tz>;
Expand Down Expand Up @@ -803,7 +806,7 @@ impl<'a> Iterable<'a, Simple> for DateTime<Tz> {
}
}

impl<'a> Iterable<'a, Simple> for Date<Tz> {
impl<'a> Iterable<'a, Simple> for NaiveDate {
type Iter = DateIterator<'a>;

fn iter(column: &'a Column<Simple>, column_type: SqlType) -> Result<Self::Iter> {
Expand Down
4 changes: 2 additions & 2 deletions clickhouse/src/types/column/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl<K: ColumnType> Column<K> {
let mut buffer = [0_u8; 4];
buffer.copy_from_slice(&ip.octets());
buffer.reverse();
inner.extend(&buffer);
inner.extend(buffer);
}

let data = Arc::new(IpColumnData::<Ipv4> {
Expand All @@ -426,7 +426,7 @@ impl<K: ColumnType> Column<K> {
for i in 0..n {
let source = self.at(i).as_str().unwrap();
let ip: Ipv6Addr = source.parse().unwrap();
inner.extend(&ip.octets());
inner.extend(ip.octets());
}

let data = Arc::new(IpColumnData::<Ipv6> {
Expand Down
12 changes: 6 additions & 6 deletions clickhouse/src/types/date_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@
// limitations under the License.

use chrono::prelude::*;
use chrono::Date;
use chrono_tz::Tz;

use crate::types::DateTimeType;
use crate::types::SqlType;
use crate::types::Value;
use crate::types::ValueRef;

pub const UNIX_EPOCH_DAY: i64 = 719_163;

pub trait DateConverter {
fn to_date(&self, tz: Tz) -> ValueRef<'static>;
fn get_stamp(source: Value) -> Self;
fn date_type() -> SqlType;

fn get_days(date: Date<Tz>) -> u16 {
const UNIX_EPOCH_DAY: i64 = 719_163;
fn get_days(date: NaiveDate) -> u16 {
let gregorian_day = i64::from(date.num_days_from_ce());
(gregorian_day - UNIX_EPOCH_DAY) as u16
}
}

impl DateConverter for u16 {
fn to_date(&self, tz: Tz) -> ValueRef<'static> {
ValueRef::Date(*self, tz)
fn to_date(&self, _: Tz) -> ValueRef<'static> {
ValueRef::Date(*self)
}

fn get_stamp(source: Value) -> Self {
Self::get_days(Date::<Tz>::from(source))
Self::get_days(NaiveDate::from(source))
}

fn date_type() -> SqlType {
Expand Down
16 changes: 9 additions & 7 deletions clickhouse/src/types/from_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::types::Enum16;
use crate::types::Enum8;
use crate::types::SqlType;
use crate::types::ValueRef;
use crate::types::UNIX_EPOCH_DAY;

pub type FromSqlResult<T> = Result<T>;

Expand Down Expand Up @@ -204,7 +205,7 @@ from_sql_vec_impl! {
String: SqlType::String => |v| v.as_string(),
&'a [u8]: SqlType::String => |v| v.as_bytes(),
Vec<u8>: SqlType::String => |v| v.as_bytes().map(<[u8]>::to_vec),
Date<Tz>: SqlType::Date => |z| Ok(z.into()),
NaiveDate: SqlType::Date => |z| Ok(z.into()),
DateTime<Tz>: SqlType::DateTime(_) => |z| Ok(z.into())
}

Expand Down Expand Up @@ -286,18 +287,19 @@ where
}
}

impl<'a> FromSql<'a> for Date<Tz> {
impl<'a> FromSql<'a> for NaiveDate {
fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
match value {
ValueRef::Date(v, tz) => {
let time = tz.timestamp(i64::from(v) * 24 * 3600, 0);
Ok(time.date())
ValueRef::Date(v) => {
let date = NaiveDate::from_num_days_from_ce_opt((v as i64 + UNIX_EPOCH_DAY) as i32)
.unwrap();
Ok(date)
}
_ => {
let from = SqlType::from(value).to_string();
Err(Error::FromSql(FromSqlError::InvalidType {
src: from,
dst: "Date<Tz>".into(),
dst: "NaiveDate".into(),
}))
}
}
Expand All @@ -308,7 +310,7 @@ impl<'a> FromSql<'a> for DateTime<Tz> {
fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
match value {
ValueRef::DateTime(v, tz) => {
let time = tz.timestamp(i64::from(v), 0);
let time = tz.timestamp_opt(i64::from(v), 0).unwrap();
Ok(time)
}
ValueRef::DateTime64(v, params) => {
Expand Down
3 changes: 2 additions & 1 deletion clickhouse/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub use self::column::Either;
pub use self::column::Simple;
pub use self::column::StringPool;
pub use self::date_converter::DateConverter;
pub use self::date_converter::UNIX_EPOCH_DAY;
pub use self::decimal::decimal2str;
pub use self::decimal::Decimal;
pub use self::decimal::NoBits;
Expand Down Expand Up @@ -188,7 +189,7 @@ has_sql_type! {
String: SqlType::String,
f32: SqlType::Float32,
f64: SqlType::Float64,
Date<Tz>: SqlType::Date,
NaiveDate: SqlType::Date,
DateTime<Tz>: SqlType::DateTime(DateTimeType::DateTime32)
}

Expand Down
1 change: 1 addition & 0 deletions clickhouse/src/types/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl convert::From<Certificate> for native_tls::Certificate {
/// Clickhouse connection options.
// the trait `std::cmp::Eq` is not implemented for `types::options::Certificate`
// so we can't use `derive(Eq)` in tls feature.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq)]
pub struct Options {
/// Address of clickhouse server (defaults to `127.0.0.1:9000`).
Expand Down
Loading