@@ -10,7 +10,7 @@ const lineFormat = require('./registry/line_format')
10
10
const parserRegistry = require ( './registry/parser_registry' )
11
11
const unwrap = require ( './registry/unwrap' )
12
12
const unwrapRegistry = require ( './registry/unwrap_registry' )
13
- const { durationToMs, sharedParamNames, getStream, Aliased } = require ( './registry/common' )
13
+ const { durationToMs, sharedParamNames, getStream, preJoinLabels } = require ( './registry/common' )
14
14
const compiler = require ( './bnf' )
15
15
const {
16
16
parseMs,
@@ -75,11 +75,6 @@ module.exports.initQuery = (joinLabels, types) => {
75
75
. addParam ( to )
76
76
. addParam ( limit )
77
77
. 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
- }
83
78
return q
84
79
}
85
80
@@ -88,7 +83,7 @@ module.exports.initQuery = (joinLabels, types) => {
88
83
* @param types {[number] || undefined}
89
84
* @returns {Select }
90
85
*/
91
- module . exports . initQueryV3_2 = ( joinLabels , types ) => {
86
+ /* module.exports.initQueryV3_2 = (joinLabels, types) => {
92
87
types = types || [bothType, logType]
93
88
const from = new Sql.Parameter(sharedParamNames.from)
94
89
const to = new Sql.Parameter(sharedParamNames.to)
@@ -108,12 +103,13 @@ module.exports.initQueryV3_2 = (joinLabels, types) => {
108
103
.addParam(from)
109
104
.addParam(to)
110
105
if (joinLabels) {
106
+ //TODO: fix join
111
107
q.join(new Aliased(`${DATABASE_NAME()}.time_series${dist}`, 'time_series'), 'left any',
112
108
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint')))
113
109
q.select([new Sql.Raw('JSONExtractKeysAndValues(time_series.labels, \'String\')'), 'labels'])
114
110
}
115
111
return q
116
- }
112
+ }*/
117
113
118
114
/**
119
115
*
@@ -193,6 +189,8 @@ module.exports.transpile = (request) => {
193
189
end
194
190
}
195
191
}
192
+ joinLabels && doStreamSelectorOperatorRegistry ( token , query )
193
+ joinLabels && preJoinLabels ( token , query )
196
194
matrixOp = matrixOp || ( token . Child ( 'summary' ) && 'summary' )
197
195
switch ( matrixOp ) {
198
196
case 'aggregation_operator' :
@@ -223,10 +221,9 @@ module.exports.transpile = (request) => {
223
221
. orderBy ( [ 'labels' , order ] , [ 'timestamp_ns' , order ] )
224
222
setQueryParam ( query , sharedParamNames . limit , limit )
225
223
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.*' ) )
230
227
}
231
228
}
232
229
if ( token . Child ( 'agg_statement' ) && token . Child ( 'compared_agg_statement_cmp' ) ) {
@@ -381,7 +378,9 @@ module.exports.transpileTail = (request) => {
381
378
}
382
379
}
383
380
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 )
385
384
query . ctx = {
386
385
...( query . ctx || { } ) ,
387
386
legacy : true
@@ -393,7 +392,6 @@ module.exports.transpileTail = (request) => {
393
392
query . order_expressions = [ ]
394
393
query . orderBy ( [ 'timestamp_ns' , 'asc' ] )
395
394
query . limit ( undefined , undefined )
396
- //logger.debug(query.toString())
397
395
return {
398
396
query : request . rawRequest ? query : query . toString ( ) ,
399
397
stream : getStream ( query )
@@ -496,11 +494,7 @@ module.exports.transpileLogRangeAggregation = (token, query) => {
496
494
* @returns {Sql.Select }
497
495
*/
498
496
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 )
504
498
for ( const pipeline of token . Children ( 'log_pipeline' ) ) {
505
499
if ( pipeline . Child ( 'line_filter_expression' ) ) {
506
500
const op = pipeline . Child ( 'line_filter_operator' ) . value
@@ -619,3 +613,13 @@ const whereBuilder = (clause) => {
619
613
const _clause = clause . slice ( 1 ) . map ( c => Array . isArray ( c ) ? `(${ whereBuilder ( c ) } )` : c )
620
614
return _clause . join ( ` ${ op } ` )
621
615
}
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