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);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
@Override
public Expression visitLeave(ArrayConstructorNode node, List<Expression> children) throws SQLException {
boolean isChildTypeUnknown = false;
Expression arrayElemChild = null;
PDataType arrayElemDataType = children.get(0).getDataType();
for (int i = 0; i < children.size(); i++) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType == null) {
isChildTypeUnknown = true;
} else if (arrayElemDataType == null) {
arrayElemDataType = childType;
isChildTypeUnknown = true;
arrayElemChild = child;
} else if (arrayElemDataType == childType || childType.isCoercibleTo(arrayElemDataType)) {
continue;
} else if (arrayElemDataType.isCoercibleTo(childType)) {
arrayElemChild = child;
arrayElemDataType = childType;
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH).setMessage("Case expressions must have common type: " + arrayElemDataType + " cannot be coerced to " + childType).build().buildException();
}
}
// make the return type be the most general number type of DECIMAL.
if (isChildTypeUnknown && arrayElemDataType != null && arrayElemDataType.isCoercibleTo(PDecimal.INSTANCE)) {
arrayElemDataType = PDecimal.INSTANCE;
}
final PDataType theArrayElemDataType = arrayElemDataType;
for (int i = 0; i < node.getChildren().size(); i++) {
ParseNode childNode = node.getChildren().get(i);
if (childNode instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) childNode, arrayElemDataType == arrayElemChild.getDataType() ? arrayElemChild : new DelegateDatum(arrayElemChild) {
@Override
public PDataType getDataType() {
return theArrayElemDataType;
}
});
}
}
ImmutableBytesWritable ptr = context.getTempPtr();
// the value object array type should match the java known type
Object[] elements = (Object[]) java.lang.reflect.Array.newInstance(theArrayElemDataType.getJavaClass(), children.size());
boolean rowKeyOrderOptimizable = context.getCurrentTable().getTable().rowKeyOrderOptimizable();
ArrayConstructorExpression arrayExpression = new ArrayConstructorExpression(children, arrayElemDataType, rowKeyOrderOptimizable);
if (ExpressionUtil.isConstant(arrayExpression)) {
for (int i = 0; i < children.size(); i++) {
Expression child = children.get(i);
child.evaluate(null, ptr);
Object value = null;
if (child.getDataType() == null) {
value = arrayElemDataType.toObject(ptr, theArrayElemDataType, child.getSortOrder());
} else {
value = arrayElemDataType.toObject(ptr, child.getDataType(), child.getSortOrder());
}
elements[i] = LiteralExpression.newConstant(value, theArrayElemDataType, child.getDeterminism()).getValue();
}
Object value = PArrayDataType.instantiatePhoenixArray(arrayElemDataType, elements);
return LiteralExpression.newConstant(value, PDataType.fromTypeId(arrayElemDataType.getSqlType() + PDataType.ARRAY_TYPE_BASE), null, null, arrayExpression.getSortOrder(), Determinism.ALWAYS, rowKeyOrderOptimizable);
}
return wrapGroupByExpression(arrayExpression);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class TimestampSubtractExpression method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
BigDecimal finalResult = BigDecimal.ZERO;
for (int i = 0; i < children.size(); i++) {
if (!children.get(i).evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
BigDecimal value;
PDataType type = children.get(i).getDataType();
SortOrder sortOrder = children.get(i).getSortOrder();
if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
} else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
value = (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder)).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
} else if (type.isCoercibleTo(PDouble.INSTANCE)) {
value = ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder))).multiply(BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
} else {
value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
}
if (i == 0) {
finalResult = value;
} else {
finalResult = finalResult.subtract(value);
}
}
Timestamp ts = DateUtil.getTimestamp(finalResult);
byte[] resultPtr = new byte[getDataType().getByteSize()];
PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
ptr.set(resultPtr);
return true;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class InListExpression method toString.
@Override
public String toString() {
int maxToStringLen = 200;
Expression firstChild = children.get(0);
PDataType type = firstChild.getDataType();
StringBuilder buf = new StringBuilder(firstChild + " IN (");
for (ImmutableBytesPtr value : values) {
if (firstChild.getSortOrder() != null) {
type.coerceBytes(value, type, firstChild.getSortOrder(), SortOrder.getDefault());
}
buf.append(type.toStringLiteral(value, null));
buf.append(',');
if (buf.length() >= maxToStringLen) {
buf.append("... ");
break;
}
}
buf.setCharAt(buf.length() - 1, ')');
return buf.toString();
}
Aggregations