MCPcopy Create free account
hub / github.com/apache/cloudberry / CopyConvertBuf

Function CopyConvertBuf

src/backend/commands/copyfromparse.c:368–496  ·  view source on GitHub ↗

* Perform encoding conversion on data in 'raw_buf', writing the converted * data into 'input_buf'. * * On entry, there must be some data to convert in 'raw_buf'. */

Source from the content-addressed store, hash-verified

366 * On entry, there must be some data to convert in 'raw_buf'.
367 */
368static void
369CopyConvertBuf(CopyFromState cstate)
370{
371 /*
372 * If the file and server encoding are the same, no encoding conversion is
373 * required. However, we still need to verify that the input is valid for
374 * the encoding.
375 */
376 if (!cstate->need_transcoding)
377 {
378 /*
379 * When conversion is not required, input_buf and raw_buf are the
380 * same. raw_buf_len is the total number of bytes in the buffer, and
381 * input_buf_len tracks how many of those bytes have already been
382 * verified.
383 */
384 int preverifiedlen = cstate->input_buf_len;
385 int unverifiedlen = cstate->raw_buf_len - cstate->input_buf_len;
386 int nverified;
387
388 if (unverifiedlen == 0)
389 {
390 /*
391 * If no more raw data is coming, report the EOF to the caller.
392 */
393 if (cstate->raw_reached_eof)
394 cstate->input_reached_eof = true;
395 return;
396 }
397
398 /*
399 * Verify the new data, including any residual unverified bytes from
400 * previous round.
401 */
402 nverified = pg_encoding_verifymbstr(cstate->file_encoding,
403 cstate->raw_buf + preverifiedlen,
404 unverifiedlen);
405 if (nverified == 0)
406 {
407 /*
408 * Could not verify anything.
409 *
410 * If there is no more raw input data coming, it means that there
411 * was an incomplete multi-byte sequence at the end. Also, if
412 * there's "enough" input left, we should be able to verify at
413 * least one character, and a failure to do so means that we've
414 * hit an invalid byte sequence.
415 */
416 if (cstate->raw_reached_eof || unverifiedlen >= pg_encoding_max_length(cstate->file_encoding))
417 cstate->input_reached_error = true;
418 return;
419 }
420 cstate->input_buf_len += nverified;
421 }
422 else
423 {
424 /*
425 * Encoding conversion is needed.

Callers 1

CopyLoadInputBufFunction · 0.85

Calls 4

pg_encoding_verifymbstrFunction · 0.85
pg_encoding_max_lengthFunction · 0.85
GetDatabaseEncodingFunction · 0.85

Tested by

no test coverage detected