aggregateWindow function conditionaly based on first element only, possible?

types.isNumeric is very slow. So filter metrics with that function before downsampling with an aggregateWindow function is not really an option. But because not every _value is a number I have to use types.isNumeric if I want to avoid to create a downsampling method naming all numeric values.

I had an idea to run types.isNumeric only once per chunk in aggregateWindow and decide only once how to handle all _values in this one chunk, theoretically this should work, because measurement is part of the group and don’t change type (at least in my situation).

Here is the not working script I have so far.

import "types"

from(bucket: "production")
    |> range(start: -30m)
    |> aggregateWindow(every: 10m,
    fn: (tables=<-, column) => 
    {
        first_element = tables |> first()       
        return if types.isNumeric(v: first_element._value) 
        then tables |> mean()
        else tables |> last()
        
    }
    , createEmpty: false)
    |> to(bucket: "production-archive")

And here are the errors I get

error @11:39-11:44: expected {A with _value: B} (record) but found stream[C]

I know that first_element is a stream here, and I want a record, but I don’t know how get a record out of a stream, and also don’t know if |> first() consumed already the first element or even the whole stream.

found getRecord() but now I don’t even understand the error :

import "types"

from(bucket: "production")
    |> range(start: -30m)
    |> aggregateWindow(every: 10m,
    fn: (tables=<-, column) => 
    {
        firstElement = getRecord(table: tables, idx: 0)
        return if types.isNumeric(v: firstElement._value) 
        then tables |> mean()
        else tables |> last()
        
    }
    , createEmpty: false)
error calling function "aggregateWindow" @5:8-17:6: error calling function "fn" @universe/universe.flux|3892:12-3892:30: error calling function "getRecord" @8:28-8:60: unexpected type for table: want {schema: [{grouped: bool, label: string, type: string}]}, got stream[t179]