Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer division (DIV) not supported #1932

Open
meriturva opened this issue Jul 15, 2024 · 1 comment
Open

Integer division (DIV) not supported #1932

meriturva opened this issue Jul 15, 2024 · 1 comment
Assignees

Comments

@meriturva
Copy link

Just an easy request, is it possible to support the DIV Integer division operator?

https://dev.mysql.com/doc/refman/8.4/en/arithmetic-functions.html#operator_div

https://mariadb.com/kb/en/div/

I usually use Math.Truncate and sometimes Math.Floor but we have direct support of integer division on MySql and MariaDb and I guess it will be nice to support it.

Thanks.

@lauxjpn lauxjpn self-assigned this Dec 8, 2024
@lauxjpn
Copy link
Collaborator

lauxjpn commented Dec 8, 2024

We already support and use it internally with MySqlBinaryExpression, with

public virtual MySqlBinaryExpression MySqlIntegerDivide(
SqlExpression left,
SqlExpression right,
RelationalTypeMapping typeMapping = null)
=> MakeBinary(
MySqlBinaryExpressionOperatorType.IntegerDivision,
left,
right,
typeMapping);

and

public virtual Expression VisitMySqlBinaryExpression(MySqlBinaryExpression mySqlBinaryExpression)
{
if (mySqlBinaryExpression.OperatorType == MySqlBinaryExpressionOperatorType.NonOptimizedEqual)
{
var equalExpression = new SqlBinaryExpression(
ExpressionType.Equal,
mySqlBinaryExpression.Left,
mySqlBinaryExpression.Right,
mySqlBinaryExpression.Type,
mySqlBinaryExpression.TypeMapping);
Visit(equalExpression);
}
else
{
Sql.Append("(");
Visit(mySqlBinaryExpression.Left);
Sql.Append(")");
switch (mySqlBinaryExpression.OperatorType)
{
case MySqlBinaryExpressionOperatorType.IntegerDivision:
Sql.Append(" DIV ");
break;
default:
throw new ArgumentOutOfRangeException();
}
Sql.Append("(");
Visit(mySqlBinaryExpression.Right);
Sql.Append(")");
}
return mySqlBinaryExpression;
}

The simplest way to expose it publicly would probably be to make it available as an extension method on EF.Functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants