Search in sources :

Example 1 with SqlParserPos

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;
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlParserPos

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);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlParserPos

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)));
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with SqlParserPos

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));
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with SqlParserPos

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);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Aggregations

SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)42 SqlNode (org.apache.calcite.sql.SqlNode)17 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)11 RelDataType (org.apache.calcite.rel.type.RelDataType)9 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)9 BigDecimal (java.math.BigDecimal)8 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)6 SqlCall (org.apache.calcite.sql.SqlCall)5 ArrayList (java.util.ArrayList)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlNodeList (org.apache.calcite.sql.SqlNodeList)4 ImmutableList (com.google.common.collect.ImmutableList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RexNode (org.apache.calcite.rex.RexNode)3 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)3 SqlOperator (org.apache.calcite.sql.SqlOperator)3 Calendar (java.util.Calendar)2 IdentityHashMap (java.util.IdentityHashMap)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2