[jira] [Work logged] (HIVE-23817) Pushing TopN Key operator PKFK inner joins
[ https://issues.apache.org/jira/browse/HIVE-23817?focusedWorklogId=464211=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-464211 ] ASF GitHub Bot logged work on HIVE-23817: - Author: ASF GitHub Bot Created on: 29/Jul/20 17:48 Start Date: 29/Jul/20 17:48 Worklog Time Spent: 10m Work Description: kasakrisz merged pull request #1228: URL: https://github.com/apache/hive/pull/1228 This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking --- Worklog Id: (was: 464211) Time Spent: 40m (was: 0.5h) > Pushing TopN Key operator PKFK inner joins > -- > > Key: HIVE-23817 > URL: https://issues.apache.org/jira/browse/HIVE-23817 > Project: Hive > Issue Type: Improvement >Reporter: Attila Magyar >Assignee: Attila Magyar >Priority: Major > Labels: pull-request-available > Time Spent: 40m > Remaining Estimate: 0h > > If there is primary key foreign key relationship between the tables we can > push the topnkey operator through the join. -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Work logged] (HIVE-23817) Pushing TopN Key operator PKFK inner joins
[ https://issues.apache.org/jira/browse/HIVE-23817?focusedWorklogId=461528=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-461528 ] ASF GitHub Bot logged work on HIVE-23817: - Author: ASF GitHub Bot Created on: 21/Jul/20 11:16 Start Date: 21/Jul/20 11:16 Worklog Time Spent: 10m Work Description: kasakrisz commented on a change in pull request #1228: URL: https://github.com/apache/hive/pull/1228#discussion_r458020139 ## File path: ql/src/test/queries/clientpositive/topnkey_inner_join.q ## @@ -0,0 +1,49 @@ +drop table if exists customer; +drop table if exists orders; + +create table customer (id int, name string, email string); +create table orders (customer_id int not null enforced, amount int); + +alter table customer add constraint pk_customer_id primary key (id) disable novalidate rely; +alter table orders add constraint fk_order_customer_id foreign key (customer_id) references customer(id) disable novalidate rely; + +insert into customer values + (4, 'Heisenberg', 'heisenb...@email.com'), + (3, 'Smith', 'sm...@email.com'), + (2, 'Jones', 'jo...@email.com'), + (1, 'Robinson', 'robin...@email.com'); + +insert into orders values + (2, 200), + (3, 40), + (1, 100), + (1, 50), + (3, 30); + +set hive.optimize.topnkey=true; +set hive.optimize.limittranspose=false; + +select 'positive: order by columns are coming from child table'; +explain select * from orders join customer on customer.id = orders.customer_id order by orders.amount limit 3; +explain select * from orders join customer on customer.id = orders.customer_id order by orders.customer_id, orders.amount limit 3; +explain select * from customer join orders on orders.customer_id = customer.id order by orders.amount, orders.customer_id limit 3; + +select 'negative: order by columns are coming from referenced table'; +explain select * from orders join customer on customer.id = orders.customer_id order by customer.name limit 3; +explain select * from orders join customer on customer.id = orders.customer_id order by customer.email, customer.name limit 3; + +select 'negative: 1st order by columns is coming from referenced table'; +explain select * from orders join customer on customer.id = orders.customer_id order by customer.name, orders.amount limit 3; + +select 'mixed/positive: 1st n order by columns are coming from child table'; +explain select * from orders join customer on customer.id = orders.customer_id order by orders.amount, customer.name limit 3; + +select 'negative: no PK/FK'; +alter table customer drop constraint pk_customer_id; +alter table orders drop constraint fk_order_customer_id; +explain select * from customer join orders on customer.id = orders.customer_id order by customer.id limit 3; + +select 'negatie: no RELY'; +alter table customer add constraint pk_customer_id primary key (id) disable novalidate; +alter table orders add constraint fk_order_customer_id foreign key (customer_id) references customer(id) disable novalidate; +explain select * from customer join orders on customer.id = orders.customer_id order by customer.id limit 3; Review comment: It is also worth to execute these queries to catch correctness issues This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking --- Worklog Id: (was: 461528) Time Spent: 0.5h (was: 20m) > Pushing TopN Key operator PKFK inner joins > -- > > Key: HIVE-23817 > URL: https://issues.apache.org/jira/browse/HIVE-23817 > Project: Hive > Issue Type: Improvement >Reporter: Attila Magyar >Assignee: Attila Magyar >Priority: Major > Labels: pull-request-available > Time Spent: 0.5h > Remaining Estimate: 0h > > If there is primary key foreign key relationship between the tables we can > push the topnkey operator through the join. -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Work logged] (HIVE-23817) Pushing TopN Key operator PKFK inner joins
[ https://issues.apache.org/jira/browse/HIVE-23817?focusedWorklogId=461405=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-461405 ] ASF GitHub Bot logged work on HIVE-23817: - Author: ASF GitHub Bot Created on: 21/Jul/20 04:26 Start Date: 21/Jul/20 04:26 Worklog Time Spent: 10m Work Description: jcamachor commented on a change in pull request #1228: URL: https://github.com/apache/hive/pull/1228#discussion_r457824052 ## File path: parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g ## @@ -471,6 +471,8 @@ Number (Digit)+ ( DOT (Digit)* (Exponent)? | Exponent)? ; +PKFK_JOIN: 'PKFK_JOIN'; Review comment: Can we prefix with `KW_` and move above with rest of keywords? ## File path: ql/src/test/queries/clientpositive/topnkey_inner_join.q ## @@ -0,0 +1,50 @@ +drop table if exists customer; +drop table if exists orders; + +create table customer (id int, name string, email string); +create table orders (customer_id int not null enforced, amount int); + +alter table customer add constraint pk_customer_id primary key (id) disable novalidate rely; +alter table orders add constraint fk_order_customer_id foreign key (customer_id) references customer(id) disable novalidate rely; + +insert into customer values + (4, 'Heisenberg', 'heisenb...@email.com'), + (3, 'Smith', 'sm...@email.com'), + (2, 'Jones', 'jo...@email.com'), + (1, 'Robinson', 'robin...@email.com'); + +insert into orders values + (2, 200), + (3, 40), + (1, 100), + (1, 50), + (3, 30); + +set hive.optimize.topnkey=true; +set hive.optimize.limittranspose=false; + +select 'positive: order by columns are coming from child table'; +-- FIXME: explain select * from customer join orders on customer.id = orders.customer_id order by customer.id limit 3; Review comment: I see this example is below. Can FIXME be removed? ## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java ## @@ -442,6 +450,40 @@ private QueryBlockInfo convertSource(RelNode r) throws CalciteSemanticException return new QueryBlockInfo(s, ast); } + /** + * Add PK-FK join information to the AST as a query hint + * @param ast + * @param join + * @param swapSides whether the left and right input of the join is swapped + */ + private void addPkFkInfoToAST(ASTNode ast, Join join, boolean swapSides) { +List joinFilters = new ArrayList<>(RelOptUtil.conjunctions(join.getCondition())); +RelMetadataQuery mq = join.getCluster().getMetadataQuery(); +HiveRelOptUtil.PKFKJoinInfo rightInputResult = +HiveRelOptUtil.extractPKFKJoin(join, joinFilters, false, mq); +HiveRelOptUtil.PKFKJoinInfo leftInputResult = +HiveRelOptUtil.extractPKFKJoin(join, joinFilters, true, mq); +// Add the fkJoinIndex (0=left, 1=right, if swapSides is false) to the AST +// check if the nonFK side is filtered +if (leftInputResult.isPkFkJoin && leftInputResult.additionalPredicates.isEmpty()) { + RelNode nonFkInput = join.getRight(); + ast.addChild(pkFkHint(swapSides ? 1 : 0, HiveRelOptUtil.isRowFilteringPlan(mq, nonFkInput))); +} else if (rightInputResult.isPkFkJoin && rightInputResult.additionalPredicates.isEmpty()) { + RelNode nonFkInput = join.getLeft(); + ast.addChild(pkFkHint(swapSides ? 0 : 1, HiveRelOptUtil.isRowFilteringPlan(mq, nonFkInput))); +} + } + + private ASTNode pkFkHint(int fkTableIndex, boolean nonFkSideIsFiltered) { +ParseDriver parseDriver = new ParseDriver(); +try { + return parseDriver.parseHint(String.format("PKFK_JOIN(%d, %s)", + fkTableIndex, nonFkSideIsFiltered ? NON_FK_FILTERED : "notFiltered")); Review comment: Naming (NON_FK_FILTERED : "notFiltered") is a bit confusing. We can simplify to NON_FK_FILTERED vs NON_FK_NOT_FILTERED? Create String in converter for both (or enum). ## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/topnkey/TopNKeyPushdownProcessor.java ## @@ -255,6 +260,88 @@ private void pushdownThroughLeftOuterJoin(TopNKeyOperator topNKey) throws Semant } } + private void pushdownInnerJoin(TopNKeyOperator topNKey, int fkJoinInputIndex, boolean nonFkSideIsFiltered) throws SemanticException { +TopNKeyDesc topNKeyDesc = topNKey.getConf(); +CommonJoinOperator join = +(CommonJoinOperator) topNKey.getParentOperators().get(0); +List> joinInputs = join.getParentOperators(); +ReduceSinkOperator fkJoinInput = (ReduceSinkOperator) joinInputs.get(fkJoinInputIndex); +if (nonFkSideIsFiltered) { + LOG.debug("Not pushing {} through {} as non FK side of the join is filtered", topNKey.getName(), join.getName()); + return; +} +// Check column origins: +// 1. If all OrderBy columns are coming from the child (FK)
[jira] [Work logged] (HIVE-23817) Pushing TopN Key operator PKFK inner joins
[ https://issues.apache.org/jira/browse/HIVE-23817?focusedWorklogId=456166=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-456166 ] ASF GitHub Bot logged work on HIVE-23817: - Author: ASF GitHub Bot Created on: 08/Jul/20 14:23 Start Date: 08/Jul/20 14:23 Worklog Time Spent: 10m Work Description: zeroflag opened a new pull request #1228: URL: https://github.com/apache/hive/pull/1228 ## NOTICE (work in progress) ### Pushing the TopNKey operator through PK-FK inner joins. Example: Customer table: ID (PK) | LAST_NAME -- | -- 1 | Robinson 2 | Jones 3 | Smith 4 | Heisenberg Order table: CUSTOMER_ID (FK) | AMOUNT -- | -- 1 | 100 1 | 50 2 | 200 3 | 30 3 | 40 Requirements for doing TopN Key pushdown. * The PRIMARY KEY constraint on Customer.ID that forbids NULL and duplicate values. * The NOT_NULL constraint on Order.CUSTOMER_ID that forbids NULL values. * Plus the FOREIGN KEY constraint between Customer.ID and Order.CUSTOMER_ID ensures that exactly one row exists in the Customer table for any given row in the Order table. In general if the first n of the order by columns are coming from the child table (FK) then we can copy the TopNKey operator with the first n columns and put it before the join. If all columns are coming from the child table we can move the TopNKey operator without keeping the original. ``` SELECT * FROM Customer, Order WHERE Customer.ID = Order.CUSTOMER_ID ORDER BY Order.AMOUNT, [Order.*], [Customer.*] LIMIT 3; ``` Result: CUSTOMER.ID (PK) | CUSTOMER.LAST_NAME | ORDER.AMOUNT -- | -- | -- 3 | Smith | 30 3 | Smith | 40 1 | Robinson | 50 1 | Robinson | 100 2 | Jones | 200 Plan ``` Top N Key Operator sort order: + keys: ORDER.AMOUNT, [ORDER.*] top n: 3 Select Operator (Order) [...] Join [...] Top N Key Operator sort order: + keys: ORDER.AMOUNT, [ORDER.*], [Customer.*] top n: 3 ``` Implementation notes PkFk join information is extracted on the calcite side and it is attached (child table index & name) to the AST as a query hint. At the physical plan level we make use of this information to decide if we can push through the topn key operator. We also need to get the origins of the columns (in the order by) to see if they're coming from the child table. This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking --- Worklog Id: (was: 456166) Remaining Estimate: 0h Time Spent: 10m > Pushing TopN Key operator PKFK inner joins > -- > > Key: HIVE-23817 > URL: https://issues.apache.org/jira/browse/HIVE-23817 > Project: Hive > Issue Type: Improvement >Reporter: Attila Magyar >Assignee: Attila Magyar >Priority: Major > Time Spent: 10m > Remaining Estimate: 0h > > If there is primary key foreign key relationship between the tables we can > push the topnkey operator through the join. -- This message was sent by Atlassian Jira (v8.3.4#803005)