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);
}
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);
}
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();
}
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();
}
}
}
}
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);
}
Aggregations