Search in sources :

Example 46 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class ParseNodeFactory method literal.

public LiteralParseNode literal(String value, String sqlTypeName) throws SQLException {
    PDataType expectedType = sqlTypeName == null ? null : PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(sqlTypeName));
    if (expectedType == null || !expectedType.isCoercibleTo(PTimestamp.INSTANCE)) {
        throw TypeMismatchException.newException(expectedType, PTimestamp.INSTANCE);
    }
    Object typedValue = expectedType.toObject(value);
    return new LiteralParseNode(typedValue);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Example 47 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class ToCharParseNode method create.

@Override
public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
    PDataType dataType = children.get(0).getDataType();
    // either date or number format string
    String formatString = (String) ((LiteralExpression) children.get(1)).getValue();
    Format formatter;
    FunctionArgumentType type;
    if (dataType.isCoercibleTo(PTimestamp.INSTANCE)) {
        if (formatString == null) {
            formatString = context.getDateFormat();
            formatter = context.getDateFormatter();
        } else {
            formatter = FunctionArgumentType.TEMPORAL.getFormatter(formatString);
        }
        type = FunctionArgumentType.TEMPORAL;
    } else if (dataType.isCoercibleTo(PDecimal.INSTANCE)) {
        if (formatString == null)
            formatString = context.getNumberFormat();
        formatter = FunctionArgumentType.NUMERIC.getFormatter(formatString);
        type = FunctionArgumentType.NUMERIC;
    } else {
        throw new SQLException(dataType + " type is unsupported for TO_CHAR().  Numeric and temporal types are supported.");
    }
    return new ToCharFunction(children, type, formatString, formatter);
}
Also used : Format(java.text.Format) PDataType(org.apache.phoenix.schema.types.PDataType) SQLException(java.sql.SQLException) FunctionArgumentType(org.apache.phoenix.expression.function.FunctionArgumentType) ToCharFunction(org.apache.phoenix.expression.function.ToCharFunction)

Example 48 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class UpgradeUtil method getAffectedDataTypes.

// Return all types that are descending and either:
// 1) variable length, which includes all array types (PHOENIX-2067)
// 2) fixed length with padding (PHOENIX-2120)
// 3) float and double (PHOENIX-2171)
// We exclude VARBINARY as we no longer support DESC for it.
private static String getAffectedDataTypes() {
    StringBuilder buf = new StringBuilder("(" + PVarchar.INSTANCE.getSqlType() + "," + +PChar.INSTANCE.getSqlType() + "," + +PBinary.INSTANCE.getSqlType() + "," + +PFloat.INSTANCE.getSqlType() + "," + +PDouble.INSTANCE.getSqlType() + "," + +PDecimal.INSTANCE.getSqlType() + ",");
    for (PDataType type : PDataType.values()) {
        if (type.isArrayType()) {
            buf.append(type.getSqlType());
            buf.append(',');
        }
    }
    buf.setCharAt(buf.length() - 1, ')');
    return buf.toString();
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Example 49 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class InListIT method testWithIntegerTypesWithVariedSaltingAndTenancy.

/**
     * Tests the given where clause against the given upserts by comparing against the list of
     * expected result strings.
     * @param upsertBodies  list of upsert bodies with the form "(pk1, pk2, ..., nonPk) VALUES (1, 7, ..., "row1")
     *                      excludes the "UPSERT INTO table_name " segment so that table name can vary
     * @param whereClause  the where clause to test. Should only refer to the pks upserted.
     * @param expecteds  a complete list of all of the expected result row names
     */
private void testWithIntegerTypesWithVariedSaltingAndTenancy(List<String> upsertBodies, String whereClause, List<String> expecteds) throws SQLException {
    // test single and multitenant tables
    for (boolean isMultiTenant : TENANCIES) {
        Connection baseConn = DriverManager.getConnection(getUrl());
        Connection conn = isMultiTenant ? DriverManager.getConnection(TENANT_URL) : baseConn;
        try {
            // test each combination of types and salting
            for (PDataType pkType : INTEGER_TYPES) {
                for (int saltBuckets : SALT_BUCKET_NUMBERS) {
                    // use a different table with a unique name for each variation
                    String tableName = initializeAndGetTable(baseConn, conn, isMultiTenant, pkType, saltBuckets);
                    // upsert the given data 
                    for (String upsertBody : upsertBodies) {
                        conn.createStatement().execute("UPSERT INTO " + tableName + " " + upsertBody);
                    }
                    conn.commit();
                    for (String hint : HINTS) {
                        String context = "where: " + whereClause + ", type: " + pkType + ", salt buckets: " + saltBuckets + ", multitenant: " + isMultiTenant + ", hint: " + hint + "";
                        // perform the query
                        String sql = "SELECT " + hint + " nonPk FROM " + tableName + " " + whereClause;
                        ResultSet rs = conn.createStatement().executeQuery(sql);
                        for (String expected : expecteds) {
                            assertTrue("did not include result '" + expected + "' (" + context + ")", rs.next());
                            assertEquals(context, expected, rs.getString(1));
                        }
                        assertFalse(context, rs.next());
                    }
                }
            }
        } finally // clean up the connections used
        {
            baseConn.close();
            if (!conn.isClosed()) {
                conn.close();
            }
        }
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 50 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(StringConcatParseNode node, List<Expression> children) throws SQLException {
    final StringConcatExpression expression = new StringConcatExpression(children);
    for (int i = 0; i < children.size(); i++) {
        ParseNode childNode = node.getChildren().get(i);
        if (childNode instanceof BindParseNode) {
            context.getBindManager().addParamMetaData((BindParseNode) childNode, expression);
        }
        PDataType type = children.get(i).getDataType();
        if (type == PVarbinary.INSTANCE) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_NOT_SUPPORTED_FOR_OPERATOR).setMessage("Concatenation does not support " + type + " in expression" + node).build().buildException();
        }
    }
    ImmutableBytesWritable ptr = context.getTempPtr();
    if (ExpressionUtil.isConstant(expression)) {
        return ExpressionUtil.getConstantExpression(expression, ptr);
    }
    return wrapGroupByExpression(expression);
}
Also used : StringConcatExpression(org.apache.phoenix.expression.StringConcatExpression) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) BindParseNode(org.apache.phoenix.parse.BindParseNode) ModulusParseNode(org.apache.phoenix.parse.ModulusParseNode) LikeParseNode(org.apache.phoenix.parse.LikeParseNode) UDFParseNode(org.apache.phoenix.parse.UDFParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) SequenceValueParseNode(org.apache.phoenix.parse.SequenceValueParseNode) InListParseNode(org.apache.phoenix.parse.InListParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ExistsParseNode(org.apache.phoenix.parse.ExistsParseNode) SubtractParseNode(org.apache.phoenix.parse.SubtractParseNode) NotParseNode(org.apache.phoenix.parse.NotParseNode) DivideParseNode(org.apache.phoenix.parse.DivideParseNode) StringConcatParseNode(org.apache.phoenix.parse.StringConcatParseNode) OrParseNode(org.apache.phoenix.parse.OrParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) FunctionParseNode(org.apache.phoenix.parse.FunctionParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) MultiplyParseNode(org.apache.phoenix.parse.MultiplyParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) CaseParseNode(org.apache.phoenix.parse.CaseParseNode) CastParseNode(org.apache.phoenix.parse.CastParseNode) AddParseNode(org.apache.phoenix.parse.AddParseNode) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) ArithmeticParseNode(org.apache.phoenix.parse.ArithmeticParseNode) IsNullParseNode(org.apache.phoenix.parse.IsNullParseNode) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6