Search in sources :

Example 11 with CompareOp

use of org.apache.hadoop.hbase.filter.CompareFilter.CompareOp in project hbase by apache.

the class SingleColumnValueExcludeFilter method parseFrom.

/**
   * @param pbBytes A pb serialized {@link SingleColumnValueExcludeFilter} instance
   * @return An instance of {@link SingleColumnValueExcludeFilter} made from <code>bytes</code>
   * @throws DeserializationException
   * @see #toByteArray
   */
public static SingleColumnValueExcludeFilter parseFrom(final byte[] pbBytes) throws DeserializationException {
    FilterProtos.SingleColumnValueExcludeFilter proto;
    try {
        proto = FilterProtos.SingleColumnValueExcludeFilter.parseFrom(pbBytes);
    } catch (InvalidProtocolBufferException e) {
        throw new DeserializationException(e);
    }
    FilterProtos.SingleColumnValueFilter parentProto = proto.getSingleColumnValueFilter();
    final CompareOp compareOp = CompareOp.valueOf(parentProto.getCompareOp().name());
    final ByteArrayComparable comparator;
    try {
        comparator = ProtobufUtil.toComparator(parentProto.getComparator());
    } catch (IOException ioe) {
        throw new DeserializationException(ioe);
    }
    return new SingleColumnValueExcludeFilter(parentProto.hasColumnFamily() ? parentProto.getColumnFamily().toByteArray() : null, parentProto.hasColumnQualifier() ? parentProto.getColumnQualifier().toByteArray() : null, compareOp, comparator, parentProto.getFilterIfMissing(), parentProto.getLatestVersionOnly());
}
Also used : InvalidProtocolBufferException(org.apache.hadoop.hbase.shaded.com.google.protobuf.InvalidProtocolBufferException) FilterProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp) IOException(java.io.IOException) DeserializationException(org.apache.hadoop.hbase.exceptions.DeserializationException)

Example 12 with CompareOp

use of org.apache.hadoop.hbase.filter.CompareFilter.CompareOp in project drill by apache.

the class MapRDBFilterBuilder method createHBaseScanSpec.

private HBaseScanSpec createHBaseScanSpec(FunctionCall call, CompareFunctionsProcessor processor) {
    String functionName = processor.getFunctionName();
    SchemaPath field = processor.getPath();
    byte[] fieldValue = processor.getValue();
    boolean sortOrderAscending = processor.isSortOrderAscending();
    boolean isRowKey = field.getAsUnescapedPath().equals(ROW_KEY);
    if (!(isRowKey || (!field.getRootSegment().isLastPath() && field.getRootSegment().getChild().isLastPath() && field.getRootSegment().getChild().isNamed()))) {
        /*
       * if the field in this function is neither the row_key nor a qualified HBase column, return.
       */
        return null;
    }
    if (processor.isRowKeyPrefixComparison()) {
        return createRowKeyPrefixScanSpec(call, processor);
    }
    CompareOp compareOp = null;
    boolean isNullTest = false;
    ByteArrayComparable comparator = new BinaryComparator(fieldValue);
    byte[] startRow = HConstants.EMPTY_START_ROW;
    byte[] stopRow = HConstants.EMPTY_END_ROW;
    switch(functionName) {
        case "equal":
            compareOp = CompareOp.EQUAL;
            if (isRowKey) {
                startRow = fieldValue;
                /* stopRow should be just greater than 'value'*/
                stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
                compareOp = CompareOp.EQUAL;
            }
            break;
        case "not_equal":
            compareOp = CompareOp.NOT_EQUAL;
            break;
        case "greater_than_or_equal_to":
            if (sortOrderAscending) {
                compareOp = CompareOp.GREATER_OR_EQUAL;
                if (isRowKey) {
                    startRow = fieldValue;
                }
            } else {
                compareOp = CompareOp.LESS_OR_EQUAL;
                if (isRowKey) {
                    // stopRow should be just greater than 'value'
                    stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
                }
            }
            break;
        case "greater_than":
            if (sortOrderAscending) {
                compareOp = CompareOp.GREATER;
                if (isRowKey) {
                    // startRow should be just greater than 'value'
                    startRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
                }
            } else {
                compareOp = CompareOp.LESS;
                if (isRowKey) {
                    stopRow = fieldValue;
                }
            }
            break;
        case "less_than_or_equal_to":
            if (sortOrderAscending) {
                compareOp = CompareOp.LESS_OR_EQUAL;
                if (isRowKey) {
                    // stopRow should be just greater than 'value'
                    stopRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
                }
            } else {
                compareOp = CompareOp.GREATER_OR_EQUAL;
                if (isRowKey) {
                    startRow = fieldValue;
                }
            }
            break;
        case "less_than":
            if (sortOrderAscending) {
                compareOp = CompareOp.LESS;
                if (isRowKey) {
                    stopRow = fieldValue;
                }
            } else {
                compareOp = CompareOp.GREATER;
                if (isRowKey) {
                    // startRow should be just greater than 'value'
                    startRow = Arrays.copyOf(fieldValue, fieldValue.length + 1);
                }
            }
            break;
        case "isnull":
        case "isNull":
        case "is null":
            if (isRowKey) {
                return null;
            }
            isNullTest = true;
            compareOp = CompareOp.EQUAL;
            comparator = new NullComparator();
            break;
        case "isnotnull":
        case "isNotNull":
        case "is not null":
            if (isRowKey) {
                return null;
            }
            compareOp = CompareOp.NOT_EQUAL;
            comparator = new NullComparator();
            break;
        case "like":
            /*
       * Convert the LIKE operand to Regular Expression pattern so that we can
       * apply RegexStringComparator()
       */
            HBaseRegexParser parser = new HBaseRegexParser(call).parse();
            compareOp = CompareOp.EQUAL;
            comparator = new RegexStringComparator(parser.getRegexString());
            /*
       * We can possibly do better if the LIKE operator is on the row_key
       */
            if (isRowKey) {
                String prefix = parser.getPrefixString();
                if (prefix != null) {
                    /*
           * If there is a literal prefix, it can help us prune the scan to a sub range
           */
                    if (prefix.equals(parser.getLikeString())) {
                        /* The operand value is literal. This turns the LIKE operator to EQUAL operator */
                        startRow = stopRow = fieldValue;
                        compareOp = null;
                    } else {
                        startRow = prefix.getBytes(Charsets.UTF_8);
                        stopRow = startRow.clone();
                        boolean isMaxVal = true;
                        for (int i = stopRow.length - 1; i >= 0; --i) {
                            int nextByteValue = (0xff & stopRow[i]) + 1;
                            if (nextByteValue < 0xff) {
                                stopRow[i] = (byte) nextByteValue;
                                isMaxVal = false;
                                break;
                            } else {
                                stopRow[i] = 0;
                            }
                        }
                        if (isMaxVal) {
                            stopRow = HConstants.EMPTY_END_ROW;
                        }
                    }
                }
            }
            break;
    }
    if (compareOp != null || startRow != HConstants.EMPTY_START_ROW || stopRow != HConstants.EMPTY_END_ROW) {
        Filter filter = null;
        if (isRowKey) {
            if (compareOp != null) {
                filter = new RowFilter(compareOp, comparator);
            }
        } else {
            byte[] family = HBaseUtils.getBytes(field.getRootSegment().getPath());
            byte[] qualifier = HBaseUtils.getBytes(field.getRootSegment().getChild().getNameSegment().getPath());
            filter = new SingleColumnValueFilter(family, qualifier, compareOp, comparator);
            ((SingleColumnValueFilter) filter).setLatestVersionOnly(true);
            if (!isNullTest) {
                ((SingleColumnValueFilter) filter).setFilterIfMissing(true);
            }
        }
        return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, filter);
    }
    // else
    return null;
}
Also used : HBaseScanSpec(org.apache.drill.exec.store.hbase.HBaseScanSpec) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) HBaseRegexParser(org.apache.drill.exec.store.hbase.HBaseRegexParser) NullComparator(org.apache.hadoop.hbase.filter.NullComparator) BinaryComparator(org.apache.hadoop.hbase.filter.BinaryComparator) RegexStringComparator(org.apache.hadoop.hbase.filter.RegexStringComparator) ByteArrayComparable(org.apache.hadoop.hbase.filter.ByteArrayComparable) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) SchemaPath(org.apache.drill.common.expression.SchemaPath) RowFilter(org.apache.hadoop.hbase.filter.RowFilter) Filter(org.apache.hadoop.hbase.filter.Filter) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

Example 13 with CompareOp

use of org.apache.hadoop.hbase.filter.CompareFilter.CompareOp in project gora by apache.

the class DefaultFactory method createFilter.

@Override
public org.apache.hadoop.hbase.filter.Filter createFilter(Filter<K, T> filter, HBaseStore<K, T> store) {
    if (filter instanceof FilterList) {
        FilterList<K, T> filterList = (FilterList<K, T>) filter;
        org.apache.hadoop.hbase.filter.FilterList hbaseFilter = new org.apache.hadoop.hbase.filter.FilterList(Operator.valueOf(filterList.getOperator().name()));
        for (Filter<K, T> rowFitler : filterList.getFilters()) {
            FilterFactory<K, T> factory = getHbaseFitlerUtil().getFactory(rowFitler);
            if (factory == null) {
                LOG.warn("HBase remote filter factory not yet implemented for " + rowFitler.getClass().getCanonicalName());
                return null;
            }
            org.apache.hadoop.hbase.filter.Filter hbaseRowFilter = factory.createFilter(rowFitler, store);
            if (hbaseRowFilter != null) {
                hbaseFilter.addFilter(hbaseRowFilter);
            }
        }
        return hbaseFilter;
    } else if (filter instanceof SingleFieldValueFilter) {
        SingleFieldValueFilter<K, T> fieldFilter = (SingleFieldValueFilter<K, T>) filter;
        HBaseColumn column = store.getMapping().getColumn(fieldFilter.getFieldName());
        CompareOp compareOp = getCompareOp(fieldFilter.getFilterOp());
        byte[] family = column.getFamily();
        byte[] qualifier = column.getQualifier();
        byte[] value = HBaseByteInterface.toBytes(fieldFilter.getOperands().get(0));
        SingleColumnValueFilter hbaseFilter = new SingleColumnValueFilter(family, qualifier, compareOp, value);
        hbaseFilter.setFilterIfMissing(fieldFilter.isFilterIfMissing());
        return hbaseFilter;
    } else if (filter instanceof MapFieldValueFilter) {
        MapFieldValueFilter<K, T> mapFilter = (MapFieldValueFilter<K, T>) filter;
        HBaseColumn column = store.getMapping().getColumn(mapFilter.getFieldName());
        CompareOp compareOp = getCompareOp(mapFilter.getFilterOp());
        byte[] family = column.getFamily();
        byte[] qualifier = HBaseByteInterface.toBytes(mapFilter.getMapKey());
        byte[] value = HBaseByteInterface.toBytes(mapFilter.getOperands().get(0));
        SingleColumnValueFilter hbaseFilter = new SingleColumnValueFilter(family, qualifier, compareOp, value);
        hbaseFilter.setFilterIfMissing(mapFilter.isFilterIfMissing());
        return hbaseFilter;
    } else {
        LOG.warn("HBase remote filter not yet implemented for " + filter.getClass().getCanonicalName());
        return null;
    }
}
Also used : HBaseColumn(org.apache.gora.hbase.store.HBaseColumn) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) FilterList(org.apache.gora.filter.FilterList) SingleFieldValueFilter(org.apache.gora.filter.SingleFieldValueFilter) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp) MapFieldValueFilter(org.apache.gora.filter.MapFieldValueFilter)

Example 14 with CompareOp

use of org.apache.hadoop.hbase.filter.CompareFilter.CompareOp in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(LikeParseNode node, List<Expression> children) throws SQLException {
    ParseNode lhsNode = node.getChildren().get(0);
    ParseNode rhsNode = node.getChildren().get(1);
    Expression lhs = children.get(0);
    Expression rhs = children.get(1);
    if (rhs.getDataType() != null && lhs.getDataType() != null && !lhs.getDataType().isCoercibleTo(rhs.getDataType()) && !rhs.getDataType().isCoercibleTo(lhs.getDataType())) {
        throw TypeMismatchException.newException(lhs.getDataType(), rhs.getDataType(), node.toString());
    }
    if (lhsNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) lhsNode, rhs);
    }
    if (rhsNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) rhsNode, lhs);
    }
    if (rhs instanceof LiteralExpression) {
        String pattern = (String) ((LiteralExpression) rhs).getValue();
        if (pattern == null || pattern.length() == 0) {
            return LiteralExpression.newConstant(null, rhs.getDeterminism());
        }
        // TODO: for pattern of '%' optimize to strlength(lhs) > 0
        // We can't use lhs IS NOT NULL b/c if lhs is NULL we need
        // to return NULL.
        int index = LikeExpression.indexOfWildcard(pattern);
        // Can't possibly be as long as the constant, then FALSE
        Integer lhsMaxLength = lhs.getMaxLength();
        if (lhsMaxLength != null && lhsMaxLength < index) {
            return LiteralExpression.newConstant(false, rhs.getDeterminism());
        }
        if (index == -1) {
            String rhsLiteral = LikeExpression.unescapeLike(pattern);
            if (lhsMaxLength != null && lhsMaxLength != rhsLiteral.length()) {
                return LiteralExpression.newConstant(false, rhs.getDeterminism());
            }
            if (node.getLikeType() == LikeType.CASE_SENSITIVE) {
                CompareOp op = node.isNegate() ? CompareOp.NOT_EQUAL : CompareOp.EQUAL;
                if (pattern.equals(rhsLiteral)) {
                    return new ComparisonExpression(children, op);
                } else {
                    rhs = LiteralExpression.newConstant(rhsLiteral, PChar.INSTANCE, rhs.getDeterminism());
                    return new ComparisonExpression(Arrays.asList(lhs, rhs), op);
                }
            }
        } else {
            byte[] wildcardString = new byte[pattern.length()];
            byte[] wildcard = { StringUtil.MULTI_CHAR_LIKE };
            StringUtil.fill(wildcardString, 0, pattern.length(), wildcard, 0, 1, false);
            if (pattern.equals(new String(wildcardString))) {
                List<Expression> compareChildren = Arrays.asList(lhs, NOT_NULL_STRING);
                return new ComparisonExpression(compareChildren, node.isNegate() ? CompareOp.LESS : CompareOp.GREATER_OR_EQUAL);
            }
        }
    }
    QueryServices services = context.getConnection().getQueryServices();
    boolean useByteBasedRegex = services.getProps().getBoolean(QueryServices.USE_BYTE_BASED_REGEX_ATTRIB, QueryServicesOptions.DEFAULT_USE_BYTE_BASED_REGEX);
    Expression expression;
    if (useByteBasedRegex) {
        expression = ByteBasedLikeExpression.create(children, node.getLikeType());
    } else {
        expression = StringBasedLikeExpression.create(children, node.getLikeType());
    }
    if (ExpressionUtil.isConstant(expression)) {
        ImmutableBytesWritable ptr = context.getTempPtr();
        if (!expression.evaluate(null, ptr)) {
            return LiteralExpression.newConstant(null, expression.getDeterminism());
        } else {
            return LiteralExpression.newConstant(Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(ptr)) ^ node.isNegate(), expression.getDeterminism());
        }
    }
    if (node.isNegate()) {
        expression = new NotExpression(expression);
    }
    return wrapGroupByExpression(expression);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) NotExpression(org.apache.phoenix.expression.NotExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) ArrayAnyComparisonExpression(org.apache.phoenix.expression.function.ArrayAnyComparisonExpression) ArrayAllComparisonExpression(org.apache.phoenix.expression.function.ArrayAllComparisonExpression) 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) 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) QueryServices(org.apache.phoenix.query.QueryServices) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 15 with CompareOp

use of org.apache.hadoop.hbase.filter.CompareFilter.CompareOp in project phoenix by apache.

the class ArrayAllAnyComparisonNode method toSQL.

@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
    List<ParseNode> children = getChildren();
    ParseNode rhs = children.get(0);
    ComparisonParseNode comp = (ComparisonParseNode) children.get(1);
    ParseNode lhs = comp.getLHS();
    CompareOp op = comp.getFilterOp();
    buf.append(' ');
    lhs.toSQL(resolver, buf);
    buf.append(" " + QueryUtil.toSQL(op) + " ");
    buf.append(getType());
    buf.append('(');
    rhs.toSQL(resolver, buf);
    buf.append(')');
}
Also used : CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

Aggregations

CompareOp (org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)15 ByteArrayComparable (org.apache.hadoop.hbase.filter.ByteArrayComparable)5 IOException (java.io.IOException)4 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)4 Expression (org.apache.phoenix.expression.Expression)4 BinaryComparator (org.apache.hadoop.hbase.filter.BinaryComparator)3 RegexStringComparator (org.apache.hadoop.hbase.filter.RegexStringComparator)3 KeyPart (org.apache.phoenix.compile.KeyPart)3 PDataType (org.apache.phoenix.schema.types.PDataType)3 InterruptedIOException (java.io.InterruptedIOException)2 List (java.util.List)2 SchemaPath (org.apache.drill.common.expression.SchemaPath)2 CellScanner (org.apache.hadoop.hbase.CellScanner)2 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)2 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)2 DeserializationException (org.apache.hadoop.hbase.exceptions.DeserializationException)2 Filter (org.apache.hadoop.hbase.filter.Filter)2 NullComparator (org.apache.hadoop.hbase.filter.NullComparator)2 RowFilter (org.apache.hadoop.hbase.filter.RowFilter)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2