From f14255f0404b6b077900d7ebac20af2deef552b5 Mon Sep 17 00:00:00 2001 From: ganyang Date: Fri, 20 May 2022 15:38:19 +0800 Subject: [PATCH] fix record type issue caused by composite datum --- src/gausskernel/runtime/executor/execQual.cpp | 27 ++++++++------ .../expected/composite_datum_record.out | 37 +++++++++++++++++++ src/test/regress/parallel_schedule0 | 1 + .../regress/sql/composite_datum_record.sql | 11 ++++++ 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 src/test/regress/expected/composite_datum_record.out create mode 100644 src/test/regress/sql/composite_datum_record.sql diff --git a/src/gausskernel/runtime/executor/execQual.cpp b/src/gausskernel/runtime/executor/execQual.cpp index f6783ca9f..ba37b055f 100644 --- a/src/gausskernel/runtime/executor/execQual.cpp +++ b/src/gausskernel/runtime/executor/execQual.cpp @@ -720,7 +720,6 @@ static Datum ExecEvalWholeRowVar( { Var* variable = (Var*)wrvstate->xprstate.expr; TupleTableSlot* slot = NULL; - TupleDesc slot_tupdesc; bool needslow = false; if (isDone != NULL) @@ -802,21 +801,13 @@ static Datum ExecEvalWholeRowVar( if (wrvstate->wrv_junkFilter != NULL) slot = ExecFilterJunk(wrvstate->wrv_junkFilter, slot); - slot_tupdesc = slot->tts_tupleDescriptor; - /* - * If it's a RECORD Var, we'll use the slot's type ID info. It's likely - * that the slot's type is also RECORD; if so, make sure it's been - * "blessed", so that the Datum can be interpreted later. - * * If the Var identifies a named composite type, we must check that the * actual tuple type is compatible with it. */ - if (variable->vartype == RECORDOID) { - if (slot_tupdesc->tdtypeid == RECORDOID && slot_tupdesc->tdtypmod < 0) - assign_record_type_typmod(slot_tupdesc); - } else { + if (variable->vartype != RECORDOID) { TupleDesc var_tupdesc; + TupleDesc slot_tupdesc; int i; /* @@ -833,6 +824,8 @@ static Datum ExecEvalWholeRowVar( */ var_tupdesc = lookup_rowtype_tupdesc(variable->vartype, -1); + slot_tupdesc = slot->tts_tupleDescriptor; + if (var_tupdesc->natts != slot_tupdesc->natts) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), @@ -886,6 +879,7 @@ static Datum ExecEvalWholeRowFast( { Var* variable = (Var*)wrvstate->xprstate.expr; TupleTableSlot* slot = NULL; + TupleDesc slot_tupdesc; HeapTuple tuple; TupleDesc tupleDesc; HeapTupleHeader dtuple; @@ -915,6 +909,17 @@ static Datum ExecEvalWholeRowFast( if (wrvstate->wrv_junkFilter != NULL) slot = ExecFilterJunk(wrvstate->wrv_junkFilter, slot); + /* + * If it's a RECORD Var, we'll use the slot's type ID info. It's likely + * that the slot's type is also RECORD; if so, make sure it's been + * "blessed", so that the Datum can be interpreted later. + */ + slot_tupdesc = slot->tts_tupleDescriptor; + if (variable->vartype == RECORDOID) { + if (slot_tupdesc->tdtypeid == RECORDOID && slot_tupdesc->tdtypmod < 0) + assign_record_type_typmod(slot_tupdesc); + } + tuple = ExecFetchSlotTuple(slot); tupleDesc = slot->tts_tupleDescriptor; diff --git a/src/test/regress/expected/composite_datum_record.out b/src/test/regress/expected/composite_datum_record.out new file mode 100644 index 000000000..a0d110a74 --- /dev/null +++ b/src/test/regress/expected/composite_datum_record.out @@ -0,0 +1,37 @@ +-- +-- check for over-optimization of whole-row Var referencing an Append plan +-- +create table int4_test(f1 int4); +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6 where f1<=0) q) from int4_test; + q +--- +(0 rows) + +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6.0 where f1<=0) q) from int4_test; + q +--- +(0 rows) + +insert into int4_test(f1) values(' 0 '); +insert into int4_test(f1) values('123456 '); +select * from int4_test; + f1 +-------- + 0 + 123456 +(2 rows) + +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6 where f1<=0) q) from int4_test; + q +--------- + (4,5,6) + (1,2,3) +(2 rows) + +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6.0 where f1<=0) q) from int4_test; + q +----------- + (4,5,6.0) + (1,2,3) +(2 rows) + diff --git a/src/test/regress/parallel_schedule0 b/src/test/regress/parallel_schedule0 index 52dc78697..0c866cf07 100644 --- a/src/test/regress/parallel_schedule0 +++ b/src/test/regress/parallel_schedule0 @@ -916,3 +916,4 @@ test: fdw_audit test: gs_global_config_audit test: detail test: gs_dump_encrypt +test: composite_datum_record diff --git a/src/test/regress/sql/composite_datum_record.sql b/src/test/regress/sql/composite_datum_record.sql new file mode 100644 index 000000000..838575cdd --- /dev/null +++ b/src/test/regress/sql/composite_datum_record.sql @@ -0,0 +1,11 @@ +-- +-- check for over-optimization of whole-row Var referencing an Append plan +-- +create table int4_test(f1 int4); +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6 where f1<=0) q) from int4_test; +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6.0 where f1<=0) q) from int4_test; +insert into int4_test(f1) values(' 0 '); +insert into int4_test(f1) values('123456 '); +select * from int4_test; +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6 where f1<=0) q) from int4_test; +select (select q from (select 1,2,3 where f1>0 union all select 4,5,6.0 where f1<=0) q) from int4_test; \ No newline at end of file