Java Code Examples for com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr. These examples are extracted from open source projects. 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 Project: elasticsearch-sql   Source File: Maker.java    License: Apache License 2.0 6 votes vote down vote up
private Object parseTermValue(Object termValue) {
    if (termValue instanceof SQLNumericLiteralExpr) {
        termValue = ((SQLNumericLiteralExpr) termValue).getNumber();
        if (termValue instanceof BigDecimal || termValue instanceof Double) {
            termValue = ((Number) termValue).doubleValue();
        } else if (termValue instanceof Float) {
            termValue = ((Number) termValue).floatValue();
        } else if (termValue instanceof BigInteger || termValue instanceof Long) {
            termValue = ((Number) termValue).longValue();
        } else if (termValue instanceof Integer) {
            termValue = ((Number) termValue).intValue();
        } else if (termValue instanceof Short) {
            termValue = ((Number) termValue).shortValue();
        } else if (termValue instanceof Byte) {
            termValue = ((Number) termValue).byteValue();
        }
    } else if (termValue instanceof SQLBooleanExpr) {
        termValue = ((SQLBooleanExpr) termValue).getValue();
    } else {
        termValue = termValue.toString();
    }

    return termValue;
}
 
Example 2
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Object expr2Object(SQLExpr expr, String charWithQuote) {
    Object value = null;
    if (expr instanceof SQLNumericLiteralExpr) {
        value = ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLCharExpr) {
        value = charWithQuote + ((SQLCharExpr) expr).getText() + charWithQuote;
    } else if (expr instanceof SQLIdentifierExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLPropertyExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLVariantRefExpr) {
        value = expr.toString();
    } else if (expr instanceof SQLAllColumnExpr) {
        value = "*";
    } else if (expr instanceof SQLValuableExpr) {
        value = ((SQLValuableExpr) expr).getValue();
    } else if (expr instanceof SQLBooleanExpr) {
        value = ((SQLBooleanExpr) expr).getValue();
    } else {
        //throw new SqlParseException("can not support this type " + expr.getClass());
    }
    return value;
}
 
Example 3
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Object getScriptValueWithQuote(SQLExpr expr, String quote) throws SqlParseException {
    if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) {
        return "doc['" + expr.toString() + "'].value";
    }  else if (expr instanceof SQLCharExpr) {
        return quote + ((SQLCharExpr) expr).getValue() + quote;
    } else if (expr instanceof SQLIntegerExpr) {
        return ((SQLIntegerExpr) expr).getValue();
    } else if (expr instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLNullExpr) {
        return ((SQLNullExpr) expr).toString().toLowerCase();
    } else if (expr instanceof  SQLBinaryOpExpr) {
        //zhongshu-comment 该分支由忠树添加
        String left = "doc['" + ((SQLBinaryOpExpr) expr).getLeft().toString() + "'].value";
        String operator = ((SQLBinaryOpExpr) expr).getOperator().getName();
        String right = "doc['" + ((SQLBinaryOpExpr) expr).getRight().toString() + "'].value";
        return left + operator + right;
    }
    throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got " + expr.getClass().toString() + " with value:" + expr.toString());
}
 
Example 4
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: 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 5
Source Project: Mycat2   Source File: DruidSelectParser.java    License: 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 6
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: Apache License 2.0 5 votes vote down vote up
private Object parseValue(SQLExpr expr) throws SqlParseException {
    if (expr instanceof SQLNumericLiteralExpr) {
        Number number = ((SQLNumericLiteralExpr) expr).getNumber();
        if(number instanceof BigDecimal){
            return number.doubleValue();
        }
        if(number instanceof BigInteger){
            return number.longValue();
        }
        return ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLCharExpr) {
        return ((SQLCharExpr) expr).getText();
    } else if (expr instanceof SQLMethodInvokeExpr) {
        return expr;
    } else if (expr instanceof SQLNullExpr) {
        return null;
    } else if (expr instanceof SQLIdentifierExpr) {
        return expr;
    } else if (expr instanceof SQLPropertyExpr) {
        return expr;
    } else {
        /*
        zhongshu-comment 解析where子查询时会抛出这样的异常:
        Failed to parse SqlExpression of type class com.alibaba.druid.sql.ast.expr.SQLQueryExpr. expression value: [email protected]
         */
        throw new SqlParseException(
                String.format("Failed to parse SqlExpression of type %s. expression value: %s", expr.getClass(), expr)
        );
    }
}
 
Example 7
Source Project: elasticsearch-sql   Source File: SQLFunctions.java    License: Apache License 2.0 4 votes vote down vote up
private static String scriptDeclare(SQLExpr a) {

        if (isProperty(a) || a instanceof SQLNumericLiteralExpr)
            return "";
        else return Util.expr2Object(a).toString() + ";";
    }
 
Example 8
Source Project: elasticsearch-sql   Source File: Paramer.java    License: Apache License 2.0 4 votes vote down vote up
public static Paramer parseParamer(SQLMethodInvokeExpr method) throws SqlParseException {
	Paramer instance = new Paramer();
	List<SQLExpr> parameters = method.getParameters();
       for (SQLExpr expr : parameters) {
           if (expr instanceof SQLCharExpr) {
               if (instance.value == null) {
                   instance.value = ((SQLCharExpr) expr).getText();
               } else {
                   instance.analysis = ((SQLCharExpr) expr).getText();
               }
           } else if (expr instanceof SQLNumericLiteralExpr) {
               instance.boost = ((SQLNumericLiteralExpr) expr).getNumber().floatValue();
           } else if (expr instanceof SQLBinaryOpExpr) {
               SQLBinaryOpExpr sqlExpr = (SQLBinaryOpExpr) expr;
               switch (Util.expr2Object(sqlExpr.getLeft()).toString()) {
                   case "query":
                       instance.value = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "analyzer":
                       instance.analysis = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "boost":
                       instance.boost = Float.parseFloat(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "slop":
                       instance.slop = Integer.parseInt(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;

                   case "fields":
                       int index;
                       for (String f : Strings.splitStringByCommaToArray(Util.expr2Object(sqlExpr.getRight()).toString())) {
                           index = f.lastIndexOf('^');
                           if (-1 < index) {
                               instance.fieldsBoosts.put(f.substring(0, index), Float.parseFloat(f.substring(index + 1)));
                           } else {
                               instance.fieldsBoosts.put(f, 1.0F);
                           }
                       }
                       break;
                   case "type":
                       instance.type = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "tie_breaker":
                       instance.tieBreaker = Float.parseFloat(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "operator":
                       instance.operator = Operator.fromString(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;

                   case "default_field":
                       instance.defaultField = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;

                   case "in_order":
                       instance.inOrder = Boolean.valueOf(Util.expr2Object(sqlExpr.getRight()).toString());
                       break;
                   case "clauses":
                       instance.clauses = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;
                   case "minimum_should_match":
                       instance.minimumShouldMatch = Util.expr2Object(sqlExpr.getRight()).toString();
                       break;

                   default:
                       break;
               }
           }
       }

	return instance;
}