Search in sources :

Example 11 with SqlParserPos

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project drill by axbaretto.

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 = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, argSquared);
    final SqlNode sum = DrillCalciteSqlAggFunctionWrapper.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 12 with SqlParserPos

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project drill by axbaretto.

the class TestDrillSQLWorker method testErrorFormating.

@Test
public void testErrorFormating() {
    String sql = "Select * from Foo\nwhere tadadidada;\n";
    validateFormattedIs(sql, new SqlParserPos(1, 2), "Select * from Foo\n" + " ^\n" + "where tadadidada;\n");
    validateFormattedIs(sql, new SqlParserPos(2, 2), "Select * from Foo\n" + "where tadadidada;\n" + " ^\n");
    validateFormattedIs(sql, new SqlParserPos(1, 10), "Select * from Foo\n" + "         ^\n" + "where tadadidada;\n");
    validateFormattedIs(sql, new SqlParserPos(-11, -10), sql);
    validateFormattedIs(sql, new SqlParserPos(0, 10), sql);
    validateFormattedIs(sql, new SqlParserPos(100, 10), sql);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) Test(org.junit.Test) SqlTest(org.apache.drill.categories.SqlTest)

Example 13 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 DrillValidator method changeNames.

private void changeNames(SqlIdentifier sqlIdentifier, List<String> newNames) {
    SqlParserPos pos = sqlIdentifier.getComponentParserPosition(0);
    List<SqlParserPos> poses = Lists.newArrayList();
    for (int i = 0; i < newNames.size(); i++) {
        poses.add(i, pos);
    }
    sqlIdentifier.setNames(newNames, poses);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos)

Example 14 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 DrillConvertletTable method expandVariance.

/**
 * This code is adapted from calcite's AvgVarianceConvertlet. The difference being
 * we add a cast to double before we perform the division. The reason we have a separate implementation
 * from calcite's code is because while injecting a similar cast, calcite will look
 * at the output type of the aggregate function which will be 'ANY' at that point and will
 * inject a cast to 'ANY' which does not solve the problem.
 */
private static SqlNode expandVariance(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 = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, argSquared);
    final SqlNode sum = DrillCalciteSqlAggFunctionWrapper.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 15 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 DrillSqlParseException method getSqlWithErrorPointer.

/**
 * Formats sql query which caused the exception by adding
 * error pointer ^ under incorrect expression.
 *
 * @return The sql with a ^ character under the error
 */
public String getSqlWithErrorPointer() {
    final String sqlErrorMessageHeader = "SQL Query: ";
    final SqlParserPos pos = getPos();
    String formattedSql = sql;
    if (pos != null) {
        // recalculates to base 0
        int issueLineNumber = pos.getLineNum() - 1;
        // recalculates to base 0
        int issueColumnNumber = pos.getColumnNum() - 1;
        int messageHeaderLength = sqlErrorMessageHeader.length();
        // If the issue happens on the first line, header width should be calculated alongside with the sql query
        int shiftLength = (issueLineNumber == 0) ? issueColumnNumber + messageHeaderLength : issueColumnNumber;
        StringBuilder sb = new StringBuilder();
        String[] lines = sql.split(DrillParserUtil.EOL);
        for (int i = 0; i < lines.length; i++) {
            sb.append(lines[i]);
            if (i == issueLineNumber) {
                sb.append(DrillParserUtil.EOL).append(StringUtils.repeat(' ', shiftLength)).append("^");
            }
            if (i < lines.length - 1) {
                sb.append(DrillParserUtil.EOL);
            }
        }
        formattedSql = sb.toString();
    }
    return sqlErrorMessageHeader + formattedSql;
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos)

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