Skip to content

Commit cc82d18

Browse files
committed
fix: add NULL argument handling for PUT and PUT_LINE
Handle NULL arguments in ora_dbms_output_put and ora_dbms_output_put_line by treating NULL as empty string, matching Oracle's behavior. - Check PG_ARGISNULL(0) before calling PG_GETARG_TEXT_PP - Treat NULL as empty string (appends nothing for PUT, empty line for PUT_LINE) - Add regression tests for NULL handling in both functions
1 parent fafcac1 commit cc82d18

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

contrib/ivorysql_ora/src/builtin_functions/dbms_output.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,25 @@ ora_dbms_output_disable(PG_FUNCTION_ARGS)
307307
*
308308
* Output a line to the buffer (with newline).
309309
* If there's pending PUT text, append it first (Oracle behavior).
310+
* Oracle behavior: NULL is treated as empty string.
310311
*/
311312
Datum
312313
ora_dbms_output_put_line(PG_FUNCTION_ARGS)
313314
{
314-
text *line_text;
315315
char *line_str;
316316

317317
/* Silently discard if buffer not enabled (Oracle behavior) */
318318
if (output_buffer == NULL || !output_buffer->enabled)
319319
PG_RETURN_VOID();
320320

321-
line_text = PG_GETARG_TEXT_PP(0);
322-
line_str = text_to_cstring(line_text);
321+
/* Handle NULL argument - treat as empty string (Oracle behavior) */
322+
if (PG_ARGISNULL(0))
323+
line_str = "";
324+
else
325+
{
326+
text *line_text = PG_GETARG_TEXT_PP(0);
327+
line_str = text_to_cstring(line_text);
328+
}
323329

324330
/* If there's pending PUT text, append it first (Oracle behavior) */
325331
if (output_buffer->current_line->len > 0)
@@ -342,19 +348,25 @@ ora_dbms_output_put_line(PG_FUNCTION_ARGS)
342348
*
343349
* Output text without newline (accumulates in current_line).
344350
* Oracle behavior: not retrievable until NEW_LINE or PUT_LINE is called.
351+
* Oracle behavior: NULL is treated as empty string (appends nothing).
345352
*/
346353
Datum
347354
ora_dbms_output_put(PG_FUNCTION_ARGS)
348355
{
349-
text *text_arg;
350356
char *str;
351357

352358
/* Silently discard if buffer not enabled */
353359
if (output_buffer == NULL || !output_buffer->enabled)
354360
PG_RETURN_VOID();
355361

356-
text_arg = PG_GETARG_TEXT_PP(0);
357-
str = text_to_cstring(text_arg);
362+
/* Handle NULL argument - treat as empty string (Oracle behavior) */
363+
if (PG_ARGISNULL(0))
364+
str = "";
365+
else
366+
{
367+
text *text_arg = PG_GETARG_TEXT_PP(0);
368+
str = text_to_cstring(text_arg);
369+
}
358370

359371
/* Accumulate in current_line without creating a line yet */
360372
appendStringInfoString(output_buffer->current_line, str);

src/pl/plisql/src/expected/plisql_dbms_output.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ CALL dbms_output.put_line(NULL);
1616
CALL dbms_output.put('First part');
1717
CALL dbms_output.put(' second part');
1818
CALL dbms_output.new_line();
19+
-- Test PUT with NULL (should append nothing)
20+
CALL dbms_output.put('Before NULL');
21+
CALL dbms_output.put(NULL);
22+
CALL dbms_output.put(' - After NULL');
23+
CALL dbms_output.new_line();
1924
-- Test multiple PUT calls
2025
CALL dbms_output.put('Line');
2126
CALL dbms_output.put(' with');

src/pl/plisql/src/sql/plisql_dbms_output.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ CALL dbms_output.put('First part');
2222
CALL dbms_output.put(' second part');
2323
CALL dbms_output.new_line();
2424

25+
-- Test PUT with NULL (should append nothing)
26+
CALL dbms_output.put('Before NULL');
27+
CALL dbms_output.put(NULL);
28+
CALL dbms_output.put(' - After NULL');
29+
CALL dbms_output.new_line();
30+
2531
-- Test multiple PUT calls
2632
CALL dbms_output.put('Line');
2733
CALL dbms_output.put(' with');

0 commit comments

Comments
 (0)