| 463 | } |
| 464 | |
| 465 | MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( |
| 466 | const ASTSelectQuery & select, |
| 467 | NamesAndTypesList available_real_columns, |
| 468 | const MergeTreeMetaBase::DataPartsVector & parts, |
| 469 | KeyCondition & key_condition, |
| 470 | const MergeTreeMetaBase & data, |
| 471 | const StorageMetadataPtr & metadata_snapshot, |
| 472 | ContextPtr context, |
| 473 | bool sample_factor_column_queried, |
| 474 | Poco::Logger * log) |
| 475 | { |
| 476 | const Settings & settings = context->getSettingsRef(); |
| 477 | /// Sampling. |
| 478 | MergeTreeDataSelectSamplingData sampling; |
| 479 | |
| 480 | RelativeSize relative_sample_size = 0; |
| 481 | RelativeSize relative_sample_offset = 0; |
| 482 | |
| 483 | auto select_sample_size = select.sampleSize(); |
| 484 | auto select_sample_offset = select.sampleOffset(); |
| 485 | |
| 486 | if (select_sample_size) |
| 487 | { |
| 488 | relative_sample_size.assign( |
| 489 | select_sample_size->as<ASTSampleRatio &>().ratio.numerator, |
| 490 | select_sample_size->as<ASTSampleRatio &>().ratio.denominator); |
| 491 | |
| 492 | if (relative_sample_size < 0) |
| 493 | throw Exception("Negative sample size", ErrorCodes::ARGUMENT_OUT_OF_BOUND); |
| 494 | |
| 495 | relative_sample_offset = 0; |
| 496 | if (select_sample_offset) |
| 497 | relative_sample_offset.assign( |
| 498 | select_sample_offset->as<ASTSampleRatio &>().ratio.numerator, |
| 499 | select_sample_offset->as<ASTSampleRatio &>().ratio.denominator); |
| 500 | |
| 501 | if (relative_sample_offset < 0) |
| 502 | throw Exception("Negative sample offset", ErrorCodes::ARGUMENT_OUT_OF_BOUND); |
| 503 | |
| 504 | /// Convert absolute value of the sampling (in form `SAMPLE 1000000` - how many rows to |
| 505 | /// read) into the relative `SAMPLE 0.1` (how much data to read). |
| 506 | size_t approx_total_rows = 0; |
| 507 | if (relative_sample_size > 1 || relative_sample_offset > 1) |
| 508 | approx_total_rows = getApproximateTotalRowsToRead(parts, metadata_snapshot, key_condition, settings, log); |
| 509 | |
| 510 | if (relative_sample_size > 1) |
| 511 | { |
| 512 | relative_sample_size = convertAbsoluteSampleSizeToRelative(select_sample_size, approx_total_rows); |
| 513 | LOG_DEBUG(log, "Selected relative sample size: {}", toString(relative_sample_size)); |
| 514 | } |
| 515 | |
| 516 | /// SAMPLE 1 is the same as the absence of SAMPLE. |
| 517 | if (relative_sample_size == RelativeSize(1)) |
| 518 | relative_sample_size = 0; |
| 519 | |
| 520 | if (relative_sample_offset > 0 && RelativeSize(0) == relative_sample_size) |
| 521 | throw Exception("Sampling offset is incorrect because no sampling", ErrorCodes::ARGUMENT_OUT_OF_BOUND); |
| 522 |
nothing calls this directly
no test coverage detected