// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Silei Xu <silei@cs.stanford.edu>

import { Ast, Type } from 'thingtalk';
import * as C from '../ast_manip';
import ThingpediaLoader from '../load-thingpedia';
import type * as $Genie from "genie-toolkit";
export default async function($runtime : typeof $Genie.SentenceGeneratorRuntime, $ttUtils : typeof $Genie.ThingTalkUtils, $options : $Genie.SentenceGeneratorTypes.GrammarOptions, $locale : $Genie.I18n.LanguagePack, $grammar : $Genie.SentenceGenerator<any, any>, $loader : ThingpediaLoader) : Promise<void> {

$loader = new ThingpediaLoader($runtime, $ttUtils, $grammar, $locale, $options);
   await (await $runtime.import("../common.genie", __dirname))($runtime, $ttUtils, $options, $locale, $grammar, $loader);

await $loader.init();
   await (await $runtime.import("./constants.genie", __dirname))($runtime, $ttUtils, $options, $locale, $grammar, $loader);
   $grammar.declareSymbol("table");
   $grammar.addRule("table", [new $runtime.NonTerminal("thingpedia_complete_query", undefined)], (($0 : Ast.Expression) : (Ast.Expression)|null => { return $0; }), C.expressionKeyFn, { identity: (true) });
   $grammar.declareSymbol("filter");
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", undefined), "is", new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '==', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", undefined), "is not", new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '==', v, true)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Array__Any", undefined), "is", new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, 'contains', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Array__Any", undefined), "is not", new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, 'contains', v, true)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), new $runtime.Choice(["is samller than", "at most"]), new $runtime.NonTerminal("constant_Numeric", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '<=', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), new $runtime.Choice(["is larger than", "at least"]), new $runtime.NonTerminal("constant_Numeric", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '>=', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '==', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that not", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, '==', v, true)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, 'contains', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that not", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0 : undefined, p : C.ParamSlot, v : Ast.Value) : (C.FilterSlot)|null => C.makeFilter($loader, p, 'contains', v, true)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that has", new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0 : undefined, v : Ast.NumberValue, p : C.ParamSlot) : (C.FilterSlot)|null => C.makeAggregateFilter(p, 'count', '*', '==', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that has", new $runtime.Choice(["less than", "at most"]), new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0 : undefined, $1 : undefined, v : Ast.NumberValue, p : C.ParamSlot) : (C.FilterSlot)|null => C.makeAggregateFilter(p, 'count', '*', '<=', v)), C.filterKeyFn, {  });
   $grammar.addRule("filter", ["that has", new $runtime.Choice(["more than", "at least"]), new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0 : undefined, $1 : undefined, v : Ast.NumberValue, p : C.ParamSlot) : (C.FilterSlot)|null => C.makeAggregateFilter(p, 'count', '*', '>=', v)), C.filterKeyFn, {  });
   $grammar.declareSymbol("filtered_table");
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), new $runtime.NonTerminal("filter", undefined)], ((t : Ast.Expression, f : C.FilterSlot) : (Ast.Expression)|null => C.addFilter(t, f)), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "and", new $runtime.NonTerminal("filter", undefined)], ((t : Ast.Expression, $0 : undefined, f : C.FilterSlot) : (Ast.Expression)|null => C.addFilter(t, f)), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1 : Ast.Expression, $0 : undefined, p : C.ParamSlot, $1 : undefined, t2 : Ast.Expression) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("table", undefined), new $runtime.NonTerminal("out_param_avp", undefined)], ((t1 : Ast.Expression, $0 : undefined, t2 : Ast.Expression, p : C.ParamSlot) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("filtered_table", undefined), "and", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1 : Ast.Expression, $0 : undefined, t2 : Ast.Expression, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("table", undefined), "not", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1 : Ast.Expression, $0 : undefined, t2 : Ast.Expression, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("filtered_table", undefined), "and not", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1 : Ast.Expression, $0 : undefined, t2 : Ast.Expression, $1 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the smallest", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]])], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => C.makeArgMaxMinTable(table, p.name, 'asc')), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the smallest", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]])], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => C.makeArgMaxMinTable(table, p.name, 'asc')), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the largest", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]])], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => C.makeArgMaxMinTable(table, p.name, 'desc')), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the largest", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]])], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => C.makeArgMaxMinTable(table, p.name, 'desc')), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the least number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'asc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'desc');
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the least number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'asc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'desc');
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the most number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'desc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'desc');
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the most number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table : Ast.Expression, $0 : undefined, p : C.ParamSlot) : (Ast.Expression)|null => {
        if (!table.schema!.out[p.name] || !table.schema!.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'desc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, Type.Number, 'desc');
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((p : C.ParamSlot, $0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((p : C.ParamSlot, $0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((p : C.ParamSlot, $0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((p : C.ParamSlot, $0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["number of", new $runtime.NonTerminal("table", undefined)], (($0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (!table.schema!.is_list)
            return null;
        const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT)
            .addArguments([new Ast.ArgumentDef(null, Ast.ArgDirection.OUT, 'count', Type.Number)]);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, table, '*', 'count', newSchema);
        return new Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["number of", new $runtime.NonTerminal("filtered_table", undefined)], (($0 : undefined, table : Ast.Expression) : (Ast.Expression)|null => {
        if (!table.schema!.is_list)
            return null;
        const newSchema = table.schema!.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT)
            .addArguments([new Ast.ArgumentDef(null, Ast.ArgDirection.OUT, 'count', Type.Number)]);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, table, '*', 'count', newSchema);
        return new Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["total", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), "of", new $runtime.NonTerminal("table", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, t : Ast.Expression) : (Ast.Expression)|null => {
        const schema = t.schema!;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;

        const newSchema = schema.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, t, p.name, 'sum', newSchema);
        return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["total", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), "of", new $runtime.NonTerminal("filtered_table", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, t : Ast.Expression) : (Ast.Expression)|null => {
        const schema = t.schema!;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;

        const newSchema = schema.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, t, p.name, 'sum', newSchema);
        return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["average", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), "of", new $runtime.NonTerminal("table", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, t : Ast.Expression) : (Ast.Expression)|null => {
        const schema = t.schema!;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;

        const newSchema = schema.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, t, p.name, 'avg', newSchema);
        return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.addRule("filtered_table", ["average", new $runtime.NonTerminal("out_param_Any", ["is_numeric" as ("is_numeric" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), (true) as ReturnType<typeof C.paramKeyFn>["is_numeric"]]), "of", new $runtime.NonTerminal("filtered_table", undefined)], (($0 : undefined, p : C.ParamSlot, $1 : undefined, t : Ast.Expression) : (Ast.Expression)|null => {
        const schema = t.schema!;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;

        const newSchema = schema.filterArguments((arg) => arg.direction !== Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new Ast.Value.Array([]);
        const agg = new Ast.AggregationExpression(null, t, p.name, 'avg', newSchema);
        return new Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {  });
   $grammar.declareSymbol("$root");
   $grammar.addRule("$root", [new $runtime.NonTerminal("table", undefined)], ((t : Ast.Expression) : (Ast.Input)|null => {
        const proj = C.sayProjection($loader, t);
        if (proj)
            return C.makeProgram($loader, proj);
        return null;
    }), undefined, {  });
   $grammar.addRule("$root", [new $runtime.NonTerminal("filtered_table", undefined)], ((t : Ast.Expression) : (Ast.Input)|null => {
        const filtered = C.sayProjection($loader, t);
        if (filtered)
            return C.makeProgram($loader, filtered);
        return null;
    }), undefined, {  });
};
