use of org.apache.calcite.tools.ValidationException in project calcite by apache.
the class SqlDdlNodes method populate.
/**
* Populates the table called {@code name} by executing {@code query}.
*/
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
// Generate, prepare and execute an "INSERT INTO table query" statement.
// (It's a bit inefficient that we convert from SqlNode to SQL and back
// again.)
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
pw.print("INSERT INTO ");
name.unparse(w, 0, 0);
pw.print(" ");
query.unparse(w, 0, 0);
pw.flush();
final String sql = sw.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.calcite.tools.ValidationException in project drill by axbaretto.
the class RefreshMetadataHandler method getPlan.
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
final SqlRefreshMetadata refreshTable = unwrap(sqlNode, SqlRefreshMetadata.class);
try {
final SchemaPlus schema = findSchema(config.getConverter().getDefaultSchema(), refreshTable.getSchemaPath());
if (schema == null) {
return direct(false, "Storage plugin or workspace does not exist [%s]", SchemaUtilites.SCHEMA_PATH_JOINER.join(refreshTable.getSchemaPath()));
}
final String tableName = refreshTable.getName();
if (tableName.contains("*") || tableName.contains("?")) {
return direct(false, "Glob path %s not supported for metadata refresh", tableName);
}
final Table table = schema.getTable(tableName);
if (table == null) {
return direct(false, "Table %s does not exist.", tableName);
}
if (!(table instanceof DrillTable)) {
return notSupported(tableName);
}
final DrillTable drillTable = (DrillTable) table;
final Object selection = drillTable.getSelection();
if (selection instanceof FileSelection && ((FileSelection) selection).isEmptyDirectory()) {
return direct(false, "Table %s is empty and doesn't contain any parquet files.", tableName);
}
if (!(selection instanceof FormatSelection)) {
return notSupported(tableName);
}
FormatSelection formatSelection = (FormatSelection) selection;
FormatPluginConfig formatConfig = formatSelection.getFormat();
if (!((formatConfig instanceof ParquetFormatConfig) || ((formatConfig instanceof NamedFormatPluginConfig) && ((NamedFormatPluginConfig) formatConfig).name.equals("parquet")))) {
return notSupported(tableName);
}
FileSystemPlugin plugin = (FileSystemPlugin) drillTable.getPlugin();
DrillFileSystem fs = new DrillFileSystem(plugin.getFormatPlugin(formatSelection.getFormat()).getFsConf());
String selectionRoot = formatSelection.getSelection().selectionRoot;
if (!fs.getFileStatus(new Path(selectionRoot)).isDirectory()) {
return notSupported(tableName);
}
if (!(formatConfig instanceof ParquetFormatConfig)) {
formatConfig = new ParquetFormatConfig();
}
Metadata.createMeta(fs, selectionRoot, (ParquetFormatConfig) formatConfig);
return direct(true, "Successfully updated metadata for table %s.", tableName);
} catch (Exception e) {
logger.error("Failed to update metadata for table '{}'", refreshTable.getName(), e);
return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
}
}
use of org.apache.calcite.tools.ValidationException in project drill by axbaretto.
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 DESCRIBE_TABLE:
if (sqlNode instanceof DrillSqlDescribeTable) {
handler = new DescribeTableHandler(config);
break;
}
case DESCRIBE_SCHEMA:
if (sqlNode instanceof SqlDescribeSchema) {
handler = new DescribeSchemaHandler(config);
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.tools.ValidationException in project beam by apache.
the class CalciteQueryPlanner method convertToBeamRel.
/**
* It parses and validate the input query, then convert into a {@link BeamRelNode} tree. Note that
* query parameters are not yet supported.
*/
@Override
public BeamRelNode convertToBeamRel(String sqlStatement, QueryParameters queryParameters) throws ParseException, SqlConversionException {
Preconditions.checkArgument(queryParameters.getKind() == Kind.NONE, "Beam SQL Calcite dialect does not yet support query parameters.");
BeamRelNode beamRelNode;
try {
SqlNode parsed = planner.parse(sqlStatement);
TableResolutionUtils.setupCustomTableResolution(connection, parsed);
SqlNode validated = planner.validate(parsed);
LOG.info("SQL:\n" + validated);
// root of original logical plan
RelRoot root = planner.rel(validated);
LOG.info("SQLPlan>\n" + RelOptUtil.toString(root.rel));
RelTraitSet desiredTraits = root.rel.getTraitSet().replace(BeamLogicalConvention.INSTANCE).replace(root.collation).simplify();
// beam physical plan
root.rel.getCluster().setMetadataProvider(ChainedRelMetadataProvider.of(ImmutableList.of(NonCumulativeCostImpl.SOURCE, RelMdNodeStats.SOURCE, root.rel.getCluster().getMetadataProvider())));
root.rel.getCluster().setMetadataQuerySupplier(BeamRelMetadataQuery::instance);
RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(root.rel.getCluster().getMetadataProvider()));
root.rel.getCluster().invalidateMetadataQuery();
beamRelNode = (BeamRelNode) planner.transform(0, desiredTraits, root.rel);
LOG.info("BEAMPlan>\n" + RelOptUtil.toString(beamRelNode));
} catch (RelConversionException | CannotPlanException e) {
throw new SqlConversionException(String.format("Unable to convert query %s", sqlStatement), e);
} catch (SqlParseException | ValidationException e) {
throw new ParseException(String.format("Unable to parse query %s", sqlStatement), e);
} finally {
planner.close();
}
return beamRelNode;
}
use of org.apache.calcite.tools.ValidationException in project druid by druid-io.
the class SqlLifecycle method validate.
private ValidationResult validate(AuthenticationResult authenticationResult) {
try (DruidPlanner planner = plannerFactory.createPlanner(sql, queryContext)) {
// set planner context for logs/metrics in case something explodes early
this.plannerContext = planner.getPlannerContext();
this.plannerContext.setAuthenticationResult(authenticationResult);
// set parameters on planner context, if parameters have already been set
this.plannerContext.setParameters(parameters);
this.validationResult = planner.validate();
return validationResult;
}// we can't collapse catch clauses since SqlPlanningException has type-sensitive constructors.
catch (SqlParseException e) {
throw new SqlPlanningException(e);
} catch (ValidationException e) {
throw new SqlPlanningException(e);
}
}
Aggregations