tp: create perfetto_sql folder and rejig various modules

This CL introduces a top level "perfetto_sql" folder to trace processor
and moves the prelude, intrinsics, stdlib and engine components into it.
Follow up work will rework the documentation to expose this grouping of
components to public.

Change-Id: I35bea692fc5c95999e3991c9952511edd4bf054b
diff --git a/Android.bp b/Android.bp
index 77ddad6..ca76452 100644
--- a/Android.bp
+++ b/Android.bp
@@ -2080,11 +2080,12 @@
         ":perfetto_src_trace_processor_lib",
         ":perfetto_src_trace_processor_metatrace",
         ":perfetto_src_trace_processor_metrics_metrics",
-        ":perfetto_src_trace_processor_prelude_functions_functions",
-        ":perfetto_src_trace_processor_prelude_functions_interface",
-        ":perfetto_src_trace_processor_prelude_operators_operators",
-        ":perfetto_src_trace_processor_prelude_table_functions_interface",
-        ":perfetto_src_trace_processor_prelude_table_functions_table_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_engine_engine",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
         ":perfetto_src_trace_processor_sorter_sorter",
         ":perfetto_src_trace_processor_sqlite_query_constraints",
         ":perfetto_src_trace_processor_sqlite_sqlite",
@@ -2281,9 +2282,9 @@
         "perfetto_src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-        "perfetto_src_trace_processor_prelude_table_functions_tables",
-        "perfetto_src_trace_processor_prelude_tables_views_tables_views",
-        "perfetto_src_trace_processor_stdlib_gen_amalgamated_stdlib",
+        "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
+        "perfetto_src_trace_processor_perfetto_sql_prelude_prelude",
+        "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
         "perfetto_src_trace_processor_tables_tables_python",
     ],
     defaults: [
@@ -10227,105 +10228,114 @@
     ],
 }
 
-// GN: //src/trace_processor/prelude/functions:functions
+// GN: //src/trace_processor/perfetto_sql/engine:engine
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_functions_functions",
+    name: "perfetto_src_trace_processor_perfetto_sql_engine_engine",
     srcs: [
-        "src/trace_processor/prelude/functions/create_function.cc",
-        "src/trace_processor/prelude/functions/create_function_internal.cc",
-        "src/trace_processor/prelude/functions/create_view_function.cc",
-        "src/trace_processor/prelude/functions/import.cc",
-        "src/trace_processor/prelude/functions/layout_functions.cc",
-        "src/trace_processor/prelude/functions/math.cc",
-        "src/trace_processor/prelude/functions/pprof_functions.cc",
-        "src/trace_processor/prelude/functions/sqlite3_str_split.cc",
-        "src/trace_processor/prelude/functions/stack_functions.cc",
-        "src/trace_processor/prelude/functions/to_ftrace.cc",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.cc",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/functions:interface
+// GN: //src/trace_processor/perfetto_sql/intrinsics/functions:functions
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_functions_interface",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_functions",
     srcs: [
-        "src/trace_processor/prelude/functions/sql_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/import.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/math.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/functions:unittests
+// GN: //src/trace_processor/perfetto_sql/intrinsics/functions:interface
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_functions_unittests",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_interface",
     srcs: [
-        "src/trace_processor/prelude/functions/sqlite3_str_split_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/operators:operators
+// GN: //src/trace_processor/perfetto_sql/intrinsics/functions:unittests
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_operators_operators",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_unittests",
     srcs: [
-        "src/trace_processor/prelude/operators/span_join_operator.cc",
-        "src/trace_processor/prelude/operators/window_operator.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split_unittest.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/operators:unittests
+// GN: //src/trace_processor/perfetto_sql/intrinsics/operators:operators
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_operators_unittests",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_operators",
     srcs: [
-        "src/trace_processor/prelude/operators/span_join_operator_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/table_functions:interface
+// GN: //src/trace_processor/perfetto_sql/intrinsics/operators:unittests
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_table_functions_interface",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_unittests",
     srcs: [
-        "src/trace_processor/prelude/table_functions/table_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator_unittest.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/table_functions:table_functions
+// GN: //src/trace_processor/perfetto_sql/intrinsics/table_functions:interface
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_table_functions_table_functions",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
     srcs: [
-        "src/trace_processor/prelude/table_functions/ancestor.cc",
-        "src/trace_processor/prelude/table_functions/connected_flow.cc",
-        "src/trace_processor/prelude/table_functions/descendant.cc",
-        "src/trace_processor/prelude/table_functions/experimental_annotated_stack.cc",
-        "src/trace_processor/prelude/table_functions/experimental_counter_dur.cc",
-        "src/trace_processor/prelude/table_functions/experimental_flamegraph.cc",
-        "src/trace_processor/prelude/table_functions/experimental_flat_slice.cc",
-        "src/trace_processor/prelude/table_functions/experimental_sched_upid.cc",
-        "src/trace_processor/prelude/table_functions/experimental_slice_layout.cc",
-        "src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.cc",
-        "src/trace_processor/prelude/table_functions/view.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/table_functions:tables
+// GN: //src/trace_processor/perfetto_sql/intrinsics/table_functions:table_functions
+filegroup {
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+    srcs: [
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/view.cc",
+    ],
+}
+
+// GN: //src/trace_processor/perfetto_sql/intrinsics/table_functions:tables
 genrule {
-    name: "perfetto_src_trace_processor_prelude_table_functions_tables",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
     srcs: [
-        "src/trace_processor/prelude/table_functions/tables.py",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables.py",
     ],
     tools: [
-        "perfetto_src_trace_processor_prelude_table_functions_tables_binary",
+        "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables_binary",
     ],
-    cmd: "$(location perfetto_src_trace_processor_prelude_table_functions_tables_binary) --gen-dir=$(genDir) --relative-input-dir=external/perfetto --inputs $(in)",
+    cmd: "$(location perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables_binary) --gen-dir=$(genDir) --relative-input-dir=external/perfetto --inputs $(in)",
     out: [
-        "src/trace_processor/prelude/table_functions/tables_py.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h",
     ],
 }
 
-// GN: //src/trace_processor/prelude/table_functions:tables
+// GN: //src/trace_processor/perfetto_sql/intrinsics/table_functions:tables
 python_binary_host {
-    name: "perfetto_src_trace_processor_prelude_table_functions_tables_binary",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables_binary",
     srcs: [
         "python/generators/trace_processor_table/public.py",
         "python/generators/trace_processor_table/serialize.py",
         "python/generators/trace_processor_table/util.py",
-        "src/trace_processor/prelude/table_functions/tables.py",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables.py",
         "src/trace_processor/tables/android_tables.py",
         "src/trace_processor/tables/counter_tables.py",
         "src/trace_processor/tables/flow_tables.py",
@@ -10341,29 +10351,73 @@
     main: "tools/gen_tp_table_headers.py",
 }
 
-// GN: //src/trace_processor/prelude/table_functions:unittests
+// GN: //src/trace_processor/perfetto_sql/intrinsics/table_functions:unittests
 filegroup {
-    name: "perfetto_src_trace_processor_prelude_table_functions_unittests",
+    name: "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_unittests",
     srcs: [
-        "src/trace_processor/prelude/table_functions/ancestor_unittest.cc",
-        "src/trace_processor/prelude/table_functions/connected_flow_unittest.cc",
-        "src/trace_processor/prelude/table_functions/descendant_unittest.cc",
-        "src/trace_processor/prelude/table_functions/experimental_counter_dur_unittest.cc",
-        "src/trace_processor/prelude/table_functions/experimental_flat_slice_unittest.cc",
-        "src/trace_processor/prelude/table_functions/experimental_slice_layout_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice_unittest.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout_unittest.cc",
     ],
 }
 
-// GN: //src/trace_processor/prelude/tables_views:tables_views
+// GN: //src/trace_processor/perfetto_sql/prelude:prelude
 genrule {
-    name: "perfetto_src_trace_processor_prelude_tables_views_tables_views",
+    name: "perfetto_src_trace_processor_perfetto_sql_prelude_prelude",
     srcs: [
-        "src/trace_processor/prelude/tables_views/tables.sql",
-        "src/trace_processor/prelude/tables_views/views.sql",
+        "src/trace_processor/perfetto_sql/prelude/tables.sql",
+        "src/trace_processor/perfetto_sql/prelude/views.sql",
     ],
     cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=prelude::tables_views --cpp-out=$(out) $(in)",
     out: [
-        "src/trace_processor/prelude/tables_views/tables_views.h",
+        "src/trace_processor/perfetto_sql/prelude/tables_views.h",
+    ],
+    tool_files: [
+        "tools/gen_amalgamated_sql.py",
+    ],
+}
+
+// GN: //src/trace_processor/perfetto_sql/stdlib:stdlib
+genrule {
+    name: "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
+    srcs: [
+        "src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/monitor_contention.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/network_packets.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_maxsdk28.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk29.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk33.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/statsd.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scroll_janks.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scrolls.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/cpu_powerups.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/speedometer.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/counters.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/cpus.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/android_broadcast.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/flat_slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/proto_path.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/thread_executing_span.sql",
+        "src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql",
+    ],
+    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=stdlib --cpp-out=$(out) $(in)",
+    out: [
+        "src/trace_processor/perfetto_sql/stdlib/stdlib.h",
     ],
     tool_files: [
         "tools/gen_amalgamated_sql.py",
@@ -10426,8 +10480,6 @@
     name: "perfetto_src_trace_processor_sqlite_sqlite",
     srcs: [
         "src/trace_processor/sqlite/db_sqlite_table.cc",
-        "src/trace_processor/sqlite/perfetto_sql_engine.cc",
-        "src/trace_processor/sqlite/perfetto_sql_parser.cc",
         "src/trace_processor/sqlite/sql_source.cc",
         "src/trace_processor/sqlite/sql_stats_table.cc",
         "src/trace_processor/sqlite/sqlite_engine.cc",
@@ -10443,57 +10495,12 @@
     name: "perfetto_src_trace_processor_sqlite_unittests",
     srcs: [
         "src/trace_processor/sqlite/db_sqlite_table_unittest.cc",
-        "src/trace_processor/sqlite/perfetto_sql_parser_unittest.cc",
         "src/trace_processor/sqlite/query_constraints_unittest.cc",
         "src/trace_processor/sqlite/sqlite_tokenizer_unittest.cc",
         "src/trace_processor/sqlite/sqlite_utils_unittest.cc",
     ],
 }
 
-// GN: //src/trace_processor/stdlib:gen_amalgamated_stdlib
-genrule {
-    name: "perfetto_src_trace_processor_stdlib_gen_amalgamated_stdlib",
-    srcs: [
-        "src/trace_processor/stdlib/android/battery.sql",
-        "src/trace_processor/stdlib/android/battery_stats.sql",
-        "src/trace_processor/stdlib/android/binder.sql",
-        "src/trace_processor/stdlib/android/monitor_contention.sql",
-        "src/trace_processor/stdlib/android/network_packets.sql",
-        "src/trace_processor/stdlib/android/process_metadata.sql",
-        "src/trace_processor/stdlib/android/slices.sql",
-        "src/trace_processor/stdlib/android/startup/internal_startups_maxsdk28.sql",
-        "src/trace_processor/stdlib/android/startup/internal_startups_minsdk29.sql",
-        "src/trace_processor/stdlib/android/startup/internal_startups_minsdk33.sql",
-        "src/trace_processor/stdlib/android/startup/startups.sql",
-        "src/trace_processor/stdlib/android/statsd.sql",
-        "src/trace_processor/stdlib/chrome/chrome_scroll_janks.sql",
-        "src/trace_processor/stdlib/chrome/chrome_scrolls.sql",
-        "src/trace_processor/stdlib/chrome/cpu_powerups.sql",
-        "src/trace_processor/stdlib/chrome/histograms.sql",
-        "src/trace_processor/stdlib/chrome/metadata.sql",
-        "src/trace_processor/stdlib/chrome/speedometer.sql",
-        "src/trace_processor/stdlib/common/counters.sql",
-        "src/trace_processor/stdlib/common/cpus.sql",
-        "src/trace_processor/stdlib/common/metadata.sql",
-        "src/trace_processor/stdlib/common/percentiles.sql",
-        "src/trace_processor/stdlib/common/slices.sql",
-        "src/trace_processor/stdlib/common/timestamps.sql",
-        "src/trace_processor/stdlib/experimental/android_broadcast.sql",
-        "src/trace_processor/stdlib/experimental/flat_slices.sql",
-        "src/trace_processor/stdlib/experimental/proto_path.sql",
-        "src/trace_processor/stdlib/experimental/slices.sql",
-        "src/trace_processor/stdlib/experimental/thread_executing_span.sql",
-        "src/trace_processor/stdlib/pkvm/hypervisor.sql",
-    ],
-    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=stdlib --cpp-out=$(out) $(in)",
-    out: [
-        "src/trace_processor/stdlib/amalgamated_stdlib.h",
-    ],
-    tool_files: [
-        "tools/gen_amalgamated_sql.py",
-    ],
-}
-
 // GN: //src/trace_processor:storage_minimal
 filegroup {
     name: "perfetto_src_trace_processor_storage_minimal",
@@ -12150,14 +12157,15 @@
         ":perfetto_src_trace_processor_metatrace",
         ":perfetto_src_trace_processor_metrics_metrics",
         ":perfetto_src_trace_processor_metrics_unittests",
-        ":perfetto_src_trace_processor_prelude_functions_functions",
-        ":perfetto_src_trace_processor_prelude_functions_interface",
-        ":perfetto_src_trace_processor_prelude_functions_unittests",
-        ":perfetto_src_trace_processor_prelude_operators_operators",
-        ":perfetto_src_trace_processor_prelude_operators_unittests",
-        ":perfetto_src_trace_processor_prelude_table_functions_interface",
-        ":perfetto_src_trace_processor_prelude_table_functions_table_functions",
-        ":perfetto_src_trace_processor_prelude_table_functions_unittests",
+        ":perfetto_src_trace_processor_perfetto_sql_engine_engine",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_unittests",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_unittests",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_unittests",
         ":perfetto_src_trace_processor_rpc_rpc",
         ":perfetto_src_trace_processor_rpc_unittests",
         ":perfetto_src_trace_processor_sorter_sorter",
@@ -12396,9 +12404,9 @@
         "perfetto_src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-        "perfetto_src_trace_processor_prelude_table_functions_tables",
-        "perfetto_src_trace_processor_prelude_tables_views_tables_views",
-        "perfetto_src_trace_processor_stdlib_gen_amalgamated_stdlib",
+        "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
+        "perfetto_src_trace_processor_perfetto_sql_prelude_prelude",
+        "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
         "perfetto_src_trace_processor_tables_py_tables_unittest",
         "perfetto_src_trace_processor_tables_tables_python",
         "perfetto_src_trace_processor_views_macros_unittest",
@@ -12840,11 +12848,12 @@
         ":perfetto_src_trace_processor_lib",
         ":perfetto_src_trace_processor_metatrace",
         ":perfetto_src_trace_processor_metrics_metrics",
-        ":perfetto_src_trace_processor_prelude_functions_functions",
-        ":perfetto_src_trace_processor_prelude_functions_interface",
-        ":perfetto_src_trace_processor_prelude_operators_operators",
-        ":perfetto_src_trace_processor_prelude_table_functions_interface",
-        ":perfetto_src_trace_processor_prelude_table_functions_table_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_engine_engine",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
         ":perfetto_src_trace_processor_rpc_httpd",
         ":perfetto_src_trace_processor_rpc_rpc",
         ":perfetto_src_trace_processor_sorter_sorter",
@@ -12921,9 +12930,9 @@
         "perfetto_src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-        "perfetto_src_trace_processor_prelude_table_functions_tables",
-        "perfetto_src_trace_processor_prelude_tables_views_tables_views",
-        "perfetto_src_trace_processor_stdlib_gen_amalgamated_stdlib",
+        "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
+        "perfetto_src_trace_processor_perfetto_sql_prelude_prelude",
+        "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
         "perfetto_src_trace_processor_tables_tables_python",
     ],
     defaults: [
@@ -13067,11 +13076,12 @@
         ":perfetto_src_trace_processor_lib",
         ":perfetto_src_trace_processor_metatrace",
         ":perfetto_src_trace_processor_metrics_metrics",
-        ":perfetto_src_trace_processor_prelude_functions_functions",
-        ":perfetto_src_trace_processor_prelude_functions_interface",
-        ":perfetto_src_trace_processor_prelude_operators_operators",
-        ":perfetto_src_trace_processor_prelude_table_functions_interface",
-        ":perfetto_src_trace_processor_prelude_table_functions_table_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_engine_engine",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
         ":perfetto_src_trace_processor_sorter_sorter",
         ":perfetto_src_trace_processor_sqlite_query_constraints",
         ":perfetto_src_trace_processor_sqlite_sqlite",
@@ -13150,9 +13160,9 @@
         "perfetto_src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
         "perfetto_src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-        "perfetto_src_trace_processor_prelude_table_functions_tables",
-        "perfetto_src_trace_processor_prelude_tables_views_tables_views",
-        "perfetto_src_trace_processor_stdlib_gen_amalgamated_stdlib",
+        "perfetto_src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
+        "perfetto_src_trace_processor_perfetto_sql_prelude_prelude",
+        "perfetto_src_trace_processor_perfetto_sql_stdlib_stdlib",
         "perfetto_src_trace_processor_tables_tables_python",
         "perfetto_src_traceconv_gen_cc_trace_descriptor",
     ],
diff --git a/BUILD b/BUILD
index 4cb49b9..9c369ca 100644
--- a/BUILD
+++ b/BUILD
@@ -103,12 +103,13 @@
         ":src_trace_processor_lib",
         ":src_trace_processor_metatrace",
         ":src_trace_processor_metrics_metrics",
-        ":src_trace_processor_prelude_functions_functions",
-        ":src_trace_processor_prelude_functions_interface",
-        ":src_trace_processor_prelude_operators_operators",
-        ":src_trace_processor_prelude_table_functions_interface",
-        ":src_trace_processor_prelude_table_functions_table_functions",
-        ":src_trace_processor_prelude_table_functions_tables",
+        ":src_trace_processor_perfetto_sql_engine_engine",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
         ":src_trace_processor_rpc_rpc",
         ":src_trace_processor_sorter_sorter",
         ":src_trace_processor_sqlite_query_constraints",
@@ -202,8 +203,8 @@
                ":src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
                ":src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-               ":src_trace_processor_prelude_tables_views_tables_views",
-               ":src_trace_processor_stdlib_gen_amalgamated_stdlib",
+               ":src_trace_processor_perfetto_sql_prelude_prelude",
+               ":src_trace_processor_perfetto_sql_stdlib_stdlib",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
            PERFETTO_CONFIG.deps.sqlite +
            PERFETTO_CONFIG.deps.sqlite_ext_percentile +
@@ -2033,129 +2034,229 @@
     ],
 )
 
-# GN target: //src/trace_processor/prelude/functions:functions
+# GN target: //src/trace_processor/perfetto_sql/engine:engine
 perfetto_filegroup(
-    name = "src_trace_processor_prelude_functions_functions",
+    name = "src_trace_processor_perfetto_sql_engine_engine",
     srcs = [
-        "src/trace_processor/prelude/functions/clock_functions.h",
-        "src/trace_processor/prelude/functions/create_function.cc",
-        "src/trace_processor/prelude/functions/create_function.h",
-        "src/trace_processor/prelude/functions/create_function_internal.cc",
-        "src/trace_processor/prelude/functions/create_function_internal.h",
-        "src/trace_processor/prelude/functions/create_view_function.cc",
-        "src/trace_processor/prelude/functions/create_view_function.h",
-        "src/trace_processor/prelude/functions/import.cc",
-        "src/trace_processor/prelude/functions/import.h",
-        "src/trace_processor/prelude/functions/layout_functions.cc",
-        "src/trace_processor/prelude/functions/layout_functions.h",
-        "src/trace_processor/prelude/functions/math.cc",
-        "src/trace_processor/prelude/functions/math.h",
-        "src/trace_processor/prelude/functions/pprof_functions.cc",
-        "src/trace_processor/prelude/functions/pprof_functions.h",
-        "src/trace_processor/prelude/functions/sqlite3_str_split.cc",
-        "src/trace_processor/prelude/functions/sqlite3_str_split.h",
-        "src/trace_processor/prelude/functions/stack_functions.cc",
-        "src/trace_processor/prelude/functions/stack_functions.h",
-        "src/trace_processor/prelude/functions/to_ftrace.cc",
-        "src/trace_processor/prelude/functions/to_ftrace.h",
-        "src/trace_processor/prelude/functions/utils.h",
-        "src/trace_processor/prelude/functions/window_functions.h",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.cc",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc",
+        "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/functions:interface
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/functions:functions
 perfetto_filegroup(
-    name = "src_trace_processor_prelude_functions_interface",
+    name = "src_trace_processor_perfetto_sql_intrinsics_functions_functions",
     srcs = [
-        "src/trace_processor/prelude/functions/sql_function.cc",
-        "src/trace_processor/prelude/functions/sql_function.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/clock_functions.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/import.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/import.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/math.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/math.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/utils.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/window_functions.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/operators:operators
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/functions:interface
 perfetto_filegroup(
-    name = "src_trace_processor_prelude_operators_operators",
+    name = "src_trace_processor_perfetto_sql_intrinsics_functions_interface",
     srcs = [
-        "src/trace_processor/prelude/operators/span_join_operator.cc",
-        "src/trace_processor/prelude/operators/span_join_operator.h",
-        "src/trace_processor/prelude/operators/window_operator.cc",
-        "src/trace_processor/prelude/operators/window_operator.h",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/table_functions:interface
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/operators:operators
 perfetto_filegroup(
-    name = "src_trace_processor_prelude_table_functions_interface",
+    name = "src_trace_processor_perfetto_sql_intrinsics_operators_operators",
     srcs = [
-        "src/trace_processor/prelude/table_functions/table_function.cc",
-        "src/trace_processor/prelude/table_functions/table_function.h",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/table_functions:table_functions
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/table_functions:interface
 perfetto_filegroup(
-    name = "src_trace_processor_prelude_table_functions_table_functions",
+    name = "src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
     srcs = [
-        "src/trace_processor/prelude/table_functions/ancestor.cc",
-        "src/trace_processor/prelude/table_functions/ancestor.h",
-        "src/trace_processor/prelude/table_functions/connected_flow.cc",
-        "src/trace_processor/prelude/table_functions/connected_flow.h",
-        "src/trace_processor/prelude/table_functions/descendant.cc",
-        "src/trace_processor/prelude/table_functions/descendant.h",
-        "src/trace_processor/prelude/table_functions/experimental_annotated_stack.cc",
-        "src/trace_processor/prelude/table_functions/experimental_annotated_stack.h",
-        "src/trace_processor/prelude/table_functions/experimental_counter_dur.cc",
-        "src/trace_processor/prelude/table_functions/experimental_counter_dur.h",
-        "src/trace_processor/prelude/table_functions/experimental_flamegraph.cc",
-        "src/trace_processor/prelude/table_functions/experimental_flamegraph.h",
-        "src/trace_processor/prelude/table_functions/experimental_flat_slice.cc",
-        "src/trace_processor/prelude/table_functions/experimental_flat_slice.h",
-        "src/trace_processor/prelude/table_functions/experimental_sched_upid.cc",
-        "src/trace_processor/prelude/table_functions/experimental_sched_upid.h",
-        "src/trace_processor/prelude/table_functions/experimental_slice_layout.cc",
-        "src/trace_processor/prelude/table_functions/experimental_slice_layout.h",
-        "src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.cc",
-        "src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h",
-        "src/trace_processor/prelude/table_functions/view.cc",
-        "src/trace_processor/prelude/table_functions/view.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/table_functions:tables
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/table_functions:table_functions
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+    srcs = [
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/view.cc",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/intrinsics/table_functions:tables
 perfetto_cc_tp_tables(
-    name = "src_trace_processor_prelude_table_functions_tables",
+    name = "src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
     srcs = [
-        "src/trace_processor/prelude/table_functions/tables.py",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables.py",
     ],
     deps = [
         ":src_trace_processor_tables_tables_python",
     ],
     outs = [
-        "src/trace_processor/prelude/table_functions/tables_py.h",
+        "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h",
     ],
 )
 
-# GN target: //src/trace_processor/prelude/tables_views:sources
-perfetto_filegroup(
-    name = "src_trace_processor_prelude_tables_views_sources",
-    srcs = [
-        "src/trace_processor/prelude/tables_views/tables.sql",
-        "src/trace_processor/prelude/tables_views/views.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/prelude/tables_views:tables_views
+# GN target: //src/trace_processor/perfetto_sql/prelude:prelude
 perfetto_cc_amalgamated_sql(
-    name = "src_trace_processor_prelude_tables_views_tables_views",
+    name = "src_trace_processor_perfetto_sql_prelude_prelude",
     deps = [
-        ":src_trace_processor_prelude_tables_views_sources",
+        ":src_trace_processor_perfetto_sql_prelude_sources",
     ],
     outs = [
-        "src/trace_processor/prelude/tables_views/tables_views.h",
+        "src/trace_processor/perfetto_sql/prelude/tables_views.h",
     ],
     namespace = "prelude::tables_views",
 )
 
+# GN target: //src/trace_processor/perfetto_sql/prelude:sources
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_prelude_sources",
+    srcs = [
+        "src/trace_processor/perfetto_sql/prelude/tables.sql",
+        "src/trace_processor/perfetto_sql/prelude/views.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/android/startup:startup
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_android_startup_startup",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_maxsdk28.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk29.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk33.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/android:android
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_android_android",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/monitor_contention.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/network_packets.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/statsd.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/chrome:chrome_sql
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_chrome_chrome_sql",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scroll_janks.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scrolls.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/cpu_powerups.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/chrome/speedometer.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/common:common
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_common_common",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/common/counters.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/cpus.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/experimental:experimental
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_experimental_experimental",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/experimental/android_broadcast.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/flat_slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/proto_path.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/experimental/thread_executing_span.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib/pkvm:pkvm
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_pkvm_pkvm",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql",
+    ],
+)
+
+# GN target: //src/trace_processor/perfetto_sql/stdlib:stdlib
+perfetto_cc_amalgamated_sql(
+    name = "src_trace_processor_perfetto_sql_stdlib_stdlib",
+    deps = [
+        ":src_trace_processor_perfetto_sql_stdlib_android_android",
+        ":src_trace_processor_perfetto_sql_stdlib_android_startup_startup",
+        ":src_trace_processor_perfetto_sql_stdlib_chrome_chrome_sql",
+        ":src_trace_processor_perfetto_sql_stdlib_common_common",
+        ":src_trace_processor_perfetto_sql_stdlib_experimental_experimental",
+        ":src_trace_processor_perfetto_sql_stdlib_pkvm_pkvm",
+    ],
+    outs = [
+        "src/trace_processor/perfetto_sql/stdlib/stdlib.h",
+    ],
+    namespace = "stdlib",
+)
+
 # GN target: //src/trace_processor/rpc:httpd
 perfetto_filegroup(
     name = "src_trace_processor_rpc_httpd",
@@ -2202,10 +2303,6 @@
     srcs = [
         "src/trace_processor/sqlite/db_sqlite_table.cc",
         "src/trace_processor/sqlite/db_sqlite_table.h",
-        "src/trace_processor/sqlite/perfetto_sql_engine.cc",
-        "src/trace_processor/sqlite/perfetto_sql_engine.h",
-        "src/trace_processor/sqlite/perfetto_sql_parser.cc",
-        "src/trace_processor/sqlite/perfetto_sql_parser.h",
         "src/trace_processor/sqlite/query_cache.h",
         "src/trace_processor/sqlite/scoped_db.h",
         "src/trace_processor/sqlite/sql_source.cc",
@@ -2225,95 +2322,6 @@
     ],
 )
 
-# GN target: //src/trace_processor/stdlib/android/startup:startup
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_android_startup_startup",
-    srcs = [
-        "src/trace_processor/stdlib/android/startup/internal_startups_maxsdk28.sql",
-        "src/trace_processor/stdlib/android/startup/internal_startups_minsdk29.sql",
-        "src/trace_processor/stdlib/android/startup/internal_startups_minsdk33.sql",
-        "src/trace_processor/stdlib/android/startup/startups.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib/android:android
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_android_android",
-    srcs = [
-        "src/trace_processor/stdlib/android/battery.sql",
-        "src/trace_processor/stdlib/android/battery_stats.sql",
-        "src/trace_processor/stdlib/android/binder.sql",
-        "src/trace_processor/stdlib/android/monitor_contention.sql",
-        "src/trace_processor/stdlib/android/network_packets.sql",
-        "src/trace_processor/stdlib/android/process_metadata.sql",
-        "src/trace_processor/stdlib/android/slices.sql",
-        "src/trace_processor/stdlib/android/statsd.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib/chrome:chrome_sql
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_chrome_chrome_sql",
-    srcs = [
-        "src/trace_processor/stdlib/chrome/chrome_scroll_janks.sql",
-        "src/trace_processor/stdlib/chrome/chrome_scrolls.sql",
-        "src/trace_processor/stdlib/chrome/cpu_powerups.sql",
-        "src/trace_processor/stdlib/chrome/histograms.sql",
-        "src/trace_processor/stdlib/chrome/metadata.sql",
-        "src/trace_processor/stdlib/chrome/speedometer.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib/common:common
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_common_common",
-    srcs = [
-        "src/trace_processor/stdlib/common/counters.sql",
-        "src/trace_processor/stdlib/common/cpus.sql",
-        "src/trace_processor/stdlib/common/metadata.sql",
-        "src/trace_processor/stdlib/common/percentiles.sql",
-        "src/trace_processor/stdlib/common/slices.sql",
-        "src/trace_processor/stdlib/common/timestamps.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib/experimental:experimental
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_experimental_experimental",
-    srcs = [
-        "src/trace_processor/stdlib/experimental/android_broadcast.sql",
-        "src/trace_processor/stdlib/experimental/flat_slices.sql",
-        "src/trace_processor/stdlib/experimental/proto_path.sql",
-        "src/trace_processor/stdlib/experimental/slices.sql",
-        "src/trace_processor/stdlib/experimental/thread_executing_span.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib/pkvm:pkvm
-perfetto_filegroup(
-    name = "src_trace_processor_stdlib_pkvm_pkvm",
-    srcs = [
-        "src/trace_processor/stdlib/pkvm/hypervisor.sql",
-    ],
-)
-
-# GN target: //src/trace_processor/stdlib:gen_amalgamated_stdlib
-perfetto_cc_amalgamated_sql(
-    name = "src_trace_processor_stdlib_gen_amalgamated_stdlib",
-    deps = [
-        ":src_trace_processor_stdlib_android_android",
-        ":src_trace_processor_stdlib_android_startup_startup",
-        ":src_trace_processor_stdlib_chrome_chrome_sql",
-        ":src_trace_processor_stdlib_common_common",
-        ":src_trace_processor_stdlib_experimental_experimental",
-        ":src_trace_processor_stdlib_pkvm_pkvm",
-    ],
-    outs = [
-        "src/trace_processor/stdlib/amalgamated_stdlib.h",
-    ],
-    namespace = "stdlib",
-)
-
 # GN target: //src/trace_processor/storage:storage
 perfetto_filegroup(
     name = "src_trace_processor_storage_storage",
@@ -5089,12 +5097,13 @@
         ":src_trace_processor_lib",
         ":src_trace_processor_metatrace",
         ":src_trace_processor_metrics_metrics",
-        ":src_trace_processor_prelude_functions_functions",
-        ":src_trace_processor_prelude_functions_interface",
-        ":src_trace_processor_prelude_operators_operators",
-        ":src_trace_processor_prelude_table_functions_interface",
-        ":src_trace_processor_prelude_table_functions_table_functions",
-        ":src_trace_processor_prelude_table_functions_tables",
+        ":src_trace_processor_perfetto_sql_engine_engine",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
         ":src_trace_processor_sorter_sorter",
         ":src_trace_processor_sqlite_query_constraints",
         ":src_trace_processor_sqlite_sqlite",
@@ -5184,8 +5193,8 @@
                ":src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
                ":src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-               ":src_trace_processor_prelude_tables_views_tables_views",
-               ":src_trace_processor_stdlib_gen_amalgamated_stdlib",
+               ":src_trace_processor_perfetto_sql_prelude_prelude",
+               ":src_trace_processor_perfetto_sql_stdlib_stdlib",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
            PERFETTO_CONFIG.deps.sqlite +
            PERFETTO_CONFIG.deps.sqlite_ext_percentile +
@@ -5247,12 +5256,13 @@
         ":src_trace_processor_lib",
         ":src_trace_processor_metatrace",
         ":src_trace_processor_metrics_metrics",
-        ":src_trace_processor_prelude_functions_functions",
-        ":src_trace_processor_prelude_functions_interface",
-        ":src_trace_processor_prelude_operators_operators",
-        ":src_trace_processor_prelude_table_functions_interface",
-        ":src_trace_processor_prelude_table_functions_table_functions",
-        ":src_trace_processor_prelude_table_functions_tables",
+        ":src_trace_processor_perfetto_sql_engine_engine",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
         ":src_trace_processor_rpc_httpd",
         ":src_trace_processor_rpc_rpc",
         ":src_trace_processor_sorter_sorter",
@@ -5334,8 +5344,8 @@
                ":src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
                ":src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-               ":src_trace_processor_prelude_tables_views_tables_views",
-               ":src_trace_processor_stdlib_gen_amalgamated_stdlib",
+               ":src_trace_processor_perfetto_sql_prelude_prelude",
+               ":src_trace_processor_perfetto_sql_stdlib_stdlib",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
            PERFETTO_CONFIG.deps.linenoise +
            PERFETTO_CONFIG.deps.protobuf_full +
@@ -5464,12 +5474,13 @@
         ":src_trace_processor_lib",
         ":src_trace_processor_metatrace",
         ":src_trace_processor_metrics_metrics",
-        ":src_trace_processor_prelude_functions_functions",
-        ":src_trace_processor_prelude_functions_interface",
-        ":src_trace_processor_prelude_operators_operators",
-        ":src_trace_processor_prelude_table_functions_interface",
-        ":src_trace_processor_prelude_table_functions_table_functions",
-        ":src_trace_processor_prelude_table_functions_tables",
+        ":src_trace_processor_perfetto_sql_engine_engine",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_operators_operators",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_interface",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_table_functions",
+        ":src_trace_processor_perfetto_sql_intrinsics_table_functions_tables",
         ":src_trace_processor_sorter_sorter",
         ":src_trace_processor_sqlite_query_constraints",
         ":src_trace_processor_sqlite_sqlite",
@@ -5549,8 +5560,8 @@
                ":src_trace_processor_metrics_gen_cc_all_webview_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
                ":src_trace_processor_metrics_sql_gen_amalgamated_sql_metrics",
-               ":src_trace_processor_prelude_tables_views_tables_views",
-               ":src_trace_processor_stdlib_gen_amalgamated_stdlib",
+               ":src_trace_processor_perfetto_sql_prelude_prelude",
+               ":src_trace_processor_perfetto_sql_stdlib_stdlib",
                ":src_traceconv_gen_cc_trace_descriptor",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
            PERFETTO_CONFIG.deps.sqlite +
diff --git a/infra/perfetto.dev/BUILD.gn b/infra/perfetto.dev/BUILD.gn
index 6e514f1..75f69b7 100644
--- a/infra/perfetto.dev/BUILD.gn
+++ b/infra/perfetto.dev/BUILD.gn
@@ -258,12 +258,12 @@
 action("gen_stdlib_docs_md") {
   script = "src/gen_stdlib_docs_md.py"
   label_info = get_label_info(
-          "../../src/trace_processor/stdlib:gen_amalgamated_stdlib_json_docs",
+          "../../src/trace_processor/perfetto_sql/stdlib:stdlib_json_docs",
           "target_gen_dir")
   absolute_input_path = label_info + "/stdlib_docs.json"
   deps = [
     "../../python:trace_processor_stdlib_docs",
-    "../../src/trace_processor/stdlib:gen_amalgamated_stdlib_json_docs",
+    "../../src/trace_processor/perfetto_sql/stdlib:stdlib_json_docs",
   ]
   outputs = [ stdlib_docs_md ]
   args = [
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index 04814a6..dbfb37f 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -169,12 +169,13 @@
       "importers/proto:minimal",
       "importers/systrace:full",
       "metrics",
-      "prelude/functions",
-      "prelude/operators",
-      "prelude/table_functions",
-      "prelude/tables_views",
+      "perfetto_sql/engine",
+      "perfetto_sql/intrinsics/functions",
+      "perfetto_sql/intrinsics/operators",
+      "perfetto_sql/intrinsics/table_functions",
+      "perfetto_sql/prelude",
+      "perfetto_sql/stdlib",
       "sqlite",
-      "stdlib:gen_amalgamated_stdlib",
       "storage",
       "tables",
       "types",
@@ -290,9 +291,9 @@
   }
   if (enable_perfetto_trace_processor_sqlite) {
     deps += [
-      "prelude/functions:unittests",
-      "prelude/operators:unittests",
-      "prelude/table_functions:unittests",
+      "perfetto_sql/intrinsics/functions:unittests",
+      "perfetto_sql/intrinsics/operators:unittests",
+      "perfetto_sql/intrinsics/table_functions:unittests",
       "sqlite:unittests",
     ]
   }
diff --git a/src/trace_processor/iterator_impl.cc b/src/trace_processor/iterator_impl.cc
index b859c57..4b3e997 100644
--- a/src/trace_processor/iterator_impl.cc
+++ b/src/trace_processor/iterator_impl.cc
@@ -18,7 +18,7 @@
 
 #include "perfetto/base/time.h"
 #include "perfetto/trace_processor/trace_processor_storage.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/trace_processor_impl.h"
diff --git a/src/trace_processor/iterator_impl.h b/src/trace_processor/iterator_impl.h
index 722c8c6..89f300b 100644
--- a/src/trace_processor/iterator_impl.h
+++ b/src/trace_processor/iterator_impl.h
@@ -30,7 +30,7 @@
 #include "perfetto/trace_processor/basic_types.h"
 #include "perfetto/trace_processor/iterator.h"
 #include "perfetto/trace_processor/status.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index 5a9757b..6fe12ad 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -47,7 +47,8 @@
     "../../../protos/perfetto/trace_processor:metrics_impl_zero",
     "../../base",
     "../../protozero:protozero",
-    "../prelude/functions",
+    "../perfetto_sql/engine",
+    "../perfetto_sql/intrinsics/functions",
     "../sqlite",
   ]
   public_deps = [
diff --git a/src/trace_processor/metrics/metrics.cc b/src/trace_processor/metrics/metrics.cc
index 5b0e727..6b00860 100644
--- a/src/trace_processor/metrics/metrics.cc
+++ b/src/trace_processor/metrics/metrics.cc
@@ -25,7 +25,7 @@
 #include "perfetto/ext/base/utils.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "sqlite3.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/tp_metatrace.h"
diff --git a/src/trace_processor/metrics/metrics.h b/src/trace_processor/metrics/metrics.h
index 889a4d9..8a861c5 100644
--- a/src/trace_processor/metrics/metrics.h
+++ b/src/trace_processor/metrics/metrics.h
@@ -27,8 +27,8 @@
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "perfetto/trace_processor/trace_processor.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/util/descriptors.h"
 
 #include "protos/perfetto/trace_processor/metrics_impl.pbzero.h"
diff --git a/src/trace_processor/prelude/operators/BUILD.gn b/src/trace_processor/perfetto_sql/engine/BUILD.gn
similarity index 71%
rename from src/trace_processor/prelude/operators/BUILD.gn
rename to src/trace_processor/perfetto_sql/engine/BUILD.gn
index 3d994a7..d3c926a 100644
--- a/src/trace_processor/prelude/operators/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/engine/BUILD.gn
@@ -1,4 +1,4 @@
-# Copyright (C) 2022 The Android Open Source Project
+# Copyright (C) 2019 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -16,19 +16,20 @@
 
 assert(enable_perfetto_trace_processor_sqlite)
 
-source_set("operators") {
+source_set("engine") {
   sources = [
-    "span_join_operator.cc",
-    "span_join_operator.h",
-    "window_operator.cc",
-    "window_operator.h",
+    "perfetto_sql_engine.cc",
+    "perfetto_sql_engine.h",
+    "perfetto_sql_parser.cc",
+    "perfetto_sql_parser.h",
   ]
   deps = [
     "../..:metatrace",
     "../../../../gn:default_deps",
     "../../../../gn:sqlite",
-    "../../../../include/perfetto/trace_processor",
     "../../../base",
+    "../../perfetto_sql/intrinsics/functions:interface",
+    "../../perfetto_sql/intrinsics/table_functions:interface",
     "../../sqlite",
     "../../util",
   ]
@@ -36,12 +37,12 @@
 
 perfetto_unittest_source_set("unittests") {
   testonly = true
-  sources = [ "span_join_operator_unittest.cc" ]
+  sources = [ "perfetto_sql_parser_unittest.cc" ]
   deps = [
-    ":operators",
+    ":engine",
     "../../../../gn:default_deps",
     "../../../../gn:gtest_and_gmock",
-    "../../../../gn:sqlite",
+    "../../../base",
     "../../sqlite",
   ]
 }
diff --git a/src/trace_processor/sqlite/perfetto_sql_engine.cc b/src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.cc
similarity index 97%
rename from src/trace_processor/sqlite/perfetto_sql_engine.cc
rename to src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.cc
index c174814..e0a0243 100644
--- a/src/trace_processor/sqlite/perfetto_sql_engine.cc
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 
 #include <optional>
 #include <string>
@@ -22,8 +22,8 @@
 
 #include "perfetto/base/status.h"
 #include "perfetto/ext/base/string_utils.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h"
 #include "src/trace_processor/sqlite/db_sqlite_table.h"
-#include "src/trace_processor/sqlite/perfetto_sql_parser.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"
diff --git a/src/trace_processor/sqlite/perfetto_sql_engine.h b/src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h
similarity index 94%
rename from src/trace_processor/sqlite/perfetto_sql_engine.h
rename to src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h
index 185c7fe..598a01d 100644
--- a/src/trace_processor/sqlite/perfetto_sql_engine.h
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h
@@ -14,10 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_ENGINE_H_
-#define SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_ENGINE_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_ENGINE_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_ENGINE_H_
 
 #include "perfetto/ext/base/status_or.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"
@@ -193,4 +195,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_ENGINE_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_ENGINE_H_
diff --git a/src/trace_processor/sqlite/perfetto_sql_parser.cc b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
similarity index 96%
rename from src/trace_processor/sqlite/perfetto_sql_parser.cc
rename to src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
index b2e0ea9..8ac2730 100644
--- a/src/trace_processor/sqlite/perfetto_sql_parser.cc
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/sqlite/perfetto_sql_parser.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h"
 #include "perfetto/base/logging.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_tokenizer.h"
diff --git a/src/trace_processor/sqlite/perfetto_sql_parser.h b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h
similarity index 92%
rename from src/trace_processor/sqlite/perfetto_sql_parser.h
rename to src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h
index a4a5cc5..7b8df99 100644
--- a/src/trace_processor/sqlite/perfetto_sql_parser.h
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_PARSER_H_
-#define SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_PARSER_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_PARSER_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_PARSER_H_
 
 #include <string_view>
 #include <variant>
@@ -84,4 +84,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_SQLITE_PERFETTO_SQL_PARSER_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_ENGINE_PERFETTO_SQL_PARSER_H_
diff --git a/src/trace_processor/sqlite/perfetto_sql_parser_unittest.cc b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser_unittest.cc
similarity index 96%
rename from src/trace_processor/sqlite/perfetto_sql_parser_unittest.cc
rename to src/trace_processor/perfetto_sql/engine/perfetto_sql_parser_unittest.cc
index 7bbf933..f82edae 100644
--- a/src/trace_processor/sqlite/perfetto_sql_parser_unittest.cc
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/sqlite/perfetto_sql_parser.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.h"
 
 #include <variant>
 #include <vector>
diff --git a/src/trace_processor/perfetto_sql/intrinsics/functions/BUILD.gn b/src/trace_processor/perfetto_sql/intrinsics/functions/BUILD.gn
new file mode 100644
index 0000000..825e4a6
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/BUILD.gn
@@ -0,0 +1,98 @@
+# Copyright (C) 2022 The Android Open Source Project
+#
+# 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.
+
+import("../../../../../gn/test.gni")
+
+assert(enable_perfetto_trace_processor_sqlite)
+
+source_set("functions") {
+  sources = [
+    "clock_functions.h",
+    "create_function.cc",
+    "create_function.h",
+    "create_function_internal.cc",
+    "create_function_internal.h",
+    "create_view_function.cc",
+    "create_view_function.h",
+    "import.cc",
+    "import.h",
+    "layout_functions.cc",
+    "layout_functions.h",
+    "math.cc",
+    "math.h",
+    "pprof_functions.cc",
+    "pprof_functions.h",
+    "sqlite3_str_split.cc",
+    "sqlite3_str_split.h",
+    "stack_functions.cc",
+    "stack_functions.h",
+    "to_ftrace.cc",
+    "to_ftrace.h",
+    "utils.h",
+    "window_functions.h",
+  ]
+  deps = [
+    "../../..:demangle",
+    "../../..:export_json",
+    "../../..:metatrace",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:sqlite",
+    "../../../../../include/perfetto/protozero:protozero",
+    "../../../../../protos/perfetto/common:zero",
+    "../../../../../protos/perfetto/trace:zero",
+    "../../../../../protos/perfetto/trace/ftrace:zero",
+    "../../../../../protos/perfetto/trace_processor:zero",
+    "../../../../base",
+    "../../../containers",
+    "../../../db",
+    "../../../importers/common",
+    "../../../importers/ftrace:ftrace_descriptors",
+    "../../../perfetto_sql/intrinsics/table_functions",
+    "../../../sqlite",
+    "../../../storage",
+    "../../../types",
+    "../../../util",
+    "../../../util:profile_builder",
+    "../../../util:sql_argument",
+    "../../../util:stdlib",
+    "../../engine",
+  ]
+  public_deps = [ ":interface" ]
+}
+
+source_set("interface") {
+  sources = [
+    "sql_function.cc",
+    "sql_function.h",
+  ]
+  deps = [
+    "../../../../../gn:default_deps",
+    "../../../../../gn:sqlite",
+    "../../../../../include/perfetto/trace_processor:basic_types",
+    "../../../../base",
+  ]
+}
+
+perfetto_unittest_source_set("unittests") {
+  testonly = true
+  sources = [ "sqlite3_str_split_unittest.cc" ]
+  deps = [
+    ":functions",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:gtest_and_gmock",
+    "../../../../../gn:sqlite",
+    "../../../../base",
+    "../../../sqlite",
+  ]
+}
diff --git a/src/trace_processor/prelude/functions/clock_functions.h b/src/trace_processor/perfetto_sql/intrinsics/functions/clock_functions.h
similarity index 92%
rename from src/trace_processor/prelude/functions/clock_functions.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/clock_functions.h
index 45e2761..2179097 100644
--- a/src/trace_processor/prelude/functions/clock_functions.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/clock_functions.h
@@ -14,18 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CLOCK_FUNCTIONS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CLOCK_FUNCTIONS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CLOCK_FUNCTIONS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CLOCK_FUNCTIONS_H_
 
 #include <sqlite3.h>
 #include <unordered_map>
 #include "perfetto/ext/base/base64.h"
 #include "protos/perfetto/common/builtin_clock.pbzero.h"
 #include "src/trace_processor/importers/common/clock_converter.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 #include "src/trace_processor/util/status_macros.h"
 
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 
 namespace perfetto {
 namespace trace_processor {
@@ -170,4 +170,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CLOCK_FUNCTIONS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CLOCK_FUNCTIONS_H_
diff --git a/src/trace_processor/prelude/functions/create_function.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function.cc
similarity index 98%
rename from src/trace_processor/prelude/functions/create_function.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_function.cc
index a16f949..2e6c6a2 100644
--- a/src/trace_processor/prelude/functions/create_function.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function.cc
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/create_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h"
 
 #include <queue>
 #include <stack>
 
 #include "perfetto/base/status.h"
 #include "perfetto/trace_processor/basic_types.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"
diff --git a/src/trace_processor/prelude/functions/create_function.h b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h
similarity index 85%
rename from src/trace_processor/prelude/functions/create_function.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h
index 612abf1..5e54006 100644
--- a/src/trace_processor/prelude/functions/create_function.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_H_
 
 #include <sqlite3.h>
 #include <unordered_map>
 
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sqlite_table.h"
 
@@ -64,4 +64,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_H_
diff --git a/src/trace_processor/prelude/functions/create_function_internal.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.cc
similarity index 97%
rename from src/trace_processor/prelude/functions/create_function_internal.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.cc
index 1e27bed..e500d9b 100644
--- a/src/trace_processor/prelude/functions/create_function_internal.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 
 #include "perfetto/base/status.h"
 #include "perfetto/ext/base/string_view.h"
diff --git a/src/trace_processor/prelude/functions/create_function_internal.h b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h
similarity index 88%
rename from src/trace_processor/prelude/functions/create_function_internal.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h
index f1b44ed..0f841a3 100644
--- a/src/trace_processor/prelude/functions/create_function_internal.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
 
 #include <sqlite3.h>
 #include <optional>
@@ -61,4 +61,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_FUNCTION_INTERNAL_H_
diff --git a/src/trace_processor/prelude/functions/create_view_function.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.cc
similarity index 98%
rename from src/trace_processor/prelude/functions/create_view_function.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.cc
index 78f1640..4fbf8ad 100644
--- a/src/trace_processor/prelude/functions/create_view_function.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/create_view_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h"
 
 #include <numeric>
 #include <optional>
@@ -23,8 +23,8 @@
 #include "perfetto/ext/base/string_utils.h"
 #include "perfetto/ext/base/string_view.h"
 #include "perfetto/trace_processor/basic_types.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sqlite_table.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
diff --git a/src/trace_processor/prelude/functions/create_view_function.h b/src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h
similarity index 79%
rename from src/trace_processor/prelude/functions/create_view_function.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h
index 50ff80f..7ce53cf 100644
--- a/src/trace_processor/prelude/functions/create_view_function.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
 
 #include <sqlite3.h>
 #include <unordered_map>
 
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 
 namespace perfetto {
 namespace trace_processor {
@@ -47,4 +47,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_CREATE_VIEW_FUNCTION_H_
diff --git a/src/trace_processor/prelude/functions/import.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/import.cc
similarity index 94%
rename from src/trace_processor/prelude/functions/import.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/import.cc
index fd47a9c..48ace71 100644
--- a/src/trace_processor/prelude/functions/import.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/import.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/import.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/import.h"
 
 #include <numeric>
 
@@ -22,7 +22,7 @@
 #include "perfetto/ext/base/string_utils.h"
 #include "perfetto/ext/base/string_view.h"
 #include "perfetto/trace_processor/basic_types.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sqlite_table.h"
diff --git a/src/trace_processor/prelude/functions/import.h b/src/trace_processor/perfetto_sql/intrinsics/functions/import.h
similarity index 78%
rename from src/trace_processor/prelude/functions/import.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/import.h
index bd53c7d..2e6784d 100644
--- a/src/trace_processor/prelude/functions/import.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/import.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_IMPORT_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_IMPORT_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_IMPORT_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_IMPORT_H_
 
 #include <sqlite3.h>
 #include <string>
@@ -23,8 +23,8 @@
 
 #include "perfetto/ext/base/flat_hash_map.h"
 #include "perfetto/trace_processor/trace_processor.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/util/sql_modules.h"
 
 namespace perfetto {
@@ -48,4 +48,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_IMPORT_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_IMPORT_H_
diff --git a/src/trace_processor/prelude/functions/layout_functions.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.cc
similarity index 98%
rename from src/trace_processor/prelude/functions/layout_functions.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.cc
index 2210355..cc5cc8a 100644
--- a/src/trace_processor/prelude/functions/layout_functions.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.cc
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "src/trace_processor/prelude/functions/layout_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h"
 
 #include <queue>
 #include <vector>
diff --git a/src/trace_processor/prelude/functions/layout_functions.h b/src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h
similarity index 84%
rename from src/trace_processor/prelude/functions/layout_functions.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h
index a88d8ab..8d3543c 100644
--- a/src/trace_processor/prelude/functions/layout_functions.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h
@@ -12,8 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_LAYOUT_FUNCTIONS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_LAYOUT_FUNCTIONS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_LAYOUT_FUNCTIONS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_LAYOUT_FUNCTIONS_H_
 
 #include <sqlite3.h>
 
@@ -37,4 +37,4 @@
 
 }  // namespace perfetto::trace_processor
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_LAYOUT_FUNCTIONS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_LAYOUT_FUNCTIONS_H_
diff --git a/src/trace_processor/prelude/functions/math.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/math.cc
similarity index 91%
rename from src/trace_processor/prelude/functions/math.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/math.cc
index 14fc253..0a0e00a 100644
--- a/src/trace_processor/prelude/functions/math.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/math.cc
@@ -12,15 +12,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "src/trace_processor/prelude/functions/math.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/math.h"
 
 #include <cmath>
 
 #include "perfetto/base/logging.h"
 #include "perfetto/base/status.h"
 #include "perfetto/trace_processor/basic_types.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/util/status_macros.h"
 
diff --git a/src/trace_processor/prelude/functions/math.h b/src/trace_processor/perfetto_sql/intrinsics/functions/math.h
similarity index 81%
rename from src/trace_processor/prelude/functions/math.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/math.h
index 746ab44..66ad4d5 100644
--- a/src/trace_processor/prelude/functions/math.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/math.h
@@ -12,8 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_MATH_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_MATH_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_MATH_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_MATH_H_
 
 #include "perfetto/base/status.h"
 
@@ -28,4 +28,4 @@
 
 }  // namespace perfetto::trace_processor
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_MATH_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_MATH_H_
diff --git a/src/trace_processor/prelude/functions/pprof_functions.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.cc
similarity index 98%
rename from src/trace_processor/prelude/functions/pprof_functions.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.cc
index 98df2d1..c3126e0 100644
--- a/src/trace_processor/prelude/functions/pprof_functions.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/pprof_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h"
 
 #include <stdlib.h>
 #include <cinttypes>
diff --git a/src/trace_processor/prelude/functions/pprof_functions.h b/src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h
similarity index 78%
rename from src/trace_processor/prelude/functions/pprof_functions.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h
index f088970..24e6d72 100644
--- a/src/trace_processor/prelude/functions/pprof_functions.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_PPROF_FUNCTIONS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_PPROF_FUNCTIONS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_PPROF_FUNCTIONS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_PPROF_FUNCTIONS_H_
 
 #include <sqlite3.h>
 
@@ -33,4 +33,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_PPROF_FUNCTIONS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_PPROF_FUNCTIONS_H_
diff --git a/src/trace_processor/prelude/functions/sql_function.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.cc
similarity index 91%
rename from src/trace_processor/prelude/functions/sql_function.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.cc
index 26bac5e..04a9996 100644
--- a/src/trace_processor/prelude/functions/sql_function.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/prelude/functions/sql_function.h b/src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h
similarity index 94%
rename from src/trace_processor/prelude/functions/sql_function.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h
index ba7fab7..cd709ea 100644
--- a/src/trace_processor/prelude/functions/sql_function.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQL_FUNCTION_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQL_FUNCTION_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQL_FUNCTION_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQL_FUNCTION_H_
 
 #include <sqlite3.h>
 #include <memory>
@@ -111,4 +111,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQL_FUNCTION_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQL_FUNCTION_H_
diff --git a/src/trace_processor/prelude/functions/sqlite3_str_split.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.cc
similarity index 96%
rename from src/trace_processor/prelude/functions/sqlite3_str_split.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.cc
index 57508f3..3cd9325 100644
--- a/src/trace_processor/prelude/functions/sqlite3_str_split.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/sqlite3_str_split.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h"
 
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 
diff --git a/src/trace_processor/prelude/functions/sqlite3_str_split.h b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h
similarity index 75%
rename from src/trace_processor/prelude/functions/sqlite3_str_split.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h
index e6a6783..1f8fbcc 100644
--- a/src/trace_processor/prelude/functions/sqlite3_str_split.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQLITE3_STR_SPLIT_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQLITE3_STR_SPLIT_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQLITE3_STR_SPLIT_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQLITE3_STR_SPLIT_H_
 
 struct sqlite3;
 
@@ -27,4 +27,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_SQLITE3_STR_SPLIT_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_SQLITE3_STR_SPLIT_H_
diff --git a/src/trace_processor/prelude/functions/sqlite3_str_split_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split_unittest.cc
similarity index 97%
rename from src/trace_processor/prelude/functions/sqlite3_str_split_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split_unittest.cc
index f5f6b15..02ed9f6 100644
--- a/src/trace_processor/prelude/functions/sqlite3_str_split_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/sqlite3_str_split.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h"
 
 #include <sqlite3.h>
 #include <string>
diff --git a/src/trace_processor/prelude/functions/stack_functions.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.cc
similarity index 97%
rename from src/trace_processor/prelude/functions/stack_functions.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.cc
index b73afa0..86d3c74 100644
--- a/src/trace_processor/prelude/functions/stack_functions.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/stack_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h"
 
 #include <stdlib.h>
 #include <cstdint>
@@ -31,8 +31,8 @@
 #include "perfetto/trace_processor/basic_types.h"
 #include "perfetto/trace_processor/status.h"
 #include "protos/perfetto/trace_processor/stack.pbzero.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/types/trace_processor_context.h"
diff --git a/src/trace_processor/prelude/functions/stack_functions.h b/src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h
similarity index 89%
rename from src/trace_processor/prelude/functions/stack_functions.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h
index 011361d..36ae626 100644
--- a/src/trace_processor/prelude/functions/stack_functions.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_STACK_FUNCTIONS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_STACK_FUNCTIONS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_STACK_FUNCTIONS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_STACK_FUNCTIONS_H_
 
 #include <sqlite3.h>
 #include <cstddef>
@@ -56,4 +56,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_STACK_FUNCTIONS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_STACK_FUNCTIONS_H_
diff --git a/src/trace_processor/prelude/functions/to_ftrace.cc b/src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.cc
similarity index 99%
rename from src/trace_processor/prelude/functions/to_ftrace.cc
rename to src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.cc
index 5bb138f..0b9a7f2 100644
--- a/src/trace_processor/prelude/functions/to_ftrace.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/functions/to_ftrace.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h"
 
 #include "perfetto/base/compiler.h"
 #include "perfetto/base/status.h"
diff --git a/src/trace_processor/prelude/functions/to_ftrace.h b/src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h
similarity index 85%
rename from src/trace_processor/prelude/functions/to_ftrace.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h
index 9c55067..b9f5b64 100644
--- a/src/trace_processor/prelude/functions/to_ftrace.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_TO_FTRACE_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_TO_FTRACE_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_TO_FTRACE_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_TO_FTRACE_H_
 
 #include "perfetto/ext/base/flat_hash_map.h"
 #include "perfetto/ext/base/string_writer.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 
@@ -61,4 +61,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_TO_FTRACE_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_TO_FTRACE_H_
diff --git a/src/trace_processor/prelude/functions/utils.h b/src/trace_processor/perfetto_sql/intrinsics/functions/utils.h
similarity index 96%
rename from src/trace_processor/prelude/functions/utils.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/utils.h
index ac848d0..f859c62 100644
--- a/src/trace_processor/prelude/functions/utils.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/utils.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_UTILS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_UTILS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_UTILS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_UTILS_H_
 
 #include <sqlite3.h>
 #include <unordered_map>
@@ -26,8 +26,8 @@
 #include "protos/perfetto/common/builtin_clock.pbzero.h"
 #include "src/trace_processor/export_json.h"
 #include "src/trace_processor/importers/common/clock_tracker.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/util/status_macros.h"
 
@@ -326,4 +326,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_UTILS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_UTILS_H_
diff --git a/src/trace_processor/prelude/functions/window_functions.h b/src/trace_processor/perfetto_sql/intrinsics/functions/window_functions.h
similarity index 91%
rename from src/trace_processor/prelude/functions/window_functions.h
rename to src/trace_processor/perfetto_sql/intrinsics/functions/window_functions.h
index a3fbeaf..ebcb844 100644
--- a/src/trace_processor/prelude/functions/window_functions.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/functions/window_functions.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_WINDOW_FUNCTIONS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_WINDOW_FUNCTIONS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_WINDOW_FUNCTIONS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_WINDOW_FUNCTIONS_H_
 
 #include <sqlite3.h>
 #include <unordered_map>
@@ -25,10 +25,10 @@
 #include "protos/perfetto/common/builtin_clock.pbzero.h"
 #include "src/trace_processor/export_json.h"
 #include "src/trace_processor/importers/common/clock_tracker.h"
-#include "src/trace_processor/prelude/functions/create_function_internal.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function_internal.h"
 #include "src/trace_processor/util/status_macros.h"
 
-#include "src/trace_processor/prelude/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
 
 namespace perfetto {
 namespace trace_processor {
@@ -153,4 +153,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_FUNCTIONS_WINDOW_FUNCTIONS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_FUNCTIONS_WINDOW_FUNCTIONS_H_
diff --git a/src/trace_processor/prelude/operators/BUILD.gn b/src/trace_processor/perfetto_sql/intrinsics/operators/BUILD.gn
similarity index 69%
copy from src/trace_processor/prelude/operators/BUILD.gn
copy to src/trace_processor/perfetto_sql/intrinsics/operators/BUILD.gn
index 3d994a7..2f47b48 100644
--- a/src/trace_processor/prelude/operators/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/test.gni")
+import("../../../../../gn/test.gni")
 
 assert(enable_perfetto_trace_processor_sqlite)
 
@@ -24,13 +24,14 @@
     "window_operator.h",
   ]
   deps = [
-    "../..:metatrace",
-    "../../../../gn:default_deps",
-    "../../../../gn:sqlite",
-    "../../../../include/perfetto/trace_processor",
-    "../../../base",
-    "../../sqlite",
-    "../../util",
+    "../../..:metatrace",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:sqlite",
+    "../../../../../include/perfetto/trace_processor",
+    "../../../../base",
+    "../../../sqlite",
+    "../../../util",
+    "../../engine",
   ]
 }
 
@@ -39,9 +40,10 @@
   sources = [ "span_join_operator_unittest.cc" ]
   deps = [
     ":operators",
-    "../../../../gn:default_deps",
-    "../../../../gn:gtest_and_gmock",
-    "../../../../gn:sqlite",
-    "../../sqlite",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:gtest_and_gmock",
+    "../../../../../gn:sqlite",
+    "../../../sqlite",
+    "../../engine",
   ]
 }
diff --git a/src/trace_processor/prelude/operators/span_join_operator.cc b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.cc
similarity index 99%
rename from src/trace_processor/prelude/operators/span_join_operator.cc
rename to src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.cc
index 153166c..e7df68f 100644
--- a/src/trace_processor/prelude/operators/span_join_operator.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/operators/span_join_operator.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h"
 
 #include <sqlite3.h>
 #include <string.h>
@@ -28,7 +28,7 @@
 #include "perfetto/ext/base/string_splitter.h"
 #include "perfetto/ext/base/string_utils.h"
 #include "perfetto/ext/base/string_view.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/tp_metatrace.h"
 #include "src/trace_processor/util/status_macros.h"
diff --git a/src/trace_processor/prelude/operators/span_join_operator.h b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h
similarity index 97%
rename from src/trace_processor/prelude/operators/span_join_operator.h
rename to src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h
index 8b442ed..2c663c8 100644
--- a/src/trace_processor/prelude/operators/span_join_operator.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_SPAN_JOIN_OPERATOR_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_SPAN_JOIN_OPERATOR_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_SPAN_JOIN_OPERATOR_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_SPAN_JOIN_OPERATOR_H_
 
 #include <sqlite3.h>
 
@@ -443,4 +443,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_SPAN_JOIN_OPERATOR_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_SPAN_JOIN_OPERATOR_H_
diff --git a/src/trace_processor/prelude/operators/span_join_operator_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator_unittest.cc
similarity index 98%
rename from src/trace_processor/prelude/operators/span_join_operator_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator_unittest.cc
index 6c76f34..2532523 100644
--- a/src/trace_processor/prelude/operators/span_join_operator_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/operators/span_join_operator.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h"
 
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"
 #include "test/gtest_and_gmock.h"
 
diff --git a/src/trace_processor/prelude/operators/window_operator.cc b/src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.cc
similarity index 98%
rename from src/trace_processor/prelude/operators/window_operator.cc
rename to src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.cc
index 68f2ff4..ba591a9 100644
--- a/src/trace_processor/prelude/operators/window_operator.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/operators/window_operator.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h"
 
 #include "perfetto/base/status.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
diff --git a/src/trace_processor/prelude/operators/window_operator.h b/src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h
similarity index 91%
rename from src/trace_processor/prelude/operators/window_operator.h
rename to src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h
index 5f4af16..8e26961 100644
--- a/src/trace_processor/prelude/operators/window_operator.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_WINDOW_OPERATOR_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_WINDOW_OPERATOR_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
 
 #include <limits>
 #include <memory>
@@ -100,4 +100,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_OPERATORS_WINDOW_OPERATOR_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
diff --git a/src/trace_processor/prelude/table_functions/BUILD.gn b/src/trace_processor/perfetto_sql/intrinsics/table_functions/BUILD.gn
similarity index 72%
rename from src/trace_processor/prelude/table_functions/BUILD.gn
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/BUILD.gn
index 2a97bca..46e6e68 100644
--- a/src/trace_processor/prelude/table_functions/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/BUILD.gn
@@ -12,8 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto.gni")
-import("../../../../gn/perfetto_tp_tables.gni")
+import("../../../../../gn/perfetto.gni")
+import("../../../../../gn/perfetto_tp_tables.gni")
 
 assert(enable_perfetto_trace_processor_sqlite)
 
@@ -44,18 +44,18 @@
   ]
   deps = [
     ":tables",
-    "../../../../gn:default_deps",
-    "../../../../gn:sqlite",
-    "../../../base",
-    "../../containers",
-    "../../db",
-    "../../importers/proto:full",
-    "../../importers/proto:minimal",
-    "../../sqlite",
-    "../../storage",
-    "../../tables",
-    "../../types",
-    "../../util",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:sqlite",
+    "../../../../base",
+    "../../../containers",
+    "../../../db",
+    "../../../importers/proto:full",
+    "../../../importers/proto:minimal",
+    "../../../sqlite",
+    "../../../storage",
+    "../../../tables",
+    "../../../types",
+    "../../../util",
   ]
   public_deps = [ ":interface" ]
 }
@@ -66,16 +66,16 @@
     "table_function.h",
   ]
   deps = [
-    "../../../../gn:default_deps",
-    "../../../base",
-    "../../db",
-    "../../sqlite:query_constraints",
+    "../../../../../gn:default_deps",
+    "../../../../base",
+    "../../../db",
+    "../../../sqlite:query_constraints",
   ]
 }
 
 perfetto_tp_tables("tables") {
   sources = [ "tables.py" ]
-  deps = [ "../../tables:tables_python" ]
+  deps = [ "../../../tables:tables_python" ]
 }
 
 source_set("unittests") {
@@ -91,10 +91,10 @@
   deps = [
     ":table_functions",
     ":tables",
-    "../../../../gn:default_deps",
-    "../../../../gn:gtest_and_gmock",
-    "../../containers",
-    "../../importers/common",
-    "../../types",
+    "../../../../../gn:default_deps",
+    "../../../../../gn:gtest_and_gmock",
+    "../../../containers",
+    "../../../importers/common",
+    "../../../types",
   ]
 }
diff --git a/src/trace_processor/prelude/table_functions/ancestor.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.cc
similarity index 97%
rename from src/trace_processor/prelude/table_functions/ancestor.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.cc
index 9089b82..3e2ecd0 100644
--- a/src/trace_processor/prelude/table_functions/ancestor.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/ancestor.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h"
 
 #include <memory>
 #include <set>
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 #include "src/trace_processor/util/status_macros.h"
diff --git a/src/trace_processor/prelude/table_functions/ancestor.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h
similarity index 86%
rename from src/trace_processor/prelude/table_functions/ancestor.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h
index 755a482..a2dfce2 100644
--- a/src/trace_processor/prelude/table_functions/ancestor.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_ANCESTOR_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_ANCESTOR_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_ANCESTOR_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_ANCESTOR_H_
 
 #include <optional>
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -62,4 +62,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_ANCESTOR_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_ANCESTOR_H_
diff --git a/src/trace_processor/prelude/table_functions/ancestor_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor_unittest.cc
similarity index 94%
rename from src/trace_processor/prelude/table_functions/ancestor_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor_unittest.cc
index afc2d33..3ee642c 100644
--- a/src/trace_processor/prelude/table_functions/ancestor_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/ancestor.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h"
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "test/gtest_and_gmock.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/prelude/table_functions/connected_flow.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.cc
similarity index 96%
rename from src/trace_processor/prelude/table_functions/connected_flow.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.cc
index 1194901..7d6fdfd 100644
--- a/src/trace_processor/prelude/table_functions/connected_flow.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.cc
@@ -14,14 +14,14 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/connected_flow.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h"
 
 #include <memory>
 #include <queue>
 #include <set>
 
-#include "src/trace_processor/prelude/table_functions/ancestor.h"
-#include "src/trace_processor/prelude/table_functions/descendant.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 
diff --git a/src/trace_processor/prelude/table_functions/connected_flow.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h
similarity index 82%
rename from src/trace_processor/prelude/table_functions/connected_flow.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h
index 3ef3fc3..9206968 100644
--- a/src/trace_processor/prelude/table_functions/connected_flow.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 #include <queue>
@@ -67,4 +67,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_CONNECTED_FLOW_H_
diff --git a/src/trace_processor/prelude/table_functions/connected_flow_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow_unittest.cc
similarity index 94%
rename from src/trace_processor/prelude/table_functions/connected_flow_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow_unittest.cc
index ad28e05..13bf240 100644
--- a/src/trace_processor/prelude/table_functions/connected_flow_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/connected_flow.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h"
 
 #include "test/gtest_and_gmock.h"
 
diff --git a/src/trace_processor/prelude/table_functions/descendant.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.cc
similarity index 97%
rename from src/trace_processor/prelude/table_functions/descendant.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.cc
index 382edf2..b0da7fd 100644
--- a/src/trace_processor/prelude/table_functions/descendant.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/descendant.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h"
 
 #include <memory>
 #include <set>
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 #include "src/trace_processor/util/status_macros.h"
diff --git a/src/trace_processor/prelude/table_functions/descendant.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h
similarity index 85%
rename from src/trace_processor/prelude/table_functions/descendant.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h
index bffd9b5..aa0c220 100644
--- a/src/trace_processor/prelude/table_functions/descendant.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_DESCENDANT_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_DESCENDANT_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DESCENDANT_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DESCENDANT_H_
 
 #include <optional>
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -61,4 +61,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_DESCENDANT_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_DESCENDANT_H_
diff --git a/src/trace_processor/prelude/table_functions/descendant_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant_unittest.cc
similarity index 92%
rename from src/trace_processor/prelude/table_functions/descendant_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant_unittest.cc
index 67399d1..43e4ca8 100644
--- a/src/trace_processor/prelude/table_functions/descendant_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant_unittest.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/descendant.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h"
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "test/gtest_and_gmock.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/prelude/table_functions/experimental_annotated_stack.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.cc
similarity index 98%
rename from src/trace_processor/prelude/table_functions/experimental_annotated_stack.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.cc
index 09363f2..2b8e862 100644
--- a/src/trace_processor/prelude/table_functions/experimental_annotated_stack.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_annotated_stack.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h"
 
 #include <optional>
 
 #include "perfetto/ext/base/string_utils.h"
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 #include "src/trace_processor/storage/trace_storage.h"
 #include "src/trace_processor/types/trace_processor_context.h"
diff --git a/src/trace_processor/prelude/table_functions/experimental_annotated_stack.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h
similarity index 80%
rename from src/trace_processor/prelude/table_functions/experimental_annotated_stack.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h
index 519470a..49878a3 100644
--- a/src/trace_processor/prelude/table_functions/experimental_annotated_stack.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 
 namespace perfetto {
 namespace trace_processor {
@@ -50,4 +50,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_ANNOTATED_STACK_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_counter_dur.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.cc
similarity index 95%
rename from src/trace_processor/prelude/table_functions/experimental_counter_dur.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.cc
index a7d9814..20e9d1f 100644
--- a/src/trace_processor/prelude/table_functions/experimental_counter_dur.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_counter_dur.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h"
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/prelude/table_functions/experimental_counter_dur.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h
similarity index 81%
rename from src/trace_processor/prelude/table_functions/experimental_counter_dur.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h
index a8a8325..038bcb0 100644
--- a/src/trace_processor/prelude/table_functions/experimental_counter_dur.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -51,4 +51,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_COUNTER_DUR_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_counter_dur_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur_unittest.cc
similarity index 94%
rename from src/trace_processor/prelude/table_functions/experimental_counter_dur_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur_unittest.cc
index d812394..0c23af7 100644
--- a/src/trace_processor/prelude/table_functions/experimental_counter_dur_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur_unittest.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_counter_dur.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h"
 
 #include "test/gtest_and_gmock.h"
 
diff --git a/src/trace_processor/prelude/table_functions/experimental_flamegraph.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.cc
similarity index 98%
rename from src/trace_processor/prelude/table_functions/experimental_flamegraph.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.cc
index ae855be..9a87d07 100644
--- a/src/trace_processor/prelude/table_functions/experimental_flamegraph.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_flamegraph.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h"
 
 #include <unordered_set>
 
diff --git a/src/trace_processor/prelude/table_functions/experimental_flamegraph.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h
similarity index 78%
rename from src/trace_processor/prelude/table_functions/experimental_flamegraph.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h
index 60792f9..8a4cae5 100644
--- a/src/trace_processor/prelude/table_functions/experimental_flamegraph.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
 
-#include "src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h"
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -58,4 +58,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAMEGRAPH_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_flat_slice.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.cc
similarity index 98%
rename from src/trace_processor/prelude/table_functions/experimental_flat_slice.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.cc
index c9bdfed..41d5dd0 100644
--- a/src/trace_processor/prelude/table_functions/experimental_flat_slice.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_flat_slice.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h"
 
 #include <memory>
 #include <set>
diff --git a/src/trace_processor/prelude/table_functions/experimental_flat_slice.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h
similarity index 89%
rename from src/trace_processor/prelude/table_functions/experimental_flat_slice.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h
index 0cb10b5..218923e 100644
--- a/src/trace_processor/prelude/table_functions/experimental_flat_slice.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
 
 #include <optional>
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -80,4 +80,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_FLAT_SLICE_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_flat_slice_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice_unittest.cc
similarity index 98%
rename from src/trace_processor/prelude/table_functions/experimental_flat_slice_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice_unittest.cc
index 3d6a0f6..857d8d8 100644
--- a/src/trace_processor/prelude/table_functions/experimental_flat_slice_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice_unittest.cc
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "src/trace_processor/prelude/table_functions/experimental_flat_slice.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h"
 
 #include "test/gtest_and_gmock.h"
 
diff --git a/src/trace_processor/prelude/table_functions/experimental_sched_upid.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.cc
similarity index 92%
rename from src/trace_processor/prelude/table_functions/experimental_sched_upid.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.cc
index ba3a0c6..3103813 100644
--- a/src/trace_processor/prelude/table_functions/experimental_sched_upid.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.cc
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_sched_upid.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h"
 
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/prelude/table_functions/experimental_sched_upid.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h
similarity index 81%
rename from src/trace_processor/prelude/table_functions/experimental_sched_upid.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h
index 6837c18..800e933 100644
--- a/src/trace_processor/prelude/table_functions/experimental_sched_upid.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
 
 #include <set>
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -51,4 +51,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SCHED_UPID_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_slice_layout.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.cc
similarity index 97%
rename from src/trace_processor/prelude/table_functions/experimental_slice_layout.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.cc
index d6276dc..e703839 100644
--- a/src/trace_processor/prelude/table_functions/experimental_slice_layout.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.cc
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_slice_layout.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h"
 
 #include <optional>
 
 #include "perfetto/ext/base/string_splitter.h"
 #include "perfetto/ext/base/string_utils.h"
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "src/trace_processor/sqlite/sqlite_utils.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/prelude/table_functions/experimental_slice_layout.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h
similarity index 84%
rename from src/trace_processor/prelude/table_functions/experimental_slice_layout.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h
index 3aa61bb..d23c628 100644
--- a/src/trace_processor/prelude/table_functions/experimental_slice_layout.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
 
 #include <set>
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -61,4 +61,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_EXPERIMENTAL_SLICE_LAYOUT_H_
diff --git a/src/trace_processor/prelude/table_functions/experimental_slice_layout_unittest.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout_unittest.cc
similarity index 97%
rename from src/trace_processor/prelude/table_functions/experimental_slice_layout_unittest.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout_unittest.cc
index 164dfbb..73fd6b5 100644
--- a/src/trace_processor/prelude/table_functions/experimental_slice_layout_unittest.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout_unittest.cc
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/experimental_slice_layout.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h"
 
 #include <algorithm>
 
 #include "src/trace_processor/containers/bit_vector.h"
-#include "src/trace_processor/prelude/table_functions/tables_py.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
 #include "test/gtest_and_gmock.h"
 
 namespace perfetto {
diff --git a/src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.cc
similarity index 99%
rename from src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.cc
index 7424134..68ce80e 100644
--- a/src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h"
 
 #include <set>
 #include <unordered_set>
diff --git a/src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h
similarity index 80%
rename from src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h
index 82c79b8..e655f37 100644
--- a/src/trace_processor/prelude/table_functions/flamegraph_construction_algorithms.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/flamegraph_construction_algorithms.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
 
 #include "src/trace_processor/storage/trace_storage.h"
 
@@ -42,4 +42,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_FLAMEGRAPH_CONSTRUCTION_ALGORITHMS_H_
diff --git a/src/trace_processor/prelude/table_functions/table_function.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.cc
similarity index 89%
rename from src/trace_processor/prelude/table_functions/table_function.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.cc
index a116686..770f51f 100644
--- a/src/trace_processor/prelude/table_functions/table_function.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/prelude/table_functions/table_function.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h
similarity index 89%
rename from src/trace_processor/prelude/table_functions/table_function.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h
index 7829bbd..4079603 100644
--- a/src/trace_processor/prelude/table_functions/table_function.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
 
 #include "perfetto/base/status.h"
 #include "src/trace_processor/db/table.h"
@@ -62,4 +62,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_TABLE_FUNCTION_H_
diff --git a/src/trace_processor/prelude/table_functions/tables.py b/src/trace_processor/perfetto_sql/intrinsics/table_functions/tables.py
similarity index 100%
rename from src/trace_processor/prelude/table_functions/tables.py
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/tables.py
diff --git a/src/trace_processor/prelude/table_functions/view.cc b/src/trace_processor/perfetto_sql/intrinsics/table_functions/view.cc
similarity index 95%
rename from src/trace_processor/prelude/table_functions/view.cc
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/view.cc
index 42d5639..fca72a4 100644
--- a/src/trace_processor/prelude/table_functions/view.cc
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/view.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/prelude/table_functions/view.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/prelude/table_functions/view.h b/src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h
similarity index 82%
rename from src/trace_processor/prelude/table_functions/view.h
rename to src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h
index 691cbd8..79b3a5c 100644
--- a/src/trace_processor/prelude/table_functions/view.h
+++ b/src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_VIEW_H_
-#define SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_VIEW_H_
+#ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_VIEW_H_
+#define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_VIEW_H_
 
 #include "perfetto/ext/base/flat_hash_map.h"
 #include "src/trace_processor/db/view.h"
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/storage/trace_storage.h"
 
 namespace perfetto {
@@ -47,4 +47,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_PRELUDE_TABLE_FUNCTIONS_VIEW_H_
+#endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TABLE_FUNCTIONS_VIEW_H_
diff --git a/src/trace_processor/prelude/tables_views/BUILD.gn b/src/trace_processor/perfetto_sql/prelude/BUILD.gn
similarity index 94%
rename from src/trace_processor/prelude/tables_views/BUILD.gn
rename to src/trace_processor/perfetto_sql/prelude/BUILD.gn
index 110d0b9..e38412e 100644
--- a/src/trace_processor/prelude/tables_views/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/prelude/BUILD.gn
@@ -17,7 +17,7 @@
 
 assert(enable_perfetto_trace_processor_sqlite)
 
-perfetto_amalgamated_sql_header("tables_views") {
+perfetto_amalgamated_sql_header("prelude") {
   deps = [ ":sources" ]
   generated_header = "tables_views.h"
   namespace = "prelude::tables_views"
diff --git a/src/trace_processor/prelude/tables_views/tables.sql b/src/trace_processor/perfetto_sql/prelude/tables.sql
similarity index 100%
rename from src/trace_processor/prelude/tables_views/tables.sql
rename to src/trace_processor/perfetto_sql/prelude/tables.sql
diff --git a/src/trace_processor/prelude/tables_views/views.sql b/src/trace_processor/perfetto_sql/prelude/views.sql
similarity index 100%
rename from src/trace_processor/prelude/tables_views/views.sql
rename to src/trace_processor/perfetto_sql/prelude/views.sql
diff --git a/src/trace_processor/stdlib/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
similarity index 81%
rename from src/trace_processor/stdlib/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/BUILD.gn
index cde8c63..e89a108 100644
--- a/src/trace_processor/stdlib/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
@@ -12,12 +12,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../gn/perfetto.gni")
-import("../../../gn/perfetto_sql.gni")
+import("../../../../gn/perfetto.gni")
+import("../../../../gn/perfetto_sql.gni")
 
 assert(enable_perfetto_trace_processor_sqlite)
 
-perfetto_amalgamated_sql_header("gen_amalgamated_stdlib") {
+perfetto_amalgamated_sql_header("stdlib") {
   deps = [
     "android",
     "chrome:chrome_sql",
@@ -25,7 +25,7 @@
     "experimental",
     "pkvm",
   ]
-  generated_header = "amalgamated_stdlib.h"
+  generated_header = "stdlib.h"
   namespace = "stdlib"
   generate_docs = true
 }
diff --git a/src/trace_processor/stdlib/android/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
similarity index 94%
rename from src/trace_processor/stdlib/android/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
index 9596be8..24187af 100644
--- a/src/trace_processor/stdlib/android/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto_sql.gni")
+import("../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("android") {
   deps = [ "startup" ]
diff --git a/src/trace_processor/stdlib/android/battery.sql b/src/trace_processor/perfetto_sql/stdlib/android/battery.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/battery.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/battery.sql
diff --git a/src/trace_processor/stdlib/android/battery_stats.sql b/src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/battery_stats.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql
diff --git a/src/trace_processor/stdlib/android/binder.sql b/src/trace_processor/perfetto_sql/stdlib/android/binder.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/binder.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/binder.sql
diff --git a/src/trace_processor/stdlib/android/monitor_contention.sql b/src/trace_processor/perfetto_sql/stdlib/android/monitor_contention.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/monitor_contention.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/monitor_contention.sql
diff --git a/src/trace_processor/stdlib/android/network_packets.sql b/src/trace_processor/perfetto_sql/stdlib/android/network_packets.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/network_packets.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/network_packets.sql
diff --git a/src/trace_processor/stdlib/android/process_metadata.sql b/src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/process_metadata.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql
diff --git a/src/trace_processor/stdlib/android/slices.sql b/src/trace_processor/perfetto_sql/stdlib/android/slices.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/slices.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/slices.sql
diff --git a/src/trace_processor/stdlib/android/startup/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/startup/BUILD.gn
similarity index 94%
rename from src/trace_processor/stdlib/android/startup/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/android/startup/BUILD.gn
index 7e8cb44..ee08f69 100644
--- a/src/trace_processor/stdlib/android/startup/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/startup/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../../gn/perfetto_sql.gni")
+import("../../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("startup") {
   sources = [
diff --git a/src/trace_processor/stdlib/android/startup/internal_startups_maxsdk28.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_maxsdk28.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/startup/internal_startups_maxsdk28.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_maxsdk28.sql
diff --git a/src/trace_processor/stdlib/android/startup/internal_startups_minsdk29.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk29.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/startup/internal_startups_minsdk29.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk29.sql
diff --git a/src/trace_processor/stdlib/android/startup/internal_startups_minsdk33.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk33.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/startup/internal_startups_minsdk33.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/startup/internal_startups_minsdk33.sql
diff --git a/src/trace_processor/stdlib/android/startup/startups.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/startup/startups.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql
diff --git a/src/trace_processor/stdlib/android/statsd.sql b/src/trace_processor/perfetto_sql/stdlib/android/statsd.sql
similarity index 100%
rename from src/trace_processor/stdlib/android/statsd.sql
rename to src/trace_processor/perfetto_sql/stdlib/android/statsd.sql
diff --git a/src/trace_processor/stdlib/chrome/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/chrome/BUILD.gn
similarity index 94%
rename from src/trace_processor/stdlib/chrome/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/chrome/BUILD.gn
index 4a498b0..70c6343 100644
--- a/src/trace_processor/stdlib/chrome/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/chrome/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto_sql.gni")
+import("../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("chrome_sql") {
   sources = [
diff --git a/src/trace_processor/stdlib/chrome/chrome_scroll_janks.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scroll_janks.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/chrome_scroll_janks.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scroll_janks.sql
diff --git a/src/trace_processor/stdlib/chrome/chrome_scrolls.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scrolls.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/chrome_scrolls.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/chrome_scrolls.sql
diff --git a/src/trace_processor/stdlib/chrome/cpu_powerups.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/cpu_powerups.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/cpu_powerups.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/cpu_powerups.sql
diff --git a/src/trace_processor/stdlib/chrome/histograms.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/histograms.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql
diff --git a/src/trace_processor/stdlib/chrome/metadata.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/metadata.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/metadata.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/metadata.sql
diff --git a/src/trace_processor/stdlib/chrome/speedometer.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/speedometer.sql
similarity index 100%
rename from src/trace_processor/stdlib/chrome/speedometer.sql
rename to src/trace_processor/perfetto_sql/stdlib/chrome/speedometer.sql
diff --git a/src/trace_processor/stdlib/common/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/common/BUILD.gn
similarity index 94%
rename from src/trace_processor/stdlib/common/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/common/BUILD.gn
index 38088fc..a3cf27b 100644
--- a/src/trace_processor/stdlib/common/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/common/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto_sql.gni")
+import("../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("common") {
   sources = [
diff --git a/src/trace_processor/stdlib/common/counters.sql b/src/trace_processor/perfetto_sql/stdlib/common/counters.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/counters.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/counters.sql
diff --git a/src/trace_processor/stdlib/common/cpus.sql b/src/trace_processor/perfetto_sql/stdlib/common/cpus.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/cpus.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/cpus.sql
diff --git a/src/trace_processor/stdlib/common/metadata.sql b/src/trace_processor/perfetto_sql/stdlib/common/metadata.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/metadata.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/metadata.sql
diff --git a/src/trace_processor/stdlib/common/percentiles.sql b/src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/percentiles.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql
diff --git a/src/trace_processor/stdlib/common/slices.sql b/src/trace_processor/perfetto_sql/stdlib/common/slices.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/slices.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/slices.sql
diff --git a/src/trace_processor/stdlib/common/timestamps.sql b/src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql
similarity index 100%
rename from src/trace_processor/stdlib/common/timestamps.sql
rename to src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql
diff --git a/src/trace_processor/stdlib/experimental/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/experimental/BUILD.gn
similarity index 94%
rename from src/trace_processor/stdlib/experimental/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/experimental/BUILD.gn
index d0f4ed3..caae0a9 100644
--- a/src/trace_processor/stdlib/experimental/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/experimental/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto_sql.gni")
+import("../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("experimental") {
   sources = [
diff --git a/src/trace_processor/stdlib/experimental/android_broadcast.sql b/src/trace_processor/perfetto_sql/stdlib/experimental/android_broadcast.sql
similarity index 100%
rename from src/trace_processor/stdlib/experimental/android_broadcast.sql
rename to src/trace_processor/perfetto_sql/stdlib/experimental/android_broadcast.sql
diff --git a/src/trace_processor/stdlib/experimental/flat_slices.sql b/src/trace_processor/perfetto_sql/stdlib/experimental/flat_slices.sql
similarity index 100%
rename from src/trace_processor/stdlib/experimental/flat_slices.sql
rename to src/trace_processor/perfetto_sql/stdlib/experimental/flat_slices.sql
diff --git a/src/trace_processor/stdlib/experimental/proto_path.sql b/src/trace_processor/perfetto_sql/stdlib/experimental/proto_path.sql
similarity index 100%
rename from src/trace_processor/stdlib/experimental/proto_path.sql
rename to src/trace_processor/perfetto_sql/stdlib/experimental/proto_path.sql
diff --git a/src/trace_processor/stdlib/experimental/slices.sql b/src/trace_processor/perfetto_sql/stdlib/experimental/slices.sql
similarity index 100%
rename from src/trace_processor/stdlib/experimental/slices.sql
rename to src/trace_processor/perfetto_sql/stdlib/experimental/slices.sql
diff --git a/src/trace_processor/stdlib/experimental/thread_executing_span.sql b/src/trace_processor/perfetto_sql/stdlib/experimental/thread_executing_span.sql
similarity index 100%
rename from src/trace_processor/stdlib/experimental/thread_executing_span.sql
rename to src/trace_processor/perfetto_sql/stdlib/experimental/thread_executing_span.sql
diff --git a/src/trace_processor/stdlib/pkvm/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/pkvm/BUILD.gn
similarity index 93%
rename from src/trace_processor/stdlib/pkvm/BUILD.gn
rename to src/trace_processor/perfetto_sql/stdlib/pkvm/BUILD.gn
index 683cdd8..857aeb1 100644
--- a/src/trace_processor/stdlib/pkvm/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/pkvm/BUILD.gn
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import("../../../../gn/perfetto_sql.gni")
+import("../../../../../gn/perfetto_sql.gni")
 
 perfetto_sql_source_set("pkvm") {
   sources = [ "hypervisor.sql" ]
diff --git a/src/trace_processor/stdlib/pkvm/hypervisor.sql b/src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql
similarity index 100%
rename from src/trace_processor/stdlib/pkvm/hypervisor.sql
rename to src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql
diff --git a/src/trace_processor/prelude/functions/BUILD.gn b/src/trace_processor/prelude/functions/BUILD.gn
deleted file mode 100644
index ddbf904..0000000
--- a/src/trace_processor/prelude/functions/BUILD.gn
+++ /dev/null
@@ -1,97 +0,0 @@
-# Copyright (C) 2022 The Android Open Source Project
-#
-# 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.
-
-import("../../../../gn/test.gni")
-
-assert(enable_perfetto_trace_processor_sqlite)
-
-source_set("functions") {
-  sources = [
-    "clock_functions.h",
-    "create_function.cc",
-    "create_function.h",
-    "create_function_internal.cc",
-    "create_function_internal.h",
-    "create_view_function.cc",
-    "create_view_function.h",
-    "import.cc",
-    "import.h",
-    "layout_functions.cc",
-    "layout_functions.h",
-    "math.cc",
-    "math.h",
-    "pprof_functions.cc",
-    "pprof_functions.h",
-    "sqlite3_str_split.cc",
-    "sqlite3_str_split.h",
-    "stack_functions.cc",
-    "stack_functions.h",
-    "to_ftrace.cc",
-    "to_ftrace.h",
-    "utils.h",
-    "window_functions.h",
-  ]
-  deps = [
-    "../..:demangle",
-    "../..:export_json",
-    "../..:metatrace",
-    "../../../../gn:default_deps",
-    "../../../../gn:sqlite",
-    "../../../../include/perfetto/protozero:protozero",
-    "../../../../protos/perfetto/common:zero",
-    "../../../../protos/perfetto/trace:zero",
-    "../../../../protos/perfetto/trace/ftrace:zero",
-    "../../../../protos/perfetto/trace_processor:zero",
-    "../../../base",
-    "../../containers",
-    "../../db",
-    "../../importers/common",
-    "../../importers/ftrace:ftrace_descriptors",
-    "../../prelude/table_functions",
-    "../../sqlite",
-    "../../storage",
-    "../../types",
-    "../../util",
-    "../../util:profile_builder",
-    "../../util:sql_argument",
-    "../../util:stdlib",
-  ]
-  public_deps = [ ":interface" ]
-}
-
-source_set("interface") {
-  sources = [
-    "sql_function.cc",
-    "sql_function.h",
-  ]
-  deps = [
-    "../../../../gn:default_deps",
-    "../../../../gn:sqlite",
-    "../../../../include/perfetto/trace_processor:basic_types",
-    "../../../base",
-  ]
-}
-
-perfetto_unittest_source_set("unittests") {
-  testonly = true
-  sources = [ "sqlite3_str_split_unittest.cc" ]
-  deps = [
-    ":functions",
-    "../../../../gn:default_deps",
-    "../../../../gn:gtest_and_gmock",
-    "../../../../gn:sqlite",
-    "../../../base",
-    "../../sqlite",
-  ]
-}
diff --git a/src/trace_processor/sqlite/BUILD.gn b/src/trace_processor/sqlite/BUILD.gn
index 6904f2c..ddb52f9 100644
--- a/src/trace_processor/sqlite/BUILD.gn
+++ b/src/trace_processor/sqlite/BUILD.gn
@@ -20,10 +20,6 @@
   sources = [
     "db_sqlite_table.cc",
     "db_sqlite_table.h",
-    "perfetto_sql_engine.cc",
-    "perfetto_sql_engine.h",
-    "perfetto_sql_parser.cc",
-    "perfetto_sql_parser.h",
     "query_cache.h",
     "scoped_db.h",
     "sql_source.cc",
@@ -54,8 +50,7 @@
     "../db",
     "../importers/common",
     "../importers/ftrace:ftrace_descriptors",
-    "../prelude/functions:interface",
-    "../prelude/table_functions:interface",
+    "../perfetto_sql/intrinsics/table_functions:interface",
     "../storage",
     "../types",
     "../util",
@@ -79,7 +74,6 @@
   testonly = true
   sources = [
     "db_sqlite_table_unittest.cc",
-    "perfetto_sql_parser_unittest.cc",
     "query_constraints_unittest.cc",
     "sqlite_tokenizer_unittest.cc",
     "sqlite_utils_unittest.cc",
diff --git a/src/trace_processor/sqlite/db_sqlite_table.h b/src/trace_processor/sqlite/db_sqlite_table.h
index 96a63e5..2407223 100644
--- a/src/trace_processor/sqlite/db_sqlite_table.h
+++ b/src/trace_processor/sqlite/db_sqlite_table.h
@@ -20,7 +20,7 @@
 #include "perfetto/base/status.h"
 #include "src/trace_processor/containers/bit_vector.h"
 #include "src/trace_processor/db/table.h"
-#include "src/trace_processor/prelude/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
 #include "src/trace_processor/sqlite/query_cache.h"
 #include "src/trace_processor/sqlite/sqlite_table.h"
 
diff --git a/src/trace_processor/sqlite/sqlite_engine.h b/src/trace_processor/sqlite/sqlite_engine.h
index 80cf7a3..13b9583 100644
--- a/src/trace_processor/sqlite/sqlite_engine.h
+++ b/src/trace_processor/sqlite/sqlite_engine.h
@@ -28,8 +28,6 @@
 #include "perfetto/ext/base/flat_hash_map.h"
 #include "perfetto/ext/base/hash.h"
 #include "src/trace_processor/db/table.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/prelude/table_functions/table_function.h"
 #include "src/trace_processor/sqlite/query_cache.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index c28e373..00d1ef5 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -47,34 +47,37 @@
 #include "src/trace_processor/importers/proto/content_analyzer.h"
 #include "src/trace_processor/importers/systrace/systrace_trace_parser.h"
 #include "src/trace_processor/iterator_impl.h"
-#include "src/trace_processor/prelude/functions/clock_functions.h"
-#include "src/trace_processor/prelude/functions/create_function.h"
-#include "src/trace_processor/prelude/functions/create_view_function.h"
-#include "src/trace_processor/prelude/functions/import.h"
-#include "src/trace_processor/prelude/functions/layout_functions.h"
-#include "src/trace_processor/prelude/functions/math.h"
-#include "src/trace_processor/prelude/functions/pprof_functions.h"
-#include "src/trace_processor/prelude/functions/sql_function.h"
-#include "src/trace_processor/prelude/functions/sqlite3_str_split.h"
-#include "src/trace_processor/prelude/functions/stack_functions.h"
-#include "src/trace_processor/prelude/functions/to_ftrace.h"
-#include "src/trace_processor/prelude/functions/utils.h"
-#include "src/trace_processor/prelude/functions/window_functions.h"
-#include "src/trace_processor/prelude/operators/span_join_operator.h"
-#include "src/trace_processor/prelude/operators/window_operator.h"
-#include "src/trace_processor/prelude/table_functions/ancestor.h"
-#include "src/trace_processor/prelude/table_functions/connected_flow.h"
-#include "src/trace_processor/prelude/table_functions/descendant.h"
-#include "src/trace_processor/prelude/table_functions/experimental_annotated_stack.h"
-#include "src/trace_processor/prelude/table_functions/experimental_counter_dur.h"
-#include "src/trace_processor/prelude/table_functions/experimental_flamegraph.h"
-#include "src/trace_processor/prelude/table_functions/experimental_flat_slice.h"
-#include "src/trace_processor/prelude/table_functions/experimental_sched_upid.h"
-#include "src/trace_processor/prelude/table_functions/experimental_slice_layout.h"
-#include "src/trace_processor/prelude/table_functions/table_function.h"
-#include "src/trace_processor/prelude/table_functions/view.h"
-#include "src/trace_processor/prelude/tables_views/tables_views.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
+#include "src/trace_processor/metrics/metrics.h"
+#include "src/trace_processor/metrics/sql/amalgamated_sql_metrics.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/clock_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/import.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/layout_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/math.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/pprof_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/stack_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/to_ftrace.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/utils.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/window_functions.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/operators/span_join_operator.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/ancestor.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/connected_flow.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/descendant.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_annotated_stack.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flamegraph.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_flat_slice.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_sched_upid.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/experimental_slice_layout.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/table_functions/view.h"
+#include "src/trace_processor/perfetto_sql/prelude/tables_views.h"
+#include "src/trace_processor/perfetto_sql/stdlib/stdlib.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sql_source.h"
 #include "src/trace_processor/sqlite/sql_stats_table.h"
@@ -96,9 +99,6 @@
 #include "src/trace_processor/metrics/all_chrome_metrics.descriptor.h"
 #include "src/trace_processor/metrics/all_webview_metrics.descriptor.h"
 #include "src/trace_processor/metrics/metrics.descriptor.h"
-#include "src/trace_processor/metrics/metrics.h"
-#include "src/trace_processor/metrics/sql/amalgamated_sql_metrics.h"
-#include "src/trace_processor/stdlib/amalgamated_stdlib.h"
 
 namespace perfetto {
 namespace trace_processor {
diff --git a/src/trace_processor/trace_processor_impl.h b/src/trace_processor/trace_processor_impl.h
index 973ec35..0cf6410 100644
--- a/src/trace_processor/trace_processor_impl.h
+++ b/src/trace_processor/trace_processor_impl.h
@@ -30,11 +30,11 @@
 #include "perfetto/trace_processor/basic_types.h"
 #include "perfetto/trace_processor/status.h"
 #include "perfetto/trace_processor/trace_processor.h"
-#include "src/trace_processor/prelude/functions/create_function.h"
-#include "src/trace_processor/prelude/functions/create_view_function.h"
-#include "src/trace_processor/prelude/functions/import.h"
+#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/create_view_function.h"
+#include "src/trace_processor/perfetto_sql/intrinsics/functions/import.h"
 #include "src/trace_processor/sqlite/db_sqlite_table.h"
-#include "src/trace_processor/sqlite/perfetto_sql_engine.h"
 #include "src/trace_processor/sqlite/query_cache.h"
 #include "src/trace_processor/sqlite/scoped_db.h"
 #include "src/trace_processor/sqlite/sqlite_engine.h"