Skip to content

Commit

Permalink
Arrow/Parquet: silently ignore column of type null in GetArrowSchema/…
Browse files Browse the repository at this point in the history
…GetArrowArray

Fixes OSGeo#9644
  • Loading branch information
rouault committed Apr 15, 2024
1 parent cf937e6 commit 6411798
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion apps/ogr2ogr_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3880,7 +3880,9 @@ bool SetupTargetLayer::CanUseWriteArrowBatch(
!psOptions->bMakeValid)
{
struct ArrowArrayStream streamSrc;
if (poSrcLayer->GetArrowStream(&streamSrc, nullptr))
const char *const apszOptions[] = {"SILENCE_GET_SCHEMA_ERROR=YES",
nullptr};
if (poSrcLayer->GetArrowStream(&streamSrc, apszOptions))
{
struct ArrowSchema schemaSrc;
if (streamSrc.get_schema(&streamSrc, &schemaSrc) == 0)
Expand Down
3 changes: 3 additions & 0 deletions autotest/generate_parquet_test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,11 @@ def generate_test_parquet():
type=pa.binary(),
)

null = pa.array([None] * 5, type=pa.null())

names = [
"boolean",
"null",
"uint8",
"int8",
"uint16",
Expand Down
Binary file modified autotest/ogr/data/arrow/test.feather
Binary file not shown.
Binary file modified autotest/ogr/data/parquet/test.parquet
Binary file not shown.
Binary file modified autotest/ogr/data/parquet/test_single_group.parquet
Binary file not shown.
46 changes: 38 additions & 8 deletions ogr/ogrsf_frmts/arrow_common/ograrrowlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4286,6 +4286,12 @@ inline int OGRArrowLayer::GetArrowSchema(struct ArrowArrayStream *stream,
/* GetArrowSchemaInternal() */
/************************************************************************/

static bool IsSilentlyIgnoredFormatForGetArrowSchemaArray(const char *format)
{
// n: null
return strcmp(format, "n") == 0;
}

inline int
OGRArrowLayer::GetArrowSchemaInternal(struct ArrowSchema *out_schema) const
{
Expand Down Expand Up @@ -4366,11 +4372,32 @@ OGRArrowLayer::GetArrowSchemaInternal(struct ArrowSchema *out_schema) const
out_schema->children[i]->release(out_schema->children[i]);
out_schema->children[i] = nullptr;
}
else if (IsSilentlyIgnoredFormatForGetArrowSchemaArray(
out_schema->children[i]->format))
{
// Silently ignore columns with null data type...
out_schema->children[i]->release(out_schema->children[i]);
}
else
{
// shouldn't happen
CPLError(CE_Failure, CPLE_AppDefined,
"fieldDesc[%d].nIdx < 0 not expected", i);
// can happen with data types we don't support
if (m_aosArrowArrayStreamOptions.FetchBool(
"SILENCE_GET_SCHEMA_ERROR", false))
{
CPLDebug(GetDriverUCName().c_str(),
"GetArrowSchema() error: fieldDesc[%d].nIdx < 0 "
"not expected: name=%s, format=%s",
i, out_schema->children[i]->name,
out_schema->children[i]->format);
}
else
{
CPLError(CE_Failure, CPLE_NotSupported,
"GetArrowSchema() error: fieldDesc[%d].nIdx < 0 "
"not expected: name=%s, format=%s",
i, out_schema->children[i]->name,
out_schema->children[i]->format);
}
for (; i < out_schema->n_children; ++i, ++j)
out_schema->children[j] = out_schema->children[i];
out_schema->n_children = j;
Expand Down Expand Up @@ -4503,14 +4530,17 @@ inline int OGRArrowLayer::GetNextArrowArray(struct ArrowArrayStream *stream,
return EIO;
}

// Remove bounding box columns from exported array
const auto RemoveBBoxColumns =
// Remove bounding box columns from exported array, or columns
// of unsupported data types that we voluntarily strip off.
const auto RemoveBBoxOrUnsupportedColumns =
[out_array, &schema](const std::set<int> &oSetBBoxArrayIndex)
{
int j = 0;
for (int i = 0; i < static_cast<int>(schema.n_children); ++i)
{
if (oSetBBoxArrayIndex.find(i) != oSetBBoxArrayIndex.end())
if (oSetBBoxArrayIndex.find(i) != oSetBBoxArrayIndex.end() ||
IsSilentlyIgnoredFormatForGetArrowSchemaArray(
schema.children[i]->format))
{
out_array->children[i]->release(out_array->children[i]);
out_array->children[i] = nullptr;
Expand All @@ -4537,11 +4567,11 @@ inline int OGRArrowLayer::GetNextArrowArray(struct ArrowArrayStream *stream,
if (iter.second.iArrayIdx >= 0)
oSetBBoxArrayIndex.insert(iter.second.iArrayIdx);
}
RemoveBBoxColumns(oSetBBoxArrayIndex);
RemoveBBoxOrUnsupportedColumns(oSetBBoxArrayIndex);
}
else
{
RemoveBBoxColumns(m_oSetBBoxArrowColumns);
RemoveBBoxOrUnsupportedColumns(m_oSetBBoxArrowColumns);
}

if (EQUAL(m_aosArrowArrayStreamOptions.FetchNameValueDef(
Expand Down

0 comments on commit 6411798

Please sign in to comment.