Search in sources :

Example 36 with IgniteSQLException

use of org.apache.ignite.internal.processors.query.IgniteSQLException in project ignite by apache.

the class DmlAstUtils method elementOrDefault.

/**
     * Do what we can to compute default value for this column (mimics H2 behavior).
     * @see Table#getDefaultValue
     * @see Column#validateConvertUpdateSequence
     * @param el SQL element.
     * @param col Column.
     * @return {@link GridSqlConst#NULL}, if {@code el} is null, or {@code el} if
     * it's not {@link GridSqlKeyword#DEFAULT}, or computed default value.
     */
private static GridSqlElement elementOrDefault(GridSqlElement el, GridSqlColumn col) {
    if (el == null)
        return GridSqlConst.NULL;
    if (el != GridSqlKeyword.DEFAULT)
        return el;
    Column h2Col = col.column();
    Expression dfltExpr = h2Col.getDefaultExpression();
    Value dfltVal;
    try {
        dfltVal = dfltExpr != null ? dfltExpr.getValue(null) : null;
    } catch (Exception ignored) {
        throw new IgniteSQLException("Failed to evaluate default value for a column " + col.columnName());
    }
    if (dfltVal != null)
        return new GridSqlConst(dfltVal);
    int type = h2Col.getType();
    DataType dt = DataType.getDataType(type);
    if (dt.decimal)
        dfltVal = ValueInt.get(0).convertTo(type);
    else if (dt.type == Value.TIMESTAMP)
        dfltVal = ValueTimestamp.fromMillis(U.currentTimeMillis());
    else if (dt.type == Value.TIME)
        dfltVal = ValueTime.fromNanos(0);
    else if (dt.type == Value.DATE)
        dfltVal = ValueDate.fromMillis(U.currentTimeMillis());
    else
        dfltVal = ValueString.get("").convertTo(type);
    return new GridSqlConst(dfltVal);
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DataType(org.h2.value.DataType) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 37 with IgniteSQLException

use of org.apache.ignite.internal.processors.query.IgniteSQLException in project ignite by apache.

the class UpdatePlanBuilder method planForInsert.

/**
     * Prepare update plan for INSERT or MERGE.
     *
     * @param stmt INSERT or MERGE statement.
     * @return Update plan.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(GridSqlStatement stmt) throws IgniteCheckedException {
    GridSqlQuery sel;
    GridSqlElement target;
    GridSqlColumn[] cols;
    boolean isTwoStepSubqry;
    int rowsNum;
    GridSqlTable tbl;
    GridH2RowDescriptor desc;
    if (stmt instanceof GridSqlInsert) {
        GridSqlInsert ins = (GridSqlInsert) stmt;
        target = ins.into();
        tbl = gridTableForElement(target);
        desc = tbl.dataTable().rowDescriptor();
        cols = ins.columns();
        sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query(), desc);
        isTwoStepSubqry = (ins.query() != null);
        rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
    } else if (stmt instanceof GridSqlMerge) {
        GridSqlMerge merge = (GridSqlMerge) stmt;
        target = merge.into();
        tbl = gridTableForElement(target);
        desc = tbl.dataTable().rowDescriptor();
        // This check also protects us from attempts to update key or its fields directly -
        // when no key except cache key can be used, it will serve only for uniqueness checks,
        // not for updates, and hence will allow putting new pairs only.
        // We don't quote _key and _val column names on CREATE TABLE, so they are always uppercase here.
        GridSqlColumn[] keys = merge.keys();
        if (keys.length != 1 || !desc.isKeyColumn(tbl.dataTable().getColumn(keys[0].columnName()).getColumnId()))
            throw new CacheException("SQL MERGE does not support arbitrary keys");
        cols = merge.columns();
        sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query(), desc);
        isTwoStepSubqry = (merge.query() != null);
        rowsNum = isTwoStepSubqry ? 0 : merge.rows().size();
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    // Let's set the flag only for subqueries that have their FROM specified.
    isTwoStepSubqry = (isTwoStepSubqry && (sel instanceof GridSqlUnion || (sel instanceof GridSqlSelect && ((GridSqlSelect) sel).from() != null)));
    int keyColIdx = -1;
    int valColIdx = -1;
    boolean hasKeyProps = false;
    boolean hasValProps = false;
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.dataTable().getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    GridCacheContext<?, ?> cctx = desc.context();
    String[] colNames = new String[cols.length];
    int[] colTypes = new int[cols.length];
    for (int i = 0; i < cols.length; i++) {
        GridSqlColumn col = cols[i];
        String colName = col.columnName();
        colNames[i] = colName;
        colTypes[i] = col.resultType().type();
        int colId = col.column().getColumnId();
        if (desc.isKeyColumn(colId)) {
            keyColIdx = i;
            continue;
        }
        if (desc.isValueColumn(colId)) {
            valColIdx = i;
            continue;
        }
        GridQueryProperty prop = desc.type().property(colName);
        assert prop != null : "Property '" + colName + "' not found.";
        if (prop.key())
            hasKeyProps = true;
        else
            hasValProps = true;
    }
    KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
    KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
    if (stmt instanceof GridSqlMerge)
        return UpdatePlan.forMerge(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
    else
        return UpdatePlan.forInsert(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
}
Also used : GridSqlMerge(org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge) GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) CacheException(javax.cache.CacheException) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridSqlQuery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlInsert(org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert)

Example 38 with IgniteSQLException

use of org.apache.ignite.internal.processors.query.IgniteSQLException in project ignite by apache.

the class UpdatePlanBuilder method planForUpdate.

/**
     * Prepare update plan for UPDATE or DELETE.
     *
     * @param stmt UPDATE or DELETE statement.
     * @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
     * @return Update plan.
     * @throws IgniteCheckedException if failed.
     */
private static UpdatePlan planForUpdate(GridSqlStatement stmt, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdateArguments fastUpdate;
    UpdateMode mode;
    if (stmt instanceof GridSqlUpdate) {
        // Let's verify that user is not trying to mess with key's columns directly
        verifyUpdateColumns(stmt);
        GridSqlUpdate update = (GridSqlUpdate) stmt;
        target = update.target();
        fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
        mode = UpdateMode.UPDATE;
    } else if (stmt instanceof GridSqlDelete) {
        GridSqlDelete del = (GridSqlDelete) stmt;
        target = del.from();
        fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
        mode = UpdateMode.DELETE;
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    GridSqlTable tbl = gridTableForElement(target);
    GridH2Table gridTbl = tbl.dataTable();
    GridH2RowDescriptor desc = gridTbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + gridTbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null)
        return UpdatePlan.forFastUpdate(mode, gridTbl, fastUpdate);
    else {
        GridSqlSelect sel;
        if (stmt instanceof GridSqlUpdate) {
            List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
            int valColIdx = -1;
            String[] colNames = new String[updatedCols.size()];
            int[] colTypes = new int[updatedCols.size()];
            for (int i = 0; i < updatedCols.size(); i++) {
                colNames[i] = updatedCols.get(i).columnName();
                colTypes[i] = updatedCols.get(i).resultType().type();
                Column column = updatedCols.get(i).column();
                if (desc.isValueColumn(column.getColumnId()))
                    valColIdx = i;
            }
            boolean hasNewVal = (valColIdx != -1);
            // Statement updates distinct properties if it does not have _val in updated columns list
            // or if its list of updated columns includes only _val, i.e. is single element.
            boolean hasProps = !hasNewVal || updatedCols.size() > 1;
            // Index of new _val in results of SELECT
            if (hasNewVal)
                valColIdx += 2;
            int newValColIdx = (hasNewVal ? valColIdx : 1);
            KeyValueSupplier newValSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
            return UpdatePlan.forUpdate(gridTbl, colNames, colTypes, newValSupplier, valColIdx, sel.getSQL());
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
            return UpdatePlan.forDelete(gridTbl, sel.getSQL());
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Example 39 with IgniteSQLException

use of org.apache.ignite.internal.processors.query.IgniteSQLException in project ignite by apache.

the class UpdatePlanBuilder method gridTableForElement.

/**
     * @param target Expression to extract the table from.
     * @return Back end table for this element.
     */
private static GridSqlTable gridTableForElement(GridSqlElement target) {
    Set<GridSqlTable> tbls = new HashSet<>();
    DmlAstUtils.collectAllGridTablesInTarget(target, tbls);
    if (tbls.size() != 1)
        throw new IgniteSQLException("Failed to determine target table", IgniteQueryErrorCode.TABLE_NOT_FOUND);
    return tbls.iterator().next();
}
Also used : GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) HashSet(java.util.HashSet)

Example 40 with IgniteSQLException

use of org.apache.ignite.internal.processors.query.IgniteSQLException in project ignite by apache.

the class JdbcResultSet method next.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public boolean next() throws SQLException {
    ensureNotClosed();
    if (it == null || (stmt.getMaxRows() > 0 && pos >= stmt.getMaxRows())) {
        curr = null;
        return false;
    } else if (it.hasNext()) {
        curr = new ArrayList<>(it.next());
        pos++;
        if (finished && !it.hasNext())
            it = null;
        return true;
    } else if (!finished) {
        JdbcConnection conn = (JdbcConnection) stmt.getConnection();
        Ignite ignite = conn.ignite();
        UUID nodeId = conn.nodeId();
        boolean loc = nodeId == null;
        // Connections from new clients send queries with new tasks, so we have to continue in the same manner
        JdbcQueryTask qryTask = new JdbcQueryTask(loc ? ignite : null, conn.cacheName(), conn.schemaName(), null, true, loc, null, fetchSize, uuid, conn.isLocalQuery(), conn.isCollocatedQuery(), conn.isDistributedJoins());
        try {
            JdbcQueryTask.QueryResult res = loc ? qryTask.call() : ignite.compute(ignite.cluster().forNodeId(nodeId)).call(qryTask);
            finished = res.isFinished();
            it = res.getRows().iterator();
            return next();
        } catch (IgniteSQLException e) {
            throw e.toJdbcException();
        } catch (Exception e) {
            throw new SQLException("Failed to query Ignite.", e);
        }
    }
    it = null;
    return false;
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Aggregations

IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)57 SQLException (java.sql.SQLException)20 ArrayList (java.util.ArrayList)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 List (java.util.List)16 Column (org.h2.table.Column)13 IgniteException (org.apache.ignite.IgniteException)11 PreparedStatement (java.sql.PreparedStatement)9 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)9 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)9 Connection (java.sql.Connection)8 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)8 LinkedHashMap (java.util.LinkedHashMap)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)7 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)7 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)7 UUID (java.util.UUID)6 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)6 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)6