MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / encode_binary

Method encode_binary

src/pgrepr/src/value.rs:437–588  ·  view source on GitHub ↗

Serializes this value to `buf` using the [binary encoding format](Format::Binary).

(&self, ty: &Type, buf: &mut BytesMut)

Source from the content-addressed store, hash-verified

435 /// Serializes this value to `buf` using the [binary encoding
436 /// format](Format::Binary).
437 pub fn encode_binary(&self, ty: &Type, buf: &mut BytesMut) -> Result<(), io::Error> {
438 // NOTE: If implementing binary encoding for a previously unsupported `Value` type,
439 // please update the `binary_encoding_error` method below.
440 let is_null = match self {
441 Value::Array { dims, elements } => {
442 let ndims = pg_len("number of array dimensions", dims.len())?;
443 let has_null = elements.iter().any(|e| e.is_none());
444 let elem_type = match ty {
445 Type::Array(elem_type) => elem_type,
446 _ => unreachable!(),
447 };
448 buf.put_i32(ndims);
449 buf.put_i32(has_null.into());
450 buf.put_u32(elem_type.oid());
451 for dim in dims {
452 buf.put_i32(pg_len("array dimension length", dim.length)?);
453 buf.put_i32(dim.lower_bound.try_into().map_err(|_| {
454 io::Error::new(
455 io::ErrorKind::InvalidData,
456 "array dimension lower bound does not fit into an i32",
457 )
458 })?);
459 }
460 for elem in elements {
461 encode_element(buf, elem.as_ref(), elem_type)?;
462 }
463 Ok(postgres_types::IsNull::No)
464 }
465 Value::Int2Vector { elements } => {
466 // this should always be `false`, but there are exceptions in postgres
467 // feels better to compute this than to assert otherwise
468 let has_null = elements.iter().any(|e| e.is_none());
469 buf.put_i32(1);
470 buf.put_i32(has_null.into());
471 buf.put_u32(TYPE_INT2_OID);
472 buf.put_i32(pg_len("int2vector dimension length", elements.len())?);
473 buf.put_i32(0);
474 for elem in elements {
475 encode_element(buf, elem.as_ref(), &Type::Int2)?;
476 }
477 Ok(postgres_types::IsNull::No)
478 }
479 Value::Bool(b) => b.to_sql(&PgType::BOOL, buf),
480 Value::Bytea(b) => b.to_sql(&PgType::BYTEA, buf),
481 Value::Char(c) => i8::reinterpret_cast(*c).to_sql(&PgType::CHAR, buf),
482 Value::Date(d) => d.pg_epoch_days().to_sql(&PgType::DATE, buf),
483 Value::Float4(f) => f.to_sql(&PgType::FLOAT4, buf),
484 Value::Float8(f) => f.to_sql(&PgType::FLOAT8, buf),
485 Value::Int2(i) => i.to_sql(&PgType::INT2, buf),
486 Value::Int4(i) => i.to_sql(&PgType::INT4, buf),
487 Value::Int8(i) => i.to_sql(&PgType::INT8, buf),
488 Value::UInt2(u) => u.to_sql(&*UINT2, buf),
489 Value::UInt4(u) => u.to_sql(&*UINT4, buf),
490 Value::UInt8(u) => u.to_sql(&*UINT8, buf),
491 Value::Interval(iv) => iv.to_sql(&PgType::INTERVAL, buf),
492 Value::Jsonb(js) => js.to_sql(&PgType::JSONB, buf),
493 Value::List(_) => {
494 // A binary encoding for list is tricky. We only get one OID to

Callers 5

encodeMethod · 0.45
encode_elementFunction · 0.45
encode_copy_row_binaryFunction · 0.45
encode_uncheckedMethod · 0.45

Calls 15

pg_lenFunction · 0.85
encode_elementFunction · 0.85
expectMethod · 0.80
anyMethod · 0.80
is_noneMethod · 0.80
pg_epoch_daysMethod · 0.80
pg_flag_bitsMethod · 0.80
lenMethod · 0.45
iterMethod · 0.45
oidMethod · 0.45
try_intoMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected