fn ast_use_pg_catalog_0_7_1(stmt: &mut sql::ast::Statement<Raw>) -> Result<(), anyhow::Error> {
    fn normalize_function_name(name: &mut UnresolvedObjectName) {
        if name.0.len() == 1 {
            let func_name = name.to_string();
            for (schema, funcs) in &[
                (PG_CATALOG_SCHEMA, &*sql::func::PG_CATALOG_BUILTINS),
                (MZ_CATALOG_SCHEMA, &*sql::func::MZ_CATALOG_BUILTINS),
                (MZ_INTERNAL_SCHEMA, &*sql::func::MZ_INTERNAL_BUILTINS),
            ] {
                if funcs.contains_key(func_name.as_str()) {
                    *name = UnresolvedObjectName(vec![Ident::new(*schema), name.0.remove(0)]);
                    break;
                }
            }
        }
    }

    struct FuncNormalizer;

    impl<'ast> VisitMut<'ast, Raw> for FuncNormalizer {
        fn visit_function_mut(&mut self, func: &'ast mut Function<Raw>) {
            normalize_function_name(&mut func.name);
            // Function args can be functions themselves, so let the visitor
            // find them.
            visit_mut::visit_function_mut(self, func)
        }
        fn visit_table_function_mut(&mut self, func: &'ast mut TableFunction<Raw>) {
            normalize_function_name(&mut func.name);
            // Function args can be functions themselves, so let the visitor
            // find them.
            visit_mut::visit_table_function_mut(self, func)
        }
    }

    match stmt {
        Statement::CreateView(CreateViewStatement {
            temporary: _,
            materialized: _,
            if_exists: _,
            definition:
                ViewDefinition {
                    name: _,
                    columns: _,
                    query,
                    with_options: _,
                },
        }) => FuncNormalizer.visit_query_mut(query),

        Statement::CreateIndex(CreateIndexStatement {
            name: _,
            on_name: _,
            key_parts,
            with_options: _,
            if_not_exists: _,
        }) => {
            if let Some(key_parts) = key_parts {
                for key_part in key_parts {
                    FuncNormalizer.visit_expr_mut(key_part);
                }
            }
        }

        Statement::CreateSink(CreateSinkStatement {
            name: _,
            from: _,
            connector: _,
            with_options: _,
            format: _,
            envelope: _,
            with_snapshot: _,
            as_of,
            if_not_exists: _,
        }) => {
            if let Some(expr) = as_of {
                FuncNormalizer.visit_expr_mut(expr);
            }
        }

        // At the time the migration was written, tables, sources, and
        // types could not contain references to functions.
        Statement::CreateTable(_) | Statement::CreateSource(_) | Statement::CreateType(_) => {}

        _ => bail!("catalog item contained inappropriate statement: {}", stmt),
    };

    Ok(())
}
// Rewrites all function references to have `pg_catalog` qualification; this
// is necessary to support resolving all built-in functions to the catalog.
// (At the time of writing Materialize did not support user-defined
// functions.)
//
// The approach is to prepend `pg_catalog` to all `UnresolvedObjectName`
// names that could refer to functions.
Rewrites all function references to have `pg_catalog` qualification; this
is necessary to support resolving all built-in functions to the catalog.
(At the time of writing Materialize did not support user-defined
functions.)

The approach is to prepend `pg_catalog` to all `UnresolvedObjectName`
names that could refer to functions.
Rewrites all function references to have ` pg_catalog ` qualification ; this is necessary to support resolving all built - in functions to the catalog . ( At the time of writing Materialize did not support user - defined functions . ) The approach is to prepend ` pg_catalog ` to all ` UnresolvedObjectName ` names that could refer to functions .
fn ast_use_pg_catalog_0_7_1(stmt: &mut sql::ast::Statement<Raw>) -> Result<(), anyhow::Error> {
    fn normalize_function_name(name: &mut UnresolvedObjectName) {
        if name.0.len() == 1 {
            let func_name = name.to_string();
            for (schema, funcs) in &[
                (PG_CATALOG_SCHEMA, &*sql::func::PG_CATALOG_BUILTINS),
                (MZ_CATALOG_SCHEMA, &*sql::func::MZ_CATALOG_BUILTINS),
                (MZ_INTERNAL_SCHEMA, &*sql::func::MZ_INTERNAL_BUILTINS),
            ] {
                if funcs.contains_key(func_name.as_str()) {
                    *name = UnresolvedObjectName(vec![Ident::new(*schema), name.0.remove(0)]);
                    break;
                }
            }
        }
    }
    struct FuncNormalizer;
    impl<'ast> VisitMut<'ast, Raw> for FuncNormalizer {
        fn visit_function_mut(&mut self, func: &'ast mut Function<Raw>) {
            normalize_function_name(&mut func.name);
            // Function args can be functions themselves, so let the visitor
            // find them.
            visit_mut::visit_function_mut(self, func)
        }
        fn visit_table_function_mut(&mut self, func: &'ast mut TableFunction<Raw>) {
            normalize_function_name(&mut func.name);
            // Function args can be functions themselves, so let the visitor
            // find them.
            visit_mut::visit_table_function_mut(self, func)
        }
    }
    match stmt {
        Statement::CreateView(CreateViewStatement {
            temporary: _,
            materialized: _,
            if_exists: _,
            definition:
                ViewDefinition {
                    name: _,
                    columns: _,
                    query,
                    with_options: _,
                },
        }) => FuncNormalizer.visit_query_mut(query),
        Statement::CreateIndex(CreateIndexStatement {
            name: _,
            on_name: _,
            key_parts,
            with_options: _,
            if_not_exists: _,
        }) => {
            if let Some(key_parts) = key_parts {
                for key_part in key_parts {
                    FuncNormalizer.visit_expr_mut(key_part);
                }
            }
        }
        Statement::CreateSink(CreateSinkStatement {
            name: _,
            from: _,
            connector: _,
            with_options: _,
            format: _,
            envelope: _,
            with_snapshot: _,
            as_of,
            if_not_exists: _,
        }) => {
            if let Some(expr) = as_of {
                FuncNormalizer.visit_expr_mut(expr);
            }
        }
        // At the time the migration was written, tables, sources, and
        // types could not contain references to functions.
        Statement::CreateTable(_) | Statement::CreateSource(_) | Statement::CreateType(_) => {}
        _ => bail!("catalog item contained inappropriate statement: {}", stmt),
    };
    Ok(())
}
fn ast_use_pg_catalog_0_7_1 ( stmt : & mut sql :: ast :: Statement < Raw > ) -> Result < ( ) , anyhow :: Error > { fn normalize_function_name ( name : & mut UnresolvedObjectName ) { if name . 0 . len ( ) == 1 { let func_name = name . to_string ( ) ; for ( schema , funcs ) in & [ ( PG_CATALOG_SCHEMA , & * sql :: func :: PG_CATALOG_BUILTINS ) , ( MZ_CATALOG_SCHEMA , & * sql :: func :: MZ_CATALOG_BUILTINS ) , ( MZ_INTERNAL_SCHEMA , & * sql :: func :: MZ_INTERNAL_BUILTINS ) , ] { if funcs . contains_key ( func_name . as_str ( ) ) { * name = UnresolvedObjectName ( vec ! [ Ident new ( schema ) name 0 remove ( 0 ) ] ) ; break ; } } } } struct FuncNormalizer ; impl < ' ast > VisitMut < ' ast , Raw > for FuncNormalizer { fn visit_function_mut ( & mut self , func : & ' ast mut Function < Raw > ) { normalize_function_name ( & mut func . name ) ; // Function args can be functions themselves, so let the visitor // find them. visit_mut :: visit_function_mut ( self , func ) } fn visit_table_function_mut ( & mut self , func : & ' ast mut TableFunction < Raw > ) { normalize_function_name ( & mut func . name ) ; // Function args can be functions themselves, so let the visitor // find them. visit_mut :: visit_table_function_mut ( self , func ) } } match stmt { Statement :: CreateView ( CreateViewStatement { temporary : _ , materialized : _ , if_exists : _ , definition : ViewDefinition { name : _ , columns : _ , query , with_options : _ , } , } ) => FuncNormalizer . visit_query_mut ( query ) , Statement :: CreateIndex ( CreateIndexStatement { name : _ , on_name : _ , key_parts , with_options : _ , if_not_exists : _ , } ) => { if let Some ( key_parts ) = key_parts { for key_part in key_parts { FuncNormalizer . visit_expr_mut ( key_part ) ; } } } Statement :: CreateSink ( CreateSinkStatement { name : _ , from : _ , connector : _ , with_options : _ , format : _ , envelope : _ , with_snapshot : _ , as_of , if_not_exists : _ , } ) => { if let Some ( expr ) = as_of { FuncNormalizer . visit_expr_mut ( expr ) ; } } // At the time the migration was written, tables, sources, and // types could not contain references to functions. Statement :: CreateTable ( _ ) | Statement :: CreateSource ( _ ) | Statement :: CreateType ( _ ) => { } _ => bail ! ( " " stmt ) , } ; Ok ( ( ) ) }
Rewrites all function references to have `pg_catalog` qualification; this
is necessary to support resolving all built-in functions to the catalog.
Rewrites all function references to have ` pg_catalog ` qualification ; this is necessary to support resolving all built - in functions to the catalog .
stmt
&mut sql::ast::Statement<Raw>
stmt
&mut sql::ast::Statement<Raw>