use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RegexpSubstrFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
AbstractBasePattern pattern = this.pattern;
if (pattern == null) {
Expression patternExpr = getPatternExpression();
if (!patternExpr.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
pattern = compilePatternSpec((String) patternExpr.getDataType().toObject(ptr, patternExpr.getSortOrder()));
}
int offset;
if (this.offset == null) {
Expression offsetExpression = getOffsetExpression();
if (!offsetExpression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
offset = offsetExpression.getDataType().getCodec().decodeInt(ptr, offsetExpression.getSortOrder());
} else {
offset = this.offset;
}
Expression strExpression = getSourceStrExpression();
if (!strExpression.evaluate(tuple, ptr)) {
return false;
}
if (ptr.get().length == 0) {
return true;
}
TYPE.coerceBytes(ptr, strExpression.getDataType(), strExpression.getSortOrder(), SortOrder.ASC);
// Account for 1 versus 0-based offset
offset = offset - (offset <= 0 ? 0 : 1);
pattern.substr(ptr, offset);
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class PhoenixRuntimeTest method testGetTenantIdExpression.
@Test
public void testGetTenantIdExpression() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
Expression e1 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_STATS_NAME);
assertNull(e1);
Expression e2 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME);
assertNotNull(e2);
Expression e3 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_NAME);
assertNotNull(e3);
conn.createStatement().execute("CREATE TABLE FOO (k VARCHAR PRIMARY KEY)");
Expression e4 = PhoenixRuntime.getTenantIdExpression(conn, "FOO");
assertNull(e4);
conn.createStatement().execute("CREATE TABLE A.BAR (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true");
Expression e5 = PhoenixRuntime.getTenantIdExpression(conn, "A.BAR");
assertNotNull(e5);
conn.createStatement().execute("CREATE INDEX I1 ON A.BAR (K2)");
Expression e5A = PhoenixRuntime.getTenantIdExpression(conn, "A.I1");
assertNotNull(e5A);
conn.createStatement().execute("CREATE TABLE BAS (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true, SALT_BUCKETS=3");
Expression e6 = PhoenixRuntime.getTenantIdExpression(conn, "BAS");
assertNotNull(e6);
conn.createStatement().execute("CREATE INDEX I2 ON BAS (K2)");
Expression e6A = PhoenixRuntime.getTenantIdExpression(conn, "I2");
assertNotNull(e6A);
try {
PhoenixRuntime.getTenantIdExpression(conn, "NOT.ATABLE");
fail();
} catch (TableNotFoundException e) {
// Expected
}
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "t1");
Connection tsconn = DriverManager.getConnection(getUrl(), props);
tsconn.createStatement().execute("CREATE VIEW V(V1 VARCHAR) AS SELECT * FROM BAS");
Expression e7 = PhoenixRuntime.getTenantIdExpression(tsconn, "V");
assertNotNull(e7);
tsconn.createStatement().execute("CREATE LOCAL INDEX I3 ON V (V1)");
try {
PhoenixRuntime.getTenantIdExpression(tsconn, "I3");
fail();
} catch (SQLFeatureNotSupportedException e) {
// Expected
}
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class OctetLengthFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
// get binary data parameter
Expression dataExpr = children.get(0);
if (!dataExpr.evaluate(tuple, ptr))
return false;
// set result
((PBinaryBase) dataExpr.getDataType()).octetLength(ptr, dataExpr.getSortOrder(), ptr);
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RoundDateExpression method create.
/**
* @param timeUnit - unit of time to round up to
* @param multiplier - determines the roll up window size.
* Create a {@link RoundDateExpression}.
*/
public static Expression create(Expression expr, TimeUnit timeUnit, int multiplier) throws SQLException {
Expression timeUnitExpr = getTimeUnitExpr(timeUnit);
Expression defaultMultiplierExpr = getMultiplierExpr(multiplier);
List<Expression> expressions = Lists.newArrayList(expr, timeUnitExpr, defaultMultiplierExpr);
return create(expressions);
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class LpadFunction method evaluate.
/**
* Left pads a string with with the given fill expression.
*/
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression outputStrLenExpr = getOutputStrLenExpr();
if (!outputStrLenExpr.evaluate(tuple, ptr)) {
return false;
}
int outputStrLen = outputStrLenExpr.getDataType().getCodec().decodeInt(ptr, outputStrLenExpr.getSortOrder());
if (outputStrLen < 0) {
return false;
}
Expression strExp = getStrExpr();
if (!strExp.evaluate(tuple, ptr)) {
return false;
}
boolean isStrCharType = getStrExpr().getDataType() == PChar.INSTANCE;
boolean isFillCharType = getFillExpr().getDataType() == PChar.INSTANCE;
SortOrder strSortOrder = getStrExpr().getSortOrder();
SortOrder fillSortOrder = getFillExpr().getSortOrder();
int inputStrLen = getUTF8Length(ptr, strSortOrder, isStrCharType);
if (outputStrLen == inputStrLen) {
// nothing to do
return true;
}
if (outputStrLen < inputStrLen) {
// truncate the string from the right
int subStrByteLength = getSubstringByteLength(ptr, outputStrLen, strSortOrder, isStrCharType);
ptr.set(ptr.get(), ptr.getOffset(), subStrByteLength);
return true;
}
// left pad the input string with the fill chars
Expression fillExpr = getFillExpr();
ImmutableBytesWritable fillPtr = new ImmutableBytesWritable();
if (!fillExpr.evaluate(tuple, fillPtr)) {
return false;
}
int fillExprLen = fillPtr.getLength();
if (fillExprLen < 1) {
// return if fill is empty
return false;
}
// if the padding to be added is not a multiple of the length of the
// fill string then we need to use part of the fill string to pad
// LPAD(ab, 5, xy) = xyxab
// padLen = 3
// numFillsPrepended = 1
// numFillCharsPrepended = 1
// length of the fill string
int fillLen = getUTF8Length(fillPtr, fillSortOrder, isFillCharType);
// length of padding to be added
int padLen = outputStrLen - inputStrLen;
// number of fill strings to be prepended
int numFillsPrepended = padLen / fillLen;
// number of chars from fill string to be prepended
int numFillCharsPrepended = padLen % fillLen;
// byte length of the input string
int strByteLength = ptr.getLength();
// byte length of the fill string
int fillByteLength = getSubstringByteLength(fillPtr, fillPtr.getLength(), fillSortOrder, isFillCharType);
// byte length of the full fills to be prepended
int fullFillsByteLength = numFillsPrepended * fillByteLength;
// byte length of the chars of fill string to be prepended
int fillCharsByteLength = getSubstringByteLength(fillPtr, numFillCharsPrepended, fillSortOrder, isFillCharType);
// byte length of the padded string =
int strWithPaddingByteLength = fullFillsByteLength + fillCharsByteLength + strByteLength;
// need to invert the fill string if the sort order of fill and
// input are different
boolean invertFill = fillSortOrder != strSortOrder;
byte[] paddedStr = StringUtil.lpad(ptr.get(), ptr.getOffset(), ptr.getLength(), fillPtr.get(), fillPtr.getOffset(), fillPtr.getLength(), invertFill, strWithPaddingByteLength);
ptr.set(paddedStr);
return true;
}
Aggregations