This commit is contained in:
Aliaksandr Valialkin 2024-05-30 11:33:36 +02:00
parent 30731f3032
commit 1790caa082
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
8 changed files with 46 additions and 4 deletions

View file

@ -28,6 +28,10 @@ func (fs fieldsSet) clone() fieldsSet {
return fsNew return fsNew
} }
func (fs fieldsSet) isEmpty() bool {
return len(fs) == 0
}
func (fs fieldsSet) getAll() []string { func (fs fieldsSet) getAll() []string {
a := make([]string, 0, len(fs)) a := make([]string, 0, len(fs))
for f := range fs { for f := range fs {

View file

@ -1542,7 +1542,6 @@ func TestQueryGetNeededColumns(t *testing.T) {
f(`* | fields f1, f2 | mv f1 f3, f4 f5`, `f1,f2`, ``) f(`* | fields f1, f2 | mv f1 f3, f4 f5`, `f1,f2`, ``)
f(`* | fields f1, f2 | mv f2 f3, f4 f5`, `f1,f2`, ``) f(`* | fields f1, f2 | mv f2 f3, f4 f5`, `f1,f2`, ``)
f(`* | fields f1, f2 | mv f2 f3, f4 f1`, `f2`, ``) f(`* | fields f1, f2 | mv f2 f3, f4 f1`, `f2`, ``)
f(`* | fields f1, f2 | stats count() r1`, ``, ``)
f(`* | fields f1, f2 | stats count_uniq() r1`, `f1,f2`, ``) f(`* | fields f1, f2 | stats count_uniq() r1`, `f1,f2`, ``)
f(`* | fields f1, f2 | stats count(f1) r1`, `f1`, ``) f(`* | fields f1, f2 | stats count(f1) r1`, `f1`, ``)
f(`* | fields f1, f2 | stats count(f1,f2,f3) r1`, `f1,f2`, ``) f(`* | fields f1, f2 | stats count(f1,f2,f3) r1`, `f1,f2`, ``)
@ -1776,11 +1775,21 @@ func TestQueryGetNeededColumns(t *testing.T) {
f(`* | rm f1, f2 | mv f2 f3 | sort by(f1)`, `*`, `f1,f2,f3`) f(`* | rm f1, f2 | mv f2 f3 | sort by(f1)`, `*`, `f1,f2,f3`)
f(`* | rm f1, f2 | fields f3`, `f3`, ``) f(`* | rm f1, f2 | fields f3`, `f3`, ``)
f(`* | rm f1, f2 | fields f1,f3`, `f3`, ``) f(`* | rm f1, f2 | fields f1,f3`, `f3`, ``)
f(`* | rm f1, f2 | stats count() f1`, ``, ``)
f(`* | rm f1, f2 | stats count(f3) r1`, `f3`, ``) f(`* | rm f1, f2 | stats count(f3) r1`, `f3`, ``)
f(`* | rm f1, f2 | stats count(f1) r1`, ``, ``) f(`* | rm f1, f2 | stats count(f1) r1`, ``, ``)
f(`* | rm f1, f2 | stats count(f1,f3) r1`, `f3`, ``) f(`* | rm f1, f2 | stats count(f1,f3) r1`, `f3`, ``)
f(`* | rm f1, f2 | stats by(f1) count(f2) r1`, ``, ``) f(`* | rm f1, f2 | stats by(f1) count(f2) r1`, ``, ``)
f(`* | rm f1, f2 | stats by(f3) count(f2) r1`, `f3`, ``) f(`* | rm f1, f2 | stats by(f3) count(f2) r1`, `f3`, ``)
f(`* | rm f1, f2 | stats by(f3) count(f4) r1`, `f3,f4`, ``) f(`* | rm f1, f2 | stats by(f3) count(f4) r1`, `f3,f4`, ``)
// Verify that fields are correctly tracked before count(*)
f(`* | copy a b, c d | count() r1`, `a`, ``)
f(`* | delete a, b | count() r1`, `*`, `a`)
f(`* | extract "<f1>bar" from x | count() r1`, `x`, ``)
f(`* | extract_regexp "(?P<f1>.*)bar" from x | count() r1`, `x`, ``)
f(`* | field_names | count() r1`, `*`, `_time`)
f(`* | limit 10 | field_names as abc | count() r1`, `*`, ``)
f(`* | fields a, b | count() r1`, `a`, ``)
f(`* | field_values a | count() r1`, `a`, ``)
f(`* | limit 10 | filter a:b c:d | count() r1`, `a,c`, ``)
} }

View file

@ -32,6 +32,11 @@ func (pc *pipeCopy) String() string {
} }
func (pc *pipeCopy) updateNeededFields(neededFields, unneededFields fieldsSet) { func (pc *pipeCopy) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.isEmpty() {
neededFields.add(pc.srcFields[0])
return
}
for i := len(pc.srcFields) - 1; i >= 0; i-- { for i := len(pc.srcFields) - 1; i >= 0; i-- {
srcField := pc.srcFields[i] srcField := pc.srcFields[i]
dstField := pc.dstFields[i] dstField := pc.dstFields[i]

View file

@ -23,11 +23,15 @@ func (pd *pipeDelete) String() string {
} }
func (pd *pipeDelete) updateNeededFields(neededFields, unneededFields fieldsSet) { func (pd *pipeDelete) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.isEmpty() {
neededFields.add("*")
unneededFields.add(pd.fields[0])
return
}
if neededFields.contains("*") { if neededFields.contains("*") {
// update only unneeded fields
unneededFields.addFields(pd.fields) unneededFields.addFields(pd.fields)
} else { } else {
// update only needed fields
neededFields.removeFields(pd.fields) neededFields.removeFields(pd.fields)
} }
} }

View file

@ -60,6 +60,11 @@ func (pe *pipeExtract) initFilterInValues(cache map[string][]string, getFieldVal
} }
func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) { func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.isEmpty() {
neededFields.add(pe.fromField)
return
}
if neededFields.contains("*") { if neededFields.contains("*") {
unneededFieldsOrig := unneededFields.clone() unneededFieldsOrig := unneededFields.clone()
needFromField := false needFromField := false

View file

@ -62,6 +62,11 @@ func (pe *pipeExtractRegexp) initFilterInValues(cache map[string][]string, getFi
} }
func (pe *pipeExtractRegexp) updateNeededFields(neededFields, unneededFields fieldsSet) { func (pe *pipeExtractRegexp) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.isEmpty() {
neededFields.add(pe.fromField)
return
}
if neededFields.contains("*") { if neededFields.contains("*") {
unneededFieldsOrig := unneededFields.clone() unneededFieldsOrig := unneededFields.clone()
needFromField := false needFromField := false

View file

@ -22,6 +22,11 @@ func (pf *pipeFieldValues) String() string {
} }
func (pf *pipeFieldValues) updateNeededFields(neededFields, unneededFields fieldsSet) { func (pf *pipeFieldValues) updateNeededFields(neededFields, unneededFields fieldsSet) {
if neededFields.isEmpty() {
neededFields.add(pf.field)
return
}
if neededFields.contains("*") { if neededFields.contains("*") {
neededFields.reset() neededFields.reset()
if !unneededFields.contains(pf.field) { if !unneededFields.contains(pf.field) {

View file

@ -29,6 +29,11 @@ func (pf *pipeFields) updateNeededFields(neededFields, unneededFields fieldsSet)
if pf.containsStar { if pf.containsStar {
return return
} }
if neededFields.isEmpty() {
neededFields.add(pf.fields[0])
return
}
if neededFields.contains("*") { if neededFields.contains("*") {
// subtract unneeded fields from pf.fields // subtract unneeded fields from pf.fields
neededFields.reset() neededFields.reset()