Skip to content

Commit

Permalink
ShowRelation to print out the relation expression
Browse files Browse the repository at this point in the history
  • Loading branch information
GoEddie committed Dec 2, 2024
1 parent c8c8b85 commit b49ab70
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Spark.Connect.Dotnet/Spark.Connect.Dotnet/Sql/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using StructType = Spark.Connect.Dotnet.Sql.Types.StructType;
using static Spark.Connect.Dotnet.Sql.Functions;
using TransformFunction = System.Linq.Expressions.Expression<System.Func<Spark.Connect.Dotnet.Sql.DataFrame, Spark.Connect.Dotnet.Sql.DataFrame>>;
using Newtonsoft.Json;

namespace Spark.Connect.Dotnet.Sql;

Expand Down Expand Up @@ -1718,6 +1719,26 @@ public DataFrame Transform(TransformFunction function)

return response;
}

public void ShowRelation()
{
try
{
using (var stringReader = new StringReader(this.Relation.ToString()))
using (var stringWriter = new StringWriter())
{
var jsonReader = new JsonTextReader(stringReader);
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
jsonWriter.WriteToken(jsonReader);
SparkSession.Console.WriteLine(stringWriter.ToString());
}
}
catch (Exception)
{
var json = this.Relation.ToString();
SparkSession.Console.WriteLine(this.Relation.ToString());
}
}
}

public enum JoinType
Expand Down
7 changes: 5 additions & 2 deletions src/example/basic_example/SimpleExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ public async Task Run()

var parquetPath = Path.Join(tempOutputFolder, "parquetFile");

dataFrame2
.WithColumn("id multiplied by 1000", Functions.Col("id") * 1000)
var df3 = dataFrame2.WithColumn("id multiplied by 1000", Functions.Col("id") * 1000);

df3
.Write()
.Format("parquet")
.Write(parquetPath);

Console.WriteLine($"Wrote Parquet to '{parquetPath}'");

df3.ShowRelation();
}
}

0 comments on commit b49ab70

Please sign in to comment.