|
1 | 1 | using System.Globalization; |
| 2 | +using System.Text.Json; |
2 | 3 | using System.Threading; |
3 | 4 |
|
4 | 5 | namespace DuckDB.NET.Test; |
@@ -422,4 +423,52 @@ public void TableFunctionMapError_PreservesInnerException() |
422 | 423 | ex.InnerException!.Message.Should().Be("custom map error"); |
423 | 424 | ex.InnerException.Should().BeSameAs(originalException); |
424 | 425 | } |
| 426 | + |
| 427 | + [Theory] |
| 428 | + [InlineData(100, false, "card_estimated")] |
| 429 | + [InlineData(50, true, "card_exact")] |
| 430 | + public void RegisterTableFunctionWithCardinality(int count, bool isExact, string funcName) |
| 431 | + { |
| 432 | + var expectedData = Enumerable.Range(0, count).ToList(); |
| 433 | + |
| 434 | + Connection.RegisterTableFunction(funcName, parameters => |
| 435 | + { |
| 436 | + return new TableFunction(new List<ColumnInfo> |
| 437 | + { |
| 438 | + new("value", typeof(int)), |
| 439 | + }, expectedData, Cardinality: new CardinalityHint((ulong)count, IsExact: isExact)); |
| 440 | + }, (item, writers, rowIndex) => |
| 441 | + { |
| 442 | + writers[0].WriteValue((int)item, rowIndex); |
| 443 | + }); |
| 444 | + |
| 445 | + var data = Connection.Query<int>($"SELECT * FROM {funcName}();").ToList(); |
| 446 | + data.Should().BeEquivalentTo(expectedData); |
| 447 | + } |
| 448 | + |
| 449 | + [Theory] |
| 450 | + [InlineData(42, false, "plan_estimated")] |
| 451 | + [InlineData(50, true, "plan_exact")] |
| 452 | + public void RegisterTableFunctionWithCardinality_AppearsInQueryPlan(int cardinality, bool isExact, string funcName) |
| 453 | + { |
| 454 | + Connection.RegisterTableFunction(funcName, _ => |
| 455 | + { |
| 456 | + return new TableFunction(new List<ColumnInfo> |
| 457 | + { |
| 458 | + new("value", typeof(int)), |
| 459 | + }, Enumerable.Range(0, 50), Cardinality: new CardinalityHint((ulong)cardinality, IsExact: isExact)); |
| 460 | + }, (item, writers, rowIndex) => |
| 461 | + { |
| 462 | + writers[0].WriteValue((int)item, rowIndex); |
| 463 | + }); |
| 464 | + |
| 465 | + var plan = Connection.Query<(string, string)>($"EXPLAIN (FORMAT JSON) SELECT * FROM {funcName}();").First(); |
| 466 | + var json = JsonDocument.Parse(plan.Item2); |
| 467 | + var reported = json.RootElement[0] |
| 468 | + .GetProperty("extra_info") |
| 469 | + .GetProperty("Estimated Cardinality") |
| 470 | + .GetString(); |
| 471 | + |
| 472 | + reported.Should().Be(cardinality.ToString()); |
| 473 | + } |
425 | 474 | } |
0 commit comments