Search in sources :

Example 11 with PlannerOp

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);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) LimitOp(herddb.model.planner.LimitOp)

Example 12 with PlannerOp

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);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ProjectOp(herddb.model.planner.ProjectOp) Projection(herddb.model.Projection) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 13 with PlannerOp

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;
    }
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ConstantExpression(herddb.sql.expressions.ConstantExpression) ValuesOp(herddb.model.planner.ValuesOp) StatementExecutionException(herddb.model.StatementExecutionException) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) ExecutionPlan(herddb.model.ExecutionPlan)

Example 14 with PlannerOp

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);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) AggregateOp(herddb.model.planner.AggregateOp) Column(herddb.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 15 with PlannerOp

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);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

Aggregations

PlannerOp (herddb.model.planner.PlannerOp)27 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)16 Column (herddb.model.Column)12 Table (herddb.model.Table)9 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)9 ArrayList (java.util.ArrayList)9 ConstantExpression (herddb.sql.expressions.ConstantExpression)8 RelDataType (org.apache.calcite.rel.type.RelDataType)8 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)8 StatementExecutionException (herddb.model.StatementExecutionException)7 ScanStatement (herddb.model.commands.ScanStatement)6 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)5 RecordFunction (herddb.model.RecordFunction)5 JdbcParameterExpression (herddb.sql.expressions.JdbcParameterExpression)5 TypedJdbcParameterExpression (herddb.sql.expressions.TypedJdbcParameterExpression)5 List (java.util.List)5 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)5 AbstractTableManager (herddb.core.AbstractTableManager)4 TableSpaceManager (herddb.core.TableSpaceManager)4 Predicate (herddb.model.Predicate)4