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: load/unload decimal with CSV/TSV/NDJSON #10176

Merged
merged 5 commits into from
Feb 23, 2023
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
8 changes: 8 additions & 0 deletions src/query/formats/src/field_encoder/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use common_expression::types::array::ArrayColumn;
use common_expression::types::decimal::DecimalColumn;
use common_expression::types::ValueType;
use common_expression::Column;
use common_io::constants::FALSE_BYTES_LOWER;
Expand Down Expand Up @@ -87,4 +88,11 @@ impl FieldEncoderRowBased for FieldEncoderJSON {
self.nested.write_tuple(columns, row_index, &mut buf, false);
self.write_string_inner(&buf, out_buf, raw)
}

fn write_decimal(&self, column: &DecimalColumn, row_index: usize, out_buf: &mut Vec<u8>) {
let data = column.index(row_index).unwrap().to_string();
out_buf.push(b'"');
out_buf.extend_from_slice(data.as_bytes());
out_buf.push(b'"');
}
}
61 changes: 30 additions & 31 deletions src/query/formats/src/field_encoder/row_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use common_arrow::arrow::bitmap::Bitmap;
use common_arrow::arrow::buffer::Buffer;
use common_expression::types::array::ArrayColumn;
use common_expression::types::date::date_to_string;
use common_expression::types::decimal::DecimalColumn;
use common_expression::types::nullable::NullableColumn;
use common_expression::types::number::NumberColumn;
use common_expression::types::string::StringColumn;
Expand All @@ -33,27 +34,28 @@ use crate::CommonSettings;
pub trait FieldEncoderRowBased {
fn common_settings(&self) -> &CommonSettings;

/// 'raw' is mainly for string now and types need to be encoded as string in a format,
/// to determine the quote/escape behavior.
/// 'raw=true' can be roughly understood as 'no quote/escape'.
/// for example, string inside tuple/struct are always quoted/escaped now.
fn write_field(&self, column: &Column, row_index: usize, out_buf: &mut Vec<u8>, raw: bool) {
match &column {
Column::Null { .. } => self.write_null(out_buf, raw),
Column::EmptyArray { .. } => self.write_empty_array(out_buf, raw),
Column::Boolean(c) => self.write_bool(c, row_index, out_buf, raw),
Column::Null { .. } => self.write_null(out_buf),
Column::EmptyArray { .. } => self.write_empty_array(out_buf),
Column::Boolean(c) => self.write_bool(c, row_index, out_buf),
Column::Number(col) => match col {
NumberColumn::UInt8(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::UInt16(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::UInt32(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::UInt64(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::Int8(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::Int16(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::Int32(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::Int64(c) => self.write_int(c, row_index, out_buf, raw),
NumberColumn::Float32(c) => self.write_float(c, row_index, out_buf, raw),
NumberColumn::Float64(c) => self.write_float(c, row_index, out_buf, raw),
NumberColumn::UInt8(c) => self.write_int(c, row_index, out_buf),
NumberColumn::UInt16(c) => self.write_int(c, row_index, out_buf),
NumberColumn::UInt32(c) => self.write_int(c, row_index, out_buf),
NumberColumn::UInt64(c) => self.write_int(c, row_index, out_buf),
NumberColumn::Int8(c) => self.write_int(c, row_index, out_buf),
NumberColumn::Int16(c) => self.write_int(c, row_index, out_buf),
NumberColumn::Int32(c) => self.write_int(c, row_index, out_buf),
NumberColumn::Int64(c) => self.write_int(c, row_index, out_buf),
NumberColumn::Float32(c) => self.write_float(c, row_index, out_buf),
NumberColumn::Float64(c) => self.write_float(c, row_index, out_buf),
},
Column::Decimal(x) => {
let data = x.index(row_index).unwrap().to_string();
out_buf.extend_from_slice(data.as_bytes());
}
Column::Decimal(c) => self.write_decimal(c, row_index, out_buf),
Column::Date(c) => self.write_date(c, row_index, out_buf, raw),
Column::Timestamp(c) => self.write_timestamp(c, row_index, out_buf, raw),
Column::String(c) => self.write_string(c, row_index, out_buf, raw),
Expand All @@ -64,7 +66,7 @@ pub trait FieldEncoderRowBased {
}
}

fn write_bool(&self, column: &Bitmap, row_index: usize, out_buf: &mut Vec<u8>, _raw: bool) {
fn write_bool(&self, column: &Bitmap, row_index: usize, out_buf: &mut Vec<u8>) {
let v = if column.get_bit(row_index) {
&self.common_settings().true_bytes
} else {
Expand All @@ -74,11 +76,11 @@ pub trait FieldEncoderRowBased {
out_buf.extend_from_slice(v);
}

fn write_null(&self, out_buf: &mut Vec<u8>, _raw: bool) {
fn write_null(&self, out_buf: &mut Vec<u8>) {
out_buf.extend_from_slice(&self.common_settings().null_bytes);
}

fn write_empty_array(&self, out_buf: &mut Vec<u8>, _raw: bool) {
fn write_empty_array(&self, out_buf: &mut Vec<u8>) {
out_buf.extend_from_slice(b"[");
out_buf.extend_from_slice(b"]");
}
Expand All @@ -91,7 +93,7 @@ pub trait FieldEncoderRowBased {
raw: bool,
) {
if !column.validity.get_bit(row_index) {
self.write_null(out_buf, raw)
self.write_null(out_buf)
} else {
self.write_field(
&T::upcast_column(column.column.clone()),
Expand All @@ -104,15 +106,8 @@ pub trait FieldEncoderRowBased {

fn write_string_inner(&self, in_buf: &[u8], out_buf: &mut Vec<u8>, raw: bool);

fn write_int<T>(
&self,
column: &Buffer<T>,
row_index: usize,
out_buf: &mut Vec<u8>,
_raw: bool,
) where
T: Marshal + Unmarshal<T> + ToLexical + PrimitiveWithFormat,
{
fn write_int<T>(&self, column: &Buffer<T>, row_index: usize, out_buf: &mut Vec<u8>)
where T: Marshal + Unmarshal<T> + ToLexical + PrimitiveWithFormat {
let v = unsafe { column.get_unchecked(row_index) };
v.write_field(out_buf, self.common_settings())
}
Expand All @@ -122,14 +117,18 @@ pub trait FieldEncoderRowBased {
column: &Buffer<OrderedFloat<T>>,
row_index: usize,
out_buf: &mut Vec<u8>,
_raw: bool,
) where
T: Marshal + Unmarshal<T> + ToLexical + PrimitiveWithFormat,
{
let v = unsafe { column.get_unchecked(row_index) };
v.0.write_field(out_buf, self.common_settings())
}

fn write_decimal(&self, column: &DecimalColumn, row_index: usize, out_buf: &mut Vec<u8>) {
let data = column.index(row_index).unwrap().to_string();
out_buf.extend_from_slice(data.as_bytes());
}

fn write_string(
&self,
column: &StringColumn,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---CSV
"a""b",1.0,"['a""b']","{""k"":""v""}","2044-05-06 10:25:02.868894"
\N,2.0,"['a\'b']","[1]","2044-05-06 10:25:02.868894"
"a""b",1.0,"['a""b']","{""k"":""v""}","2044-05-06 10:25:02.868894",10.01
\N,2.0,"['a\'b']","[1]","2044-05-06 10:25:02.868894",-10.01
1
---TSV
a"b 1.0 ['a"b'] {"k":"v"} 2044-05-06 10:25:02.868894
\N 2.0 ['a\'b'] [1] 2044-05-06 10:25:02.868894
a"b 1.0 ['a"b'] {"k":"v"} 2044-05-06 10:25:02.868894 10.01
\N 2.0 ['a\'b'] [1] 2044-05-06 10:25:02.868894 -10.01
1
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ echo "CREATE TABLE test_load_unload
b float,
c array(string),
d Variant,
e timestamp
e timestamp,
f decimal(4, 2)
);" | $MYSQL_CLIENT_CONNECT

insert_data() {
echo "insert into test_load_unload values
('a\"b', 1, ['a\"b'], parse_json('{\"k\":\"v\"}'), '2044-05-06T03:25:02.868894-07:00'),
(null, 2, ['a\'b'], parse_json('[1]'), '2044-05-06T03:25:02.868894-07:00')
('a\"b', 1, ['a\"b'], parse_json('{\"k\":\"v\"}'), '2044-05-06T03:25:02.868894-07:00', 010.011),
(null, 2, ['a\'b'], parse_json('[1]'), '2044-05-06T03:25:02.868894-07:00', -010.011)
" | $MYSQL_CLIENT_CONNECT
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---NDJSON
{"a":"a\"b","b":1.0,"e":"2044-05-06 10:25:02.868894"}
{"a":null,"b":2.0,"e":"2044-05-06 10:25:02.868894"}
{"a":"a\"b","b":1.0,"e":"2044-05-06 10:25:02.868894","f":"10.01"}
{"a":null,"b":2.0,"e":"2044-05-06 10:25:02.868894","f":"-10.01"}
1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ echo "CREATE TABLE test_load_unload
(
a VARCHAR NULL,
b float,
e timestamp
e timestamp,
f decimal(4, 2)
);" | $MYSQL_CLIENT_CONNECT

insert_data() {
echo "insert into test_load_unload values
('a\"b', 1, '2044-05-06T03:25:02.868894-07:00'),
(null, 2, '2044-05-06T03:25:02.868894-07:00')
('a\"b', 1, '2044-05-06T03:25:02.868894-07:00', 010.011),
(null, 2, '2044-05-06T03:25:02.868894-07:00', -010.011)
" | $MYSQL_CLIENT_CONNECT
}

Expand Down