@@ -275,12 +275,31 @@ static TSNode resolve_ocaml_func_name(TSNode node) {
275275 return null_node ;
276276}
277277
278- // SQL: resolve create_function name from object_reference→identifier or direct identifier.
278+ // Last identifier (DFS pre-order) under `node`. For a schema-qualified
279+ // object_reference (schema.table) this is the table name; the schema prefix is
280+ // ignored. Leaves *found false and returns `best` unchanged if none is present.
281+ static TSNode sql_last_identifier (TSNode node , TSNode best , bool * found ) {
282+ if (strcmp (ts_node_type (node ), "identifier" ) == 0 ) {
283+ best = node ;
284+ * found = true;
285+ }
286+ uint32_t cc = ts_node_child_count (node );
287+ for (uint32_t i = 0 ; i < cc ; i ++ ) {
288+ best = sql_last_identifier (ts_node_child (node , i ), best , found );
289+ }
290+ return best ;
291+ }
292+
293+ // SQL: resolve create_function / create_table / create_view name. The name sits
294+ // on an object_reference; for a schema-qualified name (schema.table) take the
295+ // last identifier (the table), not the first (the schema).
279296static TSNode resolve_sql_func_name (TSNode node ) {
280297 TSNode obj_ref = cbm_find_child_by_kind (node , "object_reference" );
281298 if (!ts_node_is_null (obj_ref )) {
282- TSNode id = cbm_find_child_by_kind (obj_ref , "identifier" );
283- if (!ts_node_is_null (id )) {
299+ bool found = false;
300+ TSNode empty = {0 };
301+ TSNode id = sql_last_identifier (obj_ref , empty , & found );
302+ if (found ) {
284303 return id ;
285304 }
286305 }
@@ -4030,13 +4049,75 @@ static bool extract_config_class_def(CBMExtractCtx *ctx, TSNode node, const char
40304049 return true;
40314050}
40324051
4052+ // Collect FROM/JOIN table references (tree-sitter-sql `relation` nodes) anywhere
4053+ // under `node` and emit them as usages scoped to enclosing_qn. pass_usages then
4054+ // resolves each ref_name to the referenced Table/View def and creates a USAGE
4055+ // lineage edge (e.g. a view -> the tables it selects from). Emitting them here
4056+ // (rather than via the generic identifier walker) sets the correct enclosing
4057+ // scope and bypasses the is_definition_name suppression that drops them.
4058+ static void collect_sql_relation_usages (CBMExtractCtx * ctx , TSNode node , const char * enclosing_qn ) {
4059+ if (strcmp (ts_node_type (node ), "relation" ) == 0 ) {
4060+ TSNode nm = resolve_sql_func_name (node ); // object_reference -> identifier
4061+ if (!ts_node_is_null (nm )) {
4062+ char * tname = cbm_node_text (ctx -> arena , nm , ctx -> source );
4063+ if (tname && tname [0 ]) {
4064+ CBMUsage usage = {0 };
4065+ usage .ref_name = tname ;
4066+ usage .enclosing_func_qn = enclosing_qn ;
4067+ cbm_usages_push (& ctx -> result -> usages , ctx -> arena , usage );
4068+ }
4069+ }
4070+ }
4071+ uint32_t n = ts_node_child_count (node );
4072+ for (uint32_t i = 0 ; i < n ; i ++ ) {
4073+ collect_sql_relation_usages (ctx , ts_node_child (node , i ), enclosing_qn );
4074+ }
4075+ }
4076+
4077+ // Handle SQL DDL relation defs: CREATE TABLE / VIEW / MATERIALIZED VIEW become
4078+ // first-class Table/View nodes rather than generic Variable nodes. The relation
4079+ // name sits on an object_reference child (the same shape create_function uses),
4080+ // so resolve_sql_func_name locates it. Also emits FROM/JOIN dependencies as
4081+ // usages so lineage edges form. Returns true if handled.
4082+ static bool extract_sql_ddl_class_def (CBMExtractCtx * ctx , TSNode node , const char * kind ) {
4083+ if (ctx -> language != CBM_LANG_SQL ) {
4084+ return false;
4085+ }
4086+ const char * label ;
4087+ if (strcmp (kind , "create_table" ) == 0 ) {
4088+ label = "Table" ;
4089+ } else if (strcmp (kind , "create_view" ) == 0 || strcmp (kind , "create_materialized_view" ) == 0 ) {
4090+ label = "View" ;
4091+ } else {
4092+ return false;
4093+ }
4094+ TSNode name_node = resolve_sql_func_name (node );
4095+ if (ts_node_is_null (name_node )) {
4096+ return false;
4097+ }
4098+ char * name = cbm_node_text (ctx -> arena , name_node , ctx -> source );
4099+ if (!name || !name [0 ]) {
4100+ return false;
4101+ }
4102+ push_simple_class_def (ctx , node , name , label );
4103+ // Must match push_simple_class_def's QN exactly (qn_safe_segment included)
4104+ // or pass_usages cannot find the enclosing def for the lineage source.
4105+ const char * qn =
4106+ cbm_fqn_compute (ctx -> arena , ctx -> project , ctx -> rel_path , qn_safe_segment (ctx -> arena , name ));
4107+ collect_sql_relation_usages (ctx , node , qn );
4108+ return true;
4109+ }
4110+
40334111static void extract_class_def (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ) {
40344112 CBMArena * a = ctx -> arena ;
40354113 const char * kind = ts_node_type (node );
40364114
40374115 if (extract_config_class_def (ctx , node , kind )) {
40384116 return ;
40394117 }
4118+ if (extract_sql_ddl_class_def (ctx , node , kind )) {
4119+ return ;
4120+ }
40404121
40414122 TSNode name_node = ts_node_child_by_field_name (node , TS_FIELD ("name" ));
40424123 // ObjC: class name is first identifier child
0 commit comments