Skip to content
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

feat: drop support #453

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parser/logql.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ line_filter_operator ::= <line_filter_operator_registry>
parser_expression ::= "|" <OWSP> <parser_fn_name> <OWSP> <opt_parameters>
parser_fn_name ::= <parser_registry>
opt_parameters ::= <parameter> <OWSP> *("," <OWSP> <parameter>) | <BLANK>
parameter ::= <label> <OWSP> "=" <OWSP> <quoted_str> | <quoted_str>
parameter ::= <label> <OWSP> "=" <OWSP> <quoted_str> | <quoted_str> | <label>

label_filter_pipeline ::= "|" <OWSP> <complex_label_filter_expression>
complex_label_filter_expression ::= (<label_filter_expression> | <bracketed_label_filter_expression>) *(<OWSP> <and_or> <WSP> <OWSP> <label_filter_expression> | <OWSP> <and_or> <WSP> <OWSP> <bracketed_label_filter_expression>)
Expand Down
15 changes: 15 additions & 0 deletions parser/registry/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,18 @@ module.exports.Aliased = class {
return `${Sql.quoteTerm(this.name)} AS ${this.alias}`
}
}

/**
* @param query {Select}
* @param name {string}
* @param patcher {function(Object): Object}
*/
module.exports.patchCol = (query, name, patcher) => {
query.select_list = query.select().map(col => {
const _name = Array.isArray(col) ? col[1] : col
if (_name === name) {
return patcher(col[0])
}
return col
})
}
47 changes: 47 additions & 0 deletions parser/registry/parser_registry/drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { hasExtraLabels, patchCol, addStream } = require('../common')
const Sql = require('@cloki/clickhouse-sql')
/**
* @param token {Token}
* @param query {Select}
* @returns {Select}
*/
const viaClickhouseQuery = (token, query) => {
const labelsToDrop = token.Children('label').map(l => l.value)
const colsToPatch = ['labels']
if (hasExtraLabels(query)) {
colsToPatch.push('extra_labels')
}
for (const colName of colsToPatch) {
patchCol(query, colName, (col) => {
const colVal = new Sql.Raw('')
colVal.toString = () =>
`arrayFilter(x -> x.1 NOT IN (${labelsToDrop.map(Sql.quoteVal).join(',')}), ${col})`
return [colVal, colName]
})
}
return query
}

/**
* @param token {Token}
* @param query {Select}
* @returns {Select}
*/
const viaStream = (token, query) => {
const labelsToDrop = token.Children('label').map(l => l.value)
addStream(query, (s) => s.map(e => {
if (!e.labels) {
return e
}
for (const l of labelsToDrop) {
delete e.labels[l]
}
return e
}))
return query
}

module.exports = {
viaClickhouseQuery,
viaStream
}
15 changes: 15 additions & 0 deletions parser/registry/parser_registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const json = require('./json')
const re = require('./regexp')
const { hasExtraLabels, getPlugins, isEOF, hasStream, addStream } = require('../common')
const logfmt = require('./logfmt')
const drop = require('./drop')
const logger = require('../../../lib/logger')

module.exports = {
Expand Down Expand Up @@ -46,6 +47,20 @@ module.exports = {
return re.viaStream(token, query)
}
},

/**
*
* @param token {Token}
* @param query {Select}
* @returns {Select}
*/
drop: (token, query) => {
if (hasStream(query)) {
return drop.viaStream(token, query)
}
return drop.viaClickhouseQuery(token, query)
},

...getPlugins('parser_registry', (plugin) => {
if (plugin.map) {
return (token, query) => {
Expand Down
Loading