Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void TestDataFrameVectorUdf()
}
}

[SkipIfSparkVersionIsGreaterOrEqualTo(Versions.V3_0_0)]
[Fact]
public void TestGroupedMapUdf()
{
DataFrame df = _spark
Expand Down Expand Up @@ -368,7 +368,7 @@ private static RecordBatch ArrowBasedCountCharacters(RecordBatch records)
returnLength);
}

[SkipIfSparkVersionIsGreaterOrEqualTo(Versions.V3_0_0)]
[Fact]
public void TestDataFrameGroupedMapUdf()
{
DataFrame df = _spark
Expand Down
10 changes: 5 additions & 5 deletions src/csharp/Microsoft.Spark.UnitTest/WorkerFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void TestArrowWorkerFunctionForBool()
new ArrowUdfWrapper<StringArray, BooleanArray, BooleanArray>(
(strings, flags) => (BooleanArray)ToArrowArray(
Enumerable.Range(0, strings.Length)
.Select(i => flags.GetBoolean(i) || strings.GetString(i).Contains("true"))
.Select(i => flags.GetValue(i).Value || strings.GetString(i).Contains("true"))
.ToArray())).Execute);

IArrowArray[] input = new[]
Expand All @@ -120,10 +120,10 @@ public void TestArrowWorkerFunctionForBool()
};
var results = (BooleanArray)func.Func(input, new[] { 0, 1 });
Assert.Equal(4, results.Length);
Assert.True(results.GetBoolean(0));
Assert.True(results.GetBoolean(1));
Assert.True(results.GetBoolean(2));
Assert.False(results.GetBoolean(3));
Assert.True(results.GetValue(0).Value);
Assert.True(results.GetValue(1).Value);
Assert.True(results.GetValue(2).Value);
Assert.False(results.GetValue(3).Value);
}

/// <summary>
Expand Down
40 changes: 33 additions & 7 deletions src/csharp/Microsoft.Spark.Worker.UnitTest/CommandExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public void TestDataFrameSqlCommandExecutorWithEmptyInput(
IpcOptions ipcOptions)
{
var udfWrapper = new Sql.DataFrameUdfWrapper<ArrowStringDataFrameColumn, ArrowStringDataFrameColumn>(
(strings) => strings.Apply(cur=> $"udf: {cur}"));
(strings) => strings.Apply(cur => $"udf: {cur}"));

var command = new SqlCommand()
{
Expand Down Expand Up @@ -874,15 +874,28 @@ await arrowWriter.WriteRecordBatchAsync(
RecordBatch outputBatch = await arrowReader.ReadNextRecordBatchAsync();

Assert.Equal(numRows, outputBatch.Length);
Assert.Equal(2, outputBatch.ColumnCount);
StringArray stringArray;
Int64Array longArray;
if (sparkVersion < new Version(Versions.V3_0_0))
{
Assert.Equal(2, outputBatch.ColumnCount);
stringArray = (StringArray)outputBatch.Column(0);
longArray = (Int64Array)outputBatch.Column(1);
}
else
{
Assert.Equal(1, outputBatch.ColumnCount);
var structArray = (StructArray)outputBatch.Column(0);
Assert.Equal(2, structArray.Fields.Count);
stringArray = (StringArray)structArray.Fields[0];
longArray = (Int64Array)structArray.Fields[1];
}

var stringArray = (StringArray)outputBatch.Column(0);
for (int i = 0; i < numRows; ++i)
{
Assert.Equal($"udf: {i}", stringArray.GetString(i));
}

var longArray = (Int64Array)outputBatch.Column(1);
for (int i = 0; i < numRows; ++i)
{
Assert.Equal(100 + i, longArray.Values[i]);
Expand Down Expand Up @@ -981,15 +994,28 @@ await arrowWriter.WriteRecordBatchAsync(
RecordBatch outputBatch = await arrowReader.ReadNextRecordBatchAsync();

Assert.Equal(numRows, outputBatch.Length);
Assert.Equal(2, outputBatch.ColumnCount);
StringArray stringArray;
DoubleArray doubleArray;
if (sparkVersion < new Version(Versions.V3_0_0))
{
Assert.Equal(2, outputBatch.ColumnCount);
stringArray = (StringArray)outputBatch.Column(0);
doubleArray = (DoubleArray)outputBatch.Column(1);
}
else
{
Assert.Equal(1, outputBatch.ColumnCount);
var structArray = (StructArray)outputBatch.Column(0);
Assert.Equal(2, structArray.Fields.Count);
stringArray = (StringArray)structArray.Fields[0];
doubleArray = (DoubleArray)structArray.Fields[1];
}

var stringArray = (StringArray)outputBatch.Column(0);
for (int i = 0; i < numRows; ++i)
{
Assert.Equal($"udf: {i}", stringArray.GetString(i));
}

var doubleArray = (DoubleArray)outputBatch.Column(1);
for (int i = 0; i < numRows; ++i)
{
Assert.Equal(100 + i, doubleArray.Values[i]);
Expand Down
44 changes: 32 additions & 12 deletions src/csharp/Microsoft.Spark.Worker/Command/SqlCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,7 @@ private CommandExecutorStat ExecuteArrowSqlCommand(

var recordBatch = new RecordBatch(resultSchema, results, numEntries);

// TODO: Remove sync-over-async once WriteRecordBatch exists.
writer.WriteRecordBatchAsync(recordBatch).GetAwaiter().GetResult();
writer.WriteRecordBatch(recordBatch);
}

WriteEnd(outputStream, ipcOptions);
Expand Down Expand Up @@ -468,8 +467,7 @@ private CommandExecutorStat ExecuteDataFrameSqlCommand(
new ArrowStreamWriter(outputStream, result.Schema, leaveOpen: true, ipcOptions);
}

// TODO: Remove sync-over-async once WriteRecordBatch exists.
writer.WriteRecordBatchAsync(result).GetAwaiter().GetResult();
writer.WriteRecordBatch(result);
}
}

Expand Down Expand Up @@ -737,6 +735,27 @@ protected internal override CommandExecutorStat ExecuteCore(
return ExecuteArrowGroupedMapCommand(inputStream, outputStream, commands);
}

private RecordBatch WrapArrowRecordBatchColumnsInAStruct(RecordBatch batch)
Comment thread
pgovind marked this conversation as resolved.
Outdated
{
if (_version >= new Version(Versions.V3_0_0))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this instead ?

Suggested change
if (_version >= new Version(Versions.V3_0_0))
if (_version.Major >= 3)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, that's actually what I had first, until I saw this:

if (SparkSettings.Version < new Version(version))
.

Are you worried about the new ? I don't mind changing it!!

@suhsteve suhsteve Oct 22, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a very minor micro optimization. We can keep, I don't mind either. 👍

{
var fields = new Field[batch.Schema.Fields.Count];
for (int i = 0; i < batch.Schema.Fields.Count; i++)
Comment thread
imback82 marked this conversation as resolved.
Outdated
{
fields[i] = batch.Schema.GetFieldByIndex(i);
}
var structType = new StructType(fields);
Comment thread
pgovind marked this conversation as resolved.
var structArray = new StructArray(
structType,
batch.Length,
batch.Arrays.Cast<Apache.Arrow.Array>(),
ArrowBuffer.Empty);
Schema schema = new Schema.Builder().Field(new Field("Struct", structType, false)).Build();
return new RecordBatch(schema, new[] { structArray }, batch.Length);
}
return batch;
Comment thread
pgovind marked this conversation as resolved.
}
Comment thread
pgovind marked this conversation as resolved.

private CommandExecutorStat ExecuteArrowGroupedMapCommand(
Stream inputStream,
Stream outputStream,
Expand All @@ -754,8 +773,9 @@ private CommandExecutorStat ExecuteArrowGroupedMapCommand(
ArrowStreamWriter writer = null;
foreach (RecordBatch input in GetInputIterator(inputStream))
{
RecordBatch result = worker.Func(input);
RecordBatch batch = worker.Func(input);

RecordBatch result = WrapArrowRecordBatchColumnsInAStruct(batch);
int numEntries = result.Length;
stat.NumEntriesProcessed += numEntries;

Expand All @@ -765,8 +785,7 @@ private CommandExecutorStat ExecuteArrowGroupedMapCommand(
new ArrowStreamWriter(outputStream, result.Schema, leaveOpen: true, ipcOptions);
}

// TODO: Remove sync-over-async once WriteRecordBatch exists.
writer.WriteRecordBatchAsync(result).GetAwaiter().GetResult();
writer.WriteRecordBatch(result);
}

WriteEnd(outputStream, ipcOptions);
Expand Down Expand Up @@ -794,20 +813,21 @@ private CommandExecutorStat ExecuteDataFrameGroupedMapCommand(
{
FxDataFrame dataFrame = FxDataFrame.FromArrowRecordBatch(input);
FxDataFrame resultDataFrame = worker.Func(dataFrame);

IEnumerable<RecordBatch> recordBatches = resultDataFrame.ToArrowRecordBatches();

foreach (RecordBatch result in recordBatches)
foreach (RecordBatch batch in recordBatches)
{
stat.NumEntriesProcessed += result.Length;
RecordBatch final = WrapArrowRecordBatchColumnsInAStruct(batch);
Comment thread
pgovind marked this conversation as resolved.
Outdated
stat.NumEntriesProcessed += final.Length;

if (writer == null)
{
writer =
new ArrowStreamWriter(outputStream, result.Schema, leaveOpen: true, ipcOptions);
new ArrowStreamWriter(outputStream, final.Schema, leaveOpen: true, ipcOptions);
}

// TODO: Remove sync-over-async once WriteRecordBatch exists.
writer.WriteRecordBatchAsync(result).GetAwaiter().GetResult();
writer.WriteRecordBatch(final);
Comment thread
pgovind marked this conversation as resolved.
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/csharp/Microsoft.Spark/Microsoft.Spark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Apache.Arrow" Version="0.15.1" />
<PackageReference Include="Apache.Arrow" Version="2.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
Expand All @@ -37,10 +37,7 @@
</ItemGroup>

<ItemGroup>
<Content Include="..\..\scala\microsoft-spark-*\target\microsoft-spark-*.jar"
Link="jars\%(Filename)%(Extension)"
Pack="true"
PackagePath="jars\%(Filename)%(Extension)" />
<Content Include="..\..\scala\microsoft-spark-*\target\microsoft-spark-*.jar" Link="jars\%(Filename)%(Extension)" Pack="true" PackagePath="jars\%(Filename)%(Extension)" />
Comment thread
pgovind marked this conversation as resolved.
<Content Include="build\**" Pack="true" PackagePath="build" />
</ItemGroup>

Expand Down