Skip to content

Commit 2c399df

Browse files
authored
Merge pull request #131 from DataObjects-NET/6.0-pgsql-apply-improvement
PostgreSQL: Add support for CrossApply and OuterApply for v10 and newer
2 parents ea1926f + 4aa7349 commit 2c399df

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v10_0/ServerInfoProvider.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
// Copyright (C) 2019-2020 Xtensive LLC.
1+
// Copyright (C) 2019-2021 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: Alexey Kulakov
55
// Created: 2019.09.25
66

7+
using Xtensive.Sql.Info;
8+
79
namespace Xtensive.Sql.Drivers.PostgreSql.v10_0
810
{
911
internal class ServerInfoProvider : v9_0.ServerInfoProvider
1012
{
13+
public override QueryInfo GetQueryInfo()
14+
{
15+
var info = base.GetQueryInfo();
16+
info.Features |= QueryFeatures.CrossApply;
17+
return info;
18+
}
19+
1120
// Constructors
1221

1322
public ServerInfoProvider(SqlDriver driver)

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v10_0/Translator.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
1-
// Copyright (C) 2019-2020 Xtensive LLC.
1+
// Copyright (C) 2019-2021 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: Alexey Kulakov
55
// Created: 2019.09.25
66

7+
using System;
8+
using Xtensive.Sql.Compiler;
9+
using Xtensive.Sql.Dml;
10+
711
namespace Xtensive.Sql.Drivers.PostgreSql.v10_0
812
{
913
internal class Translator : v9_1.Translator
1014
{
15+
public override string Translate(SqlCompilerContext context, SqlJoinExpression node, JoinSection section)
16+
{
17+
switch (section) {
18+
case JoinSection.Specification: {
19+
if (node.Expression == null) {
20+
switch (node.JoinType) {
21+
case SqlJoinType.InnerJoin:
22+
case SqlJoinType.LeftOuterJoin:
23+
case SqlJoinType.RightOuterJoin:
24+
case SqlJoinType.FullOuterJoin:
25+
throw new NotSupportedException();
26+
case SqlJoinType.CrossApply:
27+
return "CROSS JOIN LATERAL";
28+
case SqlJoinType.LeftOuterApply:
29+
return "LEFT JOIN LATERAL";
30+
}
31+
}
32+
return Translate(node.JoinType) + " JOIN";
33+
}
34+
case JoinSection.Exit: {
35+
if (node.JoinType == SqlJoinType.LeftOuterApply) {
36+
return "ON TRUE";
37+
}
38+
return string.Empty;
39+
}
40+
}
41+
return base.Translate(context, node, section);
42+
}
43+
1144
// Constructors
1245

1346
public Translator(SqlDriver driver)

Orm/Xtensive.Orm/Orm/Providers/SqlCompiler.Apply.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2009.11.13
66

@@ -17,7 +17,7 @@
1717

1818
namespace Xtensive.Orm.Providers
1919
{
20-
partial class SqlCompiler
20+
public partial class SqlCompiler
2121
{
2222
/// <inheritdoc/>
2323
protected override SqlProvider VisitApply(ApplyProvider provider)
@@ -170,15 +170,15 @@ private SqlSelect ProcessApplyViaCrossApply(ApplyProvider provider, SqlProvider
170170
? (IReadOnlyList<SqlColumn>) leftTable.Columns
171171
: left.Request.Statement.Columns;
172172

173-
var rightShouldUseReference = ShouldUseQueryReference(provider, right);
173+
var rightShouldUseReference = ShouldUseQueryReference(provider, right) || forceApplyViaReference;
174174
var rightTable = rightShouldUseReference
175175
? right.PermanentReference
176176
: right.Request.Statement.From;
177177
var rightColumns = rightShouldUseReference
178178
? (IReadOnlyList<SqlColumn>) rightTable.Columns
179179
: right.Request.Statement.Columns;
180180

181-
var joinType = provider.ApplyType==JoinType.LeftOuter
181+
var joinType = provider.ApplyType == JoinType.LeftOuter
182182
? SqlJoinType.LeftOuterApply
183183
: SqlJoinType.CrossApply;
184184

Orm/Xtensive.Orm/Orm/Providers/SqlCompiler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Vakhtina Elena
55
// Created: 2009.02.13
66

@@ -28,6 +28,7 @@ public partial class SqlCompiler : Compiler<SqlProvider>
2828
private readonly ProviderInfo providerInfo;
2929
private readonly bool temporaryTablesSupported;
3030
private readonly HashSet<Column> rootColumns = new HashSet<Column>();
31+
private readonly bool forceApplyViaReference;
3132

3233
private bool anyTemporaryTablesRequired;
3334

@@ -562,6 +563,7 @@ public SqlCompiler(HandlerAccessor handlers, CompilerConfiguration configuration
562563

563564
providerInfo = Handlers.ProviderInfo;
564565
temporaryTablesSupported = DomainHandler.TemporaryTableManager.Supported;
566+
forceApplyViaReference = handlers.ProviderInfo.ProviderName.Equals(WellKnown.Provider.PostgreSql);
565567

566568
if (!providerInfo.Supports(ProviderFeatures.FullFeaturedBooleanExpressions))
567569
booleanExpressionConverter = new BooleanExpressionConverter(Driver);

0 commit comments

Comments
 (0)