-
Notifications
You must be signed in to change notification settings - Fork 2
Add ClickHouse limit_by and deduplicate dataset methods #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
19e8668
Add limit_by dataset method for ClickHouse
tycooon 1b3af69
Introspect ClickHouse table metadata from system.tables
tycooon e304a18
Add deduplicate dataset method replacing table-wide FINAL
tycooon b54bc22
Auto-disable FINAL for deduplicated datasets and forward count settings
tycooon 2d92322
Document dedup helpers and bump to 1.16.0
tycooon e5e1852
Address review: projection and ordering across the dedup boundary
tycooon 188b0e0
Ask the struct itself whether the engine is Replacing
tycooon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ GIT | |
| PATH | ||
| remote: . | ||
| specs: | ||
| umbrellio-utils (1.15.0) | ||
| umbrellio-utils (1.16.0) | ||
| memery (~> 1) | ||
|
|
||
| GEM | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module UmbrellioUtils | ||
| module ClickHouse | ||
| # Layout of a MergeTree-family table, read out of `system.tables`. | ||
| # | ||
| # ReplacingMergeTree collapses rows sharing the full sorting key, keeping | ||
| # the one with the highest version and dropping it entirely when the | ||
| # is_deleted column is set. `Dataset#deduplicate` reproduces that by hand, | ||
| # so it needs all three parts; `version` and `is_deleted` are only | ||
| # meaningful on a Replacing engine, and nil elsewhere. | ||
| TableMetadata = Struct.new(:engine, :sorting_key, :version, :is_deleted) | ||
|
|
||
| # Reopened rather than declared with a block: constants defined inside a | ||
| # `Struct.new do ... end` block leak to the enclosing lexical scope. | ||
| class TableMetadata | ||
| class UnknownTable < StandardError | ||
| end | ||
|
|
||
| REPLICATED_ARGS_COUNT = 2 # zookeeper path + replica name | ||
|
|
||
| def replacing? | ||
| engine.include?("Replacing") | ||
| end | ||
|
|
||
| class << self | ||
| # Built first so it can answer #replacing? for itself — the engine | ||
| # test lives in one place only. | ||
| def parse(engine:, engine_full:, sorting_key:) | ||
| meta = new(engine, split_args(sorting_key), nil, nil) | ||
| return meta unless meta.replacing? | ||
|
|
||
| args = engine_args(engine_full) | ||
| args = args.drop(REPLICATED_ARGS_COUNT) if engine.start_with?("Replicated") | ||
| meta.version, meta.is_deleted = args[0]&.to_sym, args[1]&.to_sym | ||
|
|
||
| meta | ||
| end | ||
|
|
||
| # Distributed('cluster', 'database', 'table'[, sharding_key]) | ||
| def distributed_target(engine_full) | ||
| _cluster, database, table = engine_args(engine_full) | ||
| [unquote(database), unquote(table)] | ||
| end | ||
|
|
||
| # Arguments of the leading engine call, or [] when the engine takes none. | ||
| # Only a parenthesis directly after the engine name counts — later | ||
| # clauses such as `PARTITION BY toYYYYMM(created_at)` must not be read. | ||
| def engine_args(engine_full) | ||
| open_index = engine_full.index("(") | ||
| return [] unless open_index | ||
| return [] unless engine_full[0...open_index].match?(/\A\w+\z/) | ||
|
|
||
| split_args(engine_full[(open_index + 1)...close_index(engine_full, open_index)]) | ||
| end | ||
|
|
||
| # Split on top-level commas only: sorting keys hold function calls and | ||
| # engine arguments hold quoted paths, both of which may contain commas. | ||
| def split_args(source) | ||
| args = [] | ||
| current = +"" | ||
|
|
||
| scan(source) do |char, depth, in_string| | ||
| if char == "," && depth.zero? && !in_string | ||
| args << current.strip | ||
| current = +"" | ||
| else | ||
| current << char | ||
| end | ||
| end | ||
|
|
||
| args << current.strip | ||
| args.reject(&:empty?) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Single lexer for both scans, so they can't disagree about what counts | ||
| # as a parenthesis: a ZooKeeper path may legally contain one inside | ||
| # quotes, and treating it as structure truncates the argument list. | ||
| def scan(source) | ||
| depth = 0 | ||
| in_string = false | ||
|
|
||
| source.to_s.each_char.with_index do |char, index| | ||
| case char | ||
| when "'" then in_string = !in_string | ||
| when "(" then depth += 1 unless in_string | ||
| when ")" then depth -= 1 unless in_string | ||
| end | ||
|
|
||
| yield(char, depth, in_string, index) | ||
| end | ||
| end | ||
|
|
||
| def close_index(source, open_index) | ||
|
tycooon marked this conversation as resolved.
|
||
| scan(source[open_index..]) do |_char, depth, in_string, index| | ||
| return open_index + index if depth.zero? && !in_string | ||
| end | ||
|
|
||
| raise ArgumentError, "unbalanced parentheses in engine: #{source}" | ||
| end | ||
|
|
||
| def unquote(value) | ||
| value.to_s.delete_prefix("'").delete_suffix("'") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.