001package gudusoft.gsqlparser.stmt;
002/*
003 * Date: 13-7-29
004 */
005
006import gudusoft.gsqlparser.EDbVendor;
007import gudusoft.gsqlparser.ESqlStatementType;
008import gudusoft.gsqlparser.TCustomSqlStatement;
009import gudusoft.gsqlparser.TSourceToken;
010import gudusoft.gsqlparser.nodes.TObjectName;
011import gudusoft.gsqlparser.nodes.TObjectNameList;
012import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
013import gudusoft.gsqlparser.nodes.TTable;
014import gudusoft.gsqlparser.nodes.TAnalyzeSqlNode;
015
016/**
017 * ANALYZE statement for collecting table statistics.
018 *
019 * Supports various syntaxes across dialects:
020 * - Hive: ANALYZE TABLE table_name COMPUTE STATISTICS [NOSCAN] [FOR COLUMNS]
021 * - StarRocks: ANALYZE [FULL|SAMPLE] TABLE|DATABASE name [(columns)] [WITH SAMPLE ROWS|PERCENT value]
022 *              ANALYZE TABLE table UPDATE|DROP HISTOGRAM ON (columns)
023 */
024public class TAnalyzeStmt extends TCustomSqlStatement {
025
026    private TTable table;
027    private boolean noScan;
028    private boolean partitionScan;
029    private TObjectNameList columns;
030
031    // StarRocks-specific fields
032    private boolean fullCollection = false;
033    private boolean sampleCollection = false;
034    private boolean analyzeDatabase = false;
035    private TObjectName databaseName;
036    private String sampleValue;
037    private boolean sampleRows = false;
038    private boolean samplePercent = false;
039    private boolean syncMode = false;
040    private boolean asyncMode = false;
041    private boolean updateHistogram = false;
042    private boolean dropHistogram = false;
043    private TObjectNameList histogramColumns;
044    private int bucketCount = 0;
045
046    public TAnalyzeStmt(EDbVendor dbvendor) {
047        super(dbvendor);
048        sqlstatementtype = ESqlStatementType.sstanalyzeTable;
049    }
050
051    public TObjectNameList getColumns() {
052        return columns;
053    }
054
055    public boolean isNoScan() {
056        return noScan;
057    }
058
059    public boolean isPartitionScan() {
060        return partitionScan;
061    }
062
063    public TTable getTable() {
064        return table;
065    }
066
067    // StarRocks-specific getters
068    public boolean isFullCollection() {
069        return fullCollection;
070    }
071
072    public boolean isSampleCollection() {
073        return sampleCollection;
074    }
075
076    public boolean isAnalyzeDatabase() {
077        return analyzeDatabase;
078    }
079
080    public TObjectName getDatabaseName() {
081        return databaseName;
082    }
083
084    public String getSampleValue() {
085        return sampleValue;
086    }
087
088    public boolean isSampleRows() {
089        return sampleRows;
090    }
091
092    public boolean isSamplePercent() {
093        return samplePercent;
094    }
095
096    public boolean isSyncMode() {
097        return syncMode;
098    }
099
100    public boolean isAsyncMode() {
101        return asyncMode;
102    }
103
104    public boolean isUpdateHistogram() {
105        return updateHistogram;
106    }
107
108    public boolean isDropHistogram() {
109        return dropHistogram;
110    }
111
112    public TObjectNameList getHistogramColumns() {
113        return histogramColumns;
114    }
115
116    public int getBucketCount() {
117        return bucketCount;
118    }
119
120    public int doParseStatement(TCustomSqlStatement psql) {
121        if (rootNode == null) return -1;
122        super.doParseStatement(psql);
123        TAnalyzeSqlNode node = (TAnalyzeSqlNode)rootNode;
124        if (node.getFromTable() != null){
125            table = this.analyzeFromTable(node.getFromTable(),true);
126        }
127
128        this.noScan = node.isNoScan();
129        this.partitionScan = node.isPartitionScan();
130        this.columns = node.getColumns();
131
132        // StarRocks-specific fields
133        this.fullCollection = node.isFullCollection();
134        this.sampleCollection = node.isSampleCollection();
135        this.analyzeDatabase = node.isAnalyzeDatabase();
136        this.databaseName = node.getDatabaseName();
137        if (node.getSampleValue() != null) {
138            this.sampleValue = node.getSampleValue().toString();
139        }
140        this.sampleRows = node.isSampleRows();
141        this.samplePercent = node.isSamplePercent();
142        this.syncMode = node.isSyncMode();
143        this.asyncMode = node.isAsyncMode();
144        this.updateHistogram = node.isUpdateHistogram();
145        this.dropHistogram = node.isDropHistogram();
146        this.histogramColumns = node.getHistogramColumns();
147        if (node.getBucketCount() != null) {
148            try {
149                this.bucketCount = Integer.parseInt(node.getBucketCount().toString());
150            } catch (NumberFormatException e) {
151                this.bucketCount = 0;
152            }
153        }
154
155        return 0;
156    }
157
158    public void accept(TParseTreeVisitor v){
159        v.preVisit(this);
160        v.postVisit(this);
161    }
162
163    public void acceptChildren(TParseTreeVisitor v){
164        v.preVisit(this);
165        v.postVisit(this);
166    }
167
168    public void setTable(TTable table) {
169        this.table = table;
170    }
171
172    public void setNoScan(boolean noScan) {
173        this.noScan = noScan;
174    }
175
176    public void setPartitionScan(boolean partitionScan) {
177        this.partitionScan = partitionScan;
178    }
179
180    public void setColumns(TObjectNameList columns) {
181        this.columns = columns;
182    }
183}