|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/altinity/clickhouse-grafana/pkg/eval" |
| 11 | +) |
| 12 | + |
| 13 | +// QueryRequest represents the structure of the query request |
| 14 | +type QueryRequest struct { |
| 15 | + RefId string `json:"refId"` |
| 16 | + RuleUid string `json:"ruleUid"` |
| 17 | + RawQuery bool `json:"rawQuery"` |
| 18 | + Query string `json:"query"` |
| 19 | + DateTimeCol string `json:"dateTimeColDataType"` |
| 20 | + DateCol string `json:"dateColDataType"` |
| 21 | + DateTimeType string `json:"dateTimeType"` |
| 22 | + Extrapolate bool `json:"extrapolate"` |
| 23 | + SkipComments bool `json:"skip_comments"` |
| 24 | + AddMetadata bool `json:"add_metadata"` |
| 25 | + Format string `json:"format"` |
| 26 | + Round string `json:"round"` |
| 27 | + IntervalFactor int `json:"intervalFactor"` |
| 28 | + Interval string `json:"interval"` |
| 29 | + Database string `json:"database"` |
| 30 | + Table string `json:"table"` |
| 31 | + MaxDataPoints int64 `json:"maxDataPoints"` |
| 32 | + FrontendDatasource bool `json:"frontendDatasource"` |
| 33 | + UseWindowFuncForMacros bool `json:"useWindowFuncForMacros"` |
| 34 | + TimeRange struct { |
| 35 | + From string `json:"from"` |
| 36 | + To string `json:"to"` |
| 37 | + } `json:"timeRange"` |
| 38 | +} |
| 39 | + |
| 40 | +// findGroupByProperties recursively searches for GROUP BY clauses in the AST |
| 41 | +func findGroupByProperties(ast *eval.EvalAST) []interface{} { |
| 42 | + // First, check if there's a GROUP BY at this level |
| 43 | + if prop, exists := ast.Obj["group by"]; exists { |
| 44 | + switch v := prop.(type) { |
| 45 | + case *eval.EvalAST: |
| 46 | + // If the property is an AST object, add all items from its array |
| 47 | + properties := make([]interface{}, len(v.Arr)) |
| 48 | + copy(properties, v.Arr) |
| 49 | + return properties |
| 50 | + case []interface{}: |
| 51 | + // If the property is already a slice, use it directly |
| 52 | + return v |
| 53 | + default: |
| 54 | + // For any other type, add it as a single item |
| 55 | + return []interface{}{v} |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // If not found at this level, check if there's a FROM clause that might contain a subquery |
| 60 | + if from, exists := ast.Obj["from"]; exists { |
| 61 | + switch v := from.(type) { |
| 62 | + case *eval.EvalAST: |
| 63 | + // If FROM contains another AST (subquery), recursively search in it |
| 64 | + subProperties := findGroupByProperties(v) |
| 65 | + if len(subProperties) > 0 { |
| 66 | + return subProperties |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // If nothing found in subqueries, check any other properties that might contain nested ASTs |
| 72 | + for _, obj := range ast.Obj { |
| 73 | + if subAST, ok := obj.(*eval.EvalAST); ok { |
| 74 | + subProperties := findGroupByProperties(subAST) |
| 75 | + if len(subProperties) > 0 { |
| 76 | + return subProperties |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Return empty slice if nothing found |
| 82 | + return []interface{}{} |
| 83 | +} |
| 84 | + |
| 85 | +// createQuery is the debug version of createQueryWasm |
| 86 | +func createQuery(reqData QueryRequest) map[string]interface{} { |
| 87 | + // Parse time range |
| 88 | + from, err := time.Parse(time.RFC3339, reqData.TimeRange.From) |
| 89 | + if err != nil { |
| 90 | + return map[string]interface{}{ |
| 91 | + "error": "Invalid `$from` time: " + err.Error(), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + to, err := time.Parse(time.RFC3339, reqData.TimeRange.To) |
| 96 | + if err != nil { |
| 97 | + return map[string]interface{}{ |
| 98 | + "error": "Invalid `$to` time: " + err.Error(), |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Create eval.EvalQuery |
| 103 | + evalQ := eval.EvalQuery{ |
| 104 | + RefId: reqData.RefId, |
| 105 | + RuleUid: reqData.RuleUid, |
| 106 | + RawQuery: reqData.RawQuery, |
| 107 | + Query: reqData.Query, |
| 108 | + DateTimeCol: reqData.DateTimeCol, |
| 109 | + DateCol: reqData.DateCol, |
| 110 | + DateTimeType: reqData.DateTimeType, |
| 111 | + Extrapolate: reqData.Extrapolate, |
| 112 | + SkipComments: reqData.SkipComments, |
| 113 | + AddMetadata: reqData.AddMetadata, |
| 114 | + Format: reqData.Format, |
| 115 | + Round: reqData.Round, |
| 116 | + IntervalFactor: reqData.IntervalFactor, |
| 117 | + Interval: reqData.Interval, |
| 118 | + Database: reqData.Database, |
| 119 | + Table: reqData.Table, |
| 120 | + MaxDataPoints: reqData.MaxDataPoints, |
| 121 | + From: from, |
| 122 | + To: to, |
| 123 | + FrontendDatasource: reqData.FrontendDatasource, |
| 124 | + UseWindowFuncForMacros: reqData.UseWindowFuncForMacros, |
| 125 | + } |
| 126 | + |
| 127 | + // Apply macros and get AST |
| 128 | + sql, err := evalQ.ApplyMacrosAndTimeRangeToQuery() |
| 129 | + if err != nil { |
| 130 | + return map[string]interface{}{ |
| 131 | + "error": fmt.Sprintf("Failed to apply macros: %v", err), |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + scanner := eval.NewScanner(sql) |
| 136 | + ast, err := scanner.ToAST() |
| 137 | + if err != nil { |
| 138 | + return map[string]interface{}{ |
| 139 | + "error": fmt.Sprintf("Failed to parse query: %v", err), |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Use the recursive function to find GROUP BY properties at any level |
| 144 | + properties := findGroupByProperties(ast) |
| 145 | + |
| 146 | + // Return the result with detailed information about the AST structure |
| 147 | + return map[string]interface{}{ |
| 148 | + "sql": sql, |
| 149 | + "keys": properties, |
| 150 | + "debug": map[string]interface{}{ |
| 151 | + "hasFromClause": ast.HasOwnProperty("from"), |
| 152 | + "hasGroupByClause": ast.HasOwnProperty("group by"), |
| 153 | + }, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func main() { |
| 158 | + if len(os.Args) < 2 { |
| 159 | + fmt.Println("Usage: go run debug_query.go <input.json>") |
| 160 | + os.Exit(1) |
| 161 | + } |
| 162 | + |
| 163 | + // Read input file |
| 164 | + data, err := ioutil.ReadFile(os.Args[1]) |
| 165 | + if err != nil { |
| 166 | + fmt.Printf("Error reading file: %v\n", err) |
| 167 | + os.Exit(1) |
| 168 | + } |
| 169 | + |
| 170 | + // Parse input JSON |
| 171 | + var request QueryRequest |
| 172 | + err = json.Unmarshal(data, &request) |
| 173 | + if err != nil { |
| 174 | + fmt.Printf("Error parsing JSON: %v\n", err) |
| 175 | + os.Exit(1) |
| 176 | + } |
| 177 | + |
| 178 | + // Process the query |
| 179 | + result := createQuery(request) |
| 180 | + |
| 181 | + // Print result |
| 182 | + resultJSON, err := json.MarshalIndent(result, "", " ") |
| 183 | + if err != nil { |
| 184 | + fmt.Printf("Error encoding result: %v\n", err) |
| 185 | + os.Exit(1) |
| 186 | + } |
| 187 | + |
| 188 | + fmt.Println(string(resultJSON)) |
| 189 | +} |
0 commit comments