Search in sources :

Example 6 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project drill by apache.

the class DrillAvgVarianceConvertlet method convertCall.

public RexNode convertCall(SqlRexContext cx, SqlCall call) {
    assert call.operandCount() == 1;
    final SqlNode arg = call.operand(0);
    final SqlNode expr;
    switch(subtype) {
        case AVG:
            expr = expandAvg(arg);
            break;
        case STDDEV_POP:
            expr = expandVariance(arg, true, true);
            break;
        case STDDEV_SAMP:
            expr = expandVariance(arg, false, true);
            break;
        case VAR_POP:
            expr = expandVariance(arg, true, false);
            break;
        case VAR_SAMP:
            expr = expandVariance(arg, false, false);
            break;
        default:
            throw Util.unexpected(subtype);
    }
    return cx.convertExpression(expr);
}
Also used : SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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 8 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project drill by apache.

the class DrillCompoundIdentifier method getAsSqlNode.

public SqlNode getAsSqlNode() {
    if (ids.size() == 1) {
        return new SqlIdentifier(Collections.singletonList(ids.get(0).value), ids.get(0).parserPos);
    }
    int startIndex;
    SqlNode node;
    if (ids.get(1).isArray()) {
        // handle everything post zero index as item operator.
        startIndex = 1;
        node = new //
        SqlIdentifier(//
        ImmutableList.of(ids.get(0).value), //
        null, //
        ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos));
    } else {
        // handle everything post two index as item operator.
        startIndex = 2;
        node = new //
        SqlIdentifier(//
        ImmutableList.of(ids.get(0).value, ids.get(1).value), //
        null, //
        ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos, ids.get(1).parserPos));
    }
    for (int i = startIndex; i < ids.size(); i++) {
        node = ids.get(i).getNode(node);
    }
    return node;
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project incubator-gobblin by apache.

the class JdbcExtractor method hasJoinOperation.

/**
 * Check if the SELECT query has join operation
 */
public static boolean hasJoinOperation(String selectQuery) {
    if (selectQuery == null || selectQuery.length() == 0) {
        return false;
    }
    SqlParser sqlParser = SqlParser.create(selectQuery);
    try {
        SqlNode all = sqlParser.parseQuery();
        SqlSelect query;
        if (all instanceof SqlSelect) {
            query = (SqlSelect) all;
        } else if (all instanceof SqlOrderBy) {
            query = (SqlSelect) ((SqlOrderBy) all).query;
        } else {
            throw new UnsupportedOperationException("The select query is type of " + all.getClass() + " which is not supported here");
        }
        return query.getFrom().getKind() == SqlKind.JOIN;
    } catch (SqlParseException e) {
        return false;
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlParser(org.apache.calcite.sql.parser.SqlParser) SqlOrderBy(org.apache.calcite.sql.SqlOrderBy) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.

the class SqlParserUtil method toTreeEx.

/**
 * Converts a list of {expression, operator, expression, ...} into a tree,
 * taking operator precedence and associativity into account.
 *
 * @param list        List of operands and operators. This list is modified as
 *                    expressions are reduced.
 * @param start       Position of first operand in the list. Anything to the
 *                    left of this (besides the immediately preceding operand)
 *                    is ignored. Generally use value 1.
 * @param minPrec     Minimum precedence to consider. If the method encounters
 *                    an operator of lower precedence, it doesn't reduce any
 *                    further.
 * @param stopperKind If not {@link SqlKind#OTHER}, stop reading the list if
 *                    we encounter a token of this kind.
 * @return the root node of the tree which the list condenses into
 */
public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list, int start, final int minPrec, final SqlKind stopperKind) {
    final Predicate<PrecedenceClimbingParser.Token> predicate = new PredicateImpl<PrecedenceClimbingParser.Token>() {

        public boolean test(PrecedenceClimbingParser.Token t) {
            if (t instanceof PrecedenceClimbingParser.Op) {
                final SqlOperator op = ((ToTreeListItem) t.o).op;
                return stopperKind != SqlKind.OTHER && op.kind == stopperKind || minPrec > 0 && op.getLeftPrec() < minPrec;
            } else {
                return false;
            }
        }
    };
    PrecedenceClimbingParser parser = list.parser(start, predicate);
    final int beforeSize = parser.all().size();
    parser.partialParse();
    final int afterSize = parser.all().size();
    final SqlNode node = convert(parser.all().get(0));
    list.replaceSublist(start, start + beforeSize - afterSize + 1, node);
    return node;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) PrecedenceClimbingParser(org.apache.calcite.util.PrecedenceClimbingParser) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)510 RelDataType (org.apache.calcite.rel.type.RelDataType)141 SqlNodeList (org.apache.calcite.sql.SqlNodeList)98 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)84 SqlCall (org.apache.calcite.sql.SqlCall)81 ArrayList (java.util.ArrayList)78 RelNode (org.apache.calcite.rel.RelNode)60 Test (org.junit.Test)59 BitString (org.apache.calcite.util.BitString)46 SqlSelect (org.apache.calcite.sql.SqlSelect)45 SqlWriter (org.apache.calcite.sql.SqlWriter)42 RexNode (org.apache.calcite.rex.RexNode)40 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)33 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)25 SqlLiteral (org.apache.calcite.sql.SqlLiteral)23 SqlOperator (org.apache.calcite.sql.SqlOperator)23 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 ImmutableList (com.google.common.collect.ImmutableList)20 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)20