001package gudusoft.gsqlparser.resolver2.iterative;
002
003import gudusoft.gsqlparser.resolver2.model.ResolutionStatistics;
004
005/**
006 * Standard resolution strategy.
007 *
008 * <p>This is the default strategy that works well for most queries.
009 *
010 * <p>Strategy behavior:
011 * - Pass 1: Basic resolution without inference
012 * - Pass 2+: Enable inference and star expansion
013 * - Push-down: Enabled from pass 2 onward
014 * - Adaptive: Adjusts based on progress
015 *
016 * <p>This strategy balances:
017 * - Quick results (first pass is fast)
018 * - Completeness (later passes handle complex cases)
019 * - Efficiency (doesn't over-iterate)
020 */
021public class StandardResolutionStrategy implements ResolutionStrategy {
022
023    /** Whether to enable aggressive inference */
024    private final boolean aggressiveInference;
025
026    /** Minimum unresolved columns to trigger inference */
027    private final int inferenceThreshold;
028
029    public StandardResolutionStrategy() {
030        this(false, 5);
031    }
032
033    public StandardResolutionStrategy(boolean aggressiveInference, int inferenceThreshold) {
034        this.aggressiveInference = aggressiveInference;
035        this.inferenceThreshold = inferenceThreshold;
036    }
037
038    @Override
039    public String getName() {
040        return "Standard";
041    }
042
043    @Override
044    public boolean shouldPerformInference(int passNumber, ResolutionStatistics previousStats) {
045        // Aggressive mode: start inference from pass 1
046        if (aggressiveInference) {
047            return true;
048        }
049
050        // Conservative mode: start inference from pass 2
051        if (passNumber < 2) {
052            return false;
053        }
054
055        // Check if we have enough unresolved columns to warrant inference
056        if (previousStats != null) {
057            int unresolved = previousStats.getUnresolvedReferences();
058            return unresolved >= inferenceThreshold;
059        }
060
061        return true;
062    }
063
064    @Override
065    public boolean shouldExpandStars(int passNumber, ResolutionStatistics previousStats) {
066        // Always try to expand stars from pass 2 onward
067        // Pass 1 might not have enough information
068        return passNumber >= 2;
069    }
070
071    @Override
072    public boolean shouldPushDownColumns(int passNumber, ResolutionStatistics previousStats) {
073        // Enable push-down from pass 2 onward
074        // This allows the first pass to gather basic information
075        return passNumber >= 2;
076    }
077
078    @Override
079    public void preparePass(int passNumber, ResolutionStatistics previousStats) {
080        // Standard strategy doesn't need special preparation
081        // Could be extended to adjust thresholds based on progress
082    }
083
084    @Override
085    public String getDescription() {
086        return "Standard iterative resolution strategy. " +
087               "Pass 1: Basic resolution. " +
088               "Pass 2+: Inference, star expansion, and push-down enabled.";
089    }
090
091    @Override
092    public String toString() {
093        return getName() + " strategy" +
094               (aggressiveInference ? " (aggressive)" : " (conservative)");
095    }
096}