Search in sources :

Example 46 with ImmutableBytesWritable

use of org.apache.hadoop.hbase.io.ImmutableBytesWritable 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)

Example 47 with ImmutableBytesWritable

use of org.apache.hadoop.hbase.io.ImmutableBytesWritable in project phoenix by apache.

the class GlobalCache method clearTenantCache.

public long clearTenantCache() {
    long unfreedBytes = getMemoryManager().getMaxMemory() - getMemoryManager().getAvailableMemory();
    if (unfreedBytes != 0 && logger.isDebugEnabled()) {
        logger.debug("Found " + (getMemoryManager().getMaxMemory() - getMemoryManager().getAvailableMemory()) + " bytes not freed from global cache");
    }
    removeAllServerCache();
    for (Map.Entry<ImmutableBytesWritable, TenantCache> entry : perTenantCacheMap.entrySet()) {
        TenantCache cache = entry.getValue();
        long unfreedTenantBytes = cache.getMemoryManager().getMaxMemory() - cache.getMemoryManager().getAvailableMemory();
        if (unfreedTenantBytes != 0 && logger.isDebugEnabled()) {
            ImmutableBytesWritable cacheId = entry.getKey();
            logger.debug("Found " + unfreedTenantBytes + " bytes not freed for tenant " + Bytes.toStringBinary(cacheId.get(), cacheId.getOffset(), cacheId.getLength()));
        }
        unfreedBytes += unfreedTenantBytes;
        cache.removeAllServerCache();
    }
    perTenantCacheMap.clear();
    return unfreedBytes;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 48 with ImmutableBytesWritable

use of org.apache.hadoop.hbase.io.ImmutableBytesWritable in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

private Expression visitLeave(ArithmeticParseNode node, List<Expression> children, ArithmeticExpressionBinder binder, ArithmeticExpressionFactory factory) throws SQLException {
    boolean isNull = false;
    for (Expression child : children) {
        boolean isChildLiteral = (child instanceof LiteralExpression);
        isNull |= isChildLiteral && ((LiteralExpression) child).getValue() == null;
    }
    Expression expression = factory.create(node, children);
    for (int i = 0; i < node.getChildren().size(); i++) {
        ParseNode childNode = node.getChildren().get(i);
        if (childNode instanceof BindParseNode) {
            context.getBindManager().addParamMetaData((BindParseNode) childNode, binder == null ? expression : binder.getBindMetaData(i, children, expression));
        }
    }
    ImmutableBytesWritable ptr = context.getTempPtr();
    // If all children are literals, just evaluate now
    if (ExpressionUtil.isConstant(expression)) {
        return ExpressionUtil.getConstantExpression(expression, ptr);
    } else if (isNull) {
        return LiteralExpression.newConstant(null, expression.getDataType(), expression.getDeterminism());
    }
    // Otherwise create and return the expression
    return wrapGroupByExpression(expression);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) DecimalAddExpression(org.apache.phoenix.expression.DecimalAddExpression) TimestampSubtractExpression(org.apache.phoenix.expression.TimestampSubtractExpression) ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) Expression(org.apache.phoenix.expression.Expression) LikeExpression(org.apache.phoenix.expression.LikeExpression) ByteBasedLikeExpression(org.apache.phoenix.expression.ByteBasedLikeExpression) DoubleSubtractExpression(org.apache.phoenix.expression.DoubleSubtractExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) DateSubtractExpression(org.apache.phoenix.expression.DateSubtractExpression) ArrayElemRefExpression(org.apache.phoenix.expression.function.ArrayElemRefExpression) CaseExpression(org.apache.phoenix.expression.CaseExpression) DoubleDivideExpression(org.apache.phoenix.expression.DoubleDivideExpression) NotExpression(org.apache.phoenix.expression.NotExpression) DoubleAddExpression(org.apache.phoenix.expression.DoubleAddExpression) DecimalDivideExpression(org.apache.phoenix.expression.DecimalDivideExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) RoundTimestampExpression(org.apache.phoenix.expression.function.RoundTimestampExpression) StringConcatExpression(org.apache.phoenix.expression.StringConcatExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) TimestampAddExpression(org.apache.phoenix.expression.TimestampAddExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) StringBasedLikeExpression(org.apache.phoenix.expression.StringBasedLikeExpression) ArrayAnyComparisonExpression(org.apache.phoenix.expression.function.ArrayAnyComparisonExpression) DecimalSubtractExpression(org.apache.phoenix.expression.DecimalSubtractExpression) ModulusExpression(org.apache.phoenix.expression.ModulusExpression) DoubleMultiplyExpression(org.apache.phoenix.expression.DoubleMultiplyExpression) DecimalMultiplyExpression(org.apache.phoenix.expression.DecimalMultiplyExpression) DateAddExpression(org.apache.phoenix.expression.DateAddExpression) RoundDecimalExpression(org.apache.phoenix.expression.function.RoundDecimalExpression) LongAddExpression(org.apache.phoenix.expression.LongAddExpression) LongSubtractExpression(org.apache.phoenix.expression.LongSubtractExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) LongMultiplyExpression(org.apache.phoenix.expression.LongMultiplyExpression) ArrayAllComparisonExpression(org.apache.phoenix.expression.function.ArrayAllComparisonExpression) OrExpression(org.apache.phoenix.expression.OrExpression) LongDivideExpression(org.apache.phoenix.expression.LongDivideExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) 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) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 49 with ImmutableBytesWritable

use of org.apache.hadoop.hbase.io.ImmutableBytesWritable 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);
}
Also used : ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) DecimalAddExpression(org.apache.phoenix.expression.DecimalAddExpression) TimestampSubtractExpression(org.apache.phoenix.expression.TimestampSubtractExpression) ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) Expression(org.apache.phoenix.expression.Expression) LikeExpression(org.apache.phoenix.expression.LikeExpression) ByteBasedLikeExpression(org.apache.phoenix.expression.ByteBasedLikeExpression) DoubleSubtractExpression(org.apache.phoenix.expression.DoubleSubtractExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) DateSubtractExpression(org.apache.phoenix.expression.DateSubtractExpression) ArrayElemRefExpression(org.apache.phoenix.expression.function.ArrayElemRefExpression) CaseExpression(org.apache.phoenix.expression.CaseExpression) DoubleDivideExpression(org.apache.phoenix.expression.DoubleDivideExpression) NotExpression(org.apache.phoenix.expression.NotExpression) DoubleAddExpression(org.apache.phoenix.expression.DoubleAddExpression) DecimalDivideExpression(org.apache.phoenix.expression.DecimalDivideExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) RoundTimestampExpression(org.apache.phoenix.expression.function.RoundTimestampExpression) StringConcatExpression(org.apache.phoenix.expression.StringConcatExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) TimestampAddExpression(org.apache.phoenix.expression.TimestampAddExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) StringBasedLikeExpression(org.apache.phoenix.expression.StringBasedLikeExpression) ArrayAnyComparisonExpression(org.apache.phoenix.expression.function.ArrayAnyComparisonExpression) DecimalSubtractExpression(org.apache.phoenix.expression.DecimalSubtractExpression) ModulusExpression(org.apache.phoenix.expression.ModulusExpression) DoubleMultiplyExpression(org.apache.phoenix.expression.DoubleMultiplyExpression) DecimalMultiplyExpression(org.apache.phoenix.expression.DecimalMultiplyExpression) DateAddExpression(org.apache.phoenix.expression.DateAddExpression) RoundDecimalExpression(org.apache.phoenix.expression.function.RoundDecimalExpression) LongAddExpression(org.apache.phoenix.expression.LongAddExpression) LongSubtractExpression(org.apache.phoenix.expression.LongSubtractExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) LongMultiplyExpression(org.apache.phoenix.expression.LongMultiplyExpression) ArrayAllComparisonExpression(org.apache.phoenix.expression.function.ArrayAllComparisonExpression) OrExpression(org.apache.phoenix.expression.OrExpression) LongDivideExpression(org.apache.phoenix.expression.LongDivideExpression) DelegateDatum(org.apache.phoenix.schema.DelegateDatum) 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) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 50 with ImmutableBytesWritable

use of org.apache.hadoop.hbase.io.ImmutableBytesWritable in project phoenix by apache.

the class SpillableGroupByCache method getScanner.

@Override
public RegionScanner getScanner(final RegionScanner s) {
    final Iterator<Entry<ImmutableBytesWritable, Aggregator[]>> cacheIter = new EntryIterator();
    // scanner using the spillable implementation
    return new BaseRegionScanner(s) {

        @Override
        public void close() throws IOException {
            try {
                s.close();
            } finally {
                // Always close gbCache and swallow possible Exceptions
                Closeables.closeQuietly(SpillableGroupByCache.this);
            }
        }

        @Override
        public boolean next(List<Cell> results) throws IOException {
            if (!cacheIter.hasNext()) {
                return false;
            }
            Map.Entry<ImmutableBytesWritable, Aggregator[]> ce = cacheIter.next();
            ImmutableBytesWritable key = ce.getKey();
            Aggregator[] aggs = ce.getValue();
            byte[] value = aggregators.toBytes(aggs);
            if (logger.isDebugEnabled()) {
                logger.debug("Adding new distinct group: " + Bytes.toStringBinary(key.get(), key.getOffset(), key.getLength()) + " with aggregators " + aggs.toString() + " value = " + Bytes.toStringBinary(value));
            }
            results.add(KeyValueUtil.newKeyValue(key.get(), key.getOffset(), key.getLength(), SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length));
            return cacheIter.hasNext();
        }
    };
}
Also used : Entry(java.util.Map.Entry) CacheEntry(org.apache.phoenix.cache.aggcache.SpillManager.CacheEntry) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Aggregator(org.apache.phoenix.expression.aggregator.Aggregator) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)296 Test (org.junit.Test)86 Expression (org.apache.phoenix.expression.Expression)36 IOException (java.io.IOException)33 PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)30 ArrayList (java.util.ArrayList)28 Configuration (org.apache.hadoop.conf.Configuration)28 Result (org.apache.hadoop.hbase.client.Result)28 Cell (org.apache.hadoop.hbase.Cell)27 KeyValue (org.apache.hadoop.hbase.KeyValue)27 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)27 PTable (org.apache.phoenix.schema.PTable)27 PDataType (org.apache.phoenix.schema.types.PDataType)26 PSmallint (org.apache.phoenix.schema.types.PSmallint)25 PTinyint (org.apache.phoenix.schema.types.PTinyint)23 Put (org.apache.hadoop.hbase.client.Put)20 PUnsignedSmallint (org.apache.phoenix.schema.types.PUnsignedSmallint)20 PUnsignedTinyint (org.apache.phoenix.schema.types.PUnsignedTinyint)20 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)19 List (java.util.List)18