Skip to content

Commit 58f08e1

Browse files
authored
Bug fix: Fix issue where statements where not downcasted to IDataValue for case when (#816)
1 parent 8254224 commit 58f08e1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/FlowtideDotNet.Core/Compute/Columnar/ColumnarExpressionVisitor.cs

+10
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ private static IDataValue GetColumnValue(in EventBatchData data, in int column,
210210
elseStatement = System.Linq.Expressions.Expression.Constant(new NullValue(), typeof(IDataValue));
211211
}
212212

213+
if (elseStatement.Type != typeof(IDataValue))
214+
{
215+
elseStatement = System.Linq.Expressions.Expression.Convert(elseStatement, typeof(IDataValue));
216+
}
217+
213218
var expr = elseStatement;
214219
for (int i = ifThenExpression.Ifs.Count - 1; i >= 0; i--)
215220
{
@@ -227,6 +232,11 @@ private static IDataValue GetColumnValue(in EventBatchData data, in int column,
227232
ifStatement = System.Linq.Expressions.Expression.Call(toBoolMethod, ifStatement);
228233
}
229234

235+
if (thenStatement.Type != typeof(IDataValue))
236+
{
237+
thenStatement = System.Linq.Expressions.Expression.Convert(thenStatement, typeof(IDataValue));
238+
}
239+
230240
expr = System.Linq.Expressions.Expression.Condition(ifStatement, thenStatement, expr);
231241
}
232242

tests/FlowtideDotNet.AcceptanceTests/SelectTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ END AS name
7878
AssertCurrentDataEqual(Users.Select(x => new { Name = x.Gender == Entities.Gender.Female ? x.FirstName : x.LastName }));
7979
}
8080

81+
/// <summary>
82+
/// Special case test where the case did not downcast correctly to IDataValue
83+
/// </summary>
84+
/// <returns></returns>
85+
[Fact]
86+
public async Task SelectWithCaseStructWithNullElse()
87+
{
88+
GenerateData();
89+
await StartStream(@"
90+
INSERT INTO output
91+
SELECT
92+
CASE WHEN Active = false THEN named_struct('id', FirstName)
93+
ELSE NULL
94+
END AS name
95+
FROM users");
96+
await WaitForUpdate();
97+
AssertCurrentDataEqual(Users.Select(x => new { Name = x.Active == false ? new { id = x.FirstName } : null }));
98+
}
99+
81100
[Fact]
82101
public async Task SelectWithCaseNoElse()
83102
{

0 commit comments

Comments
 (0)