Java Code Examples for com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr#getRight()

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr#getRight() . 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: WhereParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private boolean explanSpecialCondWithBothSidesAreLiterals(SQLBinaryOpExpr bExpr, Where where) throws SqlParseException {
    if ((bExpr.getLeft() instanceof SQLNumericLiteralExpr || bExpr.getLeft() instanceof SQLCharExpr) &&
            (bExpr.getRight() instanceof SQLNumericLiteralExpr || bExpr.getRight() instanceof SQLCharExpr)
            ) {
        SQLMethodInvokeExpr sqlMethodInvokeExpr = new SQLMethodInvokeExpr("script", null);
        String operator = bExpr.getOperator().getName();
        if (operator.equals("=")) {
            operator = "==";
        }
        sqlMethodInvokeExpr.addParameter(
                new SQLCharExpr(Util.expr2Object(bExpr.getLeft(), "'") +
                        " " + operator + " " +
                        Util.expr2Object(bExpr.getRight(), "'"))
        );

        explanCond("AND", sqlMethodInvokeExpr, where);
        return true;
    }
    return false;
}
 
Example 2
Source File: DruidSelectParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private HavingCols buildGroupByHaving(SQLExpr having,Map<String, String> aliaColumns ){
	if (having == null) {
		return null;
	}

	SQLBinaryOpExpr expr  = ((SQLBinaryOpExpr) having);
	SQLExpr left = expr.getLeft();
	SQLBinaryOperator operator = expr.getOperator();
	SQLExpr right = expr.getRight();

	String leftValue = null;;
	if (left instanceof SQLAggregateExpr) {
		leftValue = ((SQLAggregateExpr) left).getMethodName() + "("
				+ ((SQLAggregateExpr) left).getArguments().get(0) + ")";
		String aggrColumnAlias = getAliaColumn(aliaColumns,leftValue);
		if(aggrColumnAlias != null) { // having聚合函数存在别名
			expr.setLeft(new SQLIdentifierExpr(aggrColumnAlias));
			leftValue = aggrColumnAlias;
		}
	} else if (left instanceof SQLIdentifierExpr) {
		leftValue = ((SQLIdentifierExpr) left).getName();
	}

	String rightValue = null;
	if (right instanceof  SQLNumericLiteralExpr) {
		rightValue = right.toString();
	}else if(right instanceof SQLTextLiteralExpr){
		rightValue = StringUtil.removeBackquote(right.toString());
	}

	return new HavingCols(leftValue,rightValue,operator.getName());
}
 
Example 3
Source File: DruidSelectOracleParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void parseThreeLevelPageSql(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLSubqueryTableSource from, SQLBinaryOpExpr one, SQLBinaryOperator operator)
{
       SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
	int firstrownum = right.getNumber().intValue();
	if (operator == SQLBinaryOperator.GreaterThanOrEqual&&firstrownum!=0) {
		firstrownum = firstrownum - 1;
	}
	SQLSelectQuery subSelect = from.getSelect().getQuery();
	if (subSelect instanceof OracleSelectQueryBlock)
       {  //第二层子查询
           OracleSelectQueryBlock twoSubSelect = (OracleSelectQueryBlock) subSelect;
           if (twoSubSelect.getWhere() instanceof SQLBinaryOpExpr && twoSubSelect.getFrom() instanceof SQLSubqueryTableSource)
           {
               SQLBinaryOpExpr twoWhere = (SQLBinaryOpExpr) twoSubSelect.getWhere();
               boolean isRowNum = "rownum".equalsIgnoreCase(twoWhere.getLeft().toString());
               boolean isLess = twoWhere.getOperator() == SQLBinaryOperator.LessThanOrEqual || twoWhere.getOperator() == SQLBinaryOperator.LessThan;
               if (isRowNum && twoWhere.getRight() instanceof SQLIntegerExpr && isLess)
               {
                   int lastrownum = ((SQLIntegerExpr) twoWhere.getRight()).getNumber().intValue();
                   if (operator == SQLBinaryOperator.LessThan&&lastrownum!=0) {
					lastrownum = lastrownum - 1;
				}
                   SQLSelectQuery finalQuery = ((SQLSubqueryTableSource) twoSubSelect.getFrom()).getSelect().getQuery();
                   if (finalQuery instanceof OracleSelectQueryBlock)
                   {
					setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                       parseOrderAggGroupOracle(stmt,rrs, (OracleSelectQueryBlock) finalQuery, schema);
                       isNeedParseOrderAgg=false;
                   }

               }

           }

       }
}
 
Example 4
Source File: ExpressionUtil.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * (A OR B) AND C OR B OR (E OR F) AND G = ((A AND C ) OR (A AND B)) OR B OR
 * ((E AND G) OR (F AND G))
 *
 * @param expr
 * @return
 */
private static SQLBinaryOpExpr expandOrExpression(SQLBinaryOpExpr expr) {
    SQLExpr left = expr.getLeft();
    SQLExpr right = expr.getRight();
    SQLExpr leftOp = toDNF(left);
    SQLExpr rightOp = toDNF(right);
    return new SQLBinaryOpExpr(leftOp, SQLBinaryOperator.BooleanOr, rightOp);
}
 
Example 5
Source File: ExpressionUtil.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * allow (A and B) and C
 *
 * @param expr
 * @return
 */
public static boolean isDNF(SQLExpr expr) {
    if (!isLogicalExpression(expr)) {
        return true;
    }
    SQLBinaryOpExpr binOpExpr = (SQLBinaryOpExpr) expr;
    SQLExpr left = binOpExpr.getLeft();
    SQLExpr right = binOpExpr.getRight();
    if (binOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
        boolean isAllNonLogicExpr = true;
        if (left instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr leftBinaryOp = (SQLBinaryOpExpr) left;
            if (leftBinaryOp.getOperator() == SQLBinaryOperator.BooleanOr) {
                return false;
            }
            if (isLogicalExpression(leftBinaryOp)) {
                isAllNonLogicExpr = false;
            }
        }
        if (right instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr rightBinaryOp = (SQLBinaryOpExpr) right;
            if (rightBinaryOp.getOperator() == SQLBinaryOperator.BooleanOr) {
                return false;
            }
            if (isLogicalExpression(rightBinaryOp)) {
                isAllNonLogicExpr = false;
            }
        }
        if (isAllNonLogicExpr) {
            return true;
        }
    }

    if (!isDNF(left)) {
        return false;
    }
    return isDNF(right);
}
 
Example 6
Source File: WhereParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private boolean explanSpecialCondWithBothSidesAreProperty(SQLBinaryOpExpr bExpr, Where where) throws SqlParseException {
    //join is not support
    if ((bExpr.getLeft() instanceof SQLPropertyExpr || bExpr.getLeft() instanceof SQLIdentifierExpr) &&
            (bExpr.getRight() instanceof SQLPropertyExpr || bExpr.getRight() instanceof SQLIdentifierExpr) &&
            Sets.newHashSet("=", "<", ">", ">=", "<=","<>","!=").contains(bExpr.getOperator().getName()) &&
            !Util.isFromJoinOrUnionTable(bExpr)

            ) {
        SQLMethodInvokeExpr sqlMethodInvokeExpr = new SQLMethodInvokeExpr("script", null);
        String operator = bExpr.getOperator().getName();
        if (operator.equals("=")) {
            operator = "==";
        }else
        if (operator.equals("<>")) {
            operator = "!=";
        }

        String leftProperty = Util.expr2Object(bExpr.getLeft()).toString();
        String rightProperty = Util.expr2Object(bExpr.getRight()).toString();
        if (leftProperty.split("\\.").length > 1) {

            leftProperty = leftProperty.substring(leftProperty.split("\\.")[0].length() + 1);
        }

        if (rightProperty.split("\\.").length > 1) {
            rightProperty = rightProperty.substring(rightProperty.split("\\.")[0].length() + 1);
        }

        sqlMethodInvokeExpr.addParameter(new SQLCharExpr(
                "doc['" + leftProperty + "'].value " +
                        operator +
                        " doc['" + rightProperty + "'].value"));


        explanCond("AND", sqlMethodInvokeExpr, where);
        return true;
    }
    return false;
}
 
Example 7
Source File: ScriptFilter.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public boolean tryParseFromMethodExpr(SQLMethodInvokeExpr expr) throws SqlParseException {
    if (!expr.getMethodName().toLowerCase().equals("script")) {
        return false;
    }
    List<SQLExpr> methodParameters = expr.getParameters();
    if (methodParameters.size() == 0) {
        return false;
    }
    script = Util.extendedToString(methodParameters.get(0));

    if (methodParameters.size() == 1) {
        return true;
    }

    args = new HashMap<>();
    for (int i = 1; i < methodParameters.size(); i++) {

        SQLExpr innerExpr = methodParameters.get(i);
        if (!(innerExpr instanceof SQLBinaryOpExpr)) {
            return false;
        }
        SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) innerExpr;
        if (!binaryOpExpr.getOperator().getName().equals("=")) {
            return false;
        }

        SQLExpr right = binaryOpExpr.getRight();
        Object value = Util.expr2Object(right);
        String key = Util.extendedToString(binaryOpExpr.getLeft());
        if(key.equals("script_type")){
            parseAndUpdateScriptType(value.toString());
        }
        else {
            args.put(key, value);
        }

    }
    return true;
}
 
Example 8
Source File: WhereParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private SQLMethodInvokeExpr parseSQLBinaryOpExprWhoIsConditionInWhere(SQLBinaryOpExpr soExpr) throws SqlParseException {

        if (!(soExpr.getLeft() instanceof SQLCastExpr || soExpr.getRight() instanceof SQLCastExpr)) {
            if (!(soExpr.getLeft() instanceof SQLMethodInvokeExpr ||
                soExpr.getRight() instanceof SQLMethodInvokeExpr)) {
                return null;
            }

            if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
                if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getLeft()).getMethodName())) {
                    return null;
                }
            }

            if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
                if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getRight()).getMethodName())) {
                    return null;
                }
            }
        }

        MethodField leftMethod = new MethodField(null, Lists.newArrayList(new KVValue("", Util.expr2Object(soExpr.getLeft(), "'"))), null, null);
        if (soExpr.getLeft() instanceof SQLIdentifierExpr || soExpr.getLeft() instanceof SQLPropertyExpr) {
            leftMethod = new MethodField(null, Lists.newArrayList(new KVValue("", "doc['" + Util.expr2Object(soExpr.getLeft(), "'") + "'].value")), null, null);
        } else if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
            leftMethod = parseSQLMethodInvokeExprWithFunctionInWhere((SQLMethodInvokeExpr) soExpr.getLeft());
        } else if (soExpr.getLeft() instanceof SQLCastExpr) {
            leftMethod = parseSQLCastExprInWhere((SQLCastExpr) soExpr.getLeft());
        }

        MethodField rightMethod = new MethodField(null, Lists.newArrayList(new KVValue("", Util.expr2Object(soExpr.getRight(), "'"))), null, null);
        if (soExpr.getRight() instanceof SQLIdentifierExpr || soExpr.getRight() instanceof SQLPropertyExpr) {
            rightMethod = new MethodField(null, Lists.newArrayList(new KVValue("", "doc['" + Util.expr2Object(soExpr.getRight(), "'") + "'].value")), null, null);
        } else if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
            rightMethod = parseSQLMethodInvokeExprWithFunctionInWhere((SQLMethodInvokeExpr) soExpr.getRight());
        } else if (soExpr.getRight() instanceof SQLCastExpr) {
            rightMethod = parseSQLCastExprInWhere((SQLCastExpr) soExpr.getRight());
        }

        String v1 = leftMethod.getParams().get(0).value.toString();
        String v1Dec = leftMethod.getParams().size() == 2 ? leftMethod.getParams().get(1).value.toString() + ";" : "";

        String v2 = rightMethod.getParams().get(0).value.toString();
        String v2Dec = rightMethod.getParams().size() == 2 ? rightMethod.getParams().get(1).value.toString() + ";" : "";

        String operator = soExpr.getOperator().getName();

        if ("=".equals(operator)) {
            operator = "==";
        }

        String finalStr = String.format("%s%s((Comparable)%s).compareTo(%s) %s 0", v1Dec, v2Dec, v1, v2, operator);

        SQLMethodInvokeExpr scriptMethod = new SQLMethodInvokeExpr("script", null);
        scriptMethod.addParameter(new SQLCharExpr(finalStr));
        return scriptMethod;

    }