@@ -56,8 +56,8 @@ static void InsertFieldMapping(Oid relationId, int attrIcebergFieldId,
5656 AttrNumber pg_attnum , PGType pgType ,
5757 const char * writeDefault , const char * initialDefault ,
5858 int parentFieldId );
59- static AttrNumber GetAttributeForFieldIdForInternalIcebergTable (Oid relationId , int fieldId );
60- static AttrNumber GetAttributeForFieldIdForExternalIcebergTable (char * metadataPath , Oid relationId , int fieldId );
59+ static AttrNumber GetAttributeForFieldIdForInternalIcebergTable (Oid relationId , int fieldId , bool * inCurrentSchema );
60+ static AttrNumber GetAttributeForFieldIdForExternalIcebergTable (char * metadataPath , Oid relationId , int fieldId , bool * inCurrentSchema );
6161
6262#ifdef USE_ASSERT_CHECKING
6363static List * GetAllRegisteredAttnumsForTopLevelColumns (Oid relationId );
@@ -244,30 +244,35 @@ GetRegisteredFieldForAttribute(Oid relationId, AttrNumber attrNo)
244244}
245245
246246/*
247- * GetAttributeForFieldId gets the attribute number for a given field ID.
248- */
247+ * GetAttributeForFieldId gets the attribute number for a given field ID.
248+ * It sets inCurrentSchema if there is no attribute in current postgres schema
249+ * which corresponds to the field.
250+ */
249251AttrNumber
250- GetAttributeForFieldId (Oid relationId , int fieldId )
252+ GetAttributeForFieldId (Oid relationId , int fieldId , bool * inCurrentSchema )
251253{
252254 Assert (IsAnyIcebergTable (relationId ));
253255
254256 if (IsAnyInternalIcebergTable (relationId ))
255- return GetAttributeForFieldIdForInternalIcebergTable (relationId , fieldId );
257+ {
258+ return GetAttributeForFieldIdForInternalIcebergTable (relationId , fieldId , inCurrentSchema );
259+ }
256260 else
257261 {
258262 char * currentMetadataPath = GetIcebergMetadataLocation (relationId , false);
259263
260- return GetAttributeForFieldIdForExternalIcebergTable (currentMetadataPath , relationId , fieldId );
264+ return GetAttributeForFieldIdForExternalIcebergTable (currentMetadataPath , relationId , fieldId , inCurrentSchema );
261265 }
262266}
263267
264268
265269/*
266270* GetAttributeForFieldIdForInternalIcebergTable gets the attribute number for a given field ID
267- * for internal Iceberg tables from catalog.
271+ * for internal Iceberg tables from catalog. It also sets if the corresponding attribute is in the
272+ * current postgres schema.
268273*/
269274static AttrNumber
270- GetAttributeForFieldIdForInternalIcebergTable (Oid relationId , int fieldId )
275+ GetAttributeForFieldIdForInternalIcebergTable (Oid relationId , int fieldId , bool * inCurrentSchema )
271276{
272277 DECLARE_SPI_ARGS (2 );
273278
@@ -286,17 +291,26 @@ GetAttributeForFieldIdForInternalIcebergTable(Oid relationId, int fieldId)
286291 */
287292 bool readOnly = false;
288293
289- SPI_EXECUTE ("SELECT pg_attnum FROM " MAPPING_TABLE_NAME
294+ SPI_EXECUTE ("SELECT m.pg_attnum, not attisdropped as in_current_schema "
295+ "FROM " MAPPING_TABLE_NAME " m JOIN pg_attribute p ON "
296+ " m.table_name OPERATOR(pg_catalog.=) p.attrelid "
297+ " AND m.pg_attnum OPERATOR(pg_catalog.=) p.attnum"
290298 " WHERE table_name OPERATOR(pg_catalog.=) $1"
291299 " AND field_id OPERATOR(pg_catalog.=) $2" , readOnly );
292300
293301 /* there is a primary key on these filters */
294302 Assert (SPI_processed == 1 );
295303
296- bool isNull = false;
297- AttrNumber attrNo = GET_SPI_VALUE (INT2OID , 0 , 1 , & isNull );
304+ bool isAttrNumNull = false;
305+ AttrNumber attrNo = GET_SPI_VALUE (INT2OID , 0 , 1 , & isAttrNumNull );
298306
299- Assert (!isNull );
307+ Assert (!isAttrNumNull );
308+
309+ bool inCurrentSchemaNull = false;
310+
311+ * inCurrentSchema = GET_SPI_VALUE (BOOLOID , 0 , 2 , & inCurrentSchemaNull );
312+
313+ Assert (!inCurrentSchemaNull );
300314
301315 SPI_END ();
302316
@@ -309,7 +323,7 @@ GetAttributeForFieldIdForInternalIcebergTable(Oid relationId, int fieldId)
309323* for external Iceberg tables from iceberg metadata.
310324 */
311325static AttrNumber
312- GetAttributeForFieldIdForExternalIcebergTable (char * metadataPath , Oid relationId , int fieldId )
326+ GetAttributeForFieldIdForExternalIcebergTable (char * metadataPath , Oid relationId , int fieldId , bool * inCurrentSchema )
313327{
314328 DataFileSchema * schema = GetDataFileSchemaForExternalIcebergTable (metadataPath );
315329
@@ -336,16 +350,20 @@ GetAttributeForFieldIdForExternalIcebergTable(char *metadataPath, Oid relationId
336350
337351 SPI_EXECUTE ("SELECT attnum FROM pg_attribute "
338352 "WHERE attrelid OPERATOR(pg_catalog.=) $1 "
339- "AND attname OPERATOR(pg_catalog.=) $2 "
340- "AND NOT attisdropped" , readOnly );
353+ "AND attname OPERATOR(pg_catalog.=) $2" , readOnly );
341354
342- /* there is a primary key on these filters */
343- Assert (SPI_processed == 1 );
344-
345- bool isNull = false;
346- AttrNumber attrNo = GET_SPI_VALUE (INT2OID , 0 , 1 , & isNull );
355+ AttrNumber attrNo = 0 ;
347356
348- Assert (!isNull );
357+ if (SPI_processed == 0 )
358+ {
359+ * inCurrentSchema = false;
360+ attrNo = -1 ;
361+ }
362+ else
363+ {
364+ Assert (SPI_processed == 1 );
365+ attrNo = GET_SPI_VALUE (INT2OID , 0 , 1 , inCurrentSchema );
366+ }
349367
350368 SPI_END ();
351369
0 commit comments