Skip to content

Commit 997d73a

Browse files
Copilotrenemadsen
andcommitted
Revert to skipping CAST for MariaDB json type - CAST AS CHAR breaks NULL comparison semantics
Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent b7d6816 commit 997d73a

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/EFCore.MySql/Query/ExpressionVisitors/Internal/MySqlQuerySqlGenerator.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ protected override Expression VisitJsonScalar(JsonScalarExpression jsonScalarExp
513513
castStoreType = GetCastStoreType(jsonScalarExpression.TypeMapping);
514514
// }
515515

516-
// MariaDB does not support CAST(... AS json), so use CHAR when targeting json type with JsonDataTypeEmulation enabled.
516+
// MariaDB does not support CAST(... AS json), so skip the CAST entirely when JsonDataTypeEmulation is enabled.
517+
// MariaDB stores JSON as LONGTEXT, so no explicit cast is needed - the data is already in a compatible text format.
517518
// This prevents SQL syntax errors like "near 'json) IS NULL'" on MariaDB while maintaining correct NULL comparison semantics.
518-
// MariaDB stores JSON as LONGTEXT, so casting to CHAR preserves the same behavior as the native JSON type.
519519
if (castStoreType == "json" && _options.ServerVersion.Supports.JsonDataTypeEmulation)
520520
{
521-
castStoreType = "char";
521+
castStoreType = null;
522522
}
523523

524524
if (castStoreType is not null)
@@ -726,12 +726,14 @@ private SqlUnaryExpression VisitConvert(SqlUnaryExpression sqlUnaryExpression)
726726
operandUnary.OperatorType == ExpressionType.Convert &&
727727
castMapping.Equals(GetCastStoreType(operandUnary.TypeMapping), StringComparison.OrdinalIgnoreCase);
728728

729-
// MariaDB does not support CAST(... AS json) syntax. Instead, use CAST(... AS CHAR) for MariaDB when JsonDataTypeEmulation is enabled.
730-
// This maintains correct NULL comparison semantics while avoiding SQL syntax errors on MariaDB.
731-
// MariaDB stores JSON as LONGTEXT, so casting to CHAR preserves the expected behavior.
729+
// MariaDB does not support CAST(... AS json) syntax, so skip the conversion entirely when JsonDataTypeEmulation is enabled.
730+
// MariaDB stores JSON as LONGTEXT, so no explicit cast is needed - comparisons work correctly without it.
731+
// This avoids SQL syntax errors on MariaDB while maintaining correct NULL and equality comparison semantics.
732732
if (castMapping == "json" && _options.ServerVersion.Supports.JsonDataTypeEmulation)
733733
{
734-
castMapping = "char";
734+
// For MariaDB with JsonDataTypeEmulation, skip the CAST by returning early
735+
Visit(sqlUnaryExpression.Operand);
736+
return sqlUnaryExpression;
735737
}
736738

737739
if ((castMapping == "json" && !_options.ServerVersion.Supports.JsonDataTypeEmulation ||

0 commit comments

Comments
 (0)