Skip to content

Commit deb4bb1

Browse files
committed
Optimize SqlFunctionCall/SqlCustomFunctionCall
1 parent f9d4d0f commit deb4bb1

43 files changed

Lines changed: 175 additions & 302 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Orm/Xtensive.Orm.Firebird/Sql.Drivers.Firebird/v2_5/Compiler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2011-2021 Xtensive LLC.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Csaba Beer
@@ -9,6 +9,7 @@
99
using Xtensive.Sql.Compiler;
1010
using Xtensive.Sql.Ddl;
1111
using Xtensive.Sql.Dml;
12+
using Xtensive.Core;
1213

1314
namespace Xtensive.Sql.Drivers.Firebird.v2_5
1415
{
@@ -167,9 +168,7 @@ public override void Visit(SqlFunctionCall node)
167168
{
168169
switch (node.FunctionType) {
169170
case SqlFunctionType.Concat:
170-
var exprs = new SqlExpression[node.Arguments.Count];
171-
node.Arguments.CopyTo(exprs, 0);
172-
Visit(SqlDml.Concat(exprs));
171+
Visit(SqlDml.Concat(node.Arguments.ToArray(node.Arguments.Count)));
173172
return;
174173
case SqlFunctionType.DateTimeTruncate:
175174
Visit(SqlDml.Cast(node.Arguments[0], new SqlValueType("Date")));

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Compiler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2011-2021 Xtensive LLC.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Malisa Ncube
@@ -9,6 +9,7 @@
99
using Xtensive.Sql.Ddl;
1010
using Xtensive.Sql.Dml;
1111
using Xtensive.Sql.Model;
12+
using Xtensive.Core;
1213

1314
namespace Xtensive.Sql.Drivers.MySql.v5_0
1415
{
@@ -152,9 +153,7 @@ public override void Visit(SqlFunctionCall node)
152153
SqlDml.FunctionCall("TRUNCATE", argument, SqlDml.Literal(0)).AcceptVisitor(this);
153154
return;
154155
case SqlFunctionType.Concat:
155-
var exprs = new SqlExpression[node.Arguments.Count];
156-
node.Arguments.CopyTo(exprs, 0);
157-
Visit(SqlDml.Concat(exprs));
156+
Visit(SqlDml.Concat(node.Arguments.ToArray(node.Arguments.Count)));
158157
return;
159158
case SqlFunctionType.CharLength:
160159
SqlDml.FunctionCall(translator.Translate(SqlFunctionType.CharLength), node.Arguments[0]).AcceptVisitor(this);

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v09/Compiler.cs

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -143,66 +143,65 @@ public override void Visit(SqlAlterTable node)
143143
public override void Visit(SqlFunctionCall node)
144144
{
145145
switch (node.FunctionType) {
146-
case SqlFunctionType.CharLength:
147-
(SqlDml.FunctionCall("DATALENGTH", node.Arguments) / 2).AcceptVisitor(this);
148-
return;
149-
case SqlFunctionType.PadLeft:
150-
case SqlFunctionType.PadRight:
151-
GenericPad(node).AcceptVisitor(this);
152-
return;
153-
case SqlFunctionType.Round:
154-
// Round should always be called with 2 arguments
155-
if (node.Arguments.Count==1) {
146+
case SqlFunctionType.CharLength:
147+
(SqlDml.FunctionCall("DATALENGTH", node.Arguments) / 2).AcceptVisitor(this);
148+
return;
149+
case SqlFunctionType.PadLeft:
150+
case SqlFunctionType.PadRight:
151+
GenericPad(node).AcceptVisitor(this);
152+
return;
153+
case SqlFunctionType.Round:
154+
// Round should always be called with 2 arguments
155+
if (node.Arguments.Count == 1) {
156+
Visit(SqlDml.FunctionCall(
157+
translator.Translate(SqlFunctionType.Round),
158+
node.Arguments[0],
159+
SqlDml.Literal(0)));
160+
return;
161+
}
162+
break;
163+
case SqlFunctionType.Truncate:
164+
// Truncate is implemented as round(arg, 0, 1) call in MSSQL.
165+
// It's stupid, isn't it?
156166
Visit(SqlDml.FunctionCall(
157167
translator.Translate(SqlFunctionType.Round),
158168
node.Arguments[0],
159-
SqlDml.Literal(0)));
169+
SqlDml.Literal(0),
170+
SqlDml.Literal(1)));
160171
return;
161-
}
162-
break;
163-
case SqlFunctionType.Truncate:
164-
// Truncate is implemented as round(arg, 0, 1) call in MSSQL.
165-
// It's stupid, isn't it?
166-
Visit(SqlDml.FunctionCall(
167-
translator.Translate(SqlFunctionType.Round),
168-
node.Arguments[0],
169-
SqlDml.Literal(0),
170-
SqlDml.Literal(1)));
171-
return;
172-
case SqlFunctionType.Substring:
173-
if (node.Arguments.Count==2) {
174-
node = SqlDml.Substring(node.Arguments[0], node.Arguments[1]);
175-
SqlExpression len = SqlDml.CharLength(node.Arguments[0]);
176-
node.Arguments.Add(len);
177-
Visit(node);
178-
return;
179-
}
180-
break;
181-
case SqlFunctionType.IntervalToMilliseconds:
172+
case SqlFunctionType.Substring:
173+
if (node.Arguments.Count == 2) {
174+
SqlExpression len = SqlDml.CharLength(node.Arguments[0]);
175+
node = SqlDml.Substring(node.Arguments[0], node.Arguments[1], len);
176+
Visit(node);
177+
return;
178+
}
179+
break;
180+
case SqlFunctionType.IntervalToMilliseconds:
182181
Visit(CastToLong(node.Arguments[0]) / NanosecondsPerMillisecond);
183182
return;
184-
case SqlFunctionType.IntervalConstruct:
185-
case SqlFunctionType.IntervalToNanoseconds:
186-
Visit(CastToLong(node.Arguments[0]));
187-
return;
188-
case SqlFunctionType.DateTimeAddMonths:
189-
Visit(DateAddMonth(node.Arguments[0], node.Arguments[1]));
190-
return;
191-
case SqlFunctionType.DateTimeAddYears:
192-
Visit(DateAddYear(node.Arguments[0], node.Arguments[1]));
193-
return;
194-
case SqlFunctionType.DateTimeTruncate:
195-
DateTimeTruncate(node.Arguments[0]).AcceptVisitor(this);
196-
return;
197-
case SqlFunctionType.DateTimeConstruct:
198-
Visit(DateAddDay(DateAddMonth(DateAddYear(SqlDml.Literal(new DateTime(2001, 1, 1)),
199-
node.Arguments[0] - 2001),
200-
node.Arguments[1] - 1),
201-
node.Arguments[2] - 1));
202-
return;
203-
case SqlFunctionType.DateTimeToStringIso:
204-
Visit(DateTimeToStringIso(node.Arguments[0]));
205-
return;
183+
case SqlFunctionType.IntervalConstruct:
184+
case SqlFunctionType.IntervalToNanoseconds:
185+
Visit(CastToLong(node.Arguments[0]));
186+
return;
187+
case SqlFunctionType.DateTimeAddMonths:
188+
Visit(DateAddMonth(node.Arguments[0], node.Arguments[1]));
189+
return;
190+
case SqlFunctionType.DateTimeAddYears:
191+
Visit(DateAddYear(node.Arguments[0], node.Arguments[1]));
192+
return;
193+
case SqlFunctionType.DateTimeTruncate:
194+
DateTimeTruncate(node.Arguments[0]).AcceptVisitor(this);
195+
return;
196+
case SqlFunctionType.DateTimeConstruct:
197+
Visit(DateAddDay(DateAddMonth(DateAddYear(SqlDml.Literal(new DateTime(2001, 1, 1)),
198+
node.Arguments[0] - 2001),
199+
node.Arguments[1] - 1),
200+
node.Arguments[2] - 1));
201+
return;
202+
case SqlFunctionType.DateTimeToStringIso:
203+
Visit(DateTimeToStringIso(node.Arguments[0]));
204+
return;
206205
}
207206

208207
base.Visit(node);

Orm/Xtensive.Orm/Core/ArgumentValidator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ public static void EnsureArgumentNotNullOrEmptyOrWhiteSpace(string value, [Invok
9292
/// <param name="parameterName">Name of the method parameter.</param>
9393
/// <typeparam name="T">The expected type of value.</typeparam>
9494
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95-
public static void EnsureArgumentIs<T>(object value, [InvokerParameterName] string parameterName)
95+
public static T EnsureArgumentIs<T>(object value,
96+
[InvokerParameterName, CallerArgumentExpression("value")] string parameterName = null)
9697
{
97-
EnsureArgumentNotNull(value, parameterName);
98-
if (!(value is T)) {
99-
throw new ArgumentException(string.Format(Strings.ExInvalidArgumentType, typeof(T)), parameterName);
98+
if (value is T result) {
99+
return result;
100100
}
101+
EnsureArgumentNotNull(value, parameterName);
102+
throw new ArgumentException(string.Format(Strings.ExInvalidArgumentType, typeof(T)), parameterName);
101103
}
102104

103105
/// <summary>

Orm/Xtensive.Orm/Modelling/Nesting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal Nesting(Node node, string propertyName)
8181

8282
internal Nesting(Node node)
8383
{
84-
ArgumentValidator.EnsureArgumentIs<IModel>(node, "node");
84+
ArgumentValidator.EnsureArgumentIs<IModel>(node);
8585
Node = node;
8686
Initialize();
8787
}

Orm/Xtensive.Orm/Modelling/Node.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ protected virtual void ValidateMove(Node newParent, string newName, int newIndex
433433
}
434434

435435
// Validating parent model
436-
ArgumentValidator.EnsureArgumentNotNull(newParent, nameof(newParent));
437-
ArgumentValidator.EnsureArgumentIs<Node>(newParent, nameof(newParent));
436+
ArgumentValidator.EnsureArgumentIs<Node>(newParent);
438437
var model = Model;
439438
if (model != null) {
440439
var newModel = newParent.Model;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlAggregate.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public SqlExpression Expression
3333

3434
public override void ReplaceWith(SqlExpression expression)
3535
{
36-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
37-
ArgumentValidator.EnsureArgumentIs<SqlAggregate>(expression, "expression");
38-
var replacingExpression = (SqlAggregate) expression;
36+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlAggregate>(expression);
3937
NodeType = replacingExpression.NodeType;
4038
distinct = replacingExpression.Distinct;
4139
this.expression = replacingExpression.Expression;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlArray{T}.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public static implicit operator SqlArray<T>(T[] value)
3737

3838
public override void ReplaceWith(SqlExpression expression)
3939
{
40-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
41-
ArgumentValidator.EnsureArgumentIs<SqlArray<T>>(expression, "expression");
42-
var replacingExpression = (SqlArray<T>) expression;
40+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlArray<T>>(expression);
4341
Values = replacingExpression.Values;
4442
}
4543

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlBetween.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public SqlExpression Expression
4646

4747
public override void ReplaceWith(SqlExpression expression)
4848
{
49-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
50-
ArgumentValidator.EnsureArgumentIs<SqlBetween>(expression, "expression");
51-
SqlBetween replacingExpression = expression as SqlBetween;
49+
SqlBetween replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlBetween>(expression);
5250
NodeType = replacingExpression.NodeType;
5351
left = replacingExpression.Left;
5452
right = replacingExpression.Right;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlBinary.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public static bool operator false(SqlBinary operand)
8383

8484
public override void ReplaceWith(SqlExpression expression)
8585
{
86-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
87-
ArgumentValidator.EnsureArgumentIs<SqlBinary>(expression, "expression");
88-
var replacingExpression = (SqlBinary) expression;
86+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlBinary>(expression);
8987
NodeType = replacingExpression.NodeType;
9088
Left = replacingExpression.Left;
9189
Right = replacingExpression.Right;

0 commit comments

Comments
 (0)