|
2 | 2 | // Licensed under the MIT. See LICENSE in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
5 | 7 | using System.Diagnostics.CodeAnalysis; |
6 | 8 | using System.Linq; |
7 | 9 | using System.Linq.Expressions; |
@@ -210,10 +212,169 @@ protected override ShapedQueryExpression TranslateElementAtOrDefault( |
210 | 212 | return base.TranslateElementAtOrDefault(source, index, returnDefault); |
211 | 213 | } |
212 | 214 |
|
213 | | - // TODO: Implement for EF Core 7 JSON support. |
214 | 215 | protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpression jsonQueryExpression) |
215 | 216 | { |
216 | | - return base.TransformJsonQueryToTable(jsonQueryExpression); |
| 217 | + // TODO: Implement JSON_TABLE support for structural types (entities/complex types) in JSON collections. |
| 218 | + // |
| 219 | + // Current Status: |
| 220 | + // - TransformJsonQueryToTable implementation is complete and matches Npgsql pattern |
| 221 | + // - JSON_TABLE syntax and COLUMNS clause generation is correct |
| 222 | + // - Issue is in EF Core base SelectExpression.AddCrossJoin or MySQL SQL generator |
| 223 | + // - When TranslateSelectMany calls AddCrossJoin, the CROSS JOIN keyword is not generated |
| 224 | + // - This results in invalid SQL: "FROM table1 JSON_TABLE(...)" instead of "FROM table1 CROSS JOIN JSON_TABLE(...)" |
| 225 | + // |
| 226 | + // Investigation completed: |
| 227 | + // - Npgsql uses identical CreateSelect pattern and it works for PostgreSQL |
| 228 | + // - MySQL supports both comma and CROSS JOIN syntax with JSON_TABLE (manually verified) |
| 229 | + // - The bug is in query assembly, not in provider-specific logic |
| 230 | + // - Requires either: override TranslateSelectMany, patch EF Core AddCrossJoin, or fix MySQL SQL generator |
| 231 | + // |
| 232 | + // Partial implementation preserved below for reference (currently commented out). |
| 233 | + // See commits: 11dc6b2, e17a1e9, 4b80703 for full implementation details. |
| 234 | + |
| 235 | + // For now, throw a clear exception to inform users this is not yet supported |
| 236 | + throw new InvalidOperationException( |
| 237 | + "Composing LINQ operators (such as SelectMany) over collections of structural types inside JSON documents " + |
| 238 | + "is not currently supported by the MySQL provider. This feature requires fixes in EF Core's query assembly " + |
| 239 | + "logic or MySQL-specific SQL generation. As a workaround, consider materializing the JSON data to the client " + |
| 240 | + "using .AsEnumerable() or .ToList() before performing collection operations."); |
| 241 | + |
| 242 | + /* PARTIAL IMPLEMENTATION - PRESERVED FOR FUTURE WORK |
| 243 | + |
| 244 | + // Calculate the table alias for the JSON_TABLE function based on the last named path segment |
| 245 | + // (or the JSON column name if there are none) |
| 246 | + var lastNamedPathSegment = jsonQueryExpression.Path.LastOrDefault(ps => ps.PropertyName is not null); |
| 247 | + var tableAlias = _sqlAliasManager.GenerateTableAlias( |
| 248 | + lastNamedPathSegment.PropertyName ?? jsonQueryExpression.JsonColumn.Name); |
| 249 | +
|
| 250 | + var jsonTypeMapping = jsonQueryExpression.JsonColumn.TypeMapping!; |
| 251 | +
|
| 252 | + // We now add all of the projected entity's properties and navigations into the JSON_TABLE's COLUMNS clause |
| 253 | + var columnInfos = new List<MySqlJsonTableExpression.ColumnInfo>(); |
| 254 | +
|
| 255 | + // We're only interested in properties which actually exist in the JSON, filter out uninteresting shadow keys |
| 256 | + foreach (var property in jsonQueryExpression.StructuralType.GetPropertiesInHierarchy()) |
| 257 | + { |
| 258 | + if (property.GetJsonPropertyName() is string jsonPropertyName) |
| 259 | + { |
| 260 | + columnInfos.Add( |
| 261 | + new MySqlJsonTableExpression.ColumnInfo( |
| 262 | + Name: jsonPropertyName, |
| 263 | + TypeMapping: property.GetRelationalTypeMapping(), |
| 264 | + // Path for JSON_TABLE: $[0] to access array element properties |
| 265 | + Path: [new PathSegment(_sqlExpressionFactory.Constant(0, _typeMappingSource.FindMapping(typeof(int))))], |
| 266 | + AsJson: false)); |
| 267 | + } |
| 268 | + } |
| 269 | +
|
| 270 | + // Add navigations to owned entities mapped to JSON |
| 271 | + switch (jsonQueryExpression.StructuralType) |
| 272 | + { |
| 273 | + case IEntityType entityType: |
| 274 | + foreach (var navigation in entityType.GetNavigationsInHierarchy() |
| 275 | + .Where(n => n.ForeignKey.IsOwnership |
| 276 | + && n.TargetEntityType.IsMappedToJson() |
| 277 | + && n.ForeignKey.PrincipalToDependent == n)) |
| 278 | + { |
| 279 | + var jsonNavigationName = navigation.TargetEntityType.GetJsonPropertyName(); |
| 280 | + Check.DebugAssert(jsonNavigationName is not null, $"No JSON property name for navigation {navigation.Name}"); |
| 281 | +
|
| 282 | + columnInfos.Add( |
| 283 | + new MySqlJsonTableExpression.ColumnInfo( |
| 284 | + Name: jsonNavigationName, |
| 285 | + TypeMapping: jsonTypeMapping, |
| 286 | + Path: [new PathSegment(_sqlExpressionFactory.Constant(0, _typeMappingSource.FindMapping(typeof(int))))], |
| 287 | + AsJson: true)); |
| 288 | + } |
| 289 | + break; |
| 290 | +
|
| 291 | + case IComplexType complexType: |
| 292 | + foreach (var complexProperty in complexType.GetComplexProperties()) |
| 293 | + { |
| 294 | + var jsonPropertyName = complexProperty.ComplexType.GetJsonPropertyName(); |
| 295 | + Check.DebugAssert(jsonPropertyName is not null, $"No JSON property name for complex property {complexProperty.Name}"); |
| 296 | +
|
| 297 | + columnInfos.Add( |
| 298 | + new MySqlJsonTableExpression.ColumnInfo( |
| 299 | + Name: jsonPropertyName, |
| 300 | + TypeMapping: jsonTypeMapping, |
| 301 | + Path: [new PathSegment(_sqlExpressionFactory.Constant(0, _typeMappingSource.FindMapping(typeof(int))))], |
| 302 | + AsJson: true)); |
| 303 | + } |
| 304 | + break; |
| 305 | +
|
| 306 | + default: |
| 307 | + throw new UnreachableException(); |
| 308 | + } |
| 309 | +
|
| 310 | + // MySQL JSON_TABLE requires the nested JSON document as raw JSON (not extracted as a scalar value). |
| 311 | + // We need to use JSON_EXTRACT (not JSON_VALUE) to get the JSON fragment with proper structure. |
| 312 | + // Unlike Npgsql which can use JsonScalarExpression (translates to json extraction in PostgreSQL), |
| 313 | + // MySQL's JsonScalarExpression translates to JSON_VALUE which strips quotes and can't feed JSON_TABLE. |
| 314 | + |
| 315 | + SqlExpression jsonSource; |
| 316 | + if (jsonQueryExpression.Path.Count > 0) |
| 317 | + { |
| 318 | + // Build the JSON path for extraction |
| 319 | + var pathBuilder = new System.Text.StringBuilder("$"); |
| 320 | + foreach (var segment in jsonQueryExpression.Path) |
| 321 | + { |
| 322 | + if (segment.PropertyName is not null) |
| 323 | + { |
| 324 | + pathBuilder.Append('.').Append(segment.PropertyName); |
| 325 | + } |
| 326 | + else if (segment.ArrayIndex is SqlConstantExpression { Value: int index }) |
| 327 | + { |
| 328 | + pathBuilder.Append('[').Append(index).Append(']'); |
| 329 | + } |
| 330 | + } |
| 331 | +
|
| 332 | + // Use JSON_EXTRACT to get the nested JSON document (not JSON_VALUE which extracts scalars) |
| 333 | + jsonSource = _sqlExpressionFactory.Function( |
| 334 | + "JSON_EXTRACT", |
| 335 | + [jsonQueryExpression.JsonColumn, _sqlExpressionFactory.Constant(pathBuilder.ToString())], |
| 336 | + nullable: true, |
| 337 | + argumentsPropagateNullability: [true, true], |
| 338 | + typeof(string), |
| 339 | + jsonTypeMapping); |
| 340 | + } |
| 341 | + else |
| 342 | + { |
| 343 | + // No path - use the JSON column directly |
| 344 | + jsonSource = jsonQueryExpression.JsonColumn; |
| 345 | + } |
| 346 | +
|
| 347 | + // Construct the JSON_TABLE expression with column definitions |
| 348 | + var jsonTableExpression = new MySqlJsonTableExpression( |
| 349 | + tableAlias, |
| 350 | + jsonSource, |
| 351 | + // Path to iterate over array elements: $[*] |
| 352 | + [new PathSegment(_sqlExpressionFactory.Constant("*", RelationalTypeMapping.NullMapping))], |
| 353 | + [.. columnInfos]); |
| 354 | +
|
| 355 | + // MySQL JSON_TABLE returns a 'key' column for array ordering (similar to PostgreSQL's ordinality) |
| 356 | + var keyColumnTypeMapping = _typeMappingSource.FindMapping(typeof(int))!; |
| 357 | +
|
| 358 | +#pragma warning disable EF1001 // Internal EF Core API usage. |
| 359 | + // Use CreateSelect helper method (from base class) to create the SelectExpression |
| 360 | + var selectExpression = CreateSelect( |
| 361 | + jsonQueryExpression, |
| 362 | + jsonTableExpression, |
| 363 | + "key", |
| 364 | + typeof(int), |
| 365 | + keyColumnTypeMapping); |
| 366 | +#pragma warning restore EF1001 // Internal EF Core API usage. |
| 367 | +
|
| 368 | + return new ShapedQueryExpression( |
| 369 | + selectExpression, |
| 370 | + new RelationalStructuralTypeShaperExpression( |
| 371 | + jsonQueryExpression.StructuralType, |
| 372 | + new ProjectionBindingExpression( |
| 373 | + selectExpression, |
| 374 | + new ProjectionMember(), |
| 375 | + typeof(ValueBuffer)), |
| 376 | + false)); |
| 377 | + */ |
217 | 378 | } |
218 | 379 |
|
219 | 380 | protected override ShapedQueryExpression TranslatePrimitiveCollection(SqlExpression sqlExpression, IProperty property, string tableAlias) |
|
0 commit comments