Skip to content

Commit e1b1f32

Browse files
committed
fix
1 parent ea4b257 commit e1b1f32

File tree

4 files changed

+89
-40
lines changed

4 files changed

+89
-40
lines changed

parser/registry/common.js

+46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { hashLabels, parseLabels } = require('../../common')
22
const { getPlg } = require('../../plugins/engine')
33
const Sql = require('@cloki/clickhouse-sql')
4+
const { DATABASE_NAME } = require('../../lib/utils')
45
const clusterName = require('../../common').clusterName
56
module.exports.dist = clusterName ? '_dist' : ''
67

@@ -413,3 +414,48 @@ module.exports.patchCol = (query, name, patcher) => {
413414
return col
414415
})
415416
}
417+
418+
module.exports.preJoinLabels = (token, query, dist) => {
419+
const from = query.getParam(module.exports.sharedParamNames.from)
420+
const to = query.getParam(module.exports.sharedParamNames.to)
421+
const sqlFrom = new Sql.Raw()
422+
sqlFrom.toString = () => {
423+
let fromNs = 0
424+
if (from.get()) {
425+
fromNs = from.get()
426+
}
427+
return `toDate(fromUnixTimestamp(intDiv(${fromNs}, 1000000000)))`
428+
}
429+
const sqlTo = new Sql.Raw()
430+
sqlTo.toString = () => {
431+
let toNs = 0
432+
if (to.get()) {
433+
toNs = to.get()
434+
}
435+
return `toDate(fromUnixTimestamp(intDiv(${toNs}, 1000000000)))`
436+
}
437+
let withIdxSel = query.with().idx_sel
438+
let inRightSide = new Sql.WithReference(withIdxSel)
439+
if (!withIdxSel) {
440+
withIdxSel = query.with().str_sel
441+
inRightSide = new Sql.Select()
442+
.select('fingerprint')
443+
.from(new Sql.WithReference(withIdxSel))
444+
}
445+
dist = dist || ''
446+
const timeSeriesReq = new Sql.Select()
447+
.select('fingerprint', 'labels')
448+
.from([`${DATABASE_NAME()}.time_series${dist}`, 'time_series'])
449+
.where(new Sql.And(
450+
new Sql.In('time_series.fingerprint', 'in', inRightSide),
451+
Sql.Gte(new Sql.Raw('date'), sqlFrom),
452+
Sql.Lte(new Sql.Raw('date'), sqlTo)
453+
))
454+
timeSeriesReq._toString = timeSeriesReq.toString
455+
timeSeriesReq.toString = () => {
456+
return `(${timeSeriesReq._toString()})`
457+
}
458+
query.join(new module.exports.Aliased(timeSeriesReq, 'time_series'), 'left any',
459+
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint')))
460+
query.select([new Sql.Raw('JSONExtractKeysAndValues(time_series.labels, \'String\')'), 'labels'])
461+
}

parser/registry/smart_optimizations/optimization_v3_2.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getDuration, Aliased } = require('../common')
1+
const { getDuration, preJoinLabels, dist } = require('../common')
22
const reg = require('./log_range_agg_reg_v3_2')
33
const Sql = require('@cloki/clickhouse-sql')
44
const { DATABASE_NAME, checkVersion } = require('../../../lib/utils')
@@ -51,15 +51,14 @@ module.exports.apply = (token, fromNS, toNS, stepNS) => {
5151
.select(['samples.fingerprint', 'fingerprint'])
5252
.from([`${DATABASE_NAME()}.metrics_15s${_dist}`, 'samples'])
5353
.where(tsClause)
54-
q.join(new Aliased(`${DATABASE_NAME()}.time_series`, 'time_series'), 'left any',
55-
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint')))
56-
q.select([new Sql.Raw('any(JSONExtractKeysAndValues(time_series.labels, \'String\'))'), 'labels'])
5754

5855
q.ctx = {
5956
step: stepNS / 1000000000,
6057
inline: !!clusterName
6158
}
6259

60+
preJoinLabels(token, q, dist)
61+
6362
for (const streamSelectorRule of token.Children('log_stream_selector_rule')) {
6463
q = streamSelectorReg[streamSelectorRule.Child('operator').value](streamSelectorRule, q)
6564
}

parser/transpiler.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const lineFormat = require('./registry/line_format')
1010
const parserRegistry = require('./registry/parser_registry')
1111
const unwrap = require('./registry/unwrap')
1212
const unwrapRegistry = require('./registry/unwrap_registry')
13-
const { durationToMs, sharedParamNames, getStream, Aliased } = require('./registry/common')
13+
const { durationToMs, sharedParamNames, getStream, preJoinLabels } = require('./registry/common')
1414
const compiler = require('./bnf')
1515
const {
1616
parseMs,
@@ -75,11 +75,6 @@ module.exports.initQuery = (joinLabels, types) => {
7575
.addParam(to)
7676
.addParam(limit)
7777
.addParam(matrix)
78-
if (joinLabels) {
79-
q.join(new Aliased(`${DATABASE_NAME()}.time_series`, 'time_series'), 'left any',
80-
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint')))
81-
q.select([new Sql.Raw('JSONExtractKeysAndValues(time_series.labels, \'String\')'), 'labels'])
82-
}
8378
return q
8479
}
8580

@@ -88,7 +83,7 @@ module.exports.initQuery = (joinLabels, types) => {
8883
* @param types {[number] || undefined}
8984
* @returns {Select}
9085
*/
91-
module.exports.initQueryV3_2 = (joinLabels, types) => {
86+
/*module.exports.initQueryV3_2 = (joinLabels, types) => {
9287
types = types || [bothType, logType]
9388
const from = new Sql.Parameter(sharedParamNames.from)
9489
const to = new Sql.Parameter(sharedParamNames.to)
@@ -108,12 +103,13 @@ module.exports.initQueryV3_2 = (joinLabels, types) => {
108103
.addParam(from)
109104
.addParam(to)
110105
if (joinLabels) {
106+
//TODO: fix join
111107
q.join(new Aliased(`${DATABASE_NAME()}.time_series${dist}`, 'time_series'), 'left any',
112108
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint')))
113109
q.select([new Sql.Raw('JSONExtractKeysAndValues(time_series.labels, \'String\')'), 'labels'])
114110
}
115111
return q
116-
}
112+
}*/
117113

118114
/**
119115
*
@@ -193,6 +189,8 @@ module.exports.transpile = (request) => {
193189
end
194190
}
195191
}
192+
joinLabels && doStreamSelectorOperatorRegistry(token, query)
193+
joinLabels && preJoinLabels(token, query)
196194
matrixOp = matrixOp || (token.Child('summary') && 'summary')
197195
switch (matrixOp) {
198196
case 'aggregation_operator':
@@ -223,10 +221,9 @@ module.exports.transpile = (request) => {
223221
.orderBy(['labels', order], ['timestamp_ns', order])
224222
setQueryParam(query, sharedParamNames.limit, limit)
225223
if (!joinLabels) {
226-
query.join(new Aliased(`${DATABASE_NAME()}.time_series${dist}`, 'time_series'), 'left any',
227-
Sql.Eq('sel_a.fingerprint', new Sql.Raw('time_series.fingerprint')))
228-
query.select([new Sql.Raw('JSONExtractKeysAndValues(time_series.labels, \'String\')'), 'labels'],
229-
new Sql.Raw('sel_a.*'))
224+
query.from([new Sql.WithReference(query.with().sel_a), 'samples'])
225+
preJoinLabels(token, query, dist)
226+
query.select(new Sql.Raw('samples.*'))
230227
}
231228
}
232229
if (token.Child('agg_statement') && token.Child('compared_agg_statement_cmp')) {
@@ -381,7 +378,9 @@ module.exports.transpileTail = (request) => {
381378
}
382379
}
383380

384-
let query = module.exports.initQuery(true)
381+
let query = module.exports.initQuery(false)
382+
doStreamSelectorOperatorRegistry(expression.rootToken, query)
383+
preJoinLabels(expression.rootToken, query, dist)
385384
query.ctx = {
386385
...(query.ctx || {}),
387386
legacy: true
@@ -393,7 +392,6 @@ module.exports.transpileTail = (request) => {
393392
query.order_expressions = []
394393
query.orderBy(['timestamp_ns', 'asc'])
395394
query.limit(undefined, undefined)
396-
//logger.debug(query.toString())
397395
return {
398396
query: request.rawRequest ? query : query.toString(),
399397
stream: getStream(query)
@@ -496,11 +494,7 @@ module.exports.transpileLogRangeAggregation = (token, query) => {
496494
* @returns {Sql.Select}
497495
*/
498496
module.exports.transpileLogStreamSelector = (token, query) => {
499-
const rules = token.Children('log_stream_selector_rule')
500-
for (const rule of rules) {
501-
const op = rule.Child('operator').value
502-
query = streamSelectorOperatorRegistry[op](rule, query)
503-
}
497+
doStreamSelectorOperatorRegistry(token, query)
504498
for (const pipeline of token.Children('log_pipeline')) {
505499
if (pipeline.Child('line_filter_expression')) {
506500
const op = pipeline.Child('line_filter_operator').value
@@ -619,3 +613,13 @@ const whereBuilder = (clause) => {
619613
const _clause = clause.slice(1).map(c => Array.isArray(c) ? `(${whereBuilder(c)})` : c)
620614
return _clause.join(` ${op} `)
621615
}
616+
617+
const doStreamSelectorOperatorRegistry = (token, query) => {
618+
if (!query.with().idx_sel && !query.with().str_sel) {
619+
const rules = token.Children('log_stream_selector_rule')
620+
for (const rule of rules) {
621+
const op = rule.Child('operator').value
622+
query = streamSelectorOperatorRegistry[op](rule, query)
623+
}
624+
}
625+
}

0 commit comments

Comments
 (0)