본문으로 바로가기
본문으로 바로가기

range_hashed 딕셔너리 레이아웃 유형

range_hashed

딕셔너리는 메모리 내에서 정렬된 범위 배열과 그에 해당하는 값들을 가진 해시 테이블 형태로 저장됩니다.

이 저장 방식은 hashed와 동일하게 동작하며, 키뿐만 아니라 날짜/시간(임의의 숫자 타입) 범위도 사용할 수 있습니다.

예: 테이블에는 각 광고주에 대한 할인 정보가 다음 형식으로 포함되어 있습니다.

┌─advertiser_id─┬─discount_start_date─┬─discount_end_date─┬─amount─┐
│           123 │          2015-01-16 │        2015-01-31 │   0.25 │
│           123 │          2015-01-01 │        2015-01-15 │   0.15 │
│           456 │          2015-01-01 │        2015-01-15 │   0.05 │
└───────────────┴─────────────────────┴───────────────────┴────────┘

날짜 범위용 샘플을 사용하려면 structure에서 range_minrange_max 요소를 정의합니다. 각 요소에는 nametype 요소가 포함되어야 합니다. type이 지정되지 않은 경우 기본 타입인 Date가 사용됩니다. type은 임의의 숫자 타입일 수 있습니다 (Date / DateTime / UInt64 / Int32 / 기타).

참고

range_minrange_max의 값은 Int64 타입 범위에 포함되어야 합니다.

예제:

CREATE DICTIONARY discounts_dict (
    advertiser_id UInt64,
    discount_start_date Date,
    discount_end_date Date,
    amount Float64
)
PRIMARY KEY id
SOURCE(CLICKHOUSE(TABLE 'discounts'))
LIFETIME(MIN 1 MAX 1000)
LAYOUT(RANGE_HASHED(range_lookup_strategy 'max'))
RANGE(MIN discount_start_date MAX discount_end_date)

이러한 딕셔너리를 사용하려면 범위 선택에 사용되는 추가 인수를 dictGet 함수에 전달해야 합니다:

dictGet('dict_name', 'attr_name', id, date)

쿼리 예제:

SELECT dictGet('discounts_dict', 'amount', 1, '2022-10-20'::Date);

이 FUNCTION은 지정된 id 값들에 대해, 전달된 날짜를 포함하는 날짜 범위에 대한 값을 반환합니다.

알고리즘 동작 방식:

  • id를 찾을 수 없거나 해당 id에 대한 범위를 찾을 수 없는 경우, 속성 타입의 기본값을 반환합니다.
  • 범위가 서로 겹치고 range_lookup_strategy=min인 경우, 일치하는 범위 중 range_min이 가장 작은 범위를 반환합니다. 여러 범위가 발견되면 그중 range_max가 가장 작은 범위를 반환하고, 다시 여러 범위가 발견되는 경우(여러 범위의 range_minrange_max가 동일한 경우) 이들 중 임의의 범위를 반환합니다.
  • 범위가 서로 겹치고 range_lookup_strategy=max인 경우, 일치하는 범위 중 range_min이 가장 큰 범위를 반환합니다. 여러 범위가 발견되면 그중 range_max가 가장 큰 범위를 반환하고, 다시 여러 범위가 발견되는 경우(여러 범위의 range_minrange_max가 동일한 경우) 이들 중 임의의 범위를 반환합니다.
  • range_maxNULL이면, 해당 범위는 열려 있는 범위입니다. NULL은 가능한 최대값으로 간주됩니다. range_min의 경우 열린 값으로 1970-01-01 또는 0(-MAX_INT)을 사용할 수 있습니다.

구성 예:

CREATE DICTIONARY somedict(
    Abcdef UInt64,
    StartTimeStamp UInt64,
    EndTimeStamp UInt64,
    XXXType String DEFAULT ''
)
PRIMARY KEY Abcdef
RANGE(MIN StartTimeStamp MAX EndTimeStamp)

겹치는 범위와 열린 범위를 포함한 구성 예시는 다음과 같습니다:

CREATE TABLE discounts
(
    advertiser_id UInt64,
    discount_start_date Date,
    discount_end_date Nullable(Date),
    amount Float64
)
ENGINE = Memory;

INSERT INTO discounts VALUES (1, '2015-01-01', Null, 0.1);
INSERT INTO discounts VALUES (1, '2015-01-15', Null, 0.2);
INSERT INTO discounts VALUES (2, '2015-01-01', '2015-01-15', 0.3);
INSERT INTO discounts VALUES (2, '2015-01-04', '2015-01-10', 0.4);
INSERT INTO discounts VALUES (3, '1970-01-01', '2015-01-15', 0.5);
INSERT INTO discounts VALUES (3, '1970-01-01', '2015-01-10', 0.6);

SELECT * FROM discounts ORDER BY advertiser_id, discount_start_date;
┌─advertiser_id─┬─discount_start_date─┬─discount_end_date─┬─amount─┐
│             1 │          2015-01-01 │              ᴺᵁᴸᴸ │    0.1 │
│             1 │          2015-01-15 │              ᴺᵁᴸᴸ │    0.2 │
│             2 │          2015-01-01 │        2015-01-15 │    0.3 │
│             2 │          2015-01-04 │        2015-01-10 │    0.4 │
│             3 │          1970-01-01 │        2015-01-15 │    0.5 │
│             3 │          1970-01-01 │        2015-01-10 │    0.6 │
└───────────────┴─────────────────────┴───────────────────┴────────┘

-- RANGE_LOOKUP_STRATEGY 'max'

CREATE DICTIONARY discounts_dict
(
    advertiser_id UInt64,
    discount_start_date Date,
    discount_end_date Nullable(Date),
    amount Float64
)
PRIMARY KEY advertiser_id
SOURCE(CLICKHOUSE(TABLE discounts))
LIFETIME(MIN 600 MAX 900)
LAYOUT(RANGE_HASHED(RANGE_LOOKUP_STRATEGY 'max'))
RANGE(MIN discount_start_date MAX discount_end_date);

select dictGet('discounts_dict', 'amount', 1, toDate('2015-01-14')) res;
┌─res─┐
│ 0.1 │ -- the only one range is matching: 2015-01-01 - Null
└─────┘

select dictGet('discounts_dict', 'amount', 1, toDate('2015-01-16')) res;
┌─res─┐
│ 0.2 │ -- two ranges are matching, range_min 2015-01-15 (0.2) is bigger than 2015-01-01 (0.1)
└─────┘

select dictGet('discounts_dict', 'amount', 2, toDate('2015-01-06')) res;
┌─res─┐
│ 0.4 │ -- two ranges are matching, range_min 2015-01-04 (0.4) is bigger than 2015-01-01 (0.3)
└─────┘

select dictGet('discounts_dict', 'amount', 3, toDate('2015-01-01')) res;
┌─res─┐
│ 0.5 │ -- two ranges are matching, range_min are equal, 2015-01-15 (0.5) is bigger than 2015-01-10 (0.6)
└─────┘

DROP DICTIONARY discounts_dict;

-- RANGE_LOOKUP_STRATEGY 'min'

CREATE DICTIONARY discounts_dict
(
    advertiser_id UInt64,
    discount_start_date Date,
    discount_end_date Nullable(Date),
    amount Float64
)
PRIMARY KEY advertiser_id
SOURCE(CLICKHOUSE(TABLE discounts))
LIFETIME(MIN 600 MAX 900)
LAYOUT(RANGE_HASHED(RANGE_LOOKUP_STRATEGY 'min'))
RANGE(MIN discount_start_date MAX discount_end_date);

select dictGet('discounts_dict', 'amount', 1, toDate('2015-01-14')) res;
┌─res─┐
│ 0.1 │ -- the only one range is matching: 2015-01-01 - Null
└─────┘

select dictGet('discounts_dict', 'amount', 1, toDate('2015-01-16')) res;
┌─res─┐
│ 0.1 │ -- two ranges are matching, range_min 2015-01-01 (0.1) is less than 2015-01-15 (0.2)
└─────┘

select dictGet('discounts_dict', 'amount', 2, toDate('2015-01-06')) res;
┌─res─┐
│ 0.3 │ -- two ranges are matching, range_min 2015-01-01 (0.3) is less than 2015-01-04 (0.4)
└─────┘

select dictGet('discounts_dict', 'amount', 3, toDate('2015-01-01')) res;
┌─res─┐
│ 0.6 │ -- two ranges are matching, range_min are equal, 2015-01-10 (0.6) is less than 2015-01-15 (0.5)
└─────┘

complex_key_range_hashed

딕셔너리는 메모리에서 정렬된 범위 배열과 각 범위에 대응하는 값들로 구성된 해시 테이블 형태로 저장됩니다(자세한 내용은 range_hashed 참고). 이 저장 방식은 복합 에 사용됩니다.

구성 예시:

CREATE DICTIONARY range_dictionary
(
  CountryID UInt64,
  CountryKey String,
  StartDate Date,
  EndDate Date,
  Tax Float64 DEFAULT 0.2
)
PRIMARY KEY CountryID, CountryKey
SOURCE(CLICKHOUSE(TABLE 'date_table'))
LIFETIME(MIN 1 MAX 1000)
LAYOUT(COMPLEX_KEY_RANGE_HASHED())
RANGE(MIN StartDate MAX EndDate);