Search in sources :

Example 66 with Type

use of org.hsqldb_voltpatches.types.Type in project voltdb by VoltDB.

the class ScriptReaderText method processStatement.

private void processStatement(Session session) throws IOException {
    try {
        if (statement.startsWith("/*C")) {
            int endid = statement.indexOf('*', 4);
            sessionNumber = Integer.parseInt(statement.substring(3, endid));
            statement = statement.substring(endid + 2);
        }
        rowIn.setSource(statement);
        statementType = rowIn.getStatementType();
        if (statementType == ANY_STATEMENT) {
            rowData = null;
            currentTable = null;
            return;
        } else if (statementType == COMMIT_STATEMENT) {
            rowData = null;
            currentTable = null;
            return;
        } else if (statementType == SET_SCHEMA_STATEMENT) {
            rowData = null;
            currentTable = null;
            currentSchema = rowIn.getSchemaName();
            return;
        }
        String name = rowIn.getTableName();
        String schema = session.getCurrentSchemaHsqlName().name;
        currentTable = db.schemaManager.getUserTable(session, name, schema);
        currentStore = db.persistentStoreCollection.getStore(currentTable);
        Type[] colTypes;
        if (statementType == INSERT_STATEMENT) {
            colTypes = currentTable.getColumnTypes();
        } else if (currentTable.hasPrimaryKey()) {
            colTypes = currentTable.getPrimaryKeyTypes();
        } else {
            colTypes = currentTable.getColumnTypes();
        }
        try {
            rowData = rowIn.readData(colTypes);
        } catch (Exception e) {
            throw e;
        }
    } catch (Exception e) {
        throw new IOException(e.toString());
    }
}
Also used : Type(org.hsqldb_voltpatches.types.Type) IOException(java.io.IOException) IOException(java.io.IOException) HsqlException(org.hsqldb_voltpatches.HsqlException)

Example 67 with Type

use of org.hsqldb_voltpatches.types.Type in project voltdb by VoltDB.

the class RowInputBase method readData.

/**
     *  reads row data from a stream using the JDBC types in colTypes
     *
     * @param  colTypes
     * @throws  IOException
     * @throws  HsqlException
     */
public Object[] readData(Type[] colTypes) throws IOException, HsqlException {
    int l = colTypes.length;
    Object[] data = new Object[l];
    Object o;
    Type type;
    for (int i = 0; i < l; i++) {
        if (checkNull()) {
            continue;
        }
        o = null;
        type = colTypes[i];
        switch(type.typeCode) {
            case Types.SQL_ALL_TYPES:
            case Types.SQL_CHAR:
            case Types.SQL_VARCHAR:
            case Types.VARCHAR_IGNORECASE:
                o = readChar(type);
                break;
            case Types.TINYINT:
            case Types.SQL_SMALLINT:
                o = readSmallint();
                break;
            case Types.SQL_INTEGER:
                o = readInteger();
                break;
            case Types.SQL_BIGINT:
                o = readBigint();
                break;
            //the old format for compatibility
            case Types.SQL_REAL:
            case Types.SQL_FLOAT:
            case Types.SQL_DOUBLE:
                o = readReal();
                break;
            case Types.SQL_NUMERIC:
            case Types.SQL_DECIMAL:
                o = readDecimal(type);
                break;
            case Types.SQL_DATE:
                o = readDate(type);
                break;
            case Types.SQL_TIME:
            case Types.SQL_TIME_WITH_TIME_ZONE:
                o = readTime(type);
                break;
            case Types.SQL_TIMESTAMP:
            case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
                o = readTimestamp(type);
                break;
            case Types.SQL_INTERVAL_YEAR:
            case Types.SQL_INTERVAL_YEAR_TO_MONTH:
            case Types.SQL_INTERVAL_MONTH:
                o = readYearMonthInterval(type);
                break;
            case Types.SQL_INTERVAL_DAY:
            case Types.SQL_INTERVAL_DAY_TO_HOUR:
            case Types.SQL_INTERVAL_DAY_TO_MINUTE:
            case Types.SQL_INTERVAL_DAY_TO_SECOND:
            case Types.SQL_INTERVAL_HOUR:
            case Types.SQL_INTERVAL_HOUR_TO_MINUTE:
            case Types.SQL_INTERVAL_HOUR_TO_SECOND:
            case Types.SQL_INTERVAL_MINUTE:
            case Types.SQL_INTERVAL_MINUTE_TO_SECOND:
            case Types.SQL_INTERVAL_SECOND:
                o = readDaySecondInterval(type);
                break;
            case Types.SQL_BOOLEAN:
                o = readBoole();
                break;
            case Types.OTHER:
                o = readOther();
                break;
            case Types.SQL_CLOB:
                o = readClob();
                break;
            case Types.SQL_BLOB:
                o = readBlob();
                break;
            case Types.SQL_BINARY:
            case Types.SQL_VARBINARY:
                o = readBinary();
                break;
            case Types.SQL_BIT:
            case Types.SQL_BIT_VARYING:
                o = readBit();
                break;
            default:
                throw Error.runtimeError(ErrorCode.U_S0500, "RowInputBase " + type.getNameString());
        }
        data[i] = o;
    }
    return data;
}
Also used : Type(org.hsqldb_voltpatches.types.Type)

Example 68 with Type

use of org.hsqldb_voltpatches.types.Type in project voltdb by VoltDB.

the class FunctionForVoltDB method resolveTypes.

@Override
public void resolveTypes(Session session, Expression parent) {
    Type[] paramTypes = m_def.getParamTypes();
    for (int i = 0; i < nodes.length; i++) {
        if (nodes[i] != null) {
            nodes[i].resolveTypes(session, this);
        }
    }
    switch(m_def.getId()) {
        case FunctionId.FUNC_CONCAT:
            for (int ii = 0; ii < nodes.length; ii++) {
                if (nodes[ii].dataType == null && nodes[ii].isParam) {
                    nodes[ii].dataType = Type.SQL_VARCHAR;
                }
            }
            break;
        /*
         * The types to the FIELD functions parameters are VARCHAR
         */
        case FunctionId.FUNC_VOLT_FIELD:
            if (nodes[0].dataType == null && nodes[0].isParam) {
                nodes[0].dataType = Type.SQL_VARCHAR;
            }
            if (nodes[1].dataType == null && nodes[1].isParam) {
                nodes[1].dataType = Type.SQL_VARCHAR;
            }
            break;
        /*
             * Infer parameter types to make the types of the 1st, 2nd, and (if not the last) 4th, 6th, etc.
             * arguments to DECODE as consistent as possible,
             * and the types of the 3rd, 5th, 7th, etc. and LAST arguments as consistent as possible.
             * Punt to inferring VARCHAR if the other arguments give no clue or are inconsistent
             * -- the VoltDB EE complains about NULL-typed parameters but is somewhat forgiving about
             * mixed argument types.
             */
        case FunctionId.FUNC_VOLT_DECODE:
            // Track whether parameter type hinting is needed for either key or value arguments.
            // For simplicity(?), parameters are not tracked explicitly (by position)
            // or even by category (key vs. value). So, if any parameter hinting is required at all,
            // all arguments get re-checked.
            boolean needParamType = false;
            Type inputTypeInferred = null;
            Type resultTypeInferred = null;
            for (int ii = 0; ii < nodes.length; ii++) {
                Type argType = nodes[ii].dataType;
                if (argType == null) {
                    // A param here means work to do, below.
                    if (nodes[ii].isParam || nodes[ii].valueData == null) {
                        needParamType = true;
                    }
                    continue;
                }
                // the arguments alternate between candidate inputs and candidate results.
                if ((((ii % 2) == 0) || ii == nodes.length - 1) && (ii != 0)) {
                    // that may hint at the result type or require hinting from the other result values.
                    if (resultTypeInferred == null) {
                        // Take the first result type hint.
                        resultTypeInferred = argType;
                    } else if (resultTypeInferred.typeComparisonGroup != argType.typeComparisonGroup) {
                        // Discard contradictory hints.
                        resultTypeInferred = Type.SQL_VARCHAR;
                    }
                } else {
                    // that may hint at the input type or may require hinting from the other input keys.
                    if (inputTypeInferred == null) {
                        // Take the first input type hint.
                        inputTypeInferred = argType;
                    } else if (inputTypeInferred.typeComparisonGroup != argType.typeComparisonGroup) {
                        // Discard contradictory hints, falling back to string type.
                        inputTypeInferred = Type.SQL_VARCHAR;
                    }
                }
            }
            // With any luck, there are no parameter "?" arguments to worry about.
            if (!needParamType) {
                break;
            }
            // Punt to guessing VARCHAR for lack of better information.
            if (inputTypeInferred == null) {
                inputTypeInferred = Type.SQL_VARCHAR;
            }
            if (resultTypeInferred == null) {
                resultTypeInferred = Type.SQL_VARCHAR;
            }
            for (int ii = 0; ii < nodes.length; ii++) {
                Type argType = nodes[ii].dataType;
                if ((argType != null) || !(nodes[ii].isParam || nodes[ii].valueData == null)) {
                    continue;
                }
                // is a candidate result vs. a candidate input.
                if ((((ii % 2) == 0) || ii == nodes.length - 1) && (ii != 0)) {
                    nodes[ii].dataType = resultTypeInferred;
                } else {
                    nodes[ii].dataType = inputTypeInferred;
                }
            }
            break;
        case FunctionId.FUNC_VOLT_BITNOT:
            voltResolveToBigintTypesForBitwise();
            break;
        case FunctionId.FUNC_VOLT_BIT_SHIFT_LEFT:
        case FunctionId.FUNC_VOLT_BIT_SHIFT_RIGHT:
            // the first parameter has to be BigInteger
            voltResolveToBigintType(0);
            voltResolveToBigintCompatibleType(1);
            dataType = Type.SQL_BIGINT;
            break;
        case FunctionId.FUNC_VOLT_HEX:
        case FunctionId.FUNC_VOLT_BIN:
            voltResolveToBigintType(0);
            dataType = Type.SQL_VARCHAR;
            break;
        case FunctionId.FUNC_VOLT_DISTANCE:
            // validate the types of argument is valid
            if (nodes[0].dataType == null || nodes[1].dataType == null) {
                // "data type cast needed for parameter or null literal"
                throw Error.error(ErrorCode.X_42567, "input type to DISTANCE function is ambiguous");
            } else if ((!nodes[0].dataType.isGeographyType() && !nodes[0].dataType.isGeographyPointType()) || (!nodes[1].dataType.isGeographyType() && !nodes[1].dataType.isGeographyPointType())) {
                // either of the nodes is not a valid type
                throw Error.error(ErrorCode.X_42565, "The DISTANCE function computes distances between POINT-to-POINT, POINT-to-POLYGON " + "and POLYGON-to-POINT only.");
            } else if (nodes[0].dataType.isGeographyType() && nodes[1].dataType.isGeographyType()) {
                // distance between two polygons is not supported, flag as an error
                throw Error.error(ErrorCode.X_42565, "DISTANCE between two POLYGONS not supported");
            } else if (nodes[0].dataType.isGeographyPointType() && nodes[1].dataType.isGeographyType()) {
                // distance between polygon-to-point and point-to-polygon is symmetric.
                // So, update the the expression for distance between point and polygon to
                // distance between polygon and point. This simplifies the logic and have to
                // handle only one case: polygon-to-point instead of two
                Expression tempNode = nodes[0];
                nodes[0] = nodes[1];
                nodes[1] = tempNode;
            }
            break;
        case FunctionId.FUNC_VOLT_DWITHIN:
            if (nodes[0].dataType == null || nodes[1].dataType == null) {
                // "data type cast needed for parameter or null literal"
                throw Error.error(ErrorCode.X_42567, "input type to DWITHIN function is ambiguous");
            } else if ((!nodes[0].dataType.isGeographyType() && !nodes[0].dataType.isGeographyPointType()) || (!nodes[1].dataType.isGeographyType() && !nodes[1].dataType.isGeographyPointType())) {
                // first and second argument should be geography type
                throw Error.error(ErrorCode.X_42565, "DWITHIN function evaulates if geographies are within specified distance of one-another for " + "POINT-to-POINT, POINT-to-POLYGON and POLYGON-to-POINT geographies only.");
            } else if (nodes[0].dataType.isGeographyType() && nodes[1].dataType.isGeographyType()) {
                // distance between two polygons is not supported, flag as an error
                throw Error.error(ErrorCode.X_42565, "DWITHIN between two POLYGONS not supported");
            } else if (nodes[0].dataType.isGeographyPointType() && nodes[1].dataType.isGeographyType()) {
                // Distance between polygon-to-point and point-to-polygon is symmetric. Update the
                // expression for DWITHIN between point and polygon to distance between polygon
                // and point. This consolidates logic to one case: polygon-to-point
                Expression tempNode = nodes[0];
                nodes[0] = nodes[1];
                nodes[1] = tempNode;
            }
            if ((nodes[2].dataType != null) && (!nodes[2].dataType.isNumberType())) {
                // "incompatible data type in operation"
                throw Error.error(ErrorCode.X_42565, "input type DISTANCE to DWITHIN function must be non-negative numeric value");
            }
            break;
        case FunctionId.FUNC_VOLT_ASTEXT:
            if (nodes[0].dataType == null) {
                // "data type cast needed for parameter or null literal"
                throw Error.error(ErrorCode.X_42567, "input type to ASTEXT function is ambiguous");
            }
            if (!(nodes[0].dataType.isGeographyPointType() || nodes[0].dataType.isGeographyType())) {
                // "incompatible data type in operation"
                throw Error.error(ErrorCode.X_42565, "The asText function accepts only GEOGRAPHY and GEOGRAPHY_POINT types.");
            }
            break;
        // our networking specified functions
        case FunctionId.FUNC_INET_NTOA:
            if (nodes[0].dataType != null && !nodes[0].dataType.isNumberType()) {
                throw Error.error(ErrorCode.X_42561);
            }
            dataType = Type.SQL_VARCHAR;
            break;
        case FunctionId.FUNC_INET_ATON:
            if (nodes[0].dataType != null && !nodes[0].dataType.isCharacterType()) {
                throw Error.error(ErrorCode.X_42561);
            }
            dataType = Type.SQL_BIGINT;
            break;
        case FunctionId.FUNC_INET6_ATON:
            if (nodes[0].dataType != null && !nodes[0].dataType.isCharacterType()) {
                throw Error.error(ErrorCode.X_42561);
            }
            dataType = Type.SQL_VARBINARY;
            break;
        case FunctionId.FUNC_INET6_NTOA:
            if (nodes[0].dataType != null && !nodes[0].dataType.isBinaryType()) {
                throw Error.error(ErrorCode.X_42561);
            }
            dataType = Type.SQL_VARCHAR;
            break;
        default:
            break;
    }
    for (int i = 0; i < nodes.length; i++) {
        if (nodes[i] != null) {
            if (i >= paramTypes.length) {
                // TODO support type checking for variadic functions
                break;
            }
            if (paramTypes[i] == null) {
                // accept all argument types
                continue;
            }
            if (nodes[i].dataType == null) {
                // assert that the ambiguous argument (e.g. '?' parameter) has the required type
                nodes[i].dataType = paramTypes[i];
                continue;
            } else if (paramTypes[i].canConvertFrom(nodes[i].dataType)) {
                // Add support to pass in a JDBC time string constant
                if (paramTypes[i].isDateTimeType() && nodes[i].dataType.isCharacterType()) {
                    String datetimestring = (String) nodes[i].valueData;
                    if (datetimestring != null) {
                        datetimestring = datetimestring.trim();
                        try {
                            Timestamp.valueOf(datetimestring);
                        } catch (Exception e) {
                            throw Error.error(ErrorCode.X_42561);
                        }
                        nodes[i].valueData = paramTypes[i].castToType(session, nodes[i].valueData, nodes[i].dataType);
                        nodes[i].dataType = paramTypes[i];
                    }
                } else if (paramTypes[i].isNumberType() && !nodes[i].dataType.isNumberType()) {
                    throw Error.error(ErrorCode.X_42565);
                }
                // accept compatible argument types
                continue;
            }
            // incompatible data type
            throw Error.error(ErrorCode.X_42565);
        }
    }
    dataType = m_def.getDataType();
    if (dataType == null && nodes.length > 0) {
        if (parameterArg < 0 || parameterArg >= nodes.length) {
            // incompatible data type (so says the error -- we're missing one, actually)
            throw Error.error(ErrorCode.X_42565);
        }
        Expression like_child = nodes[parameterArg];
        if (like_child != null) {
            dataType = like_child.dataType;
        }
    }
}
Also used : Type(org.hsqldb_voltpatches.types.Type)

Example 69 with Type

use of org.hsqldb_voltpatches.types.Type in project voltdb by VoltDB.

the class FunctionForVoltDB method registerUserDefinedFunction.

public static void registerUserDefinedFunction(String functionName, Class<?> returnTypeClass, Class<?>[] parameterTypeClasses) {
    Type returnType = Type.getDefaultTypeWithSize(Types.getParameterSQLTypeNumber(returnTypeClass));
    Type[] parameterTypes = new Type[parameterTypeClasses.length];
    for (int i = 0; i < parameterTypeClasses.length; i++) {
        parameterTypes[i] = Type.getDefaultTypeWithSize(Types.getParameterSQLTypeNumber(parameterTypeClasses[i]));
    }
    // A pair of parentheses + number of parameters
    int syntaxLength = 2 + parameterTypes.length;
    if (parameterTypes.length > 1) {
        // Add commas in between
        syntaxLength += parameterTypes.length - 1;
    }
    short[] syntax = new short[syntaxLength];
    syntax[0] = Tokens.OPENBRACKET;
    int idx = 1;
    for (int parId = 0; parId < parameterTypes.length; parId++) {
        if (parId > 0) {
            syntax[idx++] = Tokens.COMMA;
        }
        syntax[idx++] = Tokens.QUESTION;
    }
    syntax[syntax.length - 1] = Tokens.CLOSEBRACKET;
    FunctionId.addUserDefinedFunctionId(functionName, returnType, parameterTypes, syntax);
}
Also used : Type(org.hsqldb_voltpatches.types.Type)

Example 70 with Type

use of org.hsqldb_voltpatches.types.Type in project voltdb by VoltDB.

the class ExpressionArithmetic method resolveTypesForConcat.

void resolveTypesForConcat(Session session) {
    if (dataType != null) {
        return;
    }
    if (nodes[LEFT].isParam) {
        nodes[LEFT].dataType = nodes[RIGHT].dataType;
    } else if (nodes[RIGHT].isParam) {
        nodes[RIGHT].dataType = nodes[LEFT].dataType;
    }
    if (nodes[LEFT].dataType == null || nodes[RIGHT].dataType == null) {
        throw Error.error(ErrorCode.X_42567);
    }
    if (nodes[LEFT].dataType.isBinaryType() ^ nodes[RIGHT].dataType.isBinaryType()) {
        throw Error.error(ErrorCode.X_42565);
    }
    // conversion of right argument to character for backward compatibility
    if (nodes[LEFT].dataType.isCharacterType() && !nodes[RIGHT].dataType.isCharacterType()) {
        Type newType = CharacterType.getCharacterType(Types.SQL_VARCHAR, nodes[RIGHT].dataType.displaySize());
        nodes[RIGHT] = new ExpressionOp(nodes[RIGHT], newType);
    }
    dataType = nodes[LEFT].dataType.getCombinedType(nodes[RIGHT].dataType, OpTypes.CONCAT);
    if (nodes[LEFT].opType == OpTypes.VALUE && nodes[RIGHT].opType == OpTypes.VALUE) {
        setAsConstantValue(session);
    }
}
Also used : CharacterType(org.hsqldb_voltpatches.types.CharacterType) Type(org.hsqldb_voltpatches.types.Type) NumberType(org.hsqldb_voltpatches.types.NumberType)

Aggregations

Type (org.hsqldb_voltpatches.types.Type)72 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)20 CharacterType (org.hsqldb_voltpatches.types.CharacterType)20 NumberType (org.hsqldb_voltpatches.types.NumberType)14 SchemaObject (org.hsqldb_voltpatches.SchemaObject)11 Table (org.hsqldb_voltpatches.Table)9 Iterator (org.hsqldb_voltpatches.lib.Iterator)9 WrapperIterator (org.hsqldb_voltpatches.lib.WrapperIterator)9 IntervalType (org.hsqldb_voltpatches.types.IntervalType)9 Constraint (org.hsqldb_voltpatches.Constraint)8 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)8 HsqlException (org.hsqldb_voltpatches.HsqlException)7 TextTable (org.hsqldb_voltpatches.TextTable)6 HsqlArrayList (org.hsqldb_voltpatches.lib.HsqlArrayList)6 DTIType (org.hsqldb_voltpatches.types.DTIType)6 ColumnSchema (org.hsqldb_voltpatches.ColumnSchema)4 DateTimeType (org.hsqldb_voltpatches.types.DateTimeType)4 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2