use of herddb.model.planner.PlannerOp in project herddb by diennea.
the class CalcitePlanner method planLimit.
private PlannerOp planLimit(EnumerableLimit op, RelDataType rowType) {
PlannerOp input = convertRelNode(op.getInput(), rowType, false, false);
CompiledSQLExpression maxRows = SQLExpressionCompiler.compileExpression(op.fetch);
CompiledSQLExpression offset = SQLExpressionCompiler.compileExpression(op.offset);
return new LimitOp(input, maxRows, offset);
}
use of herddb.model.planner.PlannerOp in project herddb by diennea.
the class CalcitePlanner method planProject.
private PlannerOp planProject(EnumerableProject op, RelDataType rowType) {
PlannerOp input = convertRelNode(op.getInput(), null, false, false);
final List<RexNode> projects = op.getProjects();
final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
Projection projection = buildProjection(projects, _rowType, false, null);
return new ProjectOp(projection, input);
}
use of herddb.model.planner.PlannerOp in project herddb by diennea.
the class JSQLParserPlanner method translate.
@Override
public TranslatedQuery translate(String defaultTableSpace, String query, List<Object> parameters, boolean scan, boolean allowCache, boolean returnValues, int maxRows) throws StatementExecutionException {
ensureDefaultTableSpaceBootedLocally(defaultTableSpace);
if (parameters == null) {
parameters = Collections.emptyList();
}
/*
* Strips out leading comments
*/
int idx = SQLUtils.findQueryStart(query);
if (idx != -1) {
query = query.substring(idx);
}
query = rewriteExecuteSyntax(query);
if (query.startsWith("ALTER TABLE") && query.contains("ADD FOREIGN KEY")) {
// jsqlparser does not support unnamed foreign keys in "ALTER TABLE"
query = query.replace("ADD FOREIGN KEY", "ADD CONSTRAINT generate_unnamed FOREIGN KEY");
}
if (query.startsWith("EXPLAIN ")) {
query = query.substring("EXPLAIN ".length());
net.sf.jsqlparser.statement.Statement stmt = parseStatement(query);
if (!isCachable(stmt)) {
allowCache = false;
}
ExecutionPlan queryExecutionPlan = plan(defaultTableSpace, stmt, scan, returnValues, maxRows);
PlannerOp finalPlan = queryExecutionPlan.originalRoot;
ValuesOp values = new ValuesOp(manager.getNodeId(), new String[] { "name", "value" }, new Column[] { column("name", ColumnTypes.STRING), column("value", ColumnTypes.STRING) }, java.util.Arrays.asList(java.util.Arrays.asList(new ConstantExpression("query", ColumnTypes.NOTNULL_STRING), new ConstantExpression(query, ColumnTypes.NOTNULL_STRING)), java.util.Arrays.asList(new ConstantExpression("logicalplan", ColumnTypes.NOTNULL_STRING), new ConstantExpression(stmt + "", ColumnTypes.NOTNULL_STRING)), java.util.Arrays.asList(new ConstantExpression("plan", ColumnTypes.NOTNULL_STRING), new ConstantExpression(finalPlan + "", ColumnTypes.NOTNULL_STRING)), java.util.Arrays.asList(new ConstantExpression("finalplan", ColumnTypes.NOTNULL_STRING), // same as "plan"
new ConstantExpression(finalPlan.optimize(), ColumnTypes.NOTNULL_STRING))));
ExecutionPlan executionPlan = ExecutionPlan.simple(new SQLPlannedOperationStatement(values), values);
return new TranslatedQuery(executionPlan, new SQLStatementEvaluationContext(query, parameters, false, false));
}
if (query.startsWith("SHOW")) {
return calculateShowCreateTable(query, defaultTableSpace, parameters, manager);
}
String cacheKey = "scan:" + scan + ",defaultTableSpace:" + defaultTableSpace + ",query:" + query + ",returnValues:" + returnValues + ",maxRows:" + maxRows;
try {
boolean forceAcquireWriteLock;
if (// this looks very hacky
query.endsWith(" FOR UPDATE") && query.substring(0, 6).toLowerCase().equals("select")) {
forceAcquireWriteLock = true;
query = query.substring(0, query.length() - " FOR UPDATE".length());
} else {
forceAcquireWriteLock = false;
}
if (allowCache) {
ExecutionPlan cached = cache.get(cacheKey);
if (cached != null) {
return new TranslatedQuery(cached, new SQLStatementEvaluationContext(query, parameters, forceAcquireWriteLock, false));
}
}
if (query.startsWith(TABLE_CONSISTENCY_COMMAND)) {
ExecutionPlan executionPlan = ExecutionPlan.simple(JSQLParserPlanner.this.queryConsistencyCheckStatement(defaultTableSpace, query, parameters));
return new TranslatedQuery(executionPlan, new SQLStatementEvaluationContext(query, parameters, false, false));
}
if (query.startsWith(TABLESPACE_CONSISTENCY_COMMAND)) {
ExecutionPlan executionPlan = ExecutionPlan.simple(JSQLParserPlanner.this.queryConsistencyCheckStatement(query));
return new TranslatedQuery(executionPlan, new SQLStatementEvaluationContext(query, parameters, false, false));
}
net.sf.jsqlparser.statement.Statement stmt = parseStatement(query);
if (!isCachable(stmt)) {
allowCache = false;
}
ExecutionPlan executionPlan = plan(defaultTableSpace, stmt, scan, returnValues, maxRows);
if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
LOG.log(DUMP_QUERY_LEVEL, "Query: {0} --HerdDB Plan\n{1}", new Object[] { query, executionPlan.mainStatement });
}
if (allowCache) {
cache.put(cacheKey, executionPlan);
}
return new TranslatedQuery(executionPlan, new SQLStatementEvaluationContext(query, parameters, forceAcquireWriteLock, false));
} catch (StatementNotSupportedException err) {
if (fallback == null) {
throw new StatementExecutionException("I am sorry, I cannot plan SQL \"" + query + "\" with simple jSQLParser planner," + " consider setting " + ServerConfiguration.PROPERTY_PLANNER_TYPE + "=" + ServerConfiguration.PLANNER_TYPE_AUTO, err);
}
TranslatedQuery res = fallback.translate(defaultTableSpace, query, parameters, scan, allowCache, returnValues, maxRows);
if (allowCache) {
// cache plan from Calcite, not need to try jSQLParser again
cache.put(cacheKey, res.plan);
}
return res;
}
}
use of herddb.model.planner.PlannerOp in project herddb by diennea.
the class CalcitePlanner method planAggregate.
private PlannerOp planAggregate(EnumerableAggregate op, RelDataType rowType, boolean returnValues) {
List<RelDataTypeField> fieldList = op.getRowType().getFieldList();
List<AggregateCall> calls = op.getAggCallList();
String[] fieldnames = new String[fieldList.size()];
String[] aggtypes = new String[calls.size()];
Column[] columns = new Column[fieldList.size()];
List<Integer> groupedFiledsIndexes = op.getGroupSet().toList();
List<List<Integer>> argLists = new ArrayList<>(calls.size());
int i = 0;
int idaggcall = 0;
for (RelDataTypeField c : fieldList) {
int type = convertToHerdType(c.getType());
Column co = Column.column(c.getName(), type);
columns[i] = co;
fieldnames[i] = c.getName().toLowerCase();
i++;
}
for (AggregateCall call : calls) {
aggtypes[idaggcall++] = call.getAggregation().getName();
argLists.add(call.getArgList());
}
PlannerOp input = convertRelNode(op.getInput(), null, returnValues, false);
return new AggregateOp(input, fieldnames, columns, aggtypes, argLists, groupedFiledsIndexes);
}
use of herddb.model.planner.PlannerOp in project herddb by diennea.
the class CalcitePlanner method planEnumerableNestedLoopJoin.
private PlannerOp planEnumerableNestedLoopJoin(EnumerableNestedLoopJoin op, RelDataType rowType) {
PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
PlannerOp right = convertRelNode(op.getRight(), null, false, false);
CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
List<RelDataTypeField> fieldList = _rowType.getFieldList();
Column[] columns = new Column[fieldList.size()];
String[] fieldNames = new String[columns.length];
int i = 0;
for (RelDataTypeField field : fieldList) {
Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
fieldNames[i] = col.name;
columns[i++] = col;
}
return new NestedLoopJoinOp(fieldNames, columns, left, right, condition, op.getJoinType(), false);
}
Aggregations