Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner,expression: use constraint propagation in partition pruning #8885

Merged
merged 26 commits into from
Jan 17, 2019

Conversation

tiancaiamao
Copy link
Contributor

@tiancaiamao tiancaiamao commented Dec 29, 2018

What problem does this PR solve?

Fix #7516

Now we can prune some of the to_days function:

CREATE TABLE t1 (
a int(10) unsigned NOT NULL,
b DATETIME NOT NULL,
PRIMARY KEY (a, b)
) PARTITION BY RANGE (TO_DAYS(b))
(PARTITION p20090401 VALUES LESS THAN (TO_DAYS('2009-04-02')),
PARTITION p20090402 VALUES LESS THAN (TO_DAYS('2009-04-03')),
PARTITION p20090403 VALUES LESS THAN (TO_DAYS('2009-04-04')),
PARTITION p20090404 VALUES LESS THAN (TO_DAYS('2009-04-05')),
PARTITION p20090405 VALUES LESS THAN MAXVALUE);

The partition expression is TO_DAYS(b) < XXX and TO_DAYS(b) >= YYY, it has a function to_days.

EXPLAIN SELECT * FROM t1 WHERE b <= CAST('2009-04-03' AS DATE);
id	count	task	operator info
Union_9	9970.00	root	
├─TableReader_12	3323.33	root	data:Selection_11
│ └─Selection_11	3323.33	cop	le(test.t1.b, 2009-04-03)
│   └─TableScan_10	10000.00	cop	table:t1, partition:p20090401, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_15	3323.33	root	data:Selection_14
│ └─Selection_14	3323.33	cop	le(test.t1.b, 2009-04-03)
│   └─TableScan_13	10000.00	cop	table:t1, partition:p20090402, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableReader_18	3323.33	root	data:Selection_17
  └─Selection_17	3323.33	cop	le(test.t1.b, 2009-04-03)
    └─TableScan_16	10000.00	cop	table:t1, partition:p20090403, range:[-inf,+inf], keep order:false, stats:pseudo

From the result we can see that p20090404, p20090405 is pruned.

What is changed and how it works?

Fix those two restrictions mentioned in the issue:

  1. it doesn't support filter conditions that can't be push down to datasource
  2. it doesn't support expressions that can't calculate range

Now prune will consider both push down conditions and the filter conditions in the selection.
Leverage the constraint propagate rule in #8640
to handle expressions (function) that can't calculate range.

There are still two minor problems, but they are not introduced by this commit:

  1. The partition expressions for the first partition is col < const or col is null, current partition pruning can't prune or col is null
  2. Range calculating is still kept in the code, we should get rid of it eventually

Check List

Tests

  • Unit test
  • Integration test

This change is Reviewable

@zz-jason
Copy link
Member

zz-jason commented Jan 2, 2019

@tiancaiamao Please add some proper labels. For example, component, type.

@tiancaiamao
Copy link
Contributor Author

This is still WIP, and the changes are based on #8640
@zz-jason

@codecov-io
Copy link

codecov-io commented Jan 4, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@59c7b69). Click here to learn what that means.
The diff coverage is 72.72%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #8885   +/-   ##
=========================================
  Coverage          ?   67.14%           
=========================================
  Files             ?      372           
  Lines             ?    76968           
  Branches          ?        0           
=========================================
  Hits              ?    51679           
  Misses            ?    20657           
  Partials          ?     4632
Impacted Files Coverage Δ
planner/core/logical_plans.go 73.72% <ø> (ø)
planner/core/rule_predicate_push_down.go 90.63% <100%> (ø)
expression/constraint_propagation.go 73.45% <54.54%> (ø)
planner/core/rule_partition_processor.go 74.02% <80.95%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 59c7b69...89e597f. Read the comment docs.

@tiancaiamao
Copy link
Contributor Author

PTAL @eurekaka @winoros @zz-jason

@tiancaiamao
Copy link
Contributor Author

PTAL @eurekaka @winoros @zz-jason

expression/constraint_propagation.go Show resolved Hide resolved
planner/core/planbuilder_test.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
@tiancaiamao
Copy link
Contributor Author

PTAL @eurekaka @zz-jason

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eurekaka eurekaka added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 8, 2019
expression/constraint_propagation.go Show resolved Hide resolved
planner/core/logical_plans.go Outdated Show resolved Hide resolved
planner/core/planbuilder_test.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
}
}

if partCol == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check partCol == nil first when entering this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean check partCol == nil at the beginning of this function and return false? No we can't do that.

In to_days(c) > xx and c < yy, partCol is nil, but I want to handle it.
If this branch moved to the beginning, the canBePruned will return immediately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Could you add a comment about this to let others know why we intentionally put this check after solver.Solve()?

planner/core/rule_partition_processor.go Show resolved Hide resolved
planner/core/rule_partition_processor.go Outdated Show resolved Hide resolved
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason zz-jason requested a review from winoros January 11, 2019 08:29
@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

/run-unit-test

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

PTAL @winoros @zz-jason

@tiancaiamao
Copy link
Contributor Author

PTAL @winoros

@tiancaiamao
Copy link
Contributor Author

PTAL @winoros @zz-jason

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao tiancaiamao merged commit b339c02 into pingcap:master Jan 17, 2019
@tiancaiamao tiancaiamao deleted the re-partition-prune branch January 17, 2019 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression sig/planner SIG: Planner status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants