Skip to content

Commit

Permalink
Added IGNORE_DUP_KEY query hint for insert commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Nov 10, 2023
1 parent ee540f9 commit 49d0694
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 65 deletions.
40 changes: 20 additions & 20 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void ConstantScanTest()
Alias = "test"
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(1, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["test.firstname"]).Value);
Expand Down Expand Up @@ -84,7 +84,7 @@ public void FilterNodeTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(1, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["test.firstname"]).Value);
Expand Down Expand Up @@ -161,7 +161,7 @@ public void MergeJoinInnerTest()
JoinType = QualifiedJoinType.Inner
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(2, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["f.firstname"]).Value);
Expand Down Expand Up @@ -241,7 +241,7 @@ public void MergeJoinLeftOuterTest()
JoinType = QualifiedJoinType.LeftOuter
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(3, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["f.firstname"]).Value);
Expand Down Expand Up @@ -323,7 +323,7 @@ public void MergeJoinRightOuterTest()
JoinType = QualifiedJoinType.RightOuter
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(3, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["f.firstname"]).Value);
Expand Down Expand Up @@ -362,7 +362,7 @@ public void AssertionTest()
ErrorMessage = "Only Mark is allowed"
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).GetEnumerator();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).GetEnumerator();

Assert.IsTrue(results.MoveNext());
Assert.AreEqual("Mark", results.Current.GetAttributeValue<SqlString>("test.name").Value);
Expand Down Expand Up @@ -420,7 +420,7 @@ public void ComputeScalarTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("mul").Value)
.ToArray();

Expand Down Expand Up @@ -462,7 +462,7 @@ public void DistinctTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("test.value1").Value)
.ToArray();

Expand Down Expand Up @@ -504,7 +504,7 @@ public void DistinctCaseInsensitiveTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlString>("test.value1").Value)
.ToArray();

Expand Down Expand Up @@ -562,7 +562,7 @@ public void SortNodeTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("test.expectedorder").Value)
.ToArray();

Expand Down Expand Up @@ -627,7 +627,7 @@ public void SortNodePresortedTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("test.expectedorder").Value)
.ToArray();

Expand Down Expand Up @@ -659,11 +659,11 @@ public void TableSpoolTest()

var spool = new TableSpoolNode { Source = source };

var results1 = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results1 = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("test.value1").Value)
.ToArray();

var results2 = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results2 = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => e.GetAttributeValue<SqlInt32>("test.value1").Value)
.ToArray();

Expand Down Expand Up @@ -708,7 +708,7 @@ public void CaseInsenstiveHashMatchAggregateNodeTest()
}
};

var results = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null))
var results = spool.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null))
.Select(e => new { Name = e.GetAttributeValue<SqlString>("src.value1").Value, Count = e.GetAttributeValue<SqlInt32>("count").Value })
.ToArray();

Expand Down Expand Up @@ -749,7 +749,7 @@ public void SqlTransformSingleResult()
public void AggregateInitialTest()
{
var aggregate = CreateAggregateTest();
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).Single();
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).Single();

Assert.AreEqual(SqlInt32.Null, result["min"]);
Assert.AreEqual(SqlInt32.Null, result["max"]);
Expand All @@ -767,7 +767,7 @@ public void AggregateInitialTest()
public void AggregateSingleValueTest()
{
var aggregate = CreateAggregateTest(1);
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).Single();
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).Single();

Assert.AreEqual((SqlInt32)1, result["min"]);
Assert.AreEqual((SqlInt32)1, result["max"]);
Expand All @@ -785,7 +785,7 @@ public void AggregateSingleValueTest()
public void AggregateTwoEqualValuesTest()
{
var aggregate = CreateAggregateTest(1, 1);
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).Single();
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).Single();

Assert.AreEqual((SqlInt32)1, result["min"]);
Assert.AreEqual((SqlInt32)1, result["max"]);
Expand All @@ -803,7 +803,7 @@ public void AggregateTwoEqualValuesTest()
public void AggregateMultipleValuesTest()
{
var aggregate = CreateAggregateTest(1, 3, 1, 1);
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).Single();
var result = aggregate.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).Single();

Assert.AreEqual((SqlInt32)1, result["min"]);
Assert.AreEqual((SqlInt32)3, result["max"]);
Expand Down Expand Up @@ -962,7 +962,7 @@ public void NestedLoopJoinInnerTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(2, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["f.firstname"]).Value);
Expand Down Expand Up @@ -1034,7 +1034,7 @@ public void NestedLoopJoinLeftOuterTest()
}
};

var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null)).ToArray();
var results = node.Execute(new NodeExecutionContext(_localDataSource, new StubOptions(), null, null, null)).ToArray();

Assert.AreEqual(3, results.Length);
Assert.AreEqual("Mark", ((SqlString)results[0]["f.firstname"]).Value);
Expand Down
41 changes: 28 additions & 13 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3273,7 +3273,7 @@ GROUP BY firstname
},
};

var result = select.Execute(new NodeExecutionContext(_localDataSource, this, new Dictionary<string, DataTypeReference>(), new Dictionary<string, object>()), CommandBehavior.Default);
var result = select.Execute(new NodeExecutionContext(_localDataSource, this, new Dictionary<string, DataTypeReference>(), new Dictionary<string, object>(), null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(result);

Expand Down Expand Up @@ -3722,7 +3722,7 @@ DECLARE @test int
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -3732,7 +3732,7 @@ DECLARE @test int
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand Down Expand Up @@ -3776,7 +3776,7 @@ public void SetVariableInDeclaration()
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -3786,7 +3786,7 @@ public void SetVariableInDeclaration()
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_dataSources, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand Down Expand Up @@ -3834,7 +3834,7 @@ DECLARE @test varchar(3)
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -3844,7 +3844,7 @@ DECLARE @test varchar(3)
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand All @@ -3868,7 +3868,7 @@ DECLARE @test varchar(3)
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -3878,7 +3878,7 @@ DECLARE @test varchar(3)
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand Down Expand Up @@ -3960,7 +3960,7 @@ DECLARE @test varchar(3)
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -3970,7 +3970,7 @@ DECLARE @test varchar(3)
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand All @@ -3994,7 +3994,7 @@ DECLARE @test varchar
{
if (plan is IDataReaderExecutionPlanNode selectQuery)
{
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), CommandBehavior.Default);
var results = selectQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(results);

Expand All @@ -4004,7 +4004,7 @@ DECLARE @test varchar
}
else if (plan is IDmlQueryExecutionPlanNode dmlQuery)
{
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues), out _);
dmlQuery.Execute(new NodeExecutionContext(_localDataSource, this, parameterTypes, parameterValues, null), out _);
}
}
}
Expand Down Expand Up @@ -6473,5 +6473,20 @@ public void FoldFilterToCorrectTableAlias()
</entity>
</fetch>");
}

[TestMethod]
public void IgnoreDupKeyHint()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSource.Values, this);

var query = @"INSERT INTO account (accountid, name) VALUES ('{CD503427-E785-40D8-AD0E-FBDF4918D298}', 'Data8') OPTION (USE HINT ('IGNORE_DUP_KEY'))";

var plans = planBuilder.Build(query, null, out _);

Assert.AreEqual(1, plans.Length);

var insert = AssertNode<InsertNode>(plans[0]);
Assert.IsTrue(insert.IgnoreDuplicateKey);
}
}
}
Loading

0 comments on commit 49d0694

Please sign in to comment.