Search in sources :

Example 11 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression in project phoenix by apache.

the class ColumnDef method validateDefault.

public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException {
    String defaultStr = this.getExpression();
    if (defaultStr == null) {
        return true;
    }
    ExpressionCompiler compiler = new ExpressionCompiler(context);
    ParseNode defaultParseNode = new SQLParser(this.getExpression()).parseExpression();
    Expression defaultExpression = defaultParseNode.accept(compiler);
    if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
    }
    if (this.isRowTimestamp() || (pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    // Evaluate the expression to confirm it's validity
    LiteralExpression defaultValue = ExpressionUtil.getConstantExpression(defaultExpression, ptr);
    // A DEFAULT that evaluates to null should be ignored as it adds nothing
    if (defaultValue.getValue() == null) {
        return false;
    }
    PDataType sourceType = defaultExpression.getDataType();
    PDataType targetType = this.getDataType();
    // Ensure that coercion works (will throw if not)
    context.getTempPtr().set(ptr.get(), ptr.getOffset(), ptr.getLength());
    try {
        targetType.coerceBytes(context.getTempPtr(), defaultValue.getValue(), sourceType, defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder());
    } catch (ConstraintViolationException e) {
        if (e.getCause() instanceof SQLException) {
            SQLException sqlE = (SQLException) e.getCause();
            throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
        }
        throw e;
    }
    if (!targetType.isSizeCompatible(ptr, defaultValue.getValue(), sourceType, sortOrder, defaultValue.getMaxLength(), defaultValue.getScale(), this.getMaxLength(), this.getScale())) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_EXCEEDS_MAX_CAPACITY).setColumnName(this.getColumnDefName().getColumnName()).setMessage("DEFAULT " + this.getExpression()).build().buildException();
    }
    return true;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) SQLException(java.sql.SQLException) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) ConstraintViolationException(org.apache.phoenix.schema.ConstraintViolationException) ExpressionCompiler(org.apache.phoenix.compile.ExpressionCompiler) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 12 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression in project phoenix by apache.

the class FunctionParseNode method validateFunctionArguement.

public static void validateFunctionArguement(BuiltInFunctionInfo info, int childIndex, Expression child) throws ArgumentTypeMismatchException, ValueRangeExcpetion {
    BuiltInFunctionArgInfo arg = info.getArgs()[childIndex];
    if (arg.getAllowedTypes().length > 0) {
        boolean isCoercible = false;
        for (Class<? extends PDataType> type : arg.getAllowedTypes()) {
            if (child.getDataType().isCoercibleTo(PDataTypeFactory.getInstance().instanceFromClass(type))) {
                isCoercible = true;
                break;
            }
        }
        if (!isCoercible) {
            throw new ArgumentTypeMismatchException(arg.getAllowedTypes(), child.getDataType(), info.getName() + " argument " + (childIndex + 1));
        }
        if (child instanceof LiteralExpression) {
            LiteralExpression valueExp = (LiteralExpression) child;
            LiteralExpression minValue = arg.getMinValue();
            LiteralExpression maxValue = arg.getMaxValue();
            if (minValue != null && minValue.getDataType().compareTo(minValue.getValue(), valueExp.getValue(), valueExp.getDataType()) > 0) {
                throw new ValueRangeExcpetion(minValue, maxValue == null ? "" : maxValue, valueExp.getValue(), info.getName() + " argument " + (childIndex + 1));
            }
            if (maxValue != null && maxValue.getDataType().compareTo(maxValue.getValue(), valueExp.getValue(), valueExp.getDataType()) < 0) {
                throw new ValueRangeExcpetion(minValue == null ? "" : minValue, maxValue, valueExp.getValue(), info.getName() + " argument " + (childIndex + 1));
            }
        }
    }
    if (arg.isConstant() && !(child instanceof LiteralExpression)) {
        throw new ArgumentTypeMismatchException("constant", child.toString(), info.getName() + " argument " + (childIndex + 1));
    }
    if (!arg.getAllowedValues().isEmpty()) {
        Object value = ((LiteralExpression) child).getValue();
        if (!arg.getAllowedValues().contains(value.toString().toUpperCase())) {
            throw new ArgumentTypeMismatchException(Arrays.toString(arg.getAllowedValues().toArray(new String[0])), value.toString(), info.getName() + " argument " + (childIndex + 1));
        }
    }
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ValueRangeExcpetion(org.apache.phoenix.schema.ValueRangeExcpetion) ArgumentTypeMismatchException(org.apache.phoenix.schema.ArgumentTypeMismatchException)

Example 13 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression in project phoenix by apache.

the class FloorDateExpression method create.

public static Expression create(List<Expression> children) throws SQLException {
    Expression firstChild = children.get(0);
    PDataType firstChildDataType = firstChild.getDataType();
    if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE) {
        // Coerce TIMESTAMP to DATE, as the nanos has no affect
        List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size());
        newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE));
        newChildren.addAll(children.subList(1, children.size()));
        children = newChildren;
    }
    Object timeUnitValue = ((LiteralExpression) children.get(1)).getValue();
    TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null);
    switch(timeUnit) {
        case WEEK:
            return new FloorWeekExpression(children);
        case MONTH:
            return new FloorMonthExpression(children);
        case YEAR:
            return new FloorYearExpression(children);
        default:
            return new FloorDateExpression(children);
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 14 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression in project phoenix by apache.

the class IndexMaintainer method buildUpdateMutation.

public Put buildUpdateMutation(KeyValueBuilder kvBuilder, ValueGetter valueGetter, ImmutableBytesWritable dataRowKeyPtr, long ts, byte[] regionStartKey, byte[] regionEndKey) throws IOException {
    byte[] indexRowKey = this.buildRowKey(valueGetter, dataRowKeyPtr, regionStartKey, regionEndKey);
    Put put = null;
    // New row being inserted: add the empty key value
    if (valueGetter == null || valueGetter.getLatestValue(dataEmptyKeyValueRef) == null) {
        put = new Put(indexRowKey);
        // add the keyvalue for the empty row
        put.add(kvBuilder.buildPut(new ImmutableBytesPtr(indexRowKey), this.getEmptyKeyValueFamily(), dataEmptyKeyValueRef.getQualifierWritable(), ts, // set the value to the empty column name
        dataEmptyKeyValueRef.getQualifierWritable()));
        put.setDurability(!indexWALDisabled ? Durability.USE_DEFAULT : Durability.SKIP_WAL);
    }
    ImmutableBytesPtr rowKey = new ImmutableBytesPtr(indexRowKey);
    if (immutableStorageScheme != ImmutableStorageScheme.ONE_CELL_PER_COLUMN) {
        // map from index column family to list of pair of index column and data column (for covered columns)
        Map<ImmutableBytesPtr, List<Pair<ColumnReference, ColumnReference>>> familyToColListMap = Maps.newHashMap();
        for (ColumnReference ref : this.getCoveredColumns()) {
            ColumnReference indexColRef = this.coveredColumnsMap.get(ref);
            ImmutableBytesPtr cf = new ImmutableBytesPtr(indexColRef.getFamily());
            if (!familyToColListMap.containsKey(cf)) {
                familyToColListMap.put(cf, Lists.<Pair<ColumnReference, ColumnReference>>newArrayList());
            }
            familyToColListMap.get(cf).add(Pair.newPair(indexColRef, ref));
        }
        // iterate over each column family and create a byte[] containing all the columns 
        for (Entry<ImmutableBytesPtr, List<Pair<ColumnReference, ColumnReference>>> entry : familyToColListMap.entrySet()) {
            byte[] columnFamily = entry.getKey().copyBytesIfNecessary();
            List<Pair<ColumnReference, ColumnReference>> colRefPairs = entry.getValue();
            int maxEncodedColumnQualifier = Integer.MIN_VALUE;
            // find the max col qualifier
            for (Pair<ColumnReference, ColumnReference> colRefPair : colRefPairs) {
                maxEncodedColumnQualifier = Math.max(maxEncodedColumnQualifier, encodingScheme.decode(colRefPair.getFirst().getQualifier()));
            }
            Expression[] colValues = EncodedColumnsUtil.createColumnExpressionArray(maxEncodedColumnQualifier);
            // set the values of the columns
            for (Pair<ColumnReference, ColumnReference> colRefPair : colRefPairs) {
                ColumnReference indexColRef = colRefPair.getFirst();
                ColumnReference dataColRef = colRefPair.getSecond();
                Expression expression = new SingleCellColumnExpression(new PDatum() {

                    @Override
                    public boolean isNullable() {
                        return false;
                    }

                    @Override
                    public SortOrder getSortOrder() {
                        return null;
                    }

                    @Override
                    public Integer getScale() {
                        return null;
                    }

                    @Override
                    public Integer getMaxLength() {
                        return null;
                    }

                    @Override
                    public PDataType getDataType() {
                        return null;
                    }
                }, dataColRef.getFamily(), dataColRef.getQualifier(), encodingScheme);
                ImmutableBytesPtr ptr = new ImmutableBytesPtr();
                expression.evaluate(new ValueGetterTuple(valueGetter), ptr);
                byte[] value = ptr.copyBytesIfNecessary();
                if (value != null) {
                    int indexArrayPos = encodingScheme.decode(indexColRef.getQualifier()) - QueryConstants.ENCODED_CQ_COUNTER_INITIAL_VALUE + 1;
                    colValues[indexArrayPos] = new LiteralExpression(value);
                }
            }
            List<Expression> children = Arrays.asList(colValues);
            // we use SingleCellConstructorExpression to serialize multiple columns into a single byte[]
            SingleCellConstructorExpression singleCellConstructorExpression = new SingleCellConstructorExpression(immutableStorageScheme, children);
            ImmutableBytesWritable ptr = new ImmutableBytesWritable();
            singleCellConstructorExpression.evaluate(new BaseTuple() {
            }, ptr);
            if (put == null) {
                put = new Put(indexRowKey);
                put.setDurability(!indexWALDisabled ? Durability.USE_DEFAULT : Durability.SKIP_WAL);
            }
            ImmutableBytesPtr colFamilyPtr = new ImmutableBytesPtr(columnFamily);
            //this is a little bit of extra work for installations that are running <0.94.14, but that should be rare and is a short-term set of wrappers - it shouldn't kill GC
            put.add(kvBuilder.buildPut(rowKey, colFamilyPtr, QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES_PTR, ts, ptr));
        }
    } else {
        for (ColumnReference ref : this.getCoveredColumns()) {
            ColumnReference indexColRef = this.coveredColumnsMap.get(ref);
            ImmutableBytesPtr cq = indexColRef.getQualifierWritable();
            ImmutableBytesPtr cf = indexColRef.getFamilyWritable();
            ImmutableBytesWritable value = valueGetter.getLatestValue(ref);
            if (value != null) {
                if (put == null) {
                    put = new Put(indexRowKey);
                    put.setDurability(!indexWALDisabled ? Durability.USE_DEFAULT : Durability.SKIP_WAL);
                }
                put.add(kvBuilder.buildPut(rowKey, cf, cq, ts, value));
            }
        }
    }
    return put;
}
Also used : BaseTuple(org.apache.phoenix.schema.tuple.BaseTuple) PDatum(org.apache.phoenix.schema.PDatum) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) PDataType(org.apache.phoenix.schema.types.PDataType) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.apache.hadoop.hbase.util.Pair) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SortOrder(org.apache.phoenix.schema.SortOrder) Put(org.apache.hadoop.hbase.client.Put) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference) ValueGetterTuple(org.apache.phoenix.schema.tuple.ValueGetterTuple)

Example 15 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression in project phoenix by apache.

the class ImmutableStorageSchemeTest method testLeadingNulls.

@Test
public void testLeadingNulls() throws Exception {
    List<Expression> children = Lists.newArrayListWithExpectedSize(4);
    LiteralExpression nullExpression = LiteralExpression.newConstant(null);
    children.add(nullExpression);
    children.add(nullExpression);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY1, PVarbinary.INSTANCE));
    children.add(LiteralExpression.newConstant(BYTE_ARRAY2, PVarbinary.INSTANCE));
    SingleCellConstructorExpression singleCellConstructorExpression = new SingleCellConstructorExpression(immutableStorageScheme, children);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    singleCellConstructorExpression.evaluate(null, ptr);
    ImmutableBytesPtr ptrCopy = new ImmutableBytesPtr(ptr);
    ColumnValueDecoder decoder = immutableStorageScheme.getDecoder();
    assertTrue(decoder.decode(ptrCopy, 0));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 1));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 2));
    assertArrayEquals(BYTE_ARRAY1, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 3));
    assertArrayEquals(BYTE_ARRAY2, ptrCopy.copyBytesIfNecessary());
}
Also used : SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) Expression(org.apache.phoenix.expression.Expression) DelegateExpression(org.apache.phoenix.expression.DelegateExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Test(org.junit.Test)

Aggregations

LiteralExpression (org.apache.phoenix.expression.LiteralExpression)23 Expression (org.apache.phoenix.expression.Expression)11 PDataType (org.apache.phoenix.schema.types.PDataType)9 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)8 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)6 ArrayList (java.util.ArrayList)5 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)5 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4 ParseNode (org.apache.phoenix.parse.ParseNode)4 List (java.util.List)3 Cell (org.apache.hadoop.hbase.Cell)3 Pair (org.apache.hadoop.hbase.util.Pair)3 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)3 ResultIterator (org.apache.phoenix.iterate.ResultIterator)3 SQLException (java.sql.SQLException)2 Scan (org.apache.hadoop.hbase.client.Scan)2 AndExpression (org.apache.phoenix.expression.AndExpression)2 ArrayConstructorExpression (org.apache.phoenix.expression.ArrayConstructorExpression)2 ByteBasedLikeExpression (org.apache.phoenix.expression.ByteBasedLikeExpression)2 CaseExpression (org.apache.phoenix.expression.CaseExpression)2