forked from ayende/rhino-etl
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge from mr-miles with the added join dsl operations
Fixed a compile error and detabify
- Loading branch information
1 parent
97a92aa
commit e700811
Showing
13 changed files
with
802 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Boo.Lang.Compiler; | ||
using Boo.Lang.Compiler.Ast; | ||
using Rhino.Etl.Core; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// The on part of a join, split into two parts, the first is the condition, the second | ||
/// is the merge | ||
/// </summary> | ||
public class ActionMacro : AbstractChildMacro | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OnMacro"/> class. | ||
/// </summary> | ||
public ActionMacro() | ||
: base("leftjoin","rightjoin","innerjoin","fulljoin") | ||
{ | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Perform the actual expansion of the macro | ||
/// </summary> | ||
/// <param name="macro">The macro.</param> | ||
/// <returns></returns> | ||
protected override Statement DoExpand(MacroStatement macro) | ||
{ | ||
if (macro.Arguments.Count != 0) | ||
{ | ||
Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo,"No arguments allowed for action statement")); | ||
return null; | ||
} | ||
|
||
Method mergeRowsMethod = new Method("MergeRows"); | ||
mergeRowsMethod.Modifiers = TypeMemberModifiers.Override; | ||
mergeRowsMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName))); | ||
mergeRowsMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName))); | ||
CodeBuilder.DeclareLocal(mergeRowsMethod, "row", TypeSystemServices.Map(typeof(Row))); | ||
mergeRowsMethod.Body.Add( | ||
new BinaryExpression(BinaryOperatorType.Assign, | ||
new ReferenceExpression("row"), | ||
new MethodInvocationExpression( | ||
AstUtil.CreateReferenceExpression(typeof(Row).FullName)) | ||
) | ||
); | ||
mergeRowsMethod.Body.Add(macro.Body); | ||
mergeRowsMethod.Body.Add(new ReturnStatement(new ReferenceExpression("row"))); | ||
|
||
ParentMethods.Add(mergeRowsMethod); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Rhino.Etl.Core.Operations; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class FullJoinMacro : HashJoinMacro | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public FullJoinMacro() | ||
: base(JoinType.Full) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Boo.Lang.Compiler; | ||
using Boo.Lang.Compiler.Ast; | ||
using Rhino.Etl.Core.Operations; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// Create a class based on <see cref="JoinOperation"/> and tranform the code | ||
/// into a join condition | ||
/// </summary> | ||
public abstract class HashJoinMacro : AbstractClassGeneratorMacro<JoinOperation> | ||
{ | ||
private readonly JoinType JoinType; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JoinMacro"/> class. | ||
/// </summary> | ||
protected HashJoinMacro(JoinType joinType) | ||
: base("Initialize") | ||
{ | ||
JoinType = joinType; | ||
} | ||
|
||
/// <summary> | ||
/// Expands the specified macro | ||
/// </summary> | ||
/// <param name="macro">The macro.</param> | ||
/// <returns></returns> | ||
public override Statement Expand(MacroStatement macro) | ||
{ | ||
ICollection<StringLiteralExpression> leftKeys = null; | ||
ICollection<StringLiteralExpression> rightKeys = null; | ||
|
||
foreach(var arg in macro.Arguments) | ||
{ | ||
BinaryExpression assignment; | ||
if (!TryGetAssignment(arg, out assignment)) continue; | ||
|
||
var rf = assignment.Left as ReferenceExpression; | ||
if (rf == null) continue; | ||
|
||
var val = assignment.Right as ArrayLiteralExpression; | ||
if (val == null) continue; | ||
|
||
if (rf.Name != "RightKeyColumns" && rf.Name != "LeftKeyColumns") continue; | ||
|
||
var lst = Enumerable.OfType<StringLiteralExpression>(val.Items).ToList(); | ||
if (rf.Name == "RightKeyColumns") | ||
rightKeys = lst; | ||
else | ||
leftKeys = lst; | ||
} | ||
|
||
if (leftKeys==null || leftKeys.Count==0) | ||
{ | ||
Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "LeftKeys must be defined for a hash join")); | ||
return null; | ||
} | ||
|
||
if (rightKeys == null || rightKeys.Count==0) | ||
{ | ||
Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "RightKeys must be defined for a hash join")); | ||
return null; | ||
} | ||
|
||
if (rightKeys.Count != leftKeys.Count) | ||
{ | ||
Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Number of key columns must be the same for both sides of the join")); | ||
} | ||
|
||
var method = SetupJoinMethodDefinition(leftKeys, rightKeys); | ||
AddMethodDefinitionToClassDefinition(macro, method); | ||
return base.Expand(macro); | ||
} | ||
|
||
private Method SetupJoinMethodDefinition(IEnumerable<StringLiteralExpression> leftKeys, IEnumerable<StringLiteralExpression> rightKeys) | ||
{ | ||
var method = new Method("SetupJoinConditions"); | ||
method.Modifiers = TypeMemberModifiers.Override; | ||
|
||
CodeBuilder.DeclareLocal(method, "join", TypeSystemServices.Map(typeof(JoinOperation.JoinBuilder))); | ||
ReferenceExpression joinConstructor; | ||
switch (JoinType) | ||
{ | ||
case JoinType.Full: | ||
joinConstructor = new ReferenceExpression("FullOuterJoin"); | ||
break; | ||
case JoinType.Left: | ||
joinConstructor = new ReferenceExpression("LeftJoin"); | ||
break; | ||
case JoinType.Right: | ||
joinConstructor = new ReferenceExpression("RightJoin"); | ||
break; | ||
case JoinType.Inner: | ||
joinConstructor = new ReferenceExpression("InnerJoin"); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
method.Body.Add(new BinaryExpression(BinaryOperatorType.Assign, new ReferenceExpression("join"), joinConstructor)); | ||
method.Body.Add(new MethodInvocationExpression(new MemberReferenceExpression(new ReferenceExpression("join"), "Left"), leftKeys.OfType<Expression>().ToArray())); | ||
method.Body.Add(new MethodInvocationExpression(new MemberReferenceExpression(new ReferenceExpression("join"), "Right"), rightKeys.OfType<Expression>().ToArray())); | ||
return method; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Rhino.Etl.Core.Operations; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class InnerJoinMacro : HashJoinMacro | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public InnerJoinMacro() | ||
: base(JoinType.Inner) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Rhino.Etl.Core.Operations; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class LeftJoinMacro : HashJoinMacro | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public LeftJoinMacro() | ||
: base(JoinType.Left) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Rhino.Etl.Core.Operations; | ||
|
||
namespace Rhino.Etl.Dsl.Macros | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class RightJoinMacro : HashJoinMacro | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public RightJoinMacro() | ||
: base(JoinType.Right) | ||
{ } | ||
} | ||
} |
Oops, something went wrong.