|
| 1 | +use std::str::from_utf8; |
| 2 | +use std::sync::Arc; |
| 3 | + |
1 | 4 | use crate::data_type::{datum_to_scalar, read_scalar_value, write_scalar_value, EncodedType};
|
2 | 5 | use crate::error::FusionError;
|
3 | 6 | use crate::ipc::{Bus, Slot, SlotNumber, SlotStream, DATA_SIZE};
|
| 7 | +use crate::sql::Table; |
4 | 8 | use crate::worker::worker_id;
|
| 9 | +use ahash::AHashMap; |
5 | 10 | use anyhow::Result;
|
| 11 | +use datafusion::arrow::datatypes::{Field, Schema}; |
| 12 | +use datafusion::logical_expr::TableSource; |
6 | 13 | use datafusion::scalar::ScalarValue;
|
7 | 14 | use datafusion_sql::TableReference;
|
8 | 15 | use pgrx::pg_sys::{Oid, ParamExternData, ProcSendSignal};
|
9 | 16 | use pgrx::prelude::*;
|
10 | 17 | use pgrx::{pg_guard, PgRelation};
|
11 |
| -use rmp::decode::{read_array_len, read_bin_len, read_pfix, read_str_len, read_u16, read_u8}; |
| 18 | +use rmp::decode::{ |
| 19 | + read_array_len, read_bin_len, read_bool, read_pfix, read_str_len, read_u16, read_u32, read_u8, |
| 20 | +}; |
12 | 21 | use rmp::encode::{
|
13 | 22 | write_array_len, write_bin_len, write_bool, write_pfix, write_str, write_u16, write_u32,
|
14 | 23 | write_u8, RmpWrite,
|
15 | 24 | };
|
| 25 | +use smol_str::SmolStr; |
16 | 26 |
|
17 | 27 | #[repr(u8)]
|
18 | 28 | #[derive(Clone, Debug, Default, PartialEq)]
|
@@ -416,10 +426,59 @@ pub(crate) fn send_metadata(
|
416 | 426 | Ok(())
|
417 | 427 | }
|
418 | 428 |
|
| 429 | +#[inline] |
| 430 | +pub(crate) fn consume_metadata( |
| 431 | + stream: &mut SlotStream, |
| 432 | +) -> Result<AHashMap<TableReference, Arc<dyn TableSource>>> { |
| 433 | + // The header should be consumed before calling this function. |
| 434 | + let table_num = read_array_len(stream)?; |
| 435 | + let mut tables = AHashMap::with_capacity(table_num as usize); |
| 436 | + for _ in 0..table_num { |
| 437 | + let name_part_num = read_array_len(stream)?; |
| 438 | + assert!(name_part_num == 2 || name_part_num == 3); |
| 439 | + let oid = read_u32(stream)?; |
| 440 | + let mut schema = None; |
| 441 | + if name_part_num == 3 { |
| 442 | + let ns_len = read_str_len(stream)?; |
| 443 | + let ns_bytes = stream.look_ahead(ns_len as usize)?; |
| 444 | + schema = Some(SmolStr::new(from_utf8(ns_bytes)?)); |
| 445 | + stream.rewind(ns_len as usize)?; |
| 446 | + } |
| 447 | + let name_len = read_str_len(stream)?; |
| 448 | + let name_bytes = stream.look_ahead(name_len as usize)?; |
| 449 | + let name = from_utf8(name_bytes)?; |
| 450 | + let table_ref = match schema { |
| 451 | + Some(schema) => TableReference::partial(schema, name), |
| 452 | + None => TableReference::bare(name), |
| 453 | + }; |
| 454 | + stream.rewind(name_len as usize)?; |
| 455 | + let column_num = read_array_len(stream)?; |
| 456 | + let mut fields = Vec::with_capacity(column_num as usize); |
| 457 | + for _ in 0..column_num { |
| 458 | + let elem_num = read_array_len(stream)?; |
| 459 | + assert_eq!(elem_num, 3); |
| 460 | + let etype = read_u8(stream)?; |
| 461 | + let df_type = EncodedType::try_from(etype)?.to_arrow(); |
| 462 | + let is_nullable = read_bool(stream)?; |
| 463 | + let name_len = read_str_len(stream)?; |
| 464 | + let name_bytes = stream.look_ahead(name_len as usize)?; |
| 465 | + let name = from_utf8(name_bytes)?; |
| 466 | + let field = Field::new(name, df_type, is_nullable); |
| 467 | + stream.rewind(name_len as usize)?; |
| 468 | + fields.push(field); |
| 469 | + } |
| 470 | + let schema = Schema::new(fields); |
| 471 | + let table = Table::new(Oid::from(oid), Arc::new(schema)); |
| 472 | + tables.insert(table_ref, Arc::new(table) as Arc<dyn TableSource>); |
| 473 | + } |
| 474 | + Ok(tables) |
| 475 | +} |
| 476 | + |
419 | 477 | #[cfg(any(test, feature = "pg_test"))]
|
420 | 478 | #[pg_schema]
|
421 | 479 | mod tests {
|
422 | 480 | use super::*;
|
| 481 | + use datafusion::arrow::datatypes::DataType; |
423 | 482 | use pgrx::pg_sys::{Datum, Oid};
|
424 | 483 | use rmp::decode::{read_bool, read_u32};
|
425 | 484 | use std::ptr::addr_of_mut;
|
@@ -651,4 +710,47 @@ mod tests {
|
651 | 710 | assert_eq!(name, b"a");
|
652 | 711 | stream.rewind(name_len as usize).unwrap();
|
653 | 712 | }
|
| 713 | + #[pg_test] |
| 714 | + fn test_metadata_to_tables() { |
| 715 | + Spi::run("create table if not exists t1(a int not null, b text);").unwrap(); |
| 716 | + Spi::run("create schema if not exists s1;").unwrap(); |
| 717 | + Spi::run("create table if not exists s1.t2(a int);").unwrap(); |
| 718 | + let t1_oid = Spi::get_one::<Oid>("select 't1'::regclass::oid;") |
| 719 | + .unwrap() |
| 720 | + .unwrap(); |
| 721 | + let t2_oid = Spi::get_one::<Oid>("select 's1.t2'::regclass::oid;") |
| 722 | + .unwrap() |
| 723 | + .unwrap(); |
| 724 | + |
| 725 | + let mut slot_buf: [u8; SLOT_SIZE] = [1; SLOT_SIZE]; |
| 726 | + let ptr = addr_of_mut!(slot_buf) as *mut u8; |
| 727 | + Slot::init(ptr, slot_buf.len()); |
| 728 | + let slot = Slot::from_bytes(ptr, slot_buf.len()); |
| 729 | + let mut stream: SlotStream = slot.into(); |
| 730 | + |
| 731 | + prepare_metadata(&[(t1_oid, false), (t2_oid, true)], &mut stream).unwrap(); |
| 732 | + stream.reset(); |
| 733 | + let header = consume_header(&mut stream).unwrap(); |
| 734 | + assert_eq!(header.direction, Direction::ToWorker); |
| 735 | + assert_eq!(header.packet, Packet::Metadata); |
| 736 | + assert_eq!(header.flag, Flag::Last); |
| 737 | + |
| 738 | + let tables = consume_metadata(&mut stream).unwrap(); |
| 739 | + assert_eq!(tables.len(), 2); |
| 740 | + // t1 |
| 741 | + let t1 = tables.get(&TableReference::bare("t1")).unwrap(); |
| 742 | + assert_eq!(t1.schema().fields().len(), 2); |
| 743 | + assert_eq!(t1.schema().field(0).name(), "a"); |
| 744 | + assert_eq!(t1.schema().field(1).name(), "b"); |
| 745 | + assert_eq!(t1.schema().field(0).data_type(), &DataType::Int32); |
| 746 | + assert_eq!(t1.schema().field(1).data_type(), &DataType::Utf8); |
| 747 | + assert!(!t1.schema().field(0).is_nullable()); |
| 748 | + assert!(t1.schema().field(1).is_nullable()); |
| 749 | + // s1.t2 |
| 750 | + let t2 = tables.get(&TableReference::partial("s1", "t2")).unwrap(); |
| 751 | + assert_eq!(t2.schema().fields().len(), 1); |
| 752 | + assert_eq!(t2.schema().field(0).name(), "a"); |
| 753 | + assert_eq!(t2.schema().field(0).data_type(), &DataType::Int32); |
| 754 | + assert!(t2.schema().field(0).is_nullable()); |
| 755 | + } |
654 | 756 | }
|
0 commit comments