<commit_before>drop type if exists pgmapcss_selector_return cascade;
create type pgmapcss_selector_return as (
  conditions            text[], /* conditional expressions */
  pseudo_classes        text[], /* :closed, ... */
  layer                 text,
  text_length           int
);

drop function pgmapcss_parse_selector(text);
create or replace function pgmapcss_parse_selector (
  text
)
returns setof pgmapcss_selector_return
as $$
#variable_conflict use_variable
declare
  ret pgmapcss_selector_return;
  m text;
  selector text;
begin
  selector:=$1;
  ret.conditions=Array[]::text[];
  ret.pseudo_classes=Array[]::text[];

  -- parse object class (way, node, canvas, ...)
  m := substring(selector from '^\s*(\*|node|way|relation|area|meta|canvas)(\|.*|\[.*|:.*|\s)');
  if m is not null then
    ret.conditions=array_append(ret.conditions, ''''||m||'''=ANY(type)');
  else
    raise notice 'can''t parse object class at "%..."', substring(selector, 0, 40);
  end if;
  selector := substring(selector from '^\s*(?:\*|node|way|relation|area|meta|canvas)(\|.*|\[.*|:.*|\s)');

  -- parse zoom level
  m := substring(selector from '^\|z([0-9\-]+)(\[.*|:.*|\s)');
  if m is not null then
    ret.conditions=array_append(ret.conditions, 'zoom = '||m);
    selector := substring(selector from '^\|(?:z[0-9\-]+)(\[.*|:.*|\s)');
  end if;<commit_msg>pgmapcss_parse_selector(): select for zoom level<commit_after>drop type if exists pgmapcss_selector_return cascade;
create type pgmapcss_selector_return as (
  conditions            text[], /* conditional expressions */
  pseudo_classes        text[], /* :closed, ... */
  layer                 text,
  text_length           int
);

drop function pgmapcss_parse_selector(text);
create or replace function pgmapcss_parse_selector (
  text
)
returns setof pgmapcss_selector_return
as $$
#variable_conflict use_variable
declare
  ret pgmapcss_selector_return;
  m text;
  m1 text;
  t text;
  selector text;
  max_scale_denominator float := 3.93216e+08;
begin
  selector:=$1;
  ret.conditions=Array[]::text[];
  ret.pseudo_classes=Array[]::text[];

  -- parse object class (way, node, canvas, ...)
  m := substring(selector from '^\s*(\*|node|way|relation|area|meta|canvas)(\|.*|\[.*|:.*|\s)');
  if m is not null then
    ret.conditions=array_append(ret.conditions, ''''||m||'''=ANY(type)');
  else
    raise notice 'can''t parse object class at "%..."', substring(selector, 0, 40);
  end if;
  selector := substring(selector from '^\s*(?:\*|node|way|relation|area|meta|canvas)(\|.*|\[.*|:.*|\s)');

  -- parse zoom level
  m := substring(selector from '^\|z([0-9\-]+)(\[.*|:.*|\s)');
  if m is not null then
    -- zN
    m1 := substring(m from '^([0-9]+)$');
    if m1 is not null then
      ret.conditions=array_append(ret.conditions,
        'scale_denominator >= ' ||
        cast((max_scale_denominator / 2 ^ cast(m as int)) as text) ||
        ' and scale_denominator < ' ||
        cast((max_scale_denominator / 2 ^ (cast(m as int) - 1)) as text)
      );
    end if;

    -- zN-
    m1 := substring(m from '^([0-9]+)\-$');
    if m1 is not null then
      ret.conditions=array_append(ret.conditions,
        'scale_denominator < ' ||
        cast((max_scale_denominator / 2 ^ (cast(m1 as int) - 1)) as text)
      );
    end if;

    -- z-N
    m1 := substring(m from '^\-([0-9]+)$');
    if m1 is not null then
      ret.conditions=array_append(ret.conditions,
        'scale_denominator >= ' ||
        cast((max_scale_denominator / 2 ^ cast(m1 as int)) as text)
      );
    end if;

    -- zN-N
    m1 := substring(m from '^(?:[0-9]+)\-([0-9]+)$');
    if m1 is not null then
      t=
        'scale_denominator >= ' ||
        cast((max_scale_denominator / 2 ^ cast(m1 as int)) as text);

      m1 := substring(m from '^([0-9]+)\-(?:[0-9]+)$');
      t=t||
        ' and scale_denominator < ' ||
        cast((max_scale_denominator / 2 ^ (cast(m1 as int) - 1)) as text);

      ret.conditions=array_append(ret.conditions, t);
    end if;

    selector := substring(selector from '^\|(?:z[0-9\-]+)(\[.*|:.*|\s)');
  end if;
{'fraction_non_alphanumeric': 0.18653610771113832, 'fraction_numerical': 0.014932680538555692, 'mean_word_length': 3.05759682224429, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1524698', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1409, 'n_words': 331}