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;
}
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);
}
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);
}
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;
}
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;
}
Aggregations