use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project drill by apache.
the class DrillAvgVarianceConvertlet method expandVariance.
private SqlNode expandVariance(final SqlNode arg, boolean biased, boolean sqrt) {
/* stddev_pop(x) ==>
* power(
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / count(x),
* .5)
* stddev_samp(x) ==>
* power(
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / (count(x) - 1),
* .5)
* var_pop(x) ==>
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / count(x)
* var_samp(x) ==>
* (sum(x * x) - sum(x) * sum(x) / count(x))
* / (count(x) - 1)
*/
final SqlParserPos pos = SqlParserPos.ZERO;
// cast the argument to double
final SqlNode castHighArg = CastHighOp.createCall(pos, arg);
final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, castHighArg, castHighArg);
final SqlNode sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared);
final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, castHighArg);
final SqlNode sumSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, sum, sum);
final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, castHighArg);
final SqlNode avgSumSquared = SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquared, count);
final SqlNode diff = SqlStdOperatorTable.MINUS.createCall(pos, sumArgSquared, avgSumSquared);
final SqlNode denominator;
if (biased) {
denominator = count;
} else {
final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos);
denominator = SqlStdOperatorTable.MINUS.createCall(pos, count, one);
}
final SqlNode diffAsDouble = CastHighOp.createCall(pos, diff);
final SqlNode div = SqlStdOperatorTable.DIVIDE.createCall(pos, diffAsDouble, denominator);
SqlNode result = div;
if (sqrt) {
final SqlNumericLiteral half = SqlLiteral.createExactNumeric("0.5", pos);
result = SqlStdOperatorTable.POWER.createCall(pos, div, half);
}
return result;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project calcite by apache.
the class SqlBetweenOperator method reduceExpr.
public ReduceResult reduceExpr(int opOrdinal, TokenSequence list) {
SqlOperator op = list.op(opOrdinal);
assert op == this;
// Break the expression up into expressions. For example, a simple
// expression breaks down as follows:
//
// opOrdinal endExp1
// | |
// a + b BETWEEN c + d AND e + f
// |_____| |_____| |_____|
// exp0 exp1 exp2
// Create the expression between 'BETWEEN' and 'AND'.
SqlNode exp1 = SqlParserUtil.toTreeEx(list, opOrdinal + 1, 0, SqlKind.AND);
if ((opOrdinal + 2) >= list.size()) {
SqlParserPos lastPos = list.pos(list.size() - 1);
final int line = lastPos.getEndLineNum();
final int col = lastPos.getEndColumnNum() + 1;
SqlParserPos errPos = new SqlParserPos(line, col, line, col);
throw SqlUtil.newContextException(errPos, RESOURCE.betweenWithoutAnd());
}
if (!list.isOp(opOrdinal + 2) || list.op(opOrdinal + 2).getKind() != SqlKind.AND) {
SqlParserPos errPos = list.pos(opOrdinal + 2);
throw SqlUtil.newContextException(errPos, RESOURCE.betweenWithoutAnd());
}
// Create the expression after 'AND', but stopping if we encounter an
// operator of lower precedence.
//
// For example,
// a BETWEEN b AND c + d OR e
// becomes
// (a BETWEEN b AND c + d) OR e
// because OR has lower precedence than BETWEEN.
SqlNode exp2 = SqlParserUtil.toTreeEx(list, opOrdinal + 3, getRightPrec(), SqlKind.OTHER);
// Create the call.
SqlNode exp0 = list.node(opOrdinal - 1);
SqlCall newExp = createCall(list.pos(opOrdinal), exp0, exp1, exp2);
// Replace all of the matched nodes with the single reduced node.
return new ReduceResult(opOrdinal - 1, opOrdinal + 4, newExp);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project calcite by apache.
the class SqlNullifFunction method rewriteCall.
// ~ Methods ----------------------------------------------------------------
// override SqlOperator
public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
List<SqlNode> operands = call.getOperandList();
SqlParserPos pos = call.getParserPosition();
checkOperandCount(validator, getOperandTypeChecker(), call);
assert operands.size() == 2;
SqlNodeList whenList = new SqlNodeList(pos);
SqlNodeList thenList = new SqlNodeList(pos);
whenList.add(operands.get(1));
thenList.add(SqlLiteral.createNull(SqlParserPos.ZERO));
return SqlCase.createSwitched(pos, operands.get(0), whenList, thenList, SqlNode.clone(operands.get(0)));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project calcite by apache.
the class SqlDatePartFunction method rewriteCall.
// ~ Methods ----------------------------------------------------------------
@Override
public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
final List<SqlNode> operands = call.getOperandList();
final SqlParserPos pos = call.getParserPosition();
return SqlStdOperatorTable.EXTRACT.createCall(pos, new SqlIntervalQualifier(timeUnit, null, SqlParserPos.ZERO), operands.get(0));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project calcite by apache.
the class SqlValidatorUtil method addAlias.
/**
* Converts an expression "expr" into "expr AS alias".
*/
public static SqlNode addAlias(SqlNode expr, String alias) {
final SqlParserPos pos = expr.getParserPosition();
final SqlIdentifier id = new SqlIdentifier(alias, pos);
return SqlStdOperatorTable.AS.createCall(pos, expr, id);
}
Aggregations