The TTable node represents various kinds of table sources in SQL FROM clauses and other contexts. It can represent simple table names, subqueries, table-valued functions, and many database-specific table sources. It implements the IRelation interface and provides comprehensive attribute management.
publicvoidanalyzeObjectNameTable(TTabletable){TObjectNametableName=table.getTableName();System.out.println("Table name: "+table.getName());System.out.println("Full name: "+table.getFullName());// Check qualified name partsif(tableName.getDatabaseString()!=null){System.out.println("Database: "+table.getPrefixDatabase());}if(tableName.getSchemaString()!=null){System.out.println("Schema: "+table.getPrefixSchema());}if(tableName.getServerString()!=null){System.out.println("Server: "+table.getPrefixServer());}// Check if it's a base table or CTEif(table.isBaseTable()){System.out.println("This is a base table");}if(table.isCTEName()){System.out.println("This references a CTE: "+table.getCTE().getName());}}
publicvoidanalyzeFunctionTable(TTabletable){TFunctionCallfuncCall=table.getFuncCall();if(funcCall==null)return;System.out.println("Table-valued function:");System.out.println(" Function: "+funcCall.getFunctionName());// Analyze argumentsif(funcCall.getArgs()!=null){System.out.println(" Arguments:");for(inti=0;i<funcCall.getArgs().size();i++){TExpressionarg=funcCall.getArgs().getExpression(i);System.out.println(" "+(i+1)+": "+arg.toString());}}// Check for specific function typesStringfuncName=funcCall.getFunctionName().toString().toUpperCase();switch(funcName){case"UNNEST":System.out.println(" UNNEST function - array expansion");break;case"OPENROWSET":System.out.println(" SQL Server OPENROWSET");break;case"STRING_SPLIT":System.out.println(" SQL Server STRING_SPLIT");break;default:System.out.println(" User-defined table function");}}
publicvoidanalyzeValuesTable(TTabletable){TValueClausevalueClause=table.getValueClause();if(valueClause==null)return;System.out.println("VALUES clause:");System.out.println(" Row count: "+valueClause.getRows().size());// Analyze first row to determine structureif(valueClause.getRows().size()>0){TMultiTargetfirstRow=valueClause.getRows().get(0);if(firstRow.getColumnList()!=null){System.out.println(" Column count: "+firstRow.getColumnList().size());// Show sample values from first rowSystem.out.println(" Sample values:");for(inti=0;i<firstRow.getColumnList().size();i++){TResultColumncol=firstRow.getColumnList().getResultColumn(i);System.out.println(" Column "+(i+1)+": "+col.getExpr());}}}// Check for data type patternsanalyzeValuesDataTypes(valueClause);}
publicvoidanalyzeTableAttributes(TTabletable){// Initialize attributes if not already doneif(!table.relationAttributes.isEmpty()||table.isAttributesInitialized){initializeTableAttributes(table);}System.out.println("Available attributes:");for(TAttributeNodeattr:table.getAttributes()){System.out.println(" "+attr.getName());if(attr.getSourceTable()!=null){System.out.println(" Source table: "+attr.getSourceTable().getDisplayName());}if(attr.getSubLevelResultColumn()!=null){System.out.println(" Source column: "+attr.getSubLevelResultColumn());}}}privatevoidinitializeTableAttributes(TTabletable){switch(table.getTableType()){casesubquery:if(table.getSubquery()!=null){table.initAttributesFromSubquery(table.getSubquery(),table.getDisplayName()+".");}break;caserowList:table.initAttributeForRowList();break;casefunction:table.initAttributeForTableFunction();break;casexmltable:table.initAttributeForXMLTable();break;casejoin:table.initAttributesForJoin();break;casepivoted_table:table.initAttributesForPivotTable();break;caseunnest:// Would need TSelectSqlStatement and TSQLEnv for full initialization// table.initAttributesForUnnest(sqlEnv, select);break;}}
publicSet<String>extractTableLineage(TTabletable){Set<String>lineage=newHashSet<>();switch(table.getTableType()){caseobjectname:if(!table.isCTEName()){lineage.add(table.getFullName());}else{// For CTE, get lineage from its definitionTCTEcte=table.getCTE();if(cte.getSubquery()!=null){lineage.addAll(extractTablesFromSubquery(cte.getSubquery()));}}break;casesubquery:lineage.addAll(extractTablesFromSubquery(table.getSubquery()));break;casejoin:lineage.addAll(extractTablesFromJoin(table.getJoinExpr()));break;casepivoted_table:if(table.getPivotedTable()!=null){for(TTablesourceTable:table.getPivotedTable().getRelations()){lineage.addAll(extractTableLineage(sourceTable));}}break;default:// For other types like functions, VALUES, etc.lineage.add(table.getDisplayName());}returnlineage;}
publicvoidanalyzeTableHints(TTabletable){if(table.getTableHintList()!=null){System.out.println("Table hints detected:");for(inti=0;i<table.getTableHintList().size();i++){TTableHinthint=table.getTableHintList().getElement(i);System.out.println(" "+hint.toString());// Analyze hint performance impactanalyzeHintPerformance(hint);}}}privatevoidanalyzeHintPerformance(TTableHinthint){StringhintText=hint.toString().toUpperCase();if(hintText.contains("NOLOCK")){System.out.println(" WARNING: NOLOCK hint - may read uncommitted data");}elseif(hintText.contains("INDEX")){System.out.println(" NOTE: Index hint - forces specific index usage");}elseif(hintText.contains("TABLOCK")){System.out.println(" NOTE: Table lock hint - may impact concurrency");}}
publicvoidtrackColumnReferences(TTabletable){System.out.println("Linked columns for "+table.getDisplayName()+":");for(TObjectNamecolumn:table.getLinkedColumns()){System.out.println(" "+column.toString());// Show column source informationif(column.getSourceTable()!=null){System.out.println(" Resolved to table: "+column.getSourceTable().getDisplayName());}}// Show attributes that reference this tablefor(TObjectNameattr:table.getAttributesReferenceToThisRelation()){System.out.println(" Referenced by: "+attr.toString());}}
publicvoidanalyzeExpandedStarColumns(TTabletable){ArrayList<String>expandedColumns=table.getExpandedStarColumns();if(!expandedColumns.isEmpty()){System.out.println("Star column expansion for "+table.getDisplayName()+":");for(Stringcolumn:expandedColumns){System.out.println(" "+column);}}else{System.out.println("No star column expansion available for "+table.getDisplayName());}}
publicvoidanalyzeTablePerformance(TTabletable){switch(table.getTableType()){caseobjectname:if(table.isCTEName()){System.out.println("CTE reference - check for recursion and materialization");}else{System.out.println("Base table - generally efficient");}break;casesubquery:System.out.println("Derived table - may benefit from materialization");analyzeSubqueryComplexity(table.getSubquery());break;casefunction:System.out.println("Table function - check function efficiency");break;caserowList:introwCount=table.getValueClause().getRows().size();if(rowCount>1000){System.out.println("WARNING: Large VALUES clause ("+rowCount+" rows)");}break;casejoin:System.out.println("JOIN table - analyze join conditions and indexes");break;default:System.out.println("Special table type - review implementation details");}}
publicvoidanalyzeMemoryUsage(TTabletable){intattributeCount=table.getAttributes().size();if(attributeCount>100){System.out.println("WARNING: High attribute count ("+attributeCount+") may impact memory usage");}// Check for large VALUES clausesif(table.getTableType()==ETableSource.rowList&&table.getValueClause()!=null){introwCount=table.getValueClause().getRows().size();if(rowCount>10000){System.out.println("WARNING: Large VALUES clause may consume significant memory");}}}