private LogicalElement GetElementForTreeNode(SyntaxTreeNode node) { if (node is SelectASTNode selectASTNode) { Table table = _relationManager.GetTable(selectASTNode.From.Identifier.Identifier); ReadLogicalElement result = new RelationElement(table.TableDefinition); Condition condition = null; if (selectASTNode.Condition != null) { condition = BooleanExpressionToCondition(table.TableDefinition, selectASTNode.Condition); //// todo: IN (1,2,3) //// todo: IN (select id from products) AND x = 1 //if (TryOptimizeIN(selectASTNode, result, table.TableDefinition, out CartesianProductElement productElement)) //{ // result = productElement; //} //else //{ //} } if (selectASTNode.Join != null) { Join join = JoinNodeToJoin(table.TableDefinition, selectASTNode.Join); ReadLogicalElement joinRelation = new RelationElement(join.RightTable); if (condition != null) { (Condition leftPushedDownCondition, Condition leftover1) = TryPushdown((RelationElement)result, condition); (Condition rightPushedDownCondition, Condition leftover2) = TryPushdown((RelationElement)joinRelation, leftover1); leftPushedDownCondition = leftPushedDownCondition.Simplify(); rightPushedDownCondition = rightPushedDownCondition.Simplify(); condition = leftover2?.Simplify(); if (leftPushedDownCondition != null) { result = new SelectionElement(result, leftPushedDownCondition); } if (rightPushedDownCondition != null) { joinRelation = new SelectionElement(joinRelation, rightPushedDownCondition); } } else { result = new RelationElement(table.TableDefinition); joinRelation = new RelationElement(join.RightTable); } result = new CartesianProductElement(result, joinRelation, join.LeftColumn, join.RightColumn); } else { result = new RelationElement(table.TableDefinition); } if (condition != null) { result = new SelectionElement(result, condition); } result = new ProjectionElement(result, SelectColumnsToColumns(table, selectASTNode.SelectColumns)); return(result); } else if (node is InsertASTNode insertASTNode) { Table table = _relationManager.GetTable(insertASTNode.Into.Identifier); Set set = new Set(table.TableDefinition); set.Add(insertASTNode.Arguments.Select(x => GetValueFromFactor(x)).ToArray()); return(new InsertElement(table.TableDefinition, new MemorySetElement(table.TableDefinition, set))); } return(null); }
private (Condition, Condition) TryPushdown(RelationElement input, Condition condition, Condition pushedDownCondition = null) { if (condition == null) { return(pushedDownCondition, condition); } if (pushedDownCondition == null) { pushedDownCondition = new AndCondition(); } if (condition is AndCondition and) { if (and.Left is LeafCondition leftLeaf) { if (leftLeaf.Column.Relation.Id == input.Relation.Id) { pushedDownCondition = new AndCondition() { Left = pushedDownCondition, Right = leftLeaf }; and.Left = null; } } else { return(TryPushdown(input, and.Left, pushedDownCondition)); } if (and.Right is LeafCondition rightLeaf) { if (rightLeaf.Column.Relation.Id == input.Relation.Id) { pushedDownCondition = new AndCondition() { Left = pushedDownCondition, Right = rightLeaf }; and.Right = null; } } else { return(TryPushdown(input, and.Right, pushedDownCondition)); } } else if (condition is LeafCondition leaf) { if (leaf.Column.Relation.Id == input.Relation.Id) { pushedDownCondition = new AndCondition() { Left = pushedDownCondition, Right = leaf }; condition = null; } } return(pushedDownCondition, condition); }