001package gudusoft.gsqlparser.resolver2.scope; 002 003import gudusoft.gsqlparser.nodes.TGroupBy; 004import gudusoft.gsqlparser.resolver2.ScopeType; 005 006/** 007 * Scope for GROUP BY clause. 008 * 009 * <p>Visibility rules: 010 * - Can access columns from FROM scope 011 * - Cannot access aggregate functions or SELECT list aliases 012 * - In some databases (MySQL), can access SELECT list columns 013 * 014 * <p>Example: 015 * <pre> 016 * SELECT department, COUNT(*) as emp_count 017 * FROM employees 018 * GROUP BY department -- Can reference employees.department 019 * </pre> 020 * 021 * <p>The GROUP BY scope delegates column resolution to its parent 022 * (typically a SelectScope), which has access to the FROM scope. 023 */ 024public class GroupByScope extends AbstractScope { 025 026 /** The GROUP BY AST node */ 027 private final TGroupBy groupByNode; 028 029 /** Parent FROM scope (for column resolution) */ 030 private IScope fromScope; 031 032 public GroupByScope(IScope parent, TGroupBy groupByNode) { 033 super(parent, groupByNode, ScopeType.GROUP_BY); 034 this.groupByNode = groupByNode; 035 } 036 037 /** 038 * Set the FROM scope for column resolution. 039 * GROUP BY columns should resolve against FROM scope tables. 040 * 041 * @param fromScope the FROM scope 042 */ 043 public void setFromScope(IScope fromScope) { 044 this.fromScope = fromScope; 045 } 046 047 /** 048 * Get the FROM scope. 049 * 050 * @return the FROM scope, or null if not set 051 */ 052 public IScope getFromScope() { 053 return fromScope; 054 } 055 056 public TGroupBy getGroupByNode() { 057 return groupByNode; 058 } 059 060 @Override 061 public String toString() { 062 return "GroupByScope"; 063 } 064}