tp: bunch of cleanups for table functions

* fix the document generator to look for the right thing and remove all
  references to view functions
* remove reference to create_*_function from the documentation in favor
  of the PerfettoSQL documentation
* change all metrics to use create or replace when building table
  functions to work around issues when metrics are run twice.

Change-Id: I76f17630450ccdd80e27d019d4d8ae141fb360c9
diff --git a/python/generators/sql_processing/docs_extractor.py b/python/generators/sql_processing/docs_extractor.py
index 52fbd66..c42774e 100644
--- a/python/generators/sql_processing/docs_extractor.py
+++ b/python/generators/sql_processing/docs_extractor.py
@@ -55,7 +55,7 @@
     extracted = []
     extracted += self._extract_for_kind(ObjKind.table_view)
     extracted += self._extract_for_kind(ObjKind.function)
-    extracted += self._extract_for_kind(ObjKind.view_function)
+    extracted += self._extract_for_kind(ObjKind.table_function)
     return extracted
 
   def _extract_for_kind(self, kind: ObjKind) -> List[Extract]:
diff --git a/python/generators/sql_processing/docs_parse.py b/python/generators/sql_processing/docs_parse.py
index 8f17b1b..cd940e1 100644
--- a/python/generators/sql_processing/docs_parse.py
+++ b/python/generators/sql_processing/docs_parse.py
@@ -258,8 +258,8 @@
     self.args = args
 
 
-class ViewFunctionDocParser(AbstractDocParser):
-  """Parses documentation for CREATE_VIEW_FUNCTION statements."""
+class TableFunctionDocParser(AbstractDocParser):
+  """Parses documentation for table function statements."""
 
   def __init__(self, path: str, module: str):
     super().__init__(path, module)
@@ -293,11 +293,11 @@
   functions: List[Function] = []
   table_functions: List[TableFunction] = []
 
-  def __init__(self, errors, table_views, functions, view_functions):
+  def __init__(self, errors, table_views, functions, table_functions):
     self.errors = errors
     self.table_views = table_views
     self.functions = functions
-    self.table_functions = view_functions
+    self.table_functions = table_functions
 
 
 # Reads the provided SQL and, if possible, generates a dictionary with data
@@ -319,7 +319,7 @@
   errors = []
   table_views = []
   functions = []
-  view_functions = []
+  table_functions = []
   for doc in docs:
     if doc.obj_kind == ObjKind.table_view:
       parser = TableViewDocParser(path, module_name)
@@ -333,11 +333,11 @@
       if res:
         functions.append(res)
       errors += parser.errors
-    if doc.obj_kind == ObjKind.view_function:
-      parser = ViewFunctionDocParser(path, module_name)
+    if doc.obj_kind == ObjKind.table_function:
+      parser = TableFunctionDocParser(path, module_name)
       res = parser.parse(doc)
       if res:
-        view_functions.append(res)
+        table_functions.append(res)
       errors += parser.errors
 
-  return ParsedFile(errors, table_views, functions, view_functions)
+  return ParsedFile(errors, table_views, functions, table_functions)
diff --git a/python/generators/sql_processing/utils.py b/python/generators/sql_processing/utils.py
index c701ceb..f4ee20c 100644
--- a/python/generators/sql_processing/utils.py
+++ b/python/generators/sql_processing/utils.py
@@ -48,16 +48,14 @@
     # Sql: Anything between ' and ');. We are catching \'.
     fr"{WS}({SQL});")
 
-CREATE_VIEW_FUNCTION_PATTERN = (
-    fr"SELECT{WS}CREATE_VIEW_FUNCTION\({WS}"
-    # Function name: we are matching everything [A-Z]* between ' and ).
-    fr"{WS}'{WS}({NAME}){WS}\({WS}"
-    # Args: anything before closing bracket with '.
-    fr"{WS}({ANY_WORDS}){WS}\){WS}'{WS},{WS}"
-    # Return columns: anything between two '.
-    fr"'{WS}({ANY_NON_QUOTE}){WS}',{WS}"
+CREATE_TABLE_FUNCTION_PATTERN = (
+    fr"CREATE{WS}PERFETTO{WS}FUNCTION{WS}({NAME}){WS}"
+    # Args: anything in the brackets.
+    fr"{WS}\({WS}({ANY_WORDS}){WS}\){WS}"
+    # Type: word after RETURNS.
+    fr"{WS}RETURNS{WS}TABLE\({WS}({ANY_WORDS}){WS}\){WS}AS{WS}"
     # Sql: Anything between ' and ');. We are catching \'.
-    fr"{WS}'{WS}({SQL}){WS}'{WS}\){WS};")
+    fr"{WS}({SQL});")
 
 COLUMN_ANNOTATION_PATTERN = fr'^\s*({NAME})\s*({ANY_WORDS})'
 
@@ -71,13 +69,13 @@
 class ObjKind(str, Enum):
   table_view = 'table_view'
   function = 'function'
-  view_function = 'view_function'
+  table_function = 'table_function'
 
 
 PATTERN_BY_KIND = {
     ObjKind.table_view: CREATE_TABLE_VIEW_PATTERN,
     ObjKind.function: CREATE_FUNCTION_PATTERN,
-    ObjKind.view_function: CREATE_VIEW_FUNCTION_PATTERN,
+    ObjKind.table_function: CREATE_TABLE_FUNCTION_PATTERN,
 }