|
14 | 14 | import org.nlpcn.es4sql.domain.hints.Hint;
|
15 | 15 | import org.nlpcn.es4sql.domain.hints.HintFactory;
|
16 | 16 | import org.nlpcn.es4sql.exception.SqlParseException;
|
17 |
| -import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; |
18 | 17 | import org.nlpcn.es4sql.spatial.SpatialParamsFactory;
|
19 | 18 |
|
20 | 19 | /**
|
@@ -350,24 +349,29 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP
|
350 | 349 | return;
|
351 | 350 | }
|
352 | 351 | List<SQLSelectOrderByItem> items = orderBy.getItems();
|
353 |
| - List<String> lists = new ArrayList<>(); |
354 |
| - for (SQLSelectOrderByItem sqlSelectOrderByItem : items) { |
355 |
| - SQLExpr expr = sqlSelectOrderByItem.getExpr(); |
356 |
| - lists.add(FieldMaker.makeField(expr, null,null).toString()); |
357 |
| - if (sqlSelectOrderByItem.getType() == null) { |
358 |
| - sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); |
359 |
| - } |
360 |
| - String type = sqlSelectOrderByItem.getType().toString(); |
361 |
| - for (String name : lists) { |
362 |
| - name = name.replace("`", ""); |
363 |
| - select.addOrderBy(name, type); |
364 |
| - } |
365 |
| - lists.clear(); |
366 |
| - } |
367 | 352 |
|
368 |
| - } |
| 353 | + addOrderByToSelect(select, items, null); |
| 354 | + |
| 355 | + } |
| 356 | + |
| 357 | + private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items, String alias) throws SqlParseException { |
| 358 | + for (SQLSelectOrderByItem sqlSelectOrderByItem : items) { |
| 359 | + SQLExpr expr = sqlSelectOrderByItem.getExpr(); |
| 360 | + String orderByName = FieldMaker.makeField(expr, null, null).toString(); |
369 | 361 |
|
370 |
| - private void findLimit(MySqlSelectQueryBlock.Limit limit, Select select) { |
| 362 | + if (sqlSelectOrderByItem.getType() == null) { |
| 363 | + sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); |
| 364 | + } |
| 365 | + String type = sqlSelectOrderByItem.getType().toString(); |
| 366 | + |
| 367 | + orderByName = orderByName.replace("`", ""); |
| 368 | + if(alias!=null) orderByName = orderByName.replaceFirst(alias+"\\.",""); |
| 369 | + select.addOrderBy(orderByName, type); |
| 370 | + |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + private void findLimit(MySqlSelectQueryBlock.Limit limit, Select select) { |
371 | 375 |
|
372 | 376 | if (limit == null) {
|
373 | 377 | return;
|
@@ -419,17 +423,38 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException
|
419 | 423 | String firstTableAlias = joinedFrom.get(0).getAlias();
|
420 | 424 | String secondTableAlias = joinedFrom.get(1).getAlias();
|
421 | 425 | Map<String, Where> aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias);
|
| 426 | + Map<String, List<SQLSelectOrderByItem>> aliasToOrderBy = splitAndFindOrder(query.getOrderBy(), firstTableAlias, secondTableAlias); |
422 | 427 | List<Condition> connectedConditions = getConditionsFlatten(joinSelect.getConnectedWhere());
|
423 | 428 | joinSelect.setConnectedConditions(connectedConditions);
|
424 |
| - fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), connectedConditions); |
425 |
| - fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), connectedConditions); |
| 429 | + fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias),aliasToOrderBy.get(firstTableAlias), connectedConditions); |
| 430 | + fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), aliasToOrderBy.get(secondTableAlias),connectedConditions); |
426 | 431 |
|
427 | 432 | updateJoinLimit(query.getLimit(), joinSelect);
|
428 | 433 |
|
429 | 434 | //todo: throw error feature not supported: no group bys on joins ?
|
430 | 435 | return joinSelect;
|
431 | 436 | }
|
432 | 437 |
|
| 438 | + private Map<String, List<SQLSelectOrderByItem>> splitAndFindOrder(SQLOrderBy orderBy, String firstTableAlias, String secondTableAlias) throws SqlParseException { |
| 439 | + Map<String,List<SQLSelectOrderByItem>> aliasToOrderBys = new HashMap<>(); |
| 440 | + aliasToOrderBys.put(firstTableAlias,new ArrayList<SQLSelectOrderByItem>()); |
| 441 | + aliasToOrderBys.put(secondTableAlias,new ArrayList<SQLSelectOrderByItem>()); |
| 442 | + if(orderBy == null) return aliasToOrderBys; |
| 443 | + List<SQLSelectOrderByItem> orderByItems = orderBy.getItems(); |
| 444 | + for(SQLSelectOrderByItem orderByItem : orderByItems){ |
| 445 | + if(orderByItem.getExpr().toString().startsWith(firstTableAlias+".")){ |
| 446 | + aliasToOrderBys.get(firstTableAlias).add(orderByItem); |
| 447 | + } |
| 448 | + else if(orderByItem.getExpr().toString().startsWith(secondTableAlias+".")){ |
| 449 | + aliasToOrderBys.get(secondTableAlias).add(orderByItem); |
| 450 | + } |
| 451 | + else |
| 452 | + throw new SqlParseException("order by field on join request should have alias before, got " + orderByItem.getExpr().toString()); |
| 453 | + |
| 454 | + } |
| 455 | + return aliasToOrderBys; |
| 456 | + } |
| 457 | + |
433 | 458 | private void updateJoinLimit(MySqlSelectQueryBlock.Limit limit, JoinSelect joinSelect) {
|
434 | 459 | if(limit != null && limit.getRowCount()!= null) {
|
435 | 460 | int sizeLimit = Integer.parseInt(limit.getRowCount().toString());
|
@@ -463,9 +488,9 @@ private Map<String, Where> splitAndFindWhere(SQLExpr whereExpr, String firstTabl
|
463 | 488 | return splitWheres(where, firstTableAlias, secondTableAlias);
|
464 | 489 | }
|
465 | 490 |
|
466 |
| - private void fillTableSelectedJoin(TableOnJoinSelect tableOnJoin,MySqlSelectQueryBlock query, From tableFrom, Where where, List<Condition> conditions) throws SqlParseException { |
| 491 | + private void fillTableSelectedJoin(TableOnJoinSelect tableOnJoin, MySqlSelectQueryBlock query, From tableFrom, Where where, List<SQLSelectOrderByItem> orderBys, List<Condition> conditions) throws SqlParseException { |
467 | 492 | String alias = tableFrom.getAlias();
|
468 |
| - fillBasicTableSelectJoin(tableOnJoin, tableFrom, where, query); |
| 493 | + fillBasicTableSelectJoin(tableOnJoin, tableFrom, where,orderBys, query); |
469 | 494 | tableOnJoin.setConnectedFields(getConnectedFields(conditions, alias));
|
470 | 495 | tableOnJoin.setSelectedFields(new ArrayList<Field>(tableOnJoin.getFields()));
|
471 | 496 | tableOnJoin.setAlias(alias);
|
@@ -493,10 +518,11 @@ private List<Field> getConnectedFields(List<Condition> conditions, String alias)
|
493 | 518 | return fields;
|
494 | 519 | }
|
495 | 520 |
|
496 |
| - private void fillBasicTableSelectJoin(TableOnJoinSelect select, From from, Where where, MySqlSelectQueryBlock query) throws SqlParseException { |
| 521 | + private void fillBasicTableSelectJoin(TableOnJoinSelect select, From from, Where where, List<SQLSelectOrderByItem> orderBys, MySqlSelectQueryBlock query) throws SqlParseException { |
497 | 522 | select.getFrom().add(from);
|
498 | 523 | findSelect(query, select,from.getAlias());
|
499 | 524 | select.setWhere(where);
|
| 525 | + addOrderByToSelect(select, orderBys,from.getAlias()); |
500 | 526 | }
|
501 | 527 |
|
502 | 528 | private List<Condition> getJoinConditionsFlatten(SQLJoinTableSource from) throws SqlParseException {
|
|
0 commit comments