com.alibaba.druid.sql.ast.SQLOrderingSpecification Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.SQLOrderingSpecification. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwoTableComparator.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public TwoTableComparator(List<FieldPacket> fps1, List<FieldPacket> fps2, List<Order> leftOrders,
                          List<Order> rightOrders, boolean isAllPushDown, DMLResponseHandler.HandlerType type,
                          boolean caseInsensitive) {
    this.leftFields = HandlerTool.createFields(fps1);
    this.rightFields = HandlerTool.createFields(fps2);
    ascList = new ArrayList<>();
    for (Order order : leftOrders) {
        ascList.add(order.getSortOrder() == SQLOrderingSpecification.ASC);
    }
    comparators = new ArrayList<>();
    for (int index = 0; index < ascList.size(); index++) {
        Order leftOrder = leftOrders.get(index);
        Order rightOrder = rightOrders.get(index);
        Item leftCmpItem = HandlerTool.createItem(leftOrder.getItem(), leftFields, 0, isAllPushDown, type);
        Item rightCmpItem = HandlerTool.createItem(rightOrder.getItem(), rightFields, 0, isAllPushDown,
                type);
        ArgComparator comparator = new ArgComparator(leftCmpItem, rightCmpItem);
        comparator.setCaseInsensitive(caseInsensitive);
        comparator.setCmpFunc(null, leftCmpItem, rightCmpItem, false);
        comparators.add(comparator);
    }
}
 
Example #2
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items, String alias) throws SqlParseException {
    for (SQLSelectOrderByItem sqlSelectOrderByItem : items) {
        SQLExpr expr = sqlSelectOrderByItem.getExpr();
        Field f = FieldMaker.makeField(expr, null, null);
        String orderByName = f.toString();

        if (sqlSelectOrderByItem.getType() == null) {
            sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); //zhongshu-comment 默认是升序排序
        }
        String type = sqlSelectOrderByItem.getType().toString();

        orderByName = orderByName.replace("`", "");
        if (alias != null) orderByName = orderByName.replaceFirst(alias + "\\.", "");

        ScriptSortBuilder.ScriptSortType scriptSortType = judgeIsStringSort(expr);
        select.addOrderBy(f.getNestedPath(), orderByName, type, scriptSortType);
    }
}
 
Example #3
Source File: RowDataComparator.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public RowDataComparator(List<Field> fields, List<Order> orders) {
    sourceFields = fields;
    if (orders != null && orders.size() > 0) {
        ascList = new ArrayList<>();
        cmpFields = new ArrayList<>();
        cmpItems = new ArrayList<>();
        for (Order order : orders) {
            Item cmpItem = HandlerTool.createFieldItem(order.getItem(), sourceFields, 0);
            cmpItems.add(cmpItem);
            FieldPacket tmpFp = new FieldPacket();
            cmpItem.makeField(tmpFp);
            Field cmpField = HandlerTool.createField(tmpFp);
            cmpFields.add(cmpField);
            ascList.add(order.getSortOrder() == SQLOrderingSpecification.ASC);
        }
    }
}
 
Example #4
Source File: RowDataComparator.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public RowDataComparator(List<FieldPacket> fps, List<Order> orders, boolean allPushDown, DMLResponseHandler.HandlerType type) {
    sourceFields = HandlerTool.createFields(fps);
    if (orders != null && orders.size() > 0) {
        ascList = new ArrayList<>();
        cmpFields = new ArrayList<>();
        cmpItems = new ArrayList<>();
        for (Order order : orders) {
            if (order.getItem() instanceof ItemScalarSubQuery) {
                if (((ItemScalarSubQuery) order.getItem()).getValue() == null) {
                    continue;
                }
            }
            Item cmpItem = createOrderItem(order.getItem(), this.sourceFields, allPushDown, type);
            if (cmpItem == null) {
                continue;
            }
            cmpItems.add(cmpItem);
            FieldPacket tmpFp = new FieldPacket();
            cmpItem.makeField(tmpFp);
            Field cmpField = HandlerTool.createField(tmpFp);
            cmpFields.add(cmpField);
            ascList.add(order.getSortOrder() == SQLOrderingSpecification.ASC);
        }
    }
}
 
Example #5
Source File: PlanNode.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public PlanNode orderBy(Item c, SQLOrderingSpecification sortOrder) {
    if (sortOrder == null) {
        sortOrder = SQLOrderingSpecification.ASC;
    }
    Order order = new Order(c, sortOrder);
    if (!this.orderBys.contains(order)) {
        this.orderBys.add(order);
    }
    return this;
}
 
Example #6
Source File: JoinParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private String getSQLExprToAsc(SQLOrderingSpecification ASC){
	if (ASC==null ) {
		return " ASC ";
	}
	if (ASC==SQLOrderingSpecification.DESC){
		return " DESC ";
	}
	else {
		return " ASC ";		
	}
}
 
Example #7
Source File: HandlerTool.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * make order by from distinct
 *
 * @param selects selects
 * @return order list
 */
public static List<Order> makeOrder(List<Item> selects) {
    List<Order> orders = new ArrayList<>();
    for (Item sel : selects) {
        Order order = new Order(sel, SQLOrderingSpecification.ASC);
        orders.add(order);
    }
    return orders;
}
 
Example #8
Source File: MongoSQLParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private int getSQLExprToAsc(SQLOrderingSpecification ASC){
	if (ASC==null ) {
		return 1;
	}
	if (ASC==SQLOrderingSpecification.DESC){
		return -1;
	}
	else {
		return 1;		
	}
}
 
Example #9
Source File: OrderbyColumn.java    From baymax with Apache License 2.0 5 votes vote down vote up
public static OrderbyType buildOrderbyType(SQLOrderingSpecification type){
    if (SQLOrderingSpecification.ASC == type){
        return OrderbyType.ASC;
    }
    if (SQLOrderingSpecification.DESC == type){
        return OrderbyType.DESC;
    }
    return OrderbyType.ASC;
}
 
Example #10
Source File: ElasticSqlExprParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public MySqlOrderingExpr parseSelectGroupByItem() {
    MySqlOrderingExpr item = new MySqlOrderingExpr();

    item.setExpr(expr());

    if (lexer.token() == Token.ASC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.ASC);
    } else if (lexer.token() == Token.DESC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.DESC);
    }

    return item;
}
 
Example #11
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private int getSQLExprToAsc(SQLOrderingSpecification ASC){
	if (ASC==null ) {
		return 1;
	}
	if (ASC==SQLOrderingSpecification.DESC){
		return -1;
	}
	else {
		return 1;		
	}
}
 
Example #12
Source File: Order.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public Order(Item item, SQLOrderingSpecification sortOrder) {
    this.item = item;
    this.sortOrder = sortOrder;
}
 
Example #13
Source File: Order.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public SQLOrderingSpecification getSortOrder() {
    return sortOrder;
}
 
Example #14
Source File: Order.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public void setSortOrder(SQLOrderingSpecification sortOrder) {
    this.sortOrder = sortOrder;
}
 
Example #15
Source File: PlanNode.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public PlanNode groupBy(Item c, SQLOrderingSpecification sortOrder) {
    Order order = new Order(c, sortOrder);
    this.groups.add(order);
    return this;
}
 
Example #16
Source File: Order.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public Order(Item item) {
    this(item, SQLOrderingSpecification.ASC);
}
 
Example #17
Source File: ShardLimitSqlWithConditionRewrite.java    From Zebra with Apache License 2.0 4 votes vote down vote up
private void initConditionExpr() {
	SQLBinaryOperator startOperator, endOperator;
	// 上下线每个条件的边际表达式
	SQLBinaryOpExpr startBoundaryExpr = null, endBoundaryExpr = null;

	for (SQLSelectOrderByItem orderByEle : context.getOrderBy().getItems()) {
		if (orderByEle.getType() == null || ((SQLOrderingSpecification) orderByEle.getType()).name().equals("ASC")) {
			startOperator = SQLBinaryOperator.GreaterThan;
			endOperator = SQLBinaryOperator.LessThan;
		} else {
			startOperator = SQLBinaryOperator.LessThan;
			endOperator = SQLBinaryOperator.GreaterThan;
		}
		try {
			SQLExpr starColumnExpr = convertColumnDataToSQLExpr(startData.get(orderByEle.getExpr().toString()));
			SQLExpr endColumnExpr = convertColumnDataToSQLExpr(endData.get(orderByEle.getExpr().toString()));
			SQLExpr compareExpr = orderByEle.getExpr();
			// 如果orderby的column是一个表达式的结果,则把表达式作为新条件的比较变量
			SQLObjectImpl selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString());
			if (selectItem == null) {
				selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString().toLowerCase());

				if (selectItem == null) {
					selectItem = context.getSelectItemMap().get(orderByEle.getExpr().toString().toUpperCase());
				}
			}

			if (selectItem != null) {
				if (selectItem instanceof SQLSelectItem) {
					compareExpr = ((SQLSelectItem) selectItem).getExpr();
				}
			}

			SQLBinaryOpExpr startExpr = new SQLBinaryOpExpr(compareExpr, startOperator, starColumnExpr);
			SQLBinaryOpExpr endExpr = new SQLBinaryOpExpr(compareExpr, endOperator, endColumnExpr);

			if (startBoundaryExpr != null) {
				// 在上一个边际表达式的基础上加上and条件
				addStartConditionExpr(new SQLBinaryOpExpr(startBoundaryExpr, SQLBinaryOperator.BooleanAnd, startExpr));
				addEndConditionExpr(new SQLBinaryOpExpr(endBoundaryExpr, SQLBinaryOperator.BooleanAnd, endExpr));
				startBoundaryExpr = new SQLBinaryOpExpr(startBoundaryExpr, SQLBinaryOperator.BooleanAnd,
				      new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, starColumnExpr));
				endBoundaryExpr = new SQLBinaryOpExpr(endBoundaryExpr, SQLBinaryOperator.BooleanAnd,
				      new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, endColumnExpr));
			} else {
				addStartConditionExpr(startExpr);
				addEndConditionExpr(endExpr);
				startBoundaryExpr = new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, starColumnExpr);
				endBoundaryExpr = new SQLBinaryOpExpr(compareExpr, SQLBinaryOperator.Equality, endColumnExpr);
			}
		} catch (SQLException ignore) {
		}
	}
	// 最后补上边际条件
	addStartConditionExpr(startBoundaryExpr);
	addEndConditionExpr(endBoundaryExpr);
}
 
Example #18
Source File: OrderByDataMerger.java    From Zebra with Apache License 2.0 4 votes vote down vote up
public int compareOrderByEle(RowData o1, RowData o2, MergeContext mergeContext) {
	SQLOrderBy orderBy = mergeContext.getOrderBy();
	List<SQLSelectOrderByItem> items = orderBy.getItems();
	try {
		for (SQLSelectOrderByItem orderByEle : items) {
			SQLName identifier = (SQLName) orderByEle
			      .getExpr();
			String columnLabel = mergeContext.getColumnNameAliasMapping().get(identifier.getSimpleName());
			if (columnLabel == null) {
				columnLabel = identifier.getSimpleName();
			}

			Object value1 = o1.get(columnLabel).getValue();
			Class<?> type1 = o1.get(columnLabel).getType();
			Object value2 = o2.get(columnLabel).getValue();
			Class<?> type2 = o2.get(columnLabel).getType();

			if (!type1.equals(type2)) {
				throw new SQLException("Invalid data");
			}

			if (!Comparable.class.isAssignableFrom(type1)) {
				throw new SQLException("Can not orderBy column : " + identifier + " which isn't comparable.");
			}

			@SuppressWarnings({ "unchecked", "rawtypes" })
			int compareRes = ((Comparable) value1).compareTo((Comparable) value2);

			if (orderByEle.getType() == null || ((SQLOrderingSpecification) orderByEle.getType()).name().equals("ASC")) {
				if (compareRes != 0) {
					return compareRes;
				}
			} else {
				if (compareRes != 0) {
					return compareRes < 0 ? 1 : -1;
				}
			}
		}

		return 0;

	} catch (SQLException e) {
		throw new RuntimeException(e);
	}
}