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(format): implement format trait #5167

Merged
merged 18 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
16 changes: 8 additions & 8 deletions common/datavalues/src/types/deserializations/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ impl TypeDeserializer for BooleanDeserializer {
Ok(())
}

fn de_json(&mut self, value: &serde_json::Value, _format: &FormatSettings) -> Result<()> {
match value {
serde_json::Value::Bool(v) => self.builder.append_value(*v),
_ => return Err(ErrorCode::BadBytes("Incorrect boolean value")),
}
Ok(())
}

fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
if reader.eq_ignore_ascii_case(b"true") {
self.builder.append_value(true);
Expand Down Expand Up @@ -77,14 +85,6 @@ impl TypeDeserializer for BooleanDeserializer {
Ok(())
}

fn de_json(&mut self, value: &serde_json::Value, _format: &FormatSettings) -> Result<()> {
match value {
serde_json::Value::Bool(v) => self.builder.append_value(*v),
_ => return Err(ErrorCode::BadBytes("Incorrect boolean value")),
}
Ok(())
}

fn append_data_value(&mut self, value: DataValue, _format: &FormatSettings) -> Result<()> {
self.builder.append_value(value.as_bool()?);
Ok(())
Expand Down
30 changes: 28 additions & 2 deletions common/datavalues/src/types/deserializations/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ where
}
}

fn de_null(&mut self, _format: &FormatSettings) -> bool {
false
}

fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
let mut reader = BufferReader::new(reader);
let v: T = if !T::FLOATING {
Expand Down Expand Up @@ -98,8 +102,30 @@ where
Ok(())
}

fn de_null(&mut self, _format: &FormatSettings) -> bool {
false
fn de_text_csv<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
settings: &FormatSettings,
) -> Result<()> {
let maybe_quote = reader.ignore(|f| f == b'\'' || f == b'"')?;

if maybe_quote && reader.ignore(|f| f == b'\'' || f == b'"')? && settings.empty_as_default {
zhang2014 marked this conversation as resolved.
Show resolved Hide resolved
self.de_default(settings);
return Ok(());
}

let v: T = if !T::FLOATING {
reader.read_int_text()
} else {
reader.read_float_text()
}?;

if maybe_quote {
reader.must_ignore(|f| f == b'\'' || f == b'"')?;
}

self.builder.append_value(v);
Ok(())
}

fn append_data_value(&mut self, value: DataValue, _format: &FormatSettings) -> Result<()> {
Expand Down
33 changes: 26 additions & 7 deletions common/datavalues/src/types/deserializations/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,48 @@ impl TypeDeserializer for StringDeserializer {
}
}

fn de_text_quoted<R: BufferRead>(
fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
self.builder.append_value(reader);
Ok(())
}

fn de_text<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
_format: &FormatSettings,
) -> Result<()> {
self.buffer.clear();
reader.read_quoted_text(&mut self.buffer, b'\'')?;
reader.read_escaped_string_text(&mut self.buffer)?;
self.builder.append_value(self.buffer.as_slice());
Ok(())
}

fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
self.builder.append_value(reader);
fn de_text_quoted<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
_format: &FormatSettings,
) -> Result<()> {
self.buffer.clear();
reader.read_quoted_text(&mut self.buffer, b'\'')?;
self.builder.append_value(self.buffer.as_slice());
Ok(())
}

fn de_text<R: BufferRead>(
fn de_text_csv<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
_format: &FormatSettings,
format: &FormatSettings,
) -> Result<()> {
self.buffer.clear();
reader.read_escaped_string_text(&mut self.buffer)?;
if reader.ignore_byte(b'"')? {
reader.keep_read(&mut self.buffer, |b| b != b'"')?;
reader.must_ignore_byte(b'"')?;
} else if format.field_delimiter.is_empty() {
reader.keep_read(&mut self.buffer, |b| b != b',')?;
} else {
reader.keep_read(&mut self.buffer, |b| b != format.field_delimiter[0])?;
}

self.builder.append_value(self.buffer.as_slice());
Ok(())
}
Expand Down
22 changes: 18 additions & 4 deletions common/datavalues/src/types/deserializations/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl TypeDeserializer for VariantDeserializer {
Ok(())
}

fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
let val = serde_json::from_slice(reader)?;
self.builder.append_value(val);
Ok(())
}

fn de_text<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
Expand All @@ -87,19 +93,27 @@ impl TypeDeserializer for VariantDeserializer {
Ok(())
}

fn de_whole_text(&mut self, reader: &[u8], _format: &FormatSettings) -> Result<()> {
let val = serde_json::from_slice(reader)?;
fn de_text_quoted<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
_format: &FormatSettings,
) -> Result<()> {
self.buffer.clear();
reader.read_quoted_text(&mut self.buffer, b'\'')?;

let val = serde_json::from_slice(self.buffer.as_slice())?;

self.builder.append_value(val);
Ok(())
}

fn de_text_quoted<R: BufferRead>(
fn de_text_csv<R: BufferRead>(
&mut self,
reader: &mut CheckpointReader<R>,
_format: &FormatSettings,
) -> Result<()> {
self.buffer.clear();
reader.read_quoted_text(&mut self.buffer, b'\'')?;
reader.read_quoted_text(&mut self.buffer, b'"')?;

let val = serde_json::from_slice(self.buffer.as_slice())?;
self.builder.append_value(val);
Expand Down
2 changes: 2 additions & 0 deletions common/exception/src/exception_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ build_exceptions! {
// Network error codes.
NetworkRequestError(1073),

UnknownFormat(1074),

// Tenant error codes.
TenantIsEmpty(1101),
IndexOutOfBounds(1102),
Expand Down
33 changes: 31 additions & 2 deletions common/io/src/buffer/buffer_read_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait BufferReadExt: BufferRead {
fn ignore_bytes(&mut self, bs: &[u8]) -> Result<bool>;
fn ignore_insensitive_bytes(&mut self, bs: &[u8]) -> Result<bool>;
fn ignore_white_spaces(&mut self) -> Result<bool>;
fn ignore_white_spaces_and_byte(&mut self, b: u8) -> Result<bool>;
fn until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize>;

fn keep_read(&mut self, buf: &mut Vec<u8>, f: impl Fn(u8) -> bool) -> Result<usize>;
Expand Down Expand Up @@ -55,6 +56,11 @@ pub trait BufferReadExt: BufferRead {
Ok(())
}

fn eof(&mut self) -> Result<bool> {
let buffer = self.fill_buf()?;
Ok(buffer.is_empty())
}

fn must_eof(&mut self) -> Result<()> {
let buffer = self.fill_buf()?;
if !buffer.is_empty() {
Expand All @@ -78,9 +84,21 @@ pub trait BufferReadExt: BufferRead {

fn must_ignore_byte(&mut self, b: u8) -> Result<()> {
if !self.ignore_byte(b)? {
let buf = self.fill_buf()?;
return Err(std::io::Error::new(
ErrorKind::InvalidData,
format!("Expected to have char {}", b as char),
format!("Expected to have char {}, {}", b as char, buf[0] as char),
zhang2014 marked this conversation as resolved.
Show resolved Hide resolved
));
}
Ok(())
}

fn must_ignore_white_spaces_and_byte(&mut self, b: u8) -> Result<()> {
if !self.ignore_white_spaces_and_byte(b)? {
let buf = self.fill_buf()?;
return Err(std::io::Error::new(
ErrorKind::InvalidData,
format!("Expected to have char {}, {}", b as char, buf[0] as char),
zhang2014 marked this conversation as resolved.
Show resolved Hide resolved
));
}
Ok(())
Expand Down Expand Up @@ -108,7 +126,7 @@ pub trait BufferReadExt: BufferRead {
}

impl<R> BufferReadExt for R
where R: BufferRead
where R: BufferRead
{
fn ignores(&mut self, f: impl Fn(u8) -> bool) -> Result<usize> {
let mut bytes = 0;
Expand Down Expand Up @@ -172,6 +190,17 @@ where R: BufferRead
Ok(cnt > 0)
}

fn ignore_white_spaces_and_byte(&mut self, b: u8) -> Result<bool> {
self.ignores(|c: u8| c == b' ')?;

if self.ignore_byte(b)? {
self.ignores(|c: u8| c == b' ')?;
return Ok(true);
}

Ok(false)
}

fn until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
self.read_until(delim, buf)
}
Expand Down
36 changes: 36 additions & 0 deletions query/src/formats/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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::any::Any;

use common_datablocks::DataBlock;
use common_exception::Result;

pub trait InputState: Send {
fn as_any(&mut self) -> &mut dyn Any;
}

pub trait InputFormat: Send {
fn support_parallel(&self) -> bool {
false
}

fn create_state(&self) -> Box<dyn InputState>;

fn deserialize_data(&self, state: &mut Box<dyn InputState>) -> Result<DataBlock>;

fn read_buf(&self, buf: &[u8], state: &mut Box<dyn InputState>) -> Result<usize>;

fn skip_header(&self, buf: &[u8], state: &mut Box<dyn InputState>) -> Result<usize>;
}
Loading