github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/query/sql_parser.go (about) 1 // Copyright (c) 2017-2018 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package query 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "errors" 21 "fmt" 22 "reflect" 23 "strconv" 24 "strings" 25 26 "github.com/antlr/antlr4/runtime/Go/antlr" 27 "github.com/uber/aresdb/common" 28 "github.com/uber/aresdb/query/sql/antlrgen" 29 "github.com/uber/aresdb/query/sql/tree" 30 "github.com/uber/aresdb/query/sql/util" 31 ) 32 33 const ( 34 _aqlPrefix = "aql_" 35 36 // supported query level 37 maxlevelWith = 1 38 maxLevelQuery = 2 39 40 // slice default size 41 defaultSliceCap = 10 42 43 // query types 44 typeWithQuery = 1 45 typeSubQuery = 2 46 ) 47 48 // ExprOrigin defines the expression origin 49 type ExprOrigin int 50 51 const ( 52 // ExprOriginWhere => the expression origin is from where clause 53 ExprOriginWhere ExprOrigin = iota 54 // ExprOriginJoinOn => the expression origin is from join on clause 55 ExprOriginJoinOn 56 // ExprOriginGroupBy => the expression origin is from groupingElement clause 57 ExprOriginGroupBy 58 // ExprOriginOthers => the expression origin is from other clauses case 59 ExprOriginOthers 60 ) 61 62 // SQL2AqlContext is the context of ASTVisitor 63 type SQL2AqlContext struct { 64 /* 65 Rules Of updating level, levelWith, levelQuery and mapXXX 66 1. level: follow Treeprinter indent. Init value: 0 67 2. levelWith: increase 1 if VisitWith is called. Init value: 0 68 3. levelQuery: increase 1 if withQuery is called in VisitWith or VisitTableSubquery is called. Init value: 0 69 4. mapXXX: create a new mapXXX[mapKey] if a new query is added (ie, VisitWithQuery or VisitTableSubquery). Init value: empty map table. 70 */ 71 // level is current tree level 72 level int 73 // levelWith is current with level 74 levelWith int 75 // levelQuery is current query level 76 levelQuery int 77 // MapQueryIdentifier is a mapping table. key=generateKey(...) value=arrayOfIdentifier. 78 // Identifier can be namedQuery identifier or aliasedRelation identifier 79 MapQueryIdentifier map[int]string 80 // MapMeasures is a mapping table. key=generateKey(...) value=arrayOfMeasure 81 MapMeasures map[int][]Measure 82 // MapDimensions is a mapping table. key=generateKey(...) value=arrayOfDimension 83 MapDimensions map[int][]Dimension 84 // MapJoinTables is a mapping table. key=generateKey(...) value=arrayOfJoin 85 MapJoinTables map[int][]Join 86 // MapRowFilters is a mapping table. key=generateKey(...) value=arrayOfRowFilter 87 MapRowFilters map[int][]string 88 // MapOrderBy is a mapping table. key=generateKey(...) value=arrayOfSortField 89 MapOrderBy map[int][]SortField 90 // MapLimit is a mapping table. key=generateKey(...) value=arrayOfLimit 91 MapLimit map[int]int 92 mapKey int 93 timeNow int64 94 timeFilter TimeFilter 95 timezone string 96 exprOrigin ExprOrigin 97 fromJSON []byte 98 groupByJSON []byte 99 orderByJSON []byte 100 queryIdentifierSet map[string]int 101 exprCheck bool 102 disableMainGroupBy bool 103 exprLogicalOp tree.LogicalBinaryExpType 104 } 105 106 // ASTBuilder is a visitor 107 type ASTBuilder struct { 108 // Logger is a logger from appConfig 109 Logger common.Logger 110 // IStream is input antlr char stream 111 IStream *antlr.CommonTokenStream 112 // ParameterPosition is position in sql 113 ParameterPosition int 114 // SQL2AqlContext is the context of construncting AQL 115 SQL2AqlCtx *SQL2AqlContext 116 aql *AQLQuery 117 118 // Flag that indicates whether aggregate function is seen 119 aggFuncExists bool 120 } 121 122 func (v *ASTBuilder) defaultResult() interface{} { 123 return nil 124 } 125 126 func (v *ASTBuilder) shouldVisitNextChild(node antlr.RuleNode, currentResult interface{}) bool { 127 return true 128 } 129 130 func (v *ASTBuilder) aggregateResult(node antlr.ParseTree, aggregate interface{}, nextResult interface{}) interface{} { 131 location := v.getLocation(node) 132 if nextResult == nil { 133 panic(fmt.Errorf("%v operation not yet implemented at (line:%d, col:%d)", node.GetText(), location.Line, location.CharPosition)) 134 } 135 if aggregate == nil { 136 return nextResult 137 } 138 panic(fmt.Errorf("%v operation not yet implemented at (line:%d, col:%d)", node.GetText(), location.Line, location.CharPosition)) 139 } 140 141 func (v *ASTBuilder) getQualifiedName(ctx antlrgen.IQualifiedNameContext) *tree.QualifiedName { 142 var result *tree.QualifiedName 143 if ctxQualifiedName, ok := ctx.(*antlrgen.QualifiedNameContext); ok { 144 ctxArr := ctxQualifiedName.AllIdentifier() 145 parts := make([]string, len(ctxArr)) 146 for i, c := range ctxArr { 147 if value, ok := v.Visit(c).(*tree.Identifier); ok { 148 parts[i] = value.Value 149 } 150 } 151 result = tree.NewQualifiedName(parts, nil) 152 } 153 return result 154 } 155 156 // VisitTerminal visits the node 157 func (v *ASTBuilder) VisitTerminal(node antlr.TerminalNode) interface{} { return nil } 158 159 // VisitErrorNode visits the node 160 func (v *ASTBuilder) VisitErrorNode(node antlr.ErrorNode) interface{} { return nil } 161 162 // Visit visits the node 163 func (v *ASTBuilder) Visit(tree antlr.ParseTree) interface{} { 164 if tree == nil { 165 return nil 166 } 167 return tree.Accept(v) 168 } 169 170 // VisitChildren visits the node 171 func (v *ASTBuilder) VisitChildren(node antlr.RuleNode) interface{} { 172 result := v.defaultResult() 173 if !reflect.ValueOf(node).IsNil() { 174 n := node.GetChildCount() 175 for i := 0; i < n && v.shouldVisitNextChild(node, result); i++ { 176 var c antlr.ParseTree 177 c = node.GetChild(i).(antlr.ParseTree) 178 childResult := c.Accept(v) 179 if v.SQL2AqlCtx.exprCheck == false { 180 result = v.aggregateResult(c, result, childResult) 181 } 182 } 183 } 184 185 return result 186 } 187 188 func (v *ASTBuilder) visitIfPresent(ctx antlr.RuleContext, visitResult reflect.Type) interface{} { 189 if ctx == nil { 190 return reflect.Zero(visitResult).Interface() 191 } 192 193 return v.Visit(ctx) 194 } 195 196 func (v *ASTBuilder) visitList(ctxs []antlr.ParserRuleContext) interface{} { 197 var res = make([]interface{}, len(ctxs)) 198 for i, ctx := range ctxs { 199 res[i] = v.Visit(ctx) 200 } 201 return res 202 } 203 204 // ********************** Visit SQL grammar starts ******************** 205 206 // VisitSingleStatement visits the node 207 func (v *ASTBuilder) VisitSingleStatement(ctx *antlrgen.SingleStatementContext) interface{} { 208 return v.Visit(ctx.Statement()).(tree.INode) 209 } 210 211 // VisitSingleExpression visits the node 212 func (v *ASTBuilder) VisitSingleExpression(ctx *antlrgen.SingleExpressionContext) interface{} { 213 result, _ := v.Visit(ctx.Expression()).(tree.INode) 214 return result 215 } 216 217 // ********************** query expressions ******************** 218 219 // VisitQuery visits the node 220 func (v *ASTBuilder) VisitQuery(ctx *antlrgen.QueryContext) interface{} { 221 v.Logger.Debugf("VisitQuery: %s", ctx.GetText()) 222 223 location := v.getLocation(ctx) 224 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 225 if levelQuery >= maxLevelQuery { 226 panic(fmt.Errorf("only support %v level subquery at (line:%d, col:%d)", 227 maxLevelQuery, location.Line, location.CharPosition)) 228 } 229 230 // handle with 231 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 232 classWith := reflect.TypeOf((*tree.With)(nil)) 233 with := v.visitIfPresent(ctx.With(), classWith).(*tree.With) 234 235 // handle queryNoWith 236 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 237 body, _ := v.Visit(ctx.QueryNoWith()).(*tree.Query) 238 if body == nil { 239 panic(fmt.Errorf("missing queryNoWith body at (line:%d, col:%d)", location.Line, location.CharPosition)) 240 } 241 242 if levelQuery == 0 { 243 if valid, err := v.isValidWithOrSubQuery(v.SQL2AqlCtx); !valid || err != nil { 244 panic(fmt.Errorf("line:%d, col:%d isValidWithOrSubQuery: %v, reason :%v", 245 location.Line, location.CharPosition, valid, err)) 246 } 247 } 248 249 query := tree.NewQuery( 250 v.getLocation(ctx), 251 with, 252 body.QueryBody, 253 body.OrderBy, 254 body.Limit, 255 ) 256 query.SetValue(fmt.Sprintf("Query: (%s)", v.getText(ctx.BaseParserRuleContext))) 257 258 // reset SQL2AqlContext 259 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 260 return query 261 } 262 263 // VisitWith visits the node 264 func (v *ASTBuilder) VisitWith(ctx *antlrgen.WithContext) interface{} { 265 v.Logger.Debugf("VisitWith: %s", ctx.GetText()) 266 267 location := v.getLocation(ctx) 268 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 269 mapKey := v.SQL2AqlCtx.mapKey 270 levelWith++ 271 levelQuery++ 272 273 if levelWith > maxlevelWith { 274 panic(fmt.Errorf("only support %v level with query at (line:%d, col:%d)", 275 maxlevelWith, location.Line, location.CharPosition)) 276 } 277 278 if ctx.RECURSIVE() != nil { 279 panic(fmt.Errorf("RECURSIVE not yet supported at (line:%d, col:%d)", 280 location.Line, location.CharPosition)) 281 } 282 283 ctxArr := ctx.AllNamedQuery() 284 arrWithQuery := make([]*tree.WithQuery, len(ctxArr)) 285 for i, c := range ctxArr { 286 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 287 v.SQL2AqlCtx.mapKey = v.generateKey(levelQuery, typeWithQuery, i) 288 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey] = make([]Measure, 0, defaultSliceCap) 289 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = make([]Dimension, 0, defaultSliceCap) 290 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey] = make([]Join, 0, defaultSliceCap) 291 v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey] = make([]string, 0, defaultSliceCap) 292 v.SQL2AqlCtx.MapOrderBy[v.SQL2AqlCtx.mapKey] = make([]SortField, 0, defaultSliceCap) 293 294 arrWithQuery[i], _ = v.VisitNamedQuery(c.(*antlrgen.NamedQueryContext)).(*tree.WithQuery) 295 } 296 with := tree.NewWith(v.getLocation(ctx), false, arrWithQuery) 297 with.SetValue(fmt.Sprintf("With: (%s)", v.getText(ctx.BaseParserRuleContext))) 298 299 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith-1, levelQuery-1) 300 v.SQL2AqlCtx.mapKey = mapKey 301 return with 302 } 303 304 // VisitNamedQuery visits the node 305 func (v *ASTBuilder) VisitNamedQuery(ctx *antlrgen.NamedQueryContext) interface{} { 306 v.Logger.Debugf("VisitNamedQuery: %s", ctx.GetText()) 307 308 location := v.getLocation(ctx) 309 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 310 311 // handle name 312 if ctx.GetName() == nil { 313 panic(fmt.Errorf("missing with identifier at (line:%d, col:%d)", location.Line, location.CharPosition)) 314 } 315 identifier := v.getText(ctx.GetName()) 316 v.SQL2AqlCtx.MapQueryIdentifier[v.SQL2AqlCtx.mapKey] = identifier 317 v.addQIdentifier(v.SQL2AqlCtx, identifier, v.SQL2AqlCtx.mapKey) 318 name, _ := v.Visit(ctx.GetName()).(*tree.Identifier) 319 320 // handle columnAliases 321 var columnAliases []*tree.Identifier 322 if ctxColumnAliase, ok := ctx.ColumnAliases().(*antlrgen.ColumnAliasesContext); ok { 323 ctxArr := ctxColumnAliase.AllIdentifier() 324 columnAliases = make([]*tree.Identifier, len(ctxArr)) 325 for i, c := range ctxArr { 326 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey] = 327 append(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey], 328 Measure{ 329 Alias: v.getText(c), 330 }) 331 columnAliases[i], _ = v.Visit(c).(*tree.Identifier) 332 } 333 } 334 335 // handle query 336 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 337 ctxQuery, ok := ctx.Query().(*antlrgen.QueryContext) 338 if !ok { 339 panic(fmt.Errorf("missing with query body at (line:%d, col:%d)", location.Line, location.CharPosition)) 340 } 341 query, _ := v.VisitQuery(ctxQuery).(*tree.Query) 342 withQuery := tree.NewWithQuery(v.getLocation(ctx), 343 name, 344 query, 345 columnAliases) 346 withQuery.SetValue(fmt.Sprintf("WithQuery: (%s)", v.getText(ctx.BaseParserRuleContext))) 347 348 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 349 return withQuery 350 } 351 352 // VisitQueryNoWith visits the node 353 func (v *ASTBuilder) VisitQueryNoWith(ctx *antlrgen.QueryNoWithContext) interface{} { 354 v.Logger.Debugf("VisitQueryNoWith: %s", ctx.GetText()) 355 356 location := v.getLocation(ctx) 357 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 358 359 // handle queryTerm 360 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 361 term := v.Visit(ctx.QueryTerm()) 362 363 // handle ORDER BY 364 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 365 v.SQL2AqlCtx.exprOrigin = ExprOriginOthers 366 var orderBy = v.getOrderBy(ctx) 367 368 var query *tree.Query 369 if qSpec, ok := term.(*tree.QuerySpecification); ok { 370 qSpecNew := tree.NewQuerySpecification(v.getLocation(ctx), 371 qSpec.Select, qSpec.From, qSpec.Where, qSpec.GroupBy, qSpec.Having, orderBy, v.GetTextIfPresent(ctx.GetLimit())) 372 qSpecNew.SetValue(fmt.Sprintf("QuerySpecification: (%s)", v.getText(ctx.QueryTerm()))) 373 374 query = tree.NewQuery(v.getLocation(ctx), 375 nil, 376 qSpecNew, 377 nil, 378 "") 379 } else if qBody, ok := term.(*tree.QueryBody); ok { 380 query = tree.NewQuery(v.getLocation(ctx), 381 nil, 382 qBody, 383 orderBy, 384 v.GetTextIfPresent(ctx.GetLimit())) 385 if ctx.GetLimit() != nil { 386 limit, err := strconv.Atoi(query.Limit) 387 if levelQuery == 0 && err == nil { 388 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 389 v.SQL2AqlCtx.MapLimit[v.SQL2AqlCtx.mapKey] = limit 390 } else { 391 panic(fmt.Errorf("limit on query level %d > 0 not supported at (line:%d, col:%d)", 392 levelQuery, location.Line, location.CharPosition)) 393 } 394 } 395 396 } else { 397 panic(fmt.Errorf("invalid query term: %v at (line:%d, col:%d)", term, location.Line, location.CharPosition)) 398 } 399 query.SetValue(fmt.Sprintf("Query: (%s)", v.getText(ctx.BaseParserRuleContext))) 400 401 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 402 return query 403 } 404 405 // VisitQuerySpecification visits the node 406 func (v *ASTBuilder) VisitQuerySpecification(ctx *antlrgen.QuerySpecificationContext) interface{} { 407 v.Logger.Debugf("VisitQuerySpecification: %s", ctx.GetText()) 408 409 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 410 411 // handle from => join/table 412 // first process from clause so that subquery/withQuery identifier can be found in expression 413 v.SQL2AqlCtx.exprOrigin = ExprOriginJoinOn 414 ctxArrRelation := ctx.AllRelation() 415 arrRelations := make([]tree.IRelation, len(ctxArrRelation)) 416 for i, c := range ctxArrRelation { 417 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 418 arrRelations[i], _ = v.Visit(c).(tree.IRelation) 419 } 420 421 var myFrom tree.IRelation 422 if len(arrRelations) > 0 { 423 relationL := arrRelations[0] 424 // synthesize implicit join nodes 425 for i := 1; i < len(arrRelations); i++ { 426 relationR := arrRelations[i] 427 relationL = tree.NewJoin(v.getLocation(ctx), tree.IMPLICIT, relationL, relationR, nil) 428 relationL.SetValue(fmt.Sprintf("Join: (%s)", v.getText(ctxArrRelation[i]))) 429 } 430 myFrom = relationL 431 } 432 433 // handle select => measure 434 v.SQL2AqlCtx.exprOrigin = ExprOriginOthers 435 ctxArrSelectItem := ctx.AllSelectItem() 436 arrSelectItems := make([]tree.ISelectItem, len(ctxArrSelectItem)) 437 for i, c := range ctxArrSelectItem { 438 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 439 arrSelectItems[i], _ = v.Visit(c).(tree.ISelectItem) 440 441 if i < len(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey]) { 442 // handle subquery/withQuery with columnAliases,8 443 // subquery/withQuery columnalias has higher priority, ignore subquery/withQuery selectSingle identifier 444 switch item := arrSelectItems[i].(type) { 445 case *tree.SingleColumn: 446 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey][i].Expr = util.GetSubstring(item.Expression.GetValue()) 447 case *tree.AllColumns: 448 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey][i].Expr = v.getText(c) 449 } 450 } else { 451 // handle query or subquery/withQuery w/o columnAliases 452 switch item := arrSelectItems[i].(type) { 453 case *tree.SingleColumn: 454 var alias string 455 if item.Alias != nil { 456 alias = util.GetSubstring(item.Alias.GetValue()) 457 } 458 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey] = append(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey], 459 Measure{ 460 Alias: alias, 461 Expr: util.GetSubstring(item.Expression.GetValue()), 462 }) 463 case *tree.AllColumns: 464 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey] = append(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey], 465 Measure{ 466 Expr: v.getText(c), 467 }) 468 } 469 } 470 } 471 472 // handle where => rowfilter/timefilter 473 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 474 v.SQL2AqlCtx.exprOrigin = ExprOriginWhere 475 v.SQL2AqlCtx.exprCheck = true 476 v.visitIfPresent(ctx.GetWhere(), reflect.TypeOf((*tree.Expression)(nil))) 477 v.SQL2AqlCtx.exprCheck = false 478 myWhere := v.visitIfPresent(ctx.GetWhere(), reflect.TypeOf((*tree.Expression)(nil))).(tree.IExpression) 479 480 // handle group by => dimension 481 if v.SQL2AqlCtx.disableMainGroupBy && levelQuery == 0 && ctx.GroupBy() != nil { 482 // disable group by clause in manin query if with/subquery exists 483 location := v.getLocation(ctx.GroupBy()) 484 panic(fmt.Errorf("group by is not allowed at (line:%d, col:%d) since with/subQuery already has group by", 485 location.Line, location.CharPosition)) 486 } 487 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 488 v.SQL2AqlCtx.exprOrigin = ExprOriginGroupBy 489 myGroupBy := v.visitIfPresent(ctx.GroupBy(), reflect.TypeOf((*tree.GroupBy)(nil))).(*tree.GroupBy) 490 if ctx.GroupBy() != nil && levelQuery > 0 { 491 v.SQL2AqlCtx.disableMainGroupBy = true 492 } 493 494 // handle having => not support in AQL 495 if ctx.GetHaving() != nil { 496 location := v.getLocation(ctx.GetHaving()) 497 panic(fmt.Errorf("having not yet supported at (line:%d, col:%d)", location.Line, location.CharPosition)) 498 } 499 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 500 v.SQL2AqlCtx.exprOrigin = ExprOriginOthers 501 v.SQL2AqlCtx.exprCheck = true 502 v.visitIfPresent(ctx.GetHaving(), reflect.TypeOf((*tree.Expression)(nil))) 503 v.SQL2AqlCtx.exprCheck = false 504 myHaving := v.visitIfPresent(ctx.GetHaving(), reflect.TypeOf((*tree.Expression)(nil))).(tree.IExpression) 505 506 querySpec := tree.NewQuerySpecification( 507 v.getLocation(ctx), 508 tree.NewSelect(v.getLocation(ctx), v.isDistinct(ctx.SetQuantifier()), arrSelectItems), 509 myFrom, 510 myWhere, 511 myGroupBy, 512 myHaving, 513 nil, 514 "") 515 querySpec.SetValue(fmt.Sprintf("QuerySpecification: (%s)", v.getText(ctx.BaseParserRuleContext))) 516 517 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 518 return querySpec 519 } 520 521 // VisitSelectAll visits the node 522 func (v *ASTBuilder) VisitSelectAll(ctx *antlrgen.SelectAllContext) interface{} { 523 v.Logger.Debugf("VisitSelectAll: %s", ctx.GetText()) 524 525 var allColumns *tree.AllColumns 526 if ctx.QualifiedName() != nil { 527 allColumns = tree.NewAllColumns(v.getLocation(ctx), v.getQualifiedName(ctx.QualifiedName())) 528 } else { 529 allColumns = tree.NewAllColumns(v.getLocation(ctx), nil) 530 } 531 532 allColumns.SetValue(fmt.Sprintf("AllColumns: (%s)", v.getText(ctx.BaseParserRuleContext))) 533 return allColumns 534 } 535 536 // VisitSelectSingle visits the node 537 func (v *ASTBuilder) VisitSelectSingle(ctx *antlrgen.SelectSingleContext) interface{} { 538 v.Logger.Debugf("VisitSelectSingle: %s", ctx.GetText()) 539 540 v.SQL2AqlCtx.exprCheck = true 541 v.Visit(ctx.Expression()) 542 v.SQL2AqlCtx.exprCheck = false 543 expr, _ := v.Visit(ctx.Expression()).(tree.IExpression) 544 singleColumn := tree.NewSingleColumn(v.getLocation(ctx), 545 expr, 546 v.visitIfPresent(ctx.Identifier(), reflect.TypeOf((*tree.Identifier)(nil))).(*tree.Identifier)) 547 singleColumn.SetValue(fmt.Sprintf("SingleColumn: (%s)", v.getText(ctx.BaseParserRuleContext))) 548 return singleColumn 549 } 550 551 // VisitGroupBy visits the node 552 func (v *ASTBuilder) VisitGroupBy(ctx *antlrgen.GroupByContext) interface{} { 553 v.Logger.Debugf("VisitGroupBy: %s", ctx.GetText()) 554 555 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 556 557 ctxArr := ctx.AllGroupingElement() 558 groupingElements := make([]tree.IGroupingElement, len(ctxArr)) 559 for i, c := range ctxArr { 560 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 561 groupingElements[i], _ = v.Visit(c).(tree.IGroupingElement) 562 } 563 564 groupBy := tree.NewGroupBy( 565 v.getLocation(ctx), 566 v.isDistinct(ctx.SetQuantifier()), 567 groupingElements) 568 groupBy.SetValue(fmt.Sprintf("GroupBy: (%s)", v.getText(ctx.BaseParserRuleContext))) 569 570 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 571 return groupBy 572 } 573 574 // VisitSingleGroupingSet visits the node 575 func (v *ASTBuilder) VisitSingleGroupingSet(ctx *antlrgen.SingleGroupingSetContext) interface{} { 576 v.Logger.Debugf("VisitSingleGroupingSet: %s", ctx.GetText()) 577 578 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 579 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 580 581 ctxArr := ctx.GroupingExpressions().(*antlrgen.GroupingExpressionsContext).AllExpression() 582 columns := make([]tree.IExpression, len(ctxArr)) 583 offset := len(v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey]) 584 for i, c := range ctxArr { 585 v.SQL2AqlCtx.exprCheck = true 586 v.Visit(c) 587 v.SQL2AqlCtx.exprCheck = false 588 columns[i], _ = v.Visit(c).(tree.IExpression) 589 alias, expr := v.lookupSQLExpr(v.SQL2AqlCtx, v.SQL2AqlCtx.mapKey, util.GetSubstring(columns[i].GetValue())) 590 if len(v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey])-offset == i { 591 // timeBucket or numbericBucket is added into 592 // v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] via visitFunctionCall 593 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = 594 append(v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey], 595 Dimension{ 596 Alias: alias, 597 Expr: expr, 598 }) 599 } 600 } 601 602 simpleGroupBy := tree.NewSimpleGroupBy( 603 v.getLocation(ctx), 604 columns) 605 simpleGroupBy.SetValue(fmt.Sprintf("SimpleGroupBy: (%s)", v.getText(ctx.BaseParserRuleContext))) 606 607 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 608 return simpleGroupBy 609 } 610 611 // VisitSortItem visits the node 612 func (v *ASTBuilder) VisitSortItem(ctx *antlrgen.SortItemContext) interface{} { 613 v.Logger.Debugf("VisitSortItem: %s", ctx.GetText()) 614 615 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 616 617 if ctx.Expression() == nil { 618 return nil 619 } 620 621 var ordering tree.OrderType 622 if ctx.GetOrdering() != nil { 623 if ctx.GetOrdering().GetText() == tree.OrderTypes[tree.ASC] { 624 ordering = tree.ASC 625 } else if ctx.GetOrdering().GetText() == tree.OrderTypes[tree.DESC] { 626 ordering = tree.DESC 627 } 628 } 629 630 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 631 v.SQL2AqlCtx.exprCheck = true 632 v.Visit(ctx.Expression()) 633 v.SQL2AqlCtx.exprCheck = false 634 expr := v.Visit(ctx.Expression()).(tree.IExpression) 635 _, name := v.lookupSQLExpr(v.SQL2AqlCtx, v.SQL2AqlCtx.mapKey, util.GetSubstring(expr.GetValue())) 636 v.SQL2AqlCtx.MapOrderBy[v.SQL2AqlCtx.mapKey] = 637 append(v.SQL2AqlCtx.MapOrderBy[v.SQL2AqlCtx.mapKey], 638 SortField{ 639 Name: name, 640 Order: tree.OrderTypes[ordering], 641 }) 642 643 sortItem := tree.NewSortItem(v.getLocation(ctx), expr, ordering) 644 sortItem.SetValue(fmt.Sprintf("SortItem: (%s)", v.getText(ctx.BaseParserRuleContext))) 645 646 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 647 return sortItem 648 } 649 650 // ***************** boolean expressions ****************** 651 652 // VisitExpression visits the node 653 func (v *ASTBuilder) VisitExpression(ctx *antlrgen.ExpressionContext) interface{} { 654 v.Logger.Debugf("VisitExpression %s\n", v.getText(ctx)) 655 656 v.SQL2AqlCtx.exprLogicalOp = tree.NOOP 657 return v.VisitChildren(ctx) 658 } 659 660 // VisitLogicalBinary visits the node 661 func (v *ASTBuilder) VisitLogicalBinary(ctx *antlrgen.LogicalBinaryContext) interface{} { 662 location := v.getLocation(ctx) 663 if v.SQL2AqlCtx.exprCheck { 664 v.Logger.Debugf("VisitLogicalBinary check: %s", ctx.GetText()) 665 if ctx.GetOperator() == nil { 666 panic(fmt.Errorf("missing logicalBinary operator, (line:%d, col:%d)", 667 location.Line, location.CharPosition)) 668 } 669 670 v.Visit(ctx.GetLeft()) 671 v.Visit(ctx.GetRight()) 672 return tree.NewExpression(v.getLocation(ctx)) 673 } 674 675 v.Logger.Debugf("VisitLogicalBinary: %s", ctx.GetText()) 676 operator := v.getLogicalBinaryOperator(ctx.GetOperator().GetTokenType()) 677 if operator == tree.OR { 678 if v.SQL2AqlCtx.exprOrigin == ExprOriginWhere { 679 v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey] = 680 append(v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey], v.getText(ctx.BaseParserRuleContext)) 681 } else if v.SQL2AqlCtx.exprOrigin == ExprOriginJoinOn { 682 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 683 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions = 684 append(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions, v.getText(ctx.BaseParserRuleContext)) 685 } 686 687 expr := tree.NewExpression(v.getLocation(ctx)) 688 expr.SetValue(fmt.Sprintf("LogicalBinaryExpression: (%s)", v.getText(ctx.BaseParserRuleContext))) 689 return expr 690 } 691 692 left, _ := v.Visit(ctx.GetLeft()).(tree.IExpression) 693 right, _ := v.Visit(ctx.GetRight()).(tree.IExpression) 694 logicalBinaryExpr := tree.NewLogicalBinaryExpression( 695 v.getLocation(ctx), 696 operator, 697 left, 698 right) 699 logicalBinaryExpr.SetValue(fmt.Sprintf("LogicalBinaryExpression: (%s)", v.getText(ctx.BaseParserRuleContext))) 700 return logicalBinaryExpr 701 } 702 703 // VisitBooleanDefault visits the node 704 func (v *ASTBuilder) VisitBooleanDefault(ctx *antlrgen.BooleanDefaultContext) interface{} { 705 if v.SQL2AqlCtx.exprCheck { 706 v.Logger.Debugf("VisitBooleanDefault check: %s", ctx.GetText()) 707 return v.VisitChildren(ctx) 708 } 709 710 v.Logger.Debugf("VisitBooleanDefault: %s", ctx.GetText()) 711 if v.SQL2AqlCtx.exprOrigin == ExprOriginWhere && !strings.HasPrefix(v.getText(ctx), _aqlPrefix) { 712 v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey] = 713 append(v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey], v.getText(ctx.BaseParserRuleContext)) 714 } else if v.SQL2AqlCtx.exprOrigin == ExprOriginJoinOn { 715 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 716 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions = 717 append(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions, v.getText(ctx.BaseParserRuleContext)) 718 } 719 720 expr := tree.NewExpression(v.getLocation(ctx)) 721 expr.SetValue(fmt.Sprintf("BooleanDefault: (%s)", v.getText(ctx.BaseParserRuleContext))) 722 723 return expr 724 } 725 726 // VisitLogicalNot visits the node 727 func (v *ASTBuilder) VisitLogicalNot(ctx *antlrgen.LogicalNotContext) interface{} { 728 v.Logger.Debugf("VisitLogicalNot check: %s", ctx.GetText()) 729 730 if v.SQL2AqlCtx.exprCheck { 731 return v.VisitChildren(ctx) 732 } 733 734 v.Logger.Debugf("VisitLogicalNot: %s", ctx.GetText()) 735 if v.SQL2AqlCtx.exprOrigin == ExprOriginWhere { 736 v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey] = 737 append(v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey], v.getText(ctx.BaseParserRuleContext)) 738 } else if v.SQL2AqlCtx.exprOrigin == ExprOriginJoinOn { 739 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 740 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions = 741 append(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Conditions, v.getText(ctx.BaseParserRuleContext)) 742 } 743 744 expr := tree.NewExpression(v.getLocation(ctx)) 745 expr.SetValue(fmt.Sprintf("LogicalNot: (%s)", v.getText(ctx.BaseParserRuleContext))) 746 747 return expr 748 } 749 750 // *************** from clause ***************** 751 752 // VisitJoinRelation visits the node 753 func (v *ASTBuilder) VisitJoinRelation(ctx *antlrgen.JoinRelationContext) interface{} { 754 v.Logger.Debugf("VisitJoinRelation: %s", ctx.GetText()) 755 756 location := v.getLocation(ctx) 757 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 758 759 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 760 left, _ := v.Visit(ctx.GetLeft()).(tree.IRelation) 761 762 var right tree.IRelation 763 if ctx.CROSS() != nil { 764 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 765 right, _ = v.VisitSampledRelation(ctx.GetRight().(*antlrgen.SampledRelationContext)).(tree.IRelation) 766 join := tree.NewJoin(v.getLocation(ctx), tree.CROSS, left, right, nil) 767 join.SetValue(fmt.Sprintf("Join: (%s)", v.getText(ctx.BaseParserRuleContext))) 768 return join 769 } 770 771 var criteria tree.IJoinCriteria 772 if ctx.NATURAL() != nil { 773 if levelQuery != 0 { 774 panic(fmt.Errorf("natural join not supported at subquery/withQuery at (line:%d, col:%d)", 775 location.Line, location.CharPosition)) 776 } 777 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 778 right, _ = v.VisitSampledRelation(ctx.GetRight().(*antlrgen.SampledRelationContext)).(tree.IRelation) 779 criteria = tree.NewNaturalJoin() 780 } else { 781 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 782 right, _ = v.Visit(ctx.GetRightRelation()).(tree.IRelation) 783 784 ctxJoinCriteria, ok := ctx.JoinCriteria().(*antlrgen.JoinCriteriaContext) 785 if !ok { 786 panic(fmt.Errorf("missing join criteria at (line:%d, col:%d)", location.Line, location.CharPosition)) 787 } 788 if ctxJoinCriteria.ON() != nil { 789 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 790 v.SQL2AqlCtx.exprCheck = true 791 v.Visit(ctx.JoinCriteria().(*antlrgen.JoinCriteriaContext). 792 BooleanExpression()) 793 v.SQL2AqlCtx.exprCheck = false 794 joinOn, _ := v.Visit(ctx.JoinCriteria().(*antlrgen.JoinCriteriaContext). 795 BooleanExpression()).(tree.IExpression) 796 criteria = tree.NewJoinOn(joinOn) 797 } else if ctxJoinCriteria.USING() != nil { 798 ctxArr := ctx.JoinCriteria().(*antlrgen.JoinCriteriaContext).AllIdentifier() 799 expressions := make([]*tree.Identifier, len(ctxArr)) 800 for i, c := range ctxArr { 801 expressions[i], _ = v.Visit(c).(*tree.Identifier) 802 } 803 criteria = tree.NewJoinUsing(expressions) 804 } 805 } 806 807 joinType := v.getJoinType(ctx) 808 if joinType != tree.LEFT { 809 panic(fmt.Errorf("join type %v not supported yet at (line:%d, col:%d)", 810 tree.JoinTypes[joinType], location.Line, location.CharPosition)) 811 } 812 813 join := tree.NewJoin(v.getLocation(ctx), joinType, left, right, criteria) 814 join.SetValue(fmt.Sprintf("Join: (%s)", v.getText(ctx.BaseParserRuleContext))) 815 return join 816 } 817 818 // VisitSampledRelation visits the node 819 func (v *ASTBuilder) VisitSampledRelation(ctx *antlrgen.SampledRelationContext) interface{} { 820 v.Logger.Debugf("VisitSampledRelation: %s", ctx.GetText()) 821 822 location := v.getLocation(ctx) 823 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 824 825 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 826 child, _ := v.VisitAliasedRelation(ctx.AliasedRelation().(*antlrgen.AliasedRelationContext)).(tree.IRelation) 827 if ctx.TABLESAMPLE() != nil { 828 panic(fmt.Errorf("TABLESAMPLE not implemented at (line:%d, col:%d)", location.Line, location.CharPosition)) 829 } 830 if child != nil { 831 child.SetValue(fmt.Sprintf("SampledRelation: (%s)", v.getText(ctx.BaseParserRuleContext))) 832 } 833 834 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 835 return child 836 } 837 838 // VisitAliasedRelation visits the node 839 func (v *ASTBuilder) VisitAliasedRelation(ctx *antlrgen.AliasedRelationContext) interface{} { 840 v.Logger.Debugf("VisitAliasedRelation: %s", ctx.GetText()) 841 842 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 843 mapKey := v.SQL2AqlCtx.mapKey 844 845 // handle identifier 846 if ctx.Identifier() != nil { 847 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey] = append(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey], 848 Join{ 849 Alias: v.getText(ctx.Identifier()), 850 }) 851 } else { 852 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey] = append(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey], 853 Join{}) 854 } 855 856 // handle relationPrimary 857 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 858 child, _ := v.Visit(ctx.RelationPrimary()).(tree.IRelation) 859 if ctx.Identifier() == nil { 860 child.SetValue(fmt.Sprintf("Relation: (%s)", v.getText(ctx.BaseParserRuleContext))) 861 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 862 v.SQL2AqlCtx.mapKey = mapKey 863 return child 864 } 865 866 // handle columnAliases 867 var aliasedRelation *tree.AliasedRelation 868 identifier, _ := v.Visit(ctx.Identifier()).(*tree.Identifier) 869 if ctxColumnAliases, ok := ctx.ColumnAliases().(*antlrgen.ColumnAliasesContext); ok { 870 ctxArr := ctxColumnAliases.AllIdentifier() 871 columnAliases := make([]*tree.Identifier, len(ctxArr)) 872 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 873 subqueryKey := v.generateKey(levelQuery+1, typeSubQuery, last) 874 for i, c := range ctxArr { 875 v.setCtxLevels(v.SQL2AqlCtx, level, levelWith, levelQuery) 876 if i < len(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey]) { 877 v.SQL2AqlCtx.MapMeasures[subqueryKey][i].Alias = v.getText(c) 878 } else { 879 v.SQL2AqlCtx.MapMeasures[subqueryKey] = 880 append(v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey], 881 Measure{ 882 Alias: v.getText(c), 883 }) 884 } 885 columnAliases[i], _ = v.Visit(c).(*tree.Identifier) 886 } 887 888 aliasedRelation = tree.NewAliasedRelation( 889 v.getLocation(ctx), 890 child, 891 identifier, 892 columnAliases) 893 } else { 894 aliasedRelation = tree.NewAliasedRelation( 895 v.getLocation(ctx), 896 child, 897 identifier, 898 nil) 899 } 900 aliasedRelation.SetValue(fmt.Sprintf("AliasedRelation: (%s)", v.getText(ctx.BaseParserRuleContext))) 901 902 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery) 903 v.SQL2AqlCtx.mapKey = mapKey 904 return aliasedRelation 905 } 906 907 // VisitTableName visits the node 908 func (v *ASTBuilder) VisitTableName(ctx *antlrgen.TableNameContext) interface{} { 909 v.Logger.Debugf("VisitTableName: %s", ctx.GetText()) 910 911 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 912 // check if the table name is a withQ identifier 913 name := v.getText(ctx.BaseParserRuleContext) 914 qLevel, _, _ := v.getInfoByKey(v.SQL2AqlCtx.mapKey) 915 if qLevel == 0 && v.isWithQueryIdentifier(v.SQL2AqlCtx, name) { 916 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Alias = name 917 } else { 918 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey][last].Table = name 919 } 920 921 table := tree.NewTable(v.getLocation(ctx), v.getQualifiedName(ctx.QualifiedName())) 922 table.SetValue(fmt.Sprintf("Table: (%s)", v.getText(ctx.BaseParserRuleContext))) 923 924 return table 925 } 926 927 // VisitSubqueryRelation visits the node 928 func (v *ASTBuilder) VisitSubqueryRelation(ctx *antlrgen.SubqueryRelationContext) interface{} { 929 v.Logger.Debugf("VisitSubqueryRelation: %s", ctx.GetText()) 930 931 level, levelWith, levelQuery := v.getCtxLevels(v.SQL2AqlCtx) 932 // mapKey is the mapKey of parent query 933 mapKey := v.SQL2AqlCtx.mapKey 934 last := len(v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey]) - 1 935 936 v.setCtxLevels(v.SQL2AqlCtx, level-1, levelWith, levelQuery+1) 937 // the index in v.SQL2AqlCtx.mapKey is the index of the aliasedRelation in parent from clause 938 v.SQL2AqlCtx.mapKey = v.generateKey(levelQuery+1, typeSubQuery, last) 939 v.SQL2AqlCtx.MapQueryIdentifier[v.SQL2AqlCtx.mapKey] = v.SQL2AqlCtx.MapJoinTables[mapKey][last].Alias 940 v.addQIdentifier(v.SQL2AqlCtx, v.SQL2AqlCtx.MapJoinTables[mapKey][last].Alias, v.SQL2AqlCtx.mapKey) 941 v.SQL2AqlCtx.MapMeasures[v.SQL2AqlCtx.mapKey] = make([]Measure, 0, defaultSliceCap) 942 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = make([]Dimension, 0, defaultSliceCap) 943 v.SQL2AqlCtx.MapJoinTables[v.SQL2AqlCtx.mapKey] = make([]Join, 0, defaultSliceCap) 944 v.SQL2AqlCtx.MapRowFilters[v.SQL2AqlCtx.mapKey] = make([]string, 0, defaultSliceCap) 945 v.SQL2AqlCtx.MapOrderBy[v.SQL2AqlCtx.mapKey] = make([]SortField, 0, defaultSliceCap) 946 947 query, _ := v.VisitQuery(ctx.Query().(*antlrgen.QueryContext)).(*tree.Query) 948 tableSubquery := tree.NewTableSubquery(v.getLocation(ctx), query) 949 tableSubquery.SetValue(fmt.Sprintf("TableSubquery: (%s)", v.getText(ctx.BaseParserRuleContext))) 950 951 return tableSubquery 952 } 953 954 // ********************* primary expressions ********************** 955 956 // VisitFunctionCall visits the node 957 func (v *ASTBuilder) VisitFunctionCall(ctx *antlrgen.FunctionCallContext) interface{} { 958 v.Logger.Debugf("VisitFunctionCall: %s", ctx.GetText()) 959 960 // 1. timebucket and numbericbucket are only from groupBy clause 961 // 2. timefilter is only from where clause 962 location := v.getLocation(ctx) 963 name := v.getText(ctx.QualifiedName()) 964 udfDef, ok := util.UdfTable[name] 965 if ok { 966 if ctx.SetQuantifier() != nil || ctx.Filter() != nil || len(ctx.AllSortItem()) != 0 || ctx.AllExpression() == nil { 967 panic(fmt.Errorf("quantifier/filter/over/sort not supported in %s function at (line:%d, col:%d)", 968 name, location.Line, location.CharPosition)) 969 } 970 if len(ctx.AllExpression()) != udfDef.ArgsNum { 971 panic(fmt.Errorf("%s should have %d parameters at (line:%d, col:%d)", 972 name, udfDef.ArgsNum, location.Line, location.CharPosition)) 973 } 974 if v.hasORInPath(ctx.BaseParserRuleContext) == tree.OR { 975 panic(fmt.Errorf("function %s can not appear in OR logicalBinaryExpression at (line:%d, col:%d)", 976 name, location.Line, location.CharPosition)) 977 } 978 if (udfDef.Type == util.Timebucket || udfDef.Type == util.Numericbucket) && v.SQL2AqlCtx.exprOrigin != ExprOriginGroupBy { 979 panic(fmt.Errorf("function %s at (line:%d, col:%d) can only appear in group by clause", 980 name, location.Line, location.CharPosition)) 981 } 982 if udfDef.Type == util.Timefilter && v.SQL2AqlCtx.exprOrigin != ExprOriginWhere { 983 panic(fmt.Errorf("function %s at (line:%d, col:%d) can only appear in where clause", 984 name, location.Line, location.CharPosition)) 985 } 986 987 switch udfDef.Type { 988 case util.Timefilter: 989 v.setTimefilter(ctx.AllExpression()) 990 case util.TimeNow: 991 v.setTimeNow(ctx.AllExpression()) 992 case util.Timebucket: 993 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = append( 994 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey], 995 Dimension{ 996 Expr: util.TrimQuote(v.getText(ctx.Expression(0))), 997 TimeBucketizer: util.TrimQuote(udfDef.Definition), 998 TimeUnit: util.TrimQuote(v.getText(ctx.Expression(1))), 999 }) 1000 if len(v.SQL2AqlCtx.timezone) == 0 { 1001 v.SQL2AqlCtx.timezone = util.TrimQuote(v.getText(ctx.Expression(2))) 1002 } else if v.SQL2AqlCtx.timezone != util.TrimQuote(v.getText(ctx.Expression(2))) { 1003 panic(fmt.Errorf("different timebucket timezone %s at (line:%d, col:%d)", 1004 v.getText(ctx.Expression(2)), location.Line, location.CharPosition)) 1005 } 1006 case util.Numericbucket: 1007 v.setNumericBucketizer(ctx.AllExpression(), udfDef.Definition) 1008 } 1009 } else if strings.HasPrefix(name, _aqlPrefix) { 1010 panic(fmt.Errorf("function %s at (line:%d, col:%d) is not registered in AQL udf", 1011 name, location.Line, location.CharPosition)) 1012 } 1013 1014 if exist := util.AggregateFunctions[name]; exist { 1015 v.aggFuncExists = true 1016 } 1017 1018 return tree.NewExpression(v.getLocation(ctx)) 1019 } 1020 1021 // VisitUnquotedIdentifier visits the node 1022 func (v *ASTBuilder) VisitUnquotedIdentifier(ctx *antlrgen.UnquotedIdentifierContext) interface{} { 1023 v.Logger.Debugf("VisitUnquotedIdentifier: %s", ctx.GetText()) 1024 1025 identifier := tree.NewIdentifier(v.getLocation(ctx), v.getText(ctx.BaseParserRuleContext), false) 1026 identifier.SetValue(fmt.Sprintf("Identifier: (%s)", v.getText(ctx.BaseParserRuleContext))) 1027 1028 return identifier 1029 } 1030 1031 // VisitQuotedIdentifier visits the node 1032 func (v *ASTBuilder) VisitQuotedIdentifier(ctx *antlrgen.QuotedIdentifierContext) interface{} { 1033 v.Logger.Debugf("VisitQuotedIdentifier: %s", ctx.GetText()) 1034 1035 token := v.getText(ctx.BaseParserRuleContext) 1036 identifier := strings.Replace(token[1:len(token)-1], "\"\"", "\"", -1) 1037 quotedIdentifier := tree.NewIdentifier(v.getLocation(ctx), identifier, true) 1038 quotedIdentifier.SetValue(fmt.Sprintf("QuotedIdentifier: (%s)", v.getText(ctx.BaseParserRuleContext))) 1039 1040 return quotedIdentifier 1041 } 1042 1043 // VisitDereference visits the node 1044 func (v *ASTBuilder) VisitDereference(ctx *antlrgen.DereferenceContext) interface{} { 1045 v.Logger.Debugf("VisitDereference: %s", ctx.GetText()) 1046 1047 location := v.getLocation(ctx.GetBase()) 1048 if v.SQL2AqlCtx.mapKey == 0 { 1049 // reject the expression if subquery/withQuery identifier is used in level 0 query 1050 primaryExpr := v.getText(ctx.GetBase()) 1051 key := v.isSubOrWithQueryIdentifier(v.SQL2AqlCtx, primaryExpr) 1052 if key > 0 { 1053 panic(fmt.Errorf("subquery/withQuery identifier in expression not supported yet. (line:%d, col:%d)", 1054 location.Line, location.CharPosition)) 1055 } 1056 } 1057 1058 return v.VisitChildren(ctx) 1059 } 1060 1061 // ***************** Reserved ***************** 1062 1063 // VisitStatementDefault visits the node 1064 func (v *ASTBuilder) VisitStatementDefault(ctx *antlrgen.StatementDefaultContext) interface{} { 1065 return v.VisitChildren(ctx) 1066 } 1067 1068 // VisitUse visits the node 1069 func (v *ASTBuilder) VisitUse(ctx *antlrgen.UseContext) interface{} { 1070 return v.VisitChildren(ctx) 1071 } 1072 1073 // VisitCreateSchema visits the node 1074 func (v *ASTBuilder) VisitCreateSchema(ctx *antlrgen.CreateSchemaContext) interface{} { 1075 return v.VisitChildren(ctx) 1076 } 1077 1078 // VisitDropSchema visits the node 1079 func (v *ASTBuilder) VisitDropSchema(ctx *antlrgen.DropSchemaContext) interface{} { 1080 return v.VisitChildren(ctx) 1081 } 1082 1083 // VisitRenameSchema visits the node 1084 func (v *ASTBuilder) VisitRenameSchema(ctx *antlrgen.RenameSchemaContext) interface{} { 1085 return v.VisitChildren(ctx) 1086 } 1087 1088 // VisitCreateTableAsSelect visits the node 1089 func (v *ASTBuilder) VisitCreateTableAsSelect(ctx *antlrgen.CreateTableAsSelectContext) interface{} { 1090 return v.VisitChildren(ctx) 1091 } 1092 1093 // VisitCreateTable visits the node 1094 func (v *ASTBuilder) VisitCreateTable(ctx *antlrgen.CreateTableContext) interface{} { 1095 return v.VisitChildren(ctx) 1096 } 1097 1098 // VisitDropTable visits the node 1099 func (v *ASTBuilder) VisitDropTable(ctx *antlrgen.DropTableContext) interface{} { 1100 return v.VisitChildren(ctx) 1101 } 1102 1103 // VisitInsertInto visits the node 1104 func (v *ASTBuilder) VisitInsertInto(ctx *antlrgen.InsertIntoContext) interface{} { 1105 return v.VisitChildren(ctx) 1106 } 1107 1108 // VisitDelete visits the node 1109 func (v *ASTBuilder) VisitDelete(ctx *antlrgen.DeleteContext) interface{} { 1110 return v.VisitChildren(ctx) 1111 } 1112 1113 // VisitRenameTable visits the node 1114 func (v *ASTBuilder) VisitRenameTable(ctx *antlrgen.RenameTableContext) interface{} { 1115 return v.VisitChildren(ctx) 1116 } 1117 1118 // VisitRenameColumn visits the node 1119 func (v *ASTBuilder) VisitRenameColumn(ctx *antlrgen.RenameColumnContext) interface{} { 1120 return v.VisitChildren(ctx) 1121 } 1122 1123 // VisitDropColumn visits the node 1124 func (v *ASTBuilder) VisitDropColumn(ctx *antlrgen.DropColumnContext) interface{} { 1125 return v.VisitChildren(ctx) 1126 } 1127 1128 // VisitAddColumn visits the node 1129 func (v *ASTBuilder) VisitAddColumn(ctx *antlrgen.AddColumnContext) interface{} { 1130 return v.VisitChildren(ctx) 1131 } 1132 1133 // VisitCreateView visits the node 1134 func (v *ASTBuilder) VisitCreateView(ctx *antlrgen.CreateViewContext) interface{} { 1135 return v.VisitChildren(ctx) 1136 } 1137 1138 // VisitDropView visits the node 1139 func (v *ASTBuilder) VisitDropView(ctx *antlrgen.DropViewContext) interface{} { 1140 return v.VisitChildren(ctx) 1141 } 1142 1143 // VisitCall visits the node 1144 func (v *ASTBuilder) VisitCall(ctx *antlrgen.CallContext) interface{} { 1145 return v.VisitChildren(ctx) 1146 } 1147 1148 // VisitGrant visits the node 1149 func (v *ASTBuilder) VisitGrant(ctx *antlrgen.GrantContext) interface{} { 1150 return v.VisitChildren(ctx) 1151 } 1152 1153 // VisitRevoke visits the node 1154 func (v *ASTBuilder) VisitRevoke(ctx *antlrgen.RevokeContext) interface{} { 1155 return v.VisitChildren(ctx) 1156 } 1157 1158 // VisitShowGrants visits the node 1159 func (v *ASTBuilder) VisitShowGrants(ctx *antlrgen.ShowGrantsContext) interface{} { 1160 return v.VisitChildren(ctx) 1161 } 1162 1163 // VisitExplain visits the node 1164 func (v *ASTBuilder) VisitExplain(ctx *antlrgen.ExplainContext) interface{} { 1165 return v.VisitChildren(ctx) 1166 } 1167 1168 // VisitShowCreateTable visits the node 1169 func (v *ASTBuilder) VisitShowCreateTable(ctx *antlrgen.ShowCreateTableContext) interface{} { 1170 return v.VisitChildren(ctx) 1171 } 1172 1173 // VisitShowCreateView visits the node 1174 func (v *ASTBuilder) VisitShowCreateView(ctx *antlrgen.ShowCreateViewContext) interface{} { 1175 return v.VisitChildren(ctx) 1176 } 1177 1178 // VisitShowTables visits the node 1179 func (v *ASTBuilder) VisitShowTables(ctx *antlrgen.ShowTablesContext) interface{} { 1180 return v.VisitChildren(ctx) 1181 } 1182 1183 // VisitShowSchemas visits the node 1184 func (v *ASTBuilder) VisitShowSchemas(ctx *antlrgen.ShowSchemasContext) interface{} { 1185 return v.VisitChildren(ctx) 1186 } 1187 1188 // VisitShowCatalogs visits the node 1189 func (v *ASTBuilder) VisitShowCatalogs(ctx *antlrgen.ShowCatalogsContext) interface{} { 1190 return v.VisitChildren(ctx) 1191 } 1192 1193 // VisitShowColumns visits the node 1194 func (v *ASTBuilder) VisitShowColumns(ctx *antlrgen.ShowColumnsContext) interface{} { 1195 return v.VisitChildren(ctx) 1196 } 1197 1198 // VisitShowStats visits the node 1199 func (v *ASTBuilder) VisitShowStats(ctx *antlrgen.ShowStatsContext) interface{} { 1200 return v.VisitChildren(ctx) 1201 } 1202 1203 // VisitShowStatsForQuery visits the node 1204 func (v *ASTBuilder) VisitShowStatsForQuery(ctx *antlrgen.ShowStatsForQueryContext) interface{} { 1205 return v.VisitChildren(ctx) 1206 } 1207 1208 // VisitShowFunctions visits the node 1209 func (v *ASTBuilder) VisitShowFunctions(ctx *antlrgen.ShowFunctionsContext) interface{} { 1210 return v.VisitChildren(ctx) 1211 } 1212 1213 // VisitShowSession visits the node 1214 func (v *ASTBuilder) VisitShowSession(ctx *antlrgen.ShowSessionContext) interface{} { 1215 return v.VisitChildren(ctx) 1216 } 1217 1218 // VisitSetSession visits the node 1219 func (v *ASTBuilder) VisitSetSession(ctx *antlrgen.SetSessionContext) interface{} { 1220 return v.VisitChildren(ctx) 1221 } 1222 1223 // VisitResetSession visits the node 1224 func (v *ASTBuilder) VisitResetSession(ctx *antlrgen.ResetSessionContext) interface{} { 1225 return v.VisitChildren(ctx) 1226 } 1227 1228 // VisitShowPartitions visits the node 1229 func (v *ASTBuilder) VisitShowPartitions(ctx *antlrgen.ShowPartitionsContext) interface{} { 1230 return v.VisitChildren(ctx) 1231 } 1232 1233 // VisitPrepare visits the node 1234 func (v *ASTBuilder) VisitPrepare(ctx *antlrgen.PrepareContext) interface{} { 1235 return v.VisitChildren(ctx) 1236 } 1237 1238 // VisitDeallocate visits the node 1239 func (v *ASTBuilder) VisitDeallocate(ctx *antlrgen.DeallocateContext) interface{} { 1240 return v.VisitChildren(ctx) 1241 } 1242 1243 // VisitExecute visits the node 1244 func (v *ASTBuilder) VisitExecute(ctx *antlrgen.ExecuteContext) interface{} { 1245 return v.VisitChildren(ctx) 1246 } 1247 1248 // VisitDescribeInput visits the node 1249 func (v *ASTBuilder) VisitDescribeInput(ctx *antlrgen.DescribeInputContext) interface{} { 1250 return v.VisitChildren(ctx) 1251 } 1252 1253 // VisitDescribeOutput visits the node 1254 func (v *ASTBuilder) VisitDescribeOutput(ctx *antlrgen.DescribeOutputContext) interface{} { 1255 return v.VisitChildren(ctx) 1256 } 1257 1258 // VisitTableElement visits the node 1259 func (v *ASTBuilder) VisitTableElement(ctx *antlrgen.TableElementContext) interface{} { 1260 return v.VisitChildren(ctx) 1261 } 1262 1263 // VisitColumnDefinition visits the node 1264 func (v *ASTBuilder) VisitColumnDefinition(ctx *antlrgen.ColumnDefinitionContext) interface{} { 1265 return v.VisitChildren(ctx) 1266 } 1267 1268 // VisitLikeClause visits the node 1269 func (v *ASTBuilder) VisitLikeClause(ctx *antlrgen.LikeClauseContext) interface{} { 1270 return v.VisitChildren(ctx) 1271 } 1272 1273 // VisitProperties visits the node 1274 func (v *ASTBuilder) VisitProperties(ctx *antlrgen.PropertiesContext) interface{} { 1275 return v.VisitChildren(ctx) 1276 } 1277 1278 // VisitProperty visits the node 1279 func (v *ASTBuilder) VisitProperty(ctx *antlrgen.PropertyContext) interface{} { 1280 return v.VisitChildren(ctx) 1281 } 1282 1283 // VisitQueryTermDefault visits the node 1284 func (v *ASTBuilder) VisitQueryTermDefault(ctx *antlrgen.QueryTermDefaultContext) interface{} { 1285 return v.VisitChildren(ctx) 1286 } 1287 1288 // VisitSetOperation visits the node 1289 func (v *ASTBuilder) VisitSetOperation(ctx *antlrgen.SetOperationContext) interface{} { 1290 location := v.getLocation(ctx) 1291 panic(fmt.Errorf("setOperation at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1292 } 1293 1294 // VisitQueryPrimaryDefault visits the node 1295 func (v *ASTBuilder) VisitQueryPrimaryDefault(ctx *antlrgen.QueryPrimaryDefaultContext) interface{} { 1296 return v.VisitChildren(ctx) 1297 } 1298 1299 // VisitTable visits the node 1300 func (v *ASTBuilder) VisitTable(ctx *antlrgen.TableContext) interface{} { 1301 location := v.getLocation(ctx) 1302 panic(fmt.Errorf("table at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1303 } 1304 1305 // VisitInlineTable visits the node 1306 func (v *ASTBuilder) VisitInlineTable(ctx *antlrgen.InlineTableContext) interface{} { 1307 location := v.getLocation(ctx) 1308 panic(fmt.Errorf("inlineTable at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1309 } 1310 1311 // VisitSubquery visits the node 1312 func (v *ASTBuilder) VisitSubquery(ctx *antlrgen.SubqueryContext) interface{} { 1313 location := v.getLocation(ctx) 1314 panic(fmt.Errorf("subquery at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1315 } 1316 1317 // VisitRollup visits the node 1318 func (v *ASTBuilder) VisitRollup(ctx *antlrgen.RollupContext) interface{} { 1319 location := v.getLocation(ctx) 1320 panic(fmt.Errorf("rollup at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1321 } 1322 1323 // VisitCube visits the node 1324 func (v *ASTBuilder) VisitCube(ctx *antlrgen.CubeContext) interface{} { 1325 location := v.getLocation(ctx) 1326 panic(fmt.Errorf("cube at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1327 } 1328 1329 // VisitMultipleGroupingSets visits the node 1330 func (v *ASTBuilder) VisitMultipleGroupingSets(ctx *antlrgen.MultipleGroupingSetsContext) interface{} { 1331 location := v.getLocation(ctx) 1332 panic(fmt.Errorf("multipleGroupingSets at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1333 } 1334 1335 // VisitGroupingExpressions visits the node 1336 func (v *ASTBuilder) VisitGroupingExpressions(ctx *antlrgen.GroupingExpressionsContext) interface{} { 1337 return v.VisitChildren(ctx) 1338 } 1339 1340 // VisitGroupingSet visits the node 1341 func (v *ASTBuilder) VisitGroupingSet(ctx *antlrgen.GroupingSetContext) interface{} { 1342 return v.VisitChildren(ctx) 1343 } 1344 1345 // VisitSetQuantifier visits the node 1346 func (v *ASTBuilder) VisitSetQuantifier(ctx *antlrgen.SetQuantifierContext) interface{} { 1347 return v.VisitChildren(ctx) 1348 } 1349 1350 // VisitRelationDefault visits the node 1351 func (v *ASTBuilder) VisitRelationDefault(ctx *antlrgen.RelationDefaultContext) interface{} { 1352 return v.VisitChildren(ctx) 1353 } 1354 1355 // VisitJoinType visits the node 1356 func (v *ASTBuilder) VisitJoinType(ctx *antlrgen.JoinTypeContext) interface{} { 1357 return v.VisitChildren(ctx) 1358 } 1359 1360 // VisitJoinCriteria visits the node 1361 func (v *ASTBuilder) VisitJoinCriteria(ctx *antlrgen.JoinCriteriaContext) interface{} { 1362 return v.VisitChildren(ctx) 1363 } 1364 1365 // VisitSampleType visits the node 1366 func (v *ASTBuilder) VisitSampleType(ctx *antlrgen.SampleTypeContext) interface{} { 1367 return v.VisitChildren(ctx) 1368 } 1369 1370 // VisitColumnAliases visits the node 1371 func (v *ASTBuilder) VisitColumnAliases(ctx *antlrgen.ColumnAliasesContext) interface{} { 1372 return v.VisitChildren(ctx) 1373 } 1374 1375 // VisitUnnest visits the node 1376 func (v *ASTBuilder) VisitUnnest(ctx *antlrgen.UnnestContext) interface{} { 1377 location := v.getLocation(ctx) 1378 panic(fmt.Errorf("un-nest at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1379 } 1380 1381 // VisitLateral visits the node 1382 func (v *ASTBuilder) VisitLateral(ctx *antlrgen.LateralContext) interface{} { 1383 location := v.getLocation(ctx) 1384 panic(fmt.Errorf("lateral at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1385 } 1386 1387 // VisitParenthesizedRelation visits the node 1388 func (v *ASTBuilder) VisitParenthesizedRelation(ctx *antlrgen.ParenthesizedRelationContext) interface{} { 1389 location := v.getLocation(ctx) 1390 panic(fmt.Errorf("parenthesizedRelation at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1391 } 1392 1393 // VisitQuantifiedComparison visits the node 1394 func (v *ASTBuilder) VisitQuantifiedComparison(ctx *antlrgen.QuantifiedComparisonContext) interface{} { 1395 location := v.getLocation(ctx) 1396 panic(fmt.Errorf("quantifiedComparison at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1397 } 1398 1399 // VisitBetween visits the node 1400 func (v *ASTBuilder) VisitBetween(ctx *antlrgen.BetweenContext) interface{} { 1401 return v.VisitChildren(ctx) 1402 } 1403 1404 // VisitInList visits the node 1405 func (v *ASTBuilder) VisitInList(ctx *antlrgen.InListContext) interface{} { 1406 return v.VisitChildren(ctx) 1407 } 1408 1409 // VisitInSubquery visits the node 1410 func (v *ASTBuilder) VisitInSubquery(ctx *antlrgen.InSubqueryContext) interface{} { 1411 location := v.getLocation(ctx) 1412 panic(fmt.Errorf("inSubquery at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1413 } 1414 1415 // VisitLike visits the node 1416 func (v *ASTBuilder) VisitLike(ctx *antlrgen.LikeContext) interface{} { 1417 return v.VisitChildren(ctx) 1418 } 1419 1420 // VisitNullPredicate visits the node 1421 func (v *ASTBuilder) VisitNullPredicate(ctx *antlrgen.NullPredicateContext) interface{} { 1422 return v.VisitChildren(ctx) 1423 } 1424 1425 // VisitDistinctFrom visits the node 1426 func (v *ASTBuilder) VisitDistinctFrom(ctx *antlrgen.DistinctFromContext) interface{} { 1427 return v.VisitChildren(ctx) 1428 } 1429 1430 // VisitValueExpressionDefault visits the node 1431 func (v *ASTBuilder) VisitValueExpressionDefault(ctx *antlrgen.ValueExpressionDefaultContext) interface{} { 1432 return v.VisitChildren(ctx) 1433 } 1434 1435 // VisitConcatenation visits the node 1436 func (v *ASTBuilder) VisitConcatenation(ctx *antlrgen.ConcatenationContext) interface{} { 1437 return v.VisitChildren(ctx) 1438 } 1439 1440 // VisitArithmeticBinary visits the node 1441 func (v *ASTBuilder) VisitArithmeticBinary(ctx *antlrgen.ArithmeticBinaryContext) interface{} { 1442 return v.VisitChildren(ctx) 1443 } 1444 1445 // VisitArithmeticUnary visits the node 1446 func (v *ASTBuilder) VisitArithmeticUnary(ctx *antlrgen.ArithmeticUnaryContext) interface{} { 1447 return v.VisitChildren(ctx) 1448 } 1449 1450 // VisitAtTimeZone visits the node 1451 func (v *ASTBuilder) VisitAtTimeZone(ctx *antlrgen.AtTimeZoneContext) interface{} { 1452 return v.VisitChildren(ctx) 1453 } 1454 1455 // VisitTypeConstructor visits the node 1456 func (v *ASTBuilder) VisitTypeConstructor(ctx *antlrgen.TypeConstructorContext) interface{} { 1457 return v.VisitChildren(ctx) 1458 } 1459 1460 // VisitSpecialDateTimeFunction visits the node 1461 func (v *ASTBuilder) VisitSpecialDateTimeFunction(ctx *antlrgen.SpecialDateTimeFunctionContext) interface{} { 1462 return v.VisitChildren(ctx) 1463 } 1464 1465 // VisitSubstring visits the node 1466 func (v *ASTBuilder) VisitSubstring(ctx *antlrgen.SubstringContext) interface{} { 1467 return v.VisitChildren(ctx) 1468 } 1469 1470 // VisitCast visits the node 1471 func (v *ASTBuilder) VisitCast(ctx *antlrgen.CastContext) interface{} { 1472 return v.VisitChildren(ctx) 1473 } 1474 1475 // VisitLambda visits the node 1476 func (v *ASTBuilder) VisitLambda(ctx *antlrgen.LambdaContext) interface{} { 1477 location := v.getLocation(ctx) 1478 panic(fmt.Errorf("lambda at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1479 } 1480 1481 // VisitPredicated visits the node 1482 func (v *ASTBuilder) VisitPredicated(ctx *antlrgen.PredicatedContext) interface{} { 1483 return v.VisitChildren(ctx) 1484 } 1485 1486 // VisitComparison visits the node 1487 func (v *ASTBuilder) VisitComparison(ctx *antlrgen.ComparisonContext) interface{} { 1488 return v.VisitChildren(ctx) 1489 } 1490 1491 // VisitParenthesizedExpression visits the node 1492 func (v *ASTBuilder) VisitParenthesizedExpression(ctx *antlrgen.ParenthesizedExpressionContext) interface{} { 1493 return v.VisitChildren(ctx) 1494 } 1495 1496 // VisitParameter visits the node 1497 func (v *ASTBuilder) VisitParameter(ctx *antlrgen.ParameterContext) interface{} { 1498 return v.VisitChildren(ctx) 1499 } 1500 1501 // VisitNormalize visits the node 1502 func (v *ASTBuilder) VisitNormalize(ctx *antlrgen.NormalizeContext) interface{} { 1503 return v.VisitChildren(ctx) 1504 } 1505 1506 // VisitIntervalLiteral visits the node 1507 func (v *ASTBuilder) VisitIntervalLiteral(ctx *antlrgen.IntervalLiteralContext) interface{} { 1508 return v.VisitChildren(ctx) 1509 } 1510 1511 // VisitNumericLiteral visits the node 1512 func (v *ASTBuilder) VisitNumericLiteral(ctx *antlrgen.NumericLiteralContext) interface{} { 1513 return v.VisitChildren(ctx) 1514 } 1515 1516 // VisitBooleanLiteral visits the node 1517 func (v *ASTBuilder) VisitBooleanLiteral(ctx *antlrgen.BooleanLiteralContext) interface{} { 1518 return v.VisitChildren(ctx) 1519 } 1520 1521 // VisitSimpleCase visits the node 1522 func (v *ASTBuilder) VisitSimpleCase(ctx *antlrgen.SimpleCaseContext) interface{} { 1523 return v.VisitChildren(ctx) 1524 } 1525 1526 // VisitColumnReference \visits the node 1527 func (v *ASTBuilder) VisitColumnReference(ctx *antlrgen.ColumnReferenceContext) interface{} { 1528 return v.VisitChildren(ctx) 1529 } 1530 1531 // VisitNullLiteral visits the node 1532 func (v *ASTBuilder) VisitNullLiteral(ctx *antlrgen.NullLiteralContext) interface{} { 1533 return v.VisitChildren(ctx) 1534 } 1535 1536 // VisitRowConstructor visits the node 1537 func (v *ASTBuilder) VisitRowConstructor(ctx *antlrgen.RowConstructorContext) interface{} { 1538 location := v.getLocation(ctx) 1539 panic(fmt.Errorf("rowConstructor at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1540 } 1541 1542 // VisitSubscript visits the node 1543 func (v *ASTBuilder) VisitSubscript(ctx *antlrgen.SubscriptContext) interface{} { 1544 return v.VisitChildren(ctx) 1545 } 1546 1547 // VisitSubqueryExpression visits the node 1548 func (v *ASTBuilder) VisitSubqueryExpression(ctx *antlrgen.SubqueryExpressionContext) interface{} { 1549 location := v.getLocation(ctx) 1550 panic(fmt.Errorf("subqueryExpression at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1551 } 1552 1553 // VisitBinaryLiteral visits the node 1554 func (v *ASTBuilder) VisitBinaryLiteral(ctx *antlrgen.BinaryLiteralContext) interface{} { 1555 return v.VisitChildren(ctx) 1556 } 1557 1558 // VisitCurrentUser visits the node 1559 func (v *ASTBuilder) VisitCurrentUser(ctx *antlrgen.CurrentUserContext) interface{} { 1560 return v.VisitChildren(ctx) 1561 } 1562 1563 // VisitExtract visits the node 1564 func (v *ASTBuilder) VisitExtract(ctx *antlrgen.ExtractContext) interface{} { 1565 return v.VisitChildren(ctx) 1566 } 1567 1568 // VisitStringLiteral visits the node 1569 func (v *ASTBuilder) VisitStringLiteral(ctx *antlrgen.StringLiteralContext) interface{} { 1570 return v.VisitChildren(ctx) 1571 } 1572 1573 // VisitArrayConstructor visits the node 1574 func (v *ASTBuilder) VisitArrayConstructor(ctx *antlrgen.ArrayConstructorContext) interface{} { 1575 return v.VisitChildren(ctx) 1576 } 1577 1578 // VisitExists visits the node 1579 func (v *ASTBuilder) VisitExists(ctx *antlrgen.ExistsContext) interface{} { 1580 location := v.getLocation(ctx) 1581 panic(fmt.Errorf("exists at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1582 } 1583 1584 // VisitPosition visits the node 1585 func (v *ASTBuilder) VisitPosition(ctx *antlrgen.PositionContext) interface{} { 1586 return v.VisitChildren(ctx) 1587 } 1588 1589 // VisitSearchedCase visits the node 1590 func (v *ASTBuilder) VisitSearchedCase(ctx *antlrgen.SearchedCaseContext) interface{} { 1591 return v.VisitChildren(ctx) 1592 } 1593 1594 // VisitGroupingOperation visits the node 1595 func (v *ASTBuilder) VisitGroupingOperation(ctx *antlrgen.GroupingOperationContext) interface{} { 1596 location := v.getLocation(ctx) 1597 panic(fmt.Errorf("groupingOperation at (line:%d, col:%d) not supported yet", location.Line, location.CharPosition)) 1598 } 1599 1600 // VisitBasicStringLiteral visits the node 1601 func (v *ASTBuilder) VisitBasicStringLiteral(ctx *antlrgen.BasicStringLiteralContext) interface{} { 1602 return v.VisitChildren(ctx) 1603 } 1604 1605 // VisitUnicodeStringLiteral visits the node 1606 func (v *ASTBuilder) VisitUnicodeStringLiteral(ctx *antlrgen.UnicodeStringLiteralContext) interface{} { 1607 return v.VisitChildren(ctx) 1608 } 1609 1610 // VisitTimeZoneInterval visits the node 1611 func (v *ASTBuilder) VisitTimeZoneInterval(ctx *antlrgen.TimeZoneIntervalContext) interface{} { 1612 return v.VisitChildren(ctx) 1613 } 1614 1615 // VisitTimeZoneString visits the node 1616 func (v *ASTBuilder) VisitTimeZoneString(ctx *antlrgen.TimeZoneStringContext) interface{} { 1617 return v.VisitChildren(ctx) 1618 } 1619 1620 // VisitComparisonOperator visits the node 1621 func (v *ASTBuilder) VisitComparisonOperator(ctx *antlrgen.ComparisonOperatorContext) interface{} { 1622 return v.VisitChildren(ctx) 1623 } 1624 1625 // VisitComparisonQuantifier visits the node 1626 func (v *ASTBuilder) VisitComparisonQuantifier(ctx *antlrgen.ComparisonQuantifierContext) interface{} { 1627 return v.VisitChildren(ctx) 1628 } 1629 1630 // VisitBooleanValue visits the node 1631 func (v *ASTBuilder) VisitBooleanValue(ctx *antlrgen.BooleanValueContext) interface{} { 1632 return v.VisitChildren(ctx) 1633 } 1634 1635 // VisitInterval visits the node 1636 func (v *ASTBuilder) VisitInterval(ctx *antlrgen.IntervalContext) interface{} { 1637 return v.VisitChildren(ctx) 1638 } 1639 1640 // VisitIntervalField visits the node 1641 func (v *ASTBuilder) VisitIntervalField(ctx *antlrgen.IntervalFieldContext) interface{} { 1642 return v.VisitChildren(ctx) 1643 } 1644 1645 // VisitNormalForm visits the node 1646 func (v *ASTBuilder) VisitNormalForm(ctx *antlrgen.NormalFormContext) interface{} { 1647 return v.VisitChildren(ctx) 1648 } 1649 1650 // VisitSqltype visits the node 1651 func (v *ASTBuilder) VisitSqltype(ctx *antlrgen.SqltypeContext) interface{} { 1652 return v.VisitChildren(ctx) 1653 } 1654 1655 // VisitTypeParameter visits the node 1656 func (v *ASTBuilder) VisitTypeParameter(ctx *antlrgen.TypeParameterContext) interface{} { 1657 return v.VisitChildren(ctx) 1658 } 1659 1660 // VisitBaseType visits the node 1661 func (v *ASTBuilder) VisitBaseType(ctx *antlrgen.BaseTypeContext) interface{} { 1662 return v.VisitChildren(ctx) 1663 } 1664 1665 // VisitWhenClause visits the node 1666 func (v *ASTBuilder) VisitWhenClause(ctx *antlrgen.WhenClauseContext) interface{} { 1667 return v.VisitChildren(ctx) 1668 } 1669 1670 // VisitFilter visits the node 1671 func (v *ASTBuilder) VisitFilter(ctx *antlrgen.FilterContext) interface{} { 1672 return v.VisitChildren(ctx) 1673 } 1674 1675 // VisitExplainFormat visits the node 1676 func (v *ASTBuilder) VisitExplainFormat(ctx *antlrgen.ExplainFormatContext) interface{} { 1677 return v.VisitChildren(ctx) 1678 } 1679 1680 // VisitExplainType visits the node 1681 func (v *ASTBuilder) VisitExplainType(ctx *antlrgen.ExplainTypeContext) interface{} { 1682 return v.VisitChildren(ctx) 1683 } 1684 1685 // VisitPositionalArgument visits the node 1686 func (v *ASTBuilder) VisitPositionalArgument(ctx *antlrgen.PositionalArgumentContext) interface{} { 1687 return v.VisitChildren(ctx) 1688 } 1689 1690 // VisitNamedArgument visits the node 1691 func (v *ASTBuilder) VisitNamedArgument(ctx *antlrgen.NamedArgumentContext) interface{} { 1692 return v.VisitChildren(ctx) 1693 } 1694 1695 // VisitPrivilege visits the node 1696 func (v *ASTBuilder) VisitPrivilege(ctx *antlrgen.PrivilegeContext) interface{} { 1697 return v.VisitChildren(ctx) 1698 } 1699 1700 // VisitQualifiedName visits the node 1701 func (v *ASTBuilder) VisitQualifiedName(ctx *antlrgen.QualifiedNameContext) interface{} { 1702 return v.VisitChildren(ctx) 1703 } 1704 1705 // VisitBackQuotedIdentifier visits the node 1706 func (v *ASTBuilder) VisitBackQuotedIdentifier(ctx *antlrgen.BackQuotedIdentifierContext) interface{} { 1707 return v.VisitChildren(ctx) 1708 } 1709 1710 // VisitDigitIdentifier visits the node 1711 func (v *ASTBuilder) VisitDigitIdentifier(ctx *antlrgen.DigitIdentifierContext) interface{} { 1712 return v.VisitChildren(ctx) 1713 } 1714 1715 // VisitDecimalLiteral visits the node 1716 func (v *ASTBuilder) VisitDecimalLiteral(ctx *antlrgen.DecimalLiteralContext) interface{} { 1717 return v.VisitChildren(ctx) 1718 } 1719 1720 // VisitDoubleLiteral visits the node 1721 func (v *ASTBuilder) VisitDoubleLiteral(ctx *antlrgen.DoubleLiteralContext) interface{} { 1722 return v.VisitChildren(ctx) 1723 } 1724 1725 // VisitIntegerLiteral visits the node 1726 func (v *ASTBuilder) VisitIntegerLiteral(ctx *antlrgen.IntegerLiteralContext) interface{} { 1727 return v.VisitChildren(ctx) 1728 } 1729 1730 // VisitNonReserved visits the node 1731 func (v *ASTBuilder) VisitNonReserved(ctx *antlrgen.NonReservedContext) interface{} { 1732 return v.VisitChildren(ctx) 1733 } 1734 1735 // ***************** helpers ***************** 1736 type orderByContext interface { 1737 ORDER() antlr.TerminalNode 1738 AllSortItem() []antlrgen.ISortItemContext 1739 } 1740 1741 func (v *ASTBuilder) getOrderBy(ctx orderByContext) *tree.OrderBy { 1742 var orderBy *tree.OrderBy 1743 if ctx.ORDER() != nil { 1744 ctxArr := ctx.AllSortItem() 1745 arrSortItem := make([]*tree.SortItem, len(ctxArr)) 1746 for i, c := range ctxArr { 1747 arrSortItem[i] = v.Visit(c.(*antlrgen.SortItemContext)).(*tree.SortItem) 1748 } 1749 orderBy = tree.NewOrderBy(v.getLocation(ctx.ORDER()), arrSortItem) 1750 orderBy.SetValue(fmt.Sprintf("OrderBy: (%s)", ctx.ORDER().GetText())) 1751 } 1752 return orderBy 1753 } 1754 1755 func (v *ASTBuilder) getLocation(input interface{}) *tree.NodeLocation { 1756 var token antlr.Token 1757 switch i := input.(type) { 1758 case antlr.ParserRuleContext: 1759 util.RequireNonNull(input, "antlrgenRuleContext is null") 1760 token = i.GetStart() 1761 case antlr.TerminalNode: 1762 util.RequireNonNull(input, "terminalNode is null") 1763 token = i.GetSymbol() 1764 case antlr.Token: 1765 token = i 1766 default: 1767 token = nil 1768 } 1769 util.RequireNonNull(token, "token is null") 1770 return &tree.NodeLocation{ 1771 Line: token.GetLine(), 1772 CharPosition: token.GetColumn(), 1773 } 1774 } 1775 1776 // getText extracts string from original input sql 1777 func (v *ASTBuilder) getText(ctx antlr.ParserRuleContext) string { 1778 return v.IStream.GetTextFromTokens(ctx.GetStart(), ctx.GetStop()) 1779 } 1780 1781 func (v *ASTBuilder) setTimefilter(ctx []antlrgen.IExpressionContext) { 1782 column := util.TrimQuote(v.getText(ctx[0])) 1783 from := util.TrimQuote(v.getText(ctx[1])) 1784 to := util.TrimQuote(v.getText(ctx[2])) 1785 timezone := util.TrimQuote(v.getText(ctx[3])) 1786 1787 if v.SQL2AqlCtx.timeFilter != (TimeFilter{}) { 1788 if v.SQL2AqlCtx.timeFilter.Column != column { 1789 location := v.getLocation(ctx[0]) 1790 panic(fmt.Errorf("different timefilter on %s at (line:%d, col:%d)", 1791 column, location.Line, location.CharPosition)) 1792 } 1793 if v.SQL2AqlCtx.timeFilter.From != from { 1794 location := v.getLocation(ctx[1]) 1795 panic(fmt.Errorf("different timefilter from %s at (line:%d, col:%d)", 1796 from, location.Line, location.CharPosition)) 1797 } 1798 if v.SQL2AqlCtx.timeFilter.To != to { 1799 location := v.getLocation(ctx[2]) 1800 panic(fmt.Errorf("different timefilter to %s at (line:%d, col:%d)", 1801 to, location.Line, location.CharPosition)) 1802 } 1803 if len(v.SQL2AqlCtx.timezone) != 0 && v.SQL2AqlCtx.timezone != timezone { 1804 location := v.getLocation(ctx[2]) 1805 panic(fmt.Errorf("different timefilter timezone %s at (line:%d, col:%d)", 1806 timezone, location.Line, location.CharPosition)) 1807 } 1808 return 1809 } 1810 v.SQL2AqlCtx.timeFilter = TimeFilter{ 1811 Column: column, 1812 From: from, 1813 To: to, 1814 } 1815 v.SQL2AqlCtx.timezone = timezone 1816 } 1817 1818 func (v *ASTBuilder) setTimeNow(ctx []antlrgen.IExpressionContext) { 1819 column := util.TrimQuote(v.getText(ctx[0])) 1820 now := util.TrimQuote(v.getText(ctx[1])) 1821 1822 var tsNow int64 1823 var err error 1824 if tsNow, err = strconv.ParseInt(now, 110, 64); err != nil { 1825 location := v.getLocation(ctx[1]) 1826 panic(fmt.Errorf("invalid timestamp now on %s at (line:%d, col:%d)", 1827 column, location.Line, location.CharPosition)) 1828 } 1829 if v.SQL2AqlCtx.timeNow < tsNow { 1830 v.SQL2AqlCtx.timeNow = tsNow 1831 } 1832 1833 return 1834 } 1835 1836 func (v *ASTBuilder) setNumericBucketizer(ctx []antlrgen.IExpressionContext, def string) { 1837 switch def { 1838 case util.NumericBucketTypeBucketWidth: 1839 location := v.getLocation(ctx[1]) 1840 value, err := strconv.ParseFloat(util.TrimQuote(v.getText(ctx[1])), 64) 1841 if err != nil { 1842 panic(fmt.Errorf("expect float value at (line:%d, col:%d)", 1843 location.Line, location.CharPosition)) 1844 } 1845 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = append( 1846 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey], 1847 Dimension{ 1848 Expr: util.TrimQuote(v.getText(ctx[0])), 1849 NumericBucketizer: NumericBucketizerDef{BucketWidth: value}, 1850 }) 1851 case util.NumericBucketTypeLogBase: 1852 location := v.getLocation(ctx[1]) 1853 value, err := strconv.ParseFloat(util.TrimQuote(v.getText(ctx[1])), 64) 1854 if err != nil { 1855 panic(fmt.Errorf("expect float value at (line:%d, col:%d)", 1856 location.Line, location.CharPosition)) 1857 } 1858 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = append( 1859 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey], 1860 Dimension{ 1861 Expr: util.TrimQuote(v.getText(ctx[0])), 1862 NumericBucketizer: NumericBucketizerDef{LogBase: value}, 1863 }) 1864 case util.NumericBucketTypeManualPartitions: 1865 location := v.getLocation(ctx[1]) 1866 values := strings.Split(util.TrimQuote(v.getText(ctx[1])), ",") 1867 partitions := make([]float64, len(values), len(values)) 1868 for i, value := range values { 1869 partition, err := strconv.ParseFloat(value, 64) 1870 if err != nil { 1871 panic(fmt.Errorf("expect float value at (line:%d, col:%d)", 1872 location.Line, location.CharPosition)) 1873 } 1874 partitions[i] = partition 1875 } 1876 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey] = append( 1877 v.SQL2AqlCtx.MapDimensions[v.SQL2AqlCtx.mapKey], 1878 Dimension{ 1879 Expr: util.TrimQuote(v.getText(ctx[0])), 1880 NumericBucketizer: NumericBucketizerDef{ManualPartitions: partitions}, 1881 }) 1882 } 1883 } 1884 1885 // mergeWithOrSubQueries merge all subquery/withQuery's information into v.aql 1886 func (v *ASTBuilder) mergeWithOrSubQueries() { 1887 for i, join := range v.SQL2AqlCtx.MapJoinTables[0] { 1888 withOrSubQKey, _ := v.SQL2AqlCtx.queryIdentifierSet[join.Alias] 1889 if i == 0 { 1890 v.mergeWithOrSubQuery(withOrSubQKey, false) 1891 } else { 1892 v.mergeWithOrSubQuery(withOrSubQKey, true) 1893 } 1894 } 1895 1896 v.aql.Measures = v.SQL2AqlCtx.MapMeasures[0] 1897 v.aql.Dimensions = v.SQL2AqlCtx.MapDimensions[0] 1898 if len(v.SQL2AqlCtx.MapOrderBy[0]) != 0 { 1899 v.aql.Sorts = v.SQL2AqlCtx.MapOrderBy[0] 1900 } 1901 v.aql.Filters = v.SQL2AqlCtx.MapRowFilters[0] 1902 v.aql.TimeFilter = v.SQL2AqlCtx.timeFilter 1903 v.aql.Timezone = v.SQL2AqlCtx.timezone 1904 v.aql.Limit = v.SQL2AqlCtx.MapLimit[0] 1905 } 1906 1907 // mergeWithOrSubQuery merge one subquery/withQuery information into v.aql 1908 func (v *ASTBuilder) mergeWithOrSubQuery(key int, ignoreJoin bool) { 1909 if !ignoreJoin { 1910 v.aql.Table = v.SQL2AqlCtx.MapJoinTables[key][0].Table 1911 v.aql.Joins = v.SQL2AqlCtx.MapJoinTables[key][1:] 1912 } 1913 1914 for i, measure := range v.SQL2AqlCtx.MapMeasures[key] { 1915 if index := v.isMeasureInMain(key, i); index > -1 { 1916 // case1: this measure of subquery/withQuery is not a supportingMeasure 1917 v.SQL2AqlCtx.MapMeasures[0][index].Filters = v.SQL2AqlCtx.MapRowFilters[key] 1918 if len(v.SQL2AqlCtx.MapDimensions[0]) == 0 { 1919 v.SQL2AqlCtx.MapDimensions[0] = v.SQL2AqlCtx.MapDimensions[key] 1920 } 1921 } else { 1922 // case2: this measure of subquery/withQuery is a supportingMeasure 1923 measure.Filters = v.SQL2AqlCtx.MapRowFilters[key] 1924 v.aql.SupportingMeasures = append(v.aql.SupportingMeasures, measure) 1925 } 1926 if len(v.SQL2AqlCtx.MapDimensions[0]) == 0 && len(v.SQL2AqlCtx.MapDimensions[key]) != 0 { 1927 v.SQL2AqlCtx.MapDimensions[0] = v.SQL2AqlCtx.MapDimensions[key] 1928 } 1929 if len(v.aql.Sorts) == 0 && len(v.SQL2AqlCtx.MapOrderBy[key]) != 0 { 1930 v.aql.Sorts = v.SQL2AqlCtx.MapOrderBy[key] 1931 } 1932 } 1933 } 1934 1935 // isMeasureInMain check a measure of subquery/withQuery is also a measure of level 0 query 1936 // return the index of the measure in level 0 query; otherwise return -1 1937 func (v *ASTBuilder) isMeasureInMain(key, index int) int { 1938 if len(v.SQL2AqlCtx.MapMeasures[key][index].Alias) != 0 { 1939 for i, measure := range v.SQL2AqlCtx.MapMeasures[0] { 1940 if measure.Expr == v.SQL2AqlCtx.MapMeasures[key][index].Alias { 1941 v.SQL2AqlCtx.MapMeasures[0][i].Expr = v.SQL2AqlCtx.MapMeasures[key][index].Expr 1942 v.SQL2AqlCtx.MapMeasures[0][i].Alias = v.SQL2AqlCtx.MapMeasures[key][index].Alias 1943 return i 1944 } 1945 } 1946 } else { 1947 for i, measure := range v.SQL2AqlCtx.MapMeasures[0] { 1948 if measure.Expr == v.SQL2AqlCtx.MapMeasures[key][index].Expr { 1949 return i 1950 } 1951 } 1952 } 1953 1954 return -1 1955 } 1956 1957 // GetAQL construct AQLQuery via read through SQL2AqlCtx 1958 func (v *ASTBuilder) GetAQL() *AQLQuery { 1959 var ( 1960 table string 1961 joins []Join 1962 ) 1963 1964 if len(v.SQL2AqlCtx.MapQueryIdentifier) == 0 { 1965 // there is no subquery/withQuery in sql 1966 table = v.SQL2AqlCtx.MapJoinTables[0][0].Table 1967 1968 if len(v.SQL2AqlCtx.MapJoinTables[0]) > 1 { 1969 joins = v.SQL2AqlCtx.MapJoinTables[0][1:] 1970 } 1971 1972 v.aql = &AQLQuery{ 1973 Table: table, 1974 Joins: joins, 1975 Measures: v.SQL2AqlCtx.MapMeasures[0], 1976 Dimensions: v.SQL2AqlCtx.MapDimensions[0], 1977 Filters: v.SQL2AqlCtx.MapRowFilters[0], 1978 TimeFilter: v.SQL2AqlCtx.timeFilter, 1979 Timezone: v.SQL2AqlCtx.timezone, 1980 Now: v.SQL2AqlCtx.timeNow, 1981 Limit: v.SQL2AqlCtx.MapLimit[0], 1982 Sorts: v.SQL2AqlCtx.MapOrderBy[0], 1983 } 1984 } else { 1985 v.aql = &AQLQuery{ 1986 SupportingMeasures: make([]Measure, 0, defaultSliceCap), 1987 SupportingDimensions: make([]Dimension, 0, defaultSliceCap), 1988 } 1989 v.mergeWithOrSubQueries() 1990 } 1991 1992 return v.aql 1993 } 1994 1995 // GetTextIfPresent visits the node 1996 func (v *ASTBuilder) GetTextIfPresent(token antlr.Token) string { 1997 var text string 1998 if token != nil { 1999 text = token.GetText() 2000 } 2001 return text 2002 } 2003 2004 // isDistinct check if DISTINCT quantifier is set 2005 func (v *ASTBuilder) isDistinct(setQuantifier antlrgen.ISetQuantifierContext) bool { 2006 if setQuantifier != nil && setQuantifier.(*antlrgen.SetQuantifierContext).DISTINCT() != nil { 2007 return true 2008 } 2009 2010 return false 2011 } 2012 2013 // getLogicalBinaryOperator returns an input token's logicalBinaryExpression operator type 2014 func (v *ASTBuilder) getLogicalBinaryOperator(token int) tree.LogicalBinaryExpType { 2015 switch token { 2016 case antlrgen.SqlBaseLexerAND: 2017 return tree.AND 2018 case antlrgen.SqlBaseLexerOR: 2019 return tree.OR 2020 default: 2021 panic(fmt.Errorf("Unsupported operator: %v", token)) 2022 } 2023 } 2024 2025 func (v *ASTBuilder) getJoinType(ctx *antlrgen.JoinRelationContext) tree.JoinType { 2026 var joinType tree.JoinType 2027 if ctx.JoinType() == nil { 2028 joinType = tree.INNER 2029 } else if ctx.JoinType().(*antlrgen.JoinTypeContext).LEFT() != nil { 2030 joinType = tree.LEFT 2031 } else if ctx.JoinType().(*antlrgen.JoinTypeContext).RIGHT() != nil { 2032 joinType = tree.RIGHT 2033 } else if ctx.JoinType().(*antlrgen.JoinTypeContext).FULL() != nil { 2034 joinType = tree.FULL 2035 } else { 2036 joinType = tree.INNER 2037 } 2038 return joinType 2039 } 2040 2041 func (v *ASTBuilder) getCtxLevels(s2aCtx *SQL2AqlContext) (level, levelWith, levelQuery int) { 2042 level = s2aCtx.level + 1 2043 levelWith = s2aCtx.levelWith 2044 levelQuery = s2aCtx.levelQuery 2045 return 2046 } 2047 2048 func (v *ASTBuilder) setCtxLevels(s2aCtx *SQL2AqlContext, level, levelWith, levelQuery int) { 2049 s2aCtx.level = level 2050 s2aCtx.levelWith = levelWith 2051 s2aCtx.levelQuery = levelQuery 2052 } 2053 2054 // generateKey constructs mapKey based on levelQuery and index of the query at the current levelQuery 2055 func (v *ASTBuilder) generateKey(qLevel, qType, index int) int { 2056 return qLevel*1000 + qType*100 + index 2057 } 2058 2059 func (v *ASTBuilder) getInfoByKey(mapKey int) (qLevel, qType, index int) { 2060 qLevel = mapKey / 1000 2061 qType = (mapKey % 1000) / 100 2062 index = (mapKey % 1000) % 100 2063 return 2064 } 2065 2066 func (v *ASTBuilder) isValidWithOrSubQuery(s2aCtx *SQL2AqlContext) (bool, error) { 2067 var isTrue, exist bool 2068 var err error 2069 var mapKey int 2070 2071 // check if from clause in main query(ie. qLevel = 0) mix table with subquery/withQuery 2072 if isTrue, err = v.isQueryFromMixed(s2aCtx); isTrue { 2073 return false, err 2074 } 2075 2076 // check if all subquery/withQuery from clauses are same 2077 for i, value := range s2aCtx.MapJoinTables[0] { 2078 // exit if no subquery/withQuery 2079 if len(value.Table) > 0 { 2080 break 2081 } 2082 2083 if len(value.Alias) == 0 { // subquery has no identifier 2084 mapKey = v.generateKey(1, typeSubQuery, i) 2085 } else { 2086 mapKey, exist = s2aCtx.queryIdentifierSet[value.Alias] 2087 if !exist { 2088 err = fmt.Errorf("cannot find withQuery identifier: %s", value.Alias) 2089 return false, err 2090 } 2091 } 2092 2093 isTrue, err = v.isSameFromTables(s2aCtx, mapKey) 2094 if !isTrue { 2095 return false, err 2096 } 2097 2098 isTrue, err = v.isSameGroupBy(s2aCtx, mapKey) 2099 if !isTrue { 2100 return false, err 2101 } 2102 2103 isTrue, err = v.isSameOrderBy(s2aCtx, mapKey) 2104 if !isTrue { 2105 return false, err 2106 } 2107 } 2108 2109 return true, nil 2110 } 2111 2112 // AQL requires that the first level query is either from tables or from subqueries/withQuery 2113 func (v *ASTBuilder) isQueryFromMixed(s2aCtx *SQL2AqlContext) (bool, error) { 2114 var flagTable bool 2115 var err error 2116 if len(s2aCtx.MapJoinTables[0][0].Table) != 0 { 2117 flagTable = true 2118 } 2119 for i, join := range s2aCtx.MapJoinTables[0] { 2120 if flagTable && len(join.Table) == 0 { 2121 err = fmt.Errorf("# %d should be a tablename in from clause", i) 2122 return true, err 2123 } else if !flagTable && len(join.Table) != 0 { 2124 err = fmt.Errorf("# %d should be a subquery/withQuery in from clause", i) 2125 return true, err 2126 } 2127 } 2128 return false, nil 2129 } 2130 2131 // AQL requires that all subqueries or withQuery from clauses are same 2132 func (v *ASTBuilder) isSameFromTables(s2aCtx *SQL2AqlContext, mapKey int) (bool, error) { 2133 qLevel, _, index := v.getInfoByKey(mapKey) 2134 // generte from clause json bytes based on the first subquery/withQuery 2135 if s2aCtx.fromJSON == nil { 2136 withKeyMin := v.generateKey(qLevel, typeWithQuery, 0) 2137 subQKeyMin := v.generateKey(qLevel, typeSubQuery, 0) 2138 var err error 2139 if s2aCtx.MapJoinTables[withKeyMin] != nil { 2140 s2aCtx.fromJSON, err = json.Marshal(s2aCtx.MapJoinTables[withKeyMin]) 2141 } else { 2142 s2aCtx.fromJSON, err = json.Marshal(s2aCtx.MapJoinTables[subQKeyMin]) 2143 } 2144 2145 if err != nil { 2146 return false, errors.New("unable to encode from clause of the first subquery/withQuery") 2147 } 2148 2149 if index == 0 { 2150 return true, nil 2151 } 2152 } 2153 2154 // compare current from clause with ctx.fromJSON 2155 joins, err := json.Marshal(s2aCtx.MapJoinTables[mapKey]) 2156 if err != nil { 2157 return false, errors.New("unable to encode from clause of this subquery/withQuery") 2158 } 2159 2160 return bytes.Equal(s2aCtx.fromJSON, joins), nil 2161 } 2162 2163 // AQL requires that all subqueries or withQuery groupBy clauses are sameo 2164 func (v *ASTBuilder) isSameGroupBy(s2aCtx *SQL2AqlContext, mapKey int) (bool, error) { 2165 qLevel, _, index := v.getInfoByKey(mapKey) 2166 // generte group by clause json bytes based on the first subquery/withQuery 2167 if s2aCtx.groupByJSON == nil { 2168 withKeyMin := v.generateKey(qLevel, typeWithQuery, 0) 2169 subQKeyMin := v.generateKey(qLevel, typeSubQuery, 0) 2170 var err error 2171 if s2aCtx.MapJoinTables[withKeyMin] != nil { 2172 s2aCtx.groupByJSON, err = json.Marshal(s2aCtx.MapDimensions[withKeyMin]) 2173 } else { 2174 s2aCtx.groupByJSON, err = json.Marshal(s2aCtx.MapDimensions[subQKeyMin]) 2175 } 2176 if err != nil { 2177 return false, errors.New("unable to encode group by clause of the first subquery/withQuery") 2178 } 2179 2180 if index == 0 { 2181 return true, nil 2182 } 2183 } 2184 2185 // compare current groupBy clause with ctx.groupByJSON 2186 groupBy, err := json.Marshal(s2aCtx.MapDimensions[mapKey]) 2187 if err != nil { 2188 return false, errors.New("unable to encode group by clause of this subquery/withQuery") 2189 } 2190 2191 return bytes.EqualFold(s2aCtx.groupByJSON, groupBy), nil 2192 } 2193 2194 // AQL requires that all subqueries or withQuery orderBy clauses are same 2195 func (v *ASTBuilder) isSameOrderBy(s2aCtx *SQL2AqlContext, mapKey int) (bool, error) { 2196 qLevel, _, index := v.getInfoByKey(mapKey) 2197 // generte group by clause json bytes based on the first subquery/withQuery 2198 if s2aCtx.orderByJSON == nil { 2199 withKeyMin := v.generateKey(qLevel, typeWithQuery, 0) 2200 subQKeyMin := v.generateKey(qLevel, typeSubQuery, 0) 2201 var err error 2202 if s2aCtx.MapOrderBy[withKeyMin] != nil { 2203 s2aCtx.orderByJSON, err = json.Marshal(s2aCtx.MapOrderBy[withKeyMin]) 2204 } else { 2205 s2aCtx.orderByJSON, err = json.Marshal(s2aCtx.MapOrderBy[subQKeyMin]) 2206 } 2207 if err != nil { 2208 return false, errors.New("unable to encode order by clause of the first subquery/withQuery") 2209 } 2210 2211 if index == 0 { 2212 return true, nil 2213 } 2214 } 2215 2216 // compare current groupBy clause with ctx.groupByJSON 2217 orderBy, err := json.Marshal(s2aCtx.MapOrderBy[mapKey]) 2218 if err != nil { 2219 return false, errors.New("unable to encode order by clause of this subquery/withQuery") 2220 } 2221 2222 return bytes.EqualFold(s2aCtx.orderByJSON, orderBy), nil 2223 } 2224 2225 // addQIdentifier adds subquery/withQuery identifier and its mapKey into queryIdentifierSet 2226 func (v *ASTBuilder) addQIdentifier(s2aCtx *SQL2AqlContext, indentifier string, key int) error { 2227 if s2aCtx.queryIdentifierSet == nil { 2228 s2aCtx.queryIdentifierSet = make(map[string]int) 2229 } 2230 if _, exist := s2aCtx.queryIdentifierSet[indentifier]; exist { 2231 return fmt.Errorf("subquery/withQuery identifier: %v already exist", indentifier) 2232 } 2233 s2aCtx.queryIdentifierSet[indentifier] = key 2234 2235 return nil 2236 } 2237 2238 // isWithQueryIdentifier check if name is a withQuery identifier 2239 func (v *ASTBuilder) isWithQueryIdentifier(s2aCtx *SQL2AqlContext, name string) bool { 2240 withKeyMin := v.generateKey(1, typeWithQuery, 0) 2241 if s2aCtx.queryIdentifierSet != nil { 2242 k, exist := s2aCtx.queryIdentifierSet[name] 2243 if exist && k/withKeyMin == 1 { 2244 return true 2245 } 2246 } 2247 2248 return false 2249 } 2250 2251 // isSubOrWithQueryIdentifier check if name is a subquery/withQuery identifier 2252 func (v *ASTBuilder) isSubOrWithQueryIdentifier(s2aCtx *SQL2AqlContext, name string) int { 2253 if s2aCtx.queryIdentifierSet != nil { 2254 if k, exist := s2aCtx.queryIdentifierSet[name]; exist { 2255 return k 2256 } 2257 } 2258 2259 return -1 2260 } 2261 2262 // lookupSQLExpr is used by groupBy, orderBy, having clause whose sql expression is select column alias. 2263 // It returns the select column alias and sql expresion. 2264 func (v *ASTBuilder) lookupSQLExpr(s2aCtx *SQL2AqlContext, mapKey int, str string) (alias, sqlExpr string) { 2265 for _, measure := range s2aCtx.MapMeasures[mapKey] { 2266 if len(measure.Alias) > 0 && strings.Compare(measure.Alias, str) == 0 { 2267 alias = str 2268 sqlExpr = measure.Expr 2269 return 2270 } 2271 } 2272 sqlExpr = str 2273 return 2274 } 2275 2276 // check path from expression node to leaf node has logicalBinaryOperator OR 2277 func (v *ASTBuilder) hasORInPath(node *antlr.BaseParserRuleContext) tree.LogicalBinaryExpType { 2278 parent := node.GetParent() 2279 op := tree.NOOP 2280 for parent != nil { 2281 switch p := parent.(type) { 2282 case antlrgen.IExpressionContext: 2283 return op 2284 case *antlrgen.LogicalBinaryContext: 2285 if p.GetOperator() != nil && v.getLogicalBinaryOperator(p.GetOperator().GetTokenType()) == tree.OR { 2286 return tree.OR 2287 } 2288 op = tree.AND 2289 } 2290 parent = parent.GetParent() 2291 } 2292 return op 2293 } 2294 2295 // Parse parses input sql 2296 func Parse(sql string, logger common.Logger) (aql *AQLQuery, err error) { 2297 defer func() { 2298 if r := recover(); r != nil { 2299 var ok bool 2300 err, ok = r.(error) 2301 if !ok { 2302 err = fmt.Errorf("unkonwn error, reason: %v", r) 2303 } 2304 } 2305 }() 2306 2307 // Setup the input sql 2308 is := util.NewCaseChangingStream(antlr.NewInputStream(sql), true) 2309 2310 // Create the Lexer 2311 lexer := antlrgen.NewSqlBaseLexer(is) 2312 stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) 2313 2314 // Create the Parser 2315 p := antlrgen.NewSqlBaseParser(stream) 2316 2317 // Finally parse the sql 2318 p.GetInterpreter().SetPredictionMode(antlr.PredictionModeSLL) 2319 parseTree, ok := p.Query().(*antlrgen.QueryContext) 2320 if !ok { 2321 err = fmt.Errorf("not a query") 2322 return nil, err 2323 } 2324 2325 // Construct ASTBuilder 2326 v := &ASTBuilder{ 2327 Logger: logger, 2328 IStream: stream, 2329 ParameterPosition: 0, 2330 SQL2AqlCtx: &SQL2AqlContext{ 2331 MapQueryIdentifier: make(map[int]string), 2332 MapMeasures: make(map[int][]Measure), 2333 MapDimensions: make(map[int][]Dimension), 2334 MapJoinTables: make(map[int][]Join), 2335 MapRowFilters: make(map[int][]string), 2336 MapOrderBy: make(map[int][]SortField), 2337 MapLimit: make(map[int]int), 2338 }, 2339 } 2340 node := v.VisitQuery(parseTree) 2341 if _, ok := node.(*tree.Query); ok { 2342 aql = v.GetAQL() 2343 aql.SQLQuery = sql 2344 aqlJSON, _ := json.Marshal(aql) 2345 logger.Infof("convert SQL:\n%v\nto AQL:\n%v", sql, string(aqlJSON)) 2346 } 2347 2348 if len(aql.SupportingDimensions) > 0 || len(aql.SupportingMeasures) > 0 { 2349 err = fmt.Errorf("sub query not supported yet") 2350 return 2351 } 2352 2353 // non agg query overwrite 2354 if len(aql.Dimensions) == 0 { 2355 if v.aggFuncExists { 2356 err = fmt.Errorf("no aggregate functions allowed when no group by specified") 2357 return 2358 } 2359 for _, measure := range aql.Measures { 2360 aql.Dimensions = append(aql.Dimensions, Dimension{ 2361 Expr: measure.Expr, 2362 }) 2363 } 2364 aql.Measures = []Measure{{ 2365 Expr: "1", 2366 }} 2367 } 2368 2369 return 2370 }