Add UDFs to generate regular expressions to correctly match event properties in `events_daily` tables (#4272)

* Add `event_analysis.event_property_value_to_match_string` UDF.

* Add `event_analysis.event_property_index_to_match_string` UDF.
This commit is contained in:
Sean Rose 2023-12-14 18:25:41 -08:00 коммит произвёл GitHub
Родитель 16344266a6
Коммит 23e577af7f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 86 добавлений и 0 удалений

Просмотреть файл

Просмотреть файл

@ -0,0 +1,6 @@
---
description: >
Given an event index and property index from an `event_types` table, returns a regular expression
to match corresponding events within an `events_daily` table's `events` string that aren't missing
the specified property.
friendly_name: Event Analysis Event Property Index To Match String

Просмотреть файл

@ -0,0 +1,34 @@
CREATE OR REPLACE FUNCTION event_analysis.event_property_index_to_match_string(
event_index STRING,
property_index INTEGER
)
RETURNS STRING AS (
CONCAT(
-- Double-quote characters are used in `events_daily.events` strings to indicate missing properties.
'[^",]',
-- Event property values are stored in `events_daily.events` strings in reverse order,
-- so we expect the Nth property value to be followed by N-1 other property values.
IF(property_index > 1, CONCAT('[^,]{', (property_index - 1), '}'), ''),
event_analysis.event_index_to_match_string(event_index)
)
);
SELECT
assert.equals(r'[^",]\Qe\E,', event_analysis.event_property_index_to_match_string('e', 1)),
assert.equals(r'[^",][^,]{1}\Qe\E,', event_analysis.event_property_index_to_match_string('e', 2)),
assert.equals(
'pe,',
REGEXP_EXTRACT('""pe,', event_analysis.event_property_index_to_match_string('e', 1))
),
assert.equals(
CAST(NULL AS STRING),
REGEXP_EXTRACT('""pe,', event_analysis.event_property_index_to_match_string('e', 2))
),
assert.equals(
CAST(NULL AS STRING),
REGEXP_EXTRACT('"p"e,', event_analysis.event_property_index_to_match_string('e', 1))
),
assert.equals(
'p"e,',
REGEXP_EXTRACT('"p"e,', event_analysis.event_property_index_to_match_string('e', 2))
),

Просмотреть файл

Просмотреть файл

@ -0,0 +1,5 @@
---
description: >
Given an event index, property index, and property value from an `event_types` table, returns a
regular expression to match corresponding events within an `events_daily` table's `events` string.
friendly_name: Event Analysis Event Property Value To Match String

Просмотреть файл

@ -0,0 +1,41 @@
CREATE OR REPLACE FUNCTION event_analysis.event_property_value_to_match_string(
event_index STRING,
property_index INTEGER,
property_value STRING
)
RETURNS STRING AS (
CONCAT(
event_analysis.escape_metachars(property_value),
-- Event property values are stored in `events_daily.events` strings in reverse order,
-- so we expect the Nth property value to be followed by N-1 other property values.
IF(property_index > 1, CONCAT('[^,]{', (property_index - 1), '}'), ''),
event_analysis.event_index_to_match_string(event_index)
)
);
SELECT
assert.equals(r'\Qp\E\Qe\E,', event_analysis.event_property_value_to_match_string('e', 1, 'p')),
assert.equals(
r'\Qp\E[^,]{1}\Qe\E,',
event_analysis.event_property_value_to_match_string('e', 2, 'p')
),
assert.equals(
CAST(NULL AS STRING),
event_analysis.event_property_value_to_match_string('e', 1, NULL)
),
assert.equals(
'pe,',
REGEXP_EXTRACT('""pe,', event_analysis.event_property_value_to_match_string('e', 1, 'p'))
),
assert.equals(
CAST(NULL AS STRING),
REGEXP_EXTRACT('""pe,', event_analysis.event_property_value_to_match_string('e', 2, 'p'))
),
assert.equals(
CAST(NULL AS STRING),
REGEXP_EXTRACT('"p"e,', event_analysis.event_property_value_to_match_string('e', 1, 'p'))
),
assert.equals(
'p"e,',
REGEXP_EXTRACT('"p"e,', event_analysis.event_property_value_to_match_string('e', 2, 'p'))
),