use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DrillSqlWorker method getQueryPlan.
/**
* Converts sql query string into query physical plan.
*
* @param context query context
* @param sql sql query
* @param textPlan text plan
* @return query physical plan
*/
private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Pointer<String> textPlan) throws ForemanSetupException {
final SqlConverter parser = new SqlConverter(context);
injector.injectChecked(context.getExecutionControls(), "sql-parsing", ForemanSetupException.class);
final SqlNode sqlNode = parser.parse(sql);
final AbstractSqlHandler handler;
final SqlHandlerConfig config = new SqlHandlerConfig(context, parser);
switch(sqlNode.getKind()) {
case EXPLAIN:
handler = new ExplainHandler(config, textPlan);
break;
case SET_OPTION:
handler = new SetOptionHandler(context);
break;
case OTHER:
if (sqlNode instanceof SqlCreateTable) {
handler = ((DrillSqlCall) sqlNode).getSqlHandler(config, textPlan);
break;
}
if (sqlNode instanceof DrillSqlCall) {
handler = ((DrillSqlCall) sqlNode).getSqlHandler(config);
break;
}
// fallthrough
default:
handler = new DefaultSqlHandler(config, textPlan);
}
try {
return handler.getPlan(sqlNode);
} catch (ValidationException e) {
String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
throw UserException.validationError(e).message(errorMessage).build(logger);
} catch (AccessControlException e) {
throw UserException.permissionError(e).build(logger);
} catch (SqlUnsupportedException e) {
throw UserException.unsupportedError(e).build(logger);
} catch (IOException | RelConversionException e) {
throw new QueryInputException("Failure handling SQL.", e);
}
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DefaultSqlHandler method validateNode.
private TypedSqlNode validateNode(SqlNode sqlNode) throws ValidationException, RelConversionException, ForemanSetupException {
final SqlNode sqlNodeValidated = config.getConverter().validate(sqlNode);
final TypedSqlNode typedSqlNode = new TypedSqlNode(sqlNodeValidated, config.getConverter().getOutputType(sqlNodeValidated));
// Check if the unsupported functionality is used
UnsupportedOperatorsVisitor visitor = UnsupportedOperatorsVisitor.createVisitor(context);
try {
sqlNodeValidated.accept(visitor);
} catch (UnsupportedOperationException ex) {
// If the exception due to the unsupported functionalities
visitor.convertException();
// If it is not, let this exception move forward to higher logic
throw ex;
}
return typedSqlNode;
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DrillAvgVarianceConvertlet method expandAvg.
private SqlNode expandAvg(final SqlNode arg) {
final SqlParserPos pos = SqlParserPos.ZERO;
final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
final SqlNode sumAsDouble = CastHighOp.createCall(pos, sum);
return SqlStdOperatorTable.DIVIDE.createCall(pos, sumAsDouble, count);
}
Aggregations