Skip to content

Commit

Permalink
fix(csharp/src/Drivers/BigQuery): Fix failure when returning empty sc…
Browse files Browse the repository at this point in the history
…hema data from BigQuery (#1330)

Closes #1329
  • Loading branch information
CurtHagenlocher authored Nov 29, 2023
1 parent 34f2d83 commit 2e7f013
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
26 changes: 11 additions & 15 deletions csharp/src/Drivers/BigQuery/BigQueryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ private ListArray CreateNestedListArray(List<IArrowArray> arrayList, IArrowType

if (data == null)
{
EmptyArrayCreationVisitor visitor = new EmptyArrayCreationVisitor(0);
EmptyArrayCreationVisitor visitor = new EmptyArrayCreationVisitor();
dataType.Accept(visitor);
data = visitor.Result;
}
Expand Down Expand Up @@ -992,47 +992,43 @@ private class EmptyArrayCreationVisitor :
IArrowTypeVisitor<MapType>
{
public ArrayData Result { get; private set; }
private readonly int _length;

public EmptyArrayCreationVisitor(int length)
{
_length = length;
}
const int Length = 0;

public void Visit(BooleanType type)
{
Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty });
}

public void Visit(FixedWidthType type)
{
Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty });
}

public void Visit(BinaryType type)
{
Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty, ArrowBuffer.Empty });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty, ArrowBuffer.Empty });
}

public void Visit(StringType type)
{
Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty, ArrowBuffer.Empty });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty, ArrowBuffer.Empty });
}

public void Visit(ListType type)
{
type.ValueDataType.Accept(this);
ArrayData child = Result;

Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty }, new[] { child });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty, ArrowBuffer.Empty }, new[] { child });
}

public void Visit(FixedSizeListType type)
{
type.ValueDataType.Accept(this);
ArrayData child = Result;

Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty }, new[] { child });
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty }, new[] { child });
}

public void Visit(StructType type)
Expand All @@ -1044,7 +1040,7 @@ public void Visit(StructType type)
children[i] = Result;
}

Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty }, children);
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty }, children);
}

public void Visit(UnionType type)
Expand All @@ -1070,7 +1066,7 @@ public void Visit(UnionType type)
buffers[1] = ArrowBuffer.Empty;
}

Result = new ArrayData(type, _length, _length, 0, buffers, children);
Result = new ArrayData(type, Length, Length, 0, buffers, children);
}

public void Visit(MapType type)
Expand All @@ -1081,7 +1077,7 @@ public void Visit(MapType type)
type.ValueField.DataType.Accept(this);
children[1] = Result;

Result = new ArrayData(type, _length, _length, 0, new[] { ArrowBuffer.Empty }, children);
Result = new ArrayData(type, Length, Length, 0, new[] { ArrowBuffer.Empty }, children);
}

public void Visit(IArrowType type)
Expand Down
27 changes: 27 additions & 0 deletions csharp/test/Drivers/BigQuery/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Data;
using Apache.Arrow.Adbc.Client;
Expand Down Expand Up @@ -129,19 +130,45 @@ public void VerifySchemaTables()
catalogs = adbcConnection.GetSchema("Catalogs", new[] { catalog });
Assert.Equal(1, catalogs.Rows.Count);

string random = "X" + Guid.NewGuid().ToString("N");

catalogs = adbcConnection.GetSchema("Catalogs", new[] { random });
Assert.Equal(0, catalogs.Rows.Count);

var schemas = adbcConnection.GetSchema("Schemas", new[] { catalog });
Assert.Equal(2, schemas.Columns.Count);
var schema = (string)schemas.Rows[0].ItemArray[1];

schemas = adbcConnection.GetSchema("Schemas", new[] { catalog, schema });
Assert.Equal(1, schemas.Rows.Count);

schemas = adbcConnection.GetSchema("Schemas", new[] { random });
Assert.Equal(0, schemas.Rows.Count);

schemas = adbcConnection.GetSchema("Schemas", new[] { catalog, random });
Assert.Equal(0, schemas.Rows.Count);

schemas = adbcConnection.GetSchema("Schemas", new[] { random, random });
Assert.Equal(0, schemas.Rows.Count);

var tableTypes = adbcConnection.GetSchema("TableTypes");
Assert.Equal(1, tableTypes.Columns.Count);

var tables = adbcConnection.GetSchema("Tables", new[] { catalog, schema });
Assert.Equal(4, tables.Columns.Count);

tables = adbcConnection.GetSchema("Tables", new[] { catalog, random });
Assert.Equal(0, tables.Rows.Count);

tables = adbcConnection.GetSchema("Tables", new[] { random, schema });
Assert.Equal(0, tables.Rows.Count);

tables = adbcConnection.GetSchema("Tables", new[] { random, random });
Assert.Equal(0, tables.Rows.Count);

tables = adbcConnection.GetSchema("Tables", new[] { catalog, schema, random });
Assert.Equal(0, tables.Rows.Count);

var columns = adbcConnection.GetSchema("Columns", new[] { catalog, schema });
Assert.Equal(16, columns.Columns.Count);
}
Expand Down

0 comments on commit 2e7f013

Please sign in to comment.