Skip to content

feat: avoid building dictionary for massive unique column values #1365

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

Merged
merged 6 commits into from
Dec 18, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions analytic_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ datafusion = { workspace = true }
future_ext = { workspace = true }
futures = { workspace = true }
generic_error = { workspace = true }
hash_ext = { workspace = true }
hex = { workspace = true }
hyperloglog = { workspace = true }
id_allocator = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions analytic_engine/src/instance/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ use table_engine::{
};
use wal::manager::WalLocation;

use super::open::{TableContext, TablesOfShardContext};
use crate::{
engine::build_space_id,
instance::{close::Closer, drop::Dropper, open::OpenTablesOfShardResult, Instance},
instance::{
close::Closer,
drop::Dropper,
open::{OpenTablesOfShardResult, TableContext, TablesOfShardContext},
Instance,
},
space::{MemSizeOptions, Space, SpaceAndTable, SpaceContext, SpaceId, SpaceRef},
};

Expand Down
4 changes: 2 additions & 2 deletions analytic_engine/src/instance/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use snafu::ResultExt;
use table_engine::{engine::TableDef, table::TableId};
use wal::manager::WalManagerRef;

use super::{engine::OpenTablesOfShard, flush_compaction::Flusher};
use crate::{
compaction::scheduler::SchedulerImpl,
context::OpenContext,
engine,
instance::{
engine::{OpenManifest, ReadMetaUpdate, Result},
engine::{OpenManifest, OpenTablesOfShard, ReadMetaUpdate, Result},
flush_compaction::Flusher,
mem_collector::MemUsageCollector,
wal_replayer::{ReplayMode, WalReplayer},
Instance, SpaceStore,
Expand Down
3 changes: 1 addition & 2 deletions analytic_engine/src/instance/serial_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ use tokio::sync::{
watch::{self, Receiver, Sender},
};

use super::flush_compaction::{BackgroundFlushFailed, TableFlushOptions};
use crate::{
instance::flush_compaction::{Other, Result},
instance::flush_compaction::{BackgroundFlushFailed, Other, Result, TableFlushOptions},
table::data::TableData,
};

Expand Down
14 changes: 11 additions & 3 deletions analytic_engine/src/sst/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use crate::{
header::HeaderParser,
meta_data::cache::MetaCacheRef,
metrics::MaybeTableLevelMetrics as SstMaybeTableLevelMetrics,
parquet::{writer::ParquetSstWriter, AsyncParquetReader, ThreadedReader},
parquet::{
writer::{ParquetSstWriter, WriteOptions},
AsyncParquetReader, ThreadedReader,
},
reader::SstReader,
writer::SstWriter,
},
Expand Down Expand Up @@ -200,11 +203,16 @@ impl Factory for FactoryImpl {
store_picker: &'a ObjectStorePickerRef,
level: Level,
) -> Result<Box<dyn SstWriter + Send + 'a>> {
let write_options = WriteOptions {
num_rows_per_row_group: options.num_rows_per_row_group,
max_buffer_size: options.max_buffer_size,
compression: options.compression.into(),
sst_level: level,
};
Ok(Box::new(ParquetSstWriter::new(
path,
level,
write_options,
store_picker,
options,
)))
}
}
2 changes: 1 addition & 1 deletion analytic_engine/src/sst/meta_data/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ mod tests {
use parquet::{arrow::ArrowWriter, file::footer};
use parquet_ext::ParquetMetaData;

use super::MetaData;
use super::*;
use crate::{
sst::parquet::{
encoding::{self, META_PATH_KEY, META_VERSION_KEY},
Expand Down
5 changes: 2 additions & 3 deletions analytic_engine/src/sst/meta_data/metadata_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ use object_store::{ObjectStoreRef, Path};
use parquet::{data_type::AsBytes, file::metadata::KeyValue};
use snafu::{ensure, OptionExt, ResultExt};

use super::UnknownMetaVersion;
use crate::sst::{
meta_data::{
DecodeCustomMetaData, FetchAndDecodeSstMeta, FetchFromStore, KvMetaDataNotFound,
KvMetaPathEmpty,
KvMetaPathEmpty, UnknownMetaVersion,
},
parquet::{
encoding::{self, decode_sst_meta_data_from_bytes, META_VERSION_CURRENT, META_VERSION_V1},
meta_data::{ParquetMetaData, ParquetMetaDataRef},
},
};

define_result!(super::Error);
define_result!(crate::sst::meta_data::Error);

#[async_trait]
pub trait CustomMetadataReader {
Expand Down
5 changes: 3 additions & 2 deletions analytic_engine/src/sst/parquet/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use tokio::sync::{
};
use trace_metric::{MetricsCollector, TraceMetricWhenDrop};

use super::meta_data::ColumnValueSet;
use crate::{
prefetchable_stream::{NoopPrefetcher, PrefetchableStream},
sst::{
Expand All @@ -68,7 +67,9 @@ use crate::{
},
metrics::MaybeTableLevelMetrics,
parquet::{
encoding::ParquetDecoder, meta_data::ParquetFilter, row_group_pruner::RowGroupPruner,
encoding::ParquetDecoder,
meta_data::{ColumnValueSet, ParquetFilter},
row_group_pruner::RowGroupPruner,
},
reader::{error::*, Result, SstReader},
},
Expand Down
54 changes: 31 additions & 23 deletions analytic_engine/src/sst/parquet/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::convert::TryFrom;
use std::{collections::HashMap, convert::TryFrom};

use arrow::{compute, record_batch::RecordBatch as ArrowRecordBatch};
use async_trait::async_trait;
Expand All @@ -26,6 +26,7 @@ use parquet::{
arrow::AsyncArrowWriter,
basic::Compression,
file::{metadata::KeyValue, properties::WriterProperties},
schema::types::ColumnPath,
};
use prost::{bytes, Message};
use snafu::{ensure, Backtrace, OptionExt, ResultExt, Snafu};
Expand Down Expand Up @@ -237,25 +238,40 @@ struct ColumnarRecordEncoder<W> {
arrow_schema: ArrowSchemaRef,
}

#[derive(Debug, Clone)]
pub struct ColumnEncoding {
pub enable_dict: bool,
}

#[derive(Debug, Clone)]
pub struct EncodeOptions {
pub num_rows_per_row_group: usize,
pub max_buffer_size: usize,
pub compression: Compression,
pub column_encodings: HashMap<String, ColumnEncoding>,
}

impl<W: AsyncWrite + Send + Unpin> ColumnarRecordEncoder<W> {
fn try_new(
sink: W,
schema: &Schema,
num_rows_per_row_group: usize,
max_buffer_size: usize,
compression: Compression,
) -> Result<Self> {
fn try_new(sink: W, schema: &Schema, options: &EncodeOptions) -> Result<Self> {
let arrow_schema = schema.to_arrow_schema_ref();

let write_props = WriterProperties::builder()
.set_max_row_group_size(num_rows_per_row_group)
.set_compression(compression)
.build();
let write_props = {
let mut builder = WriterProperties::builder()
.set_max_row_group_size(options.num_rows_per_row_group)
.set_compression(options.compression);

for (col_name, encoding) in &options.column_encodings {
let col_path = ColumnPath::new(vec![col_name.to_string()]);
builder = builder.set_column_dictionary_enabled(col_path, encoding.enable_dict);
}

builder.build()
};

let arrow_writer = AsyncArrowWriter::try_new(
sink,
arrow_schema.clone(),
max_buffer_size,
options.max_buffer_size,
Some(write_props),
)
.box_err()
Expand Down Expand Up @@ -326,18 +342,10 @@ impl ParquetEncoder {
pub fn try_new<W: AsyncWrite + Unpin + Send + 'static>(
sink: W,
schema: &Schema,
num_rows_per_row_group: usize,
max_buffer_size: usize,
compression: Compression,
options: &EncodeOptions,
) -> Result<Self> {
Ok(ParquetEncoder {
record_encoder: Box::new(ColumnarRecordEncoder::try_new(
sink,
schema,
num_rows_per_row_group,
max_buffer_size,
compression,
)?),
record_encoder: Box::new(ColumnarRecordEncoder::try_new(sink, schema, options)?),
})
}

Expand Down
10 changes: 5 additions & 5 deletions analytic_engine/src/sst/parquet/meta_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ pub struct ParquetMetaData {

pub type ParquetMetaDataRef = Arc<ParquetMetaData>;

impl From<MetaData> for ParquetMetaData {
fn from(meta: MetaData) -> Self {
impl From<&MetaData> for ParquetMetaData {
fn from(meta: &MetaData) -> Self {
Self {
min_key: meta.min_key,
max_key: meta.max_key,
min_key: meta.min_key.clone(),
max_key: meta.max_key.clone(),
time_range: meta.time_range,
max_sequence: meta.max_sequence,
schema: meta.schema,
schema: meta.schema.clone(),
parquet_filter: None,
column_values: None,
}
Expand Down
3 changes: 1 addition & 2 deletions analytic_engine/src/sst/parquet/row_group_pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ use parquet_ext::prune::{
use snafu::ensure;
use trace_metric::{MetricsCollector, TraceMetricWhenDrop};

use super::meta_data::ColumnValueSet;
use crate::sst::{
parquet::meta_data::ParquetFilter,
parquet::meta_data::{ColumnValueSet, ParquetFilter},
reader::error::{OtherNoCause, Result},
};

Expand Down
Loading