Search in sources :

Example 21 with LiteralExpression

use of org.apache.phoenix.expression.LiteralExpression 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 22 with LiteralExpression

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

the class ListJarsQueryPlan method iterator.

@Override
public ResultIterator iterator(ParallelScanGrouper scanGrouper) throws SQLException {
    return new ResultIterator() {

        private RemoteIterator<LocatedFileStatus> listFiles = null;

        @Override
        public void close() throws SQLException {
        }

        @Override
        public Tuple next() throws SQLException {
            try {
                if (first) {
                    String dynamicJarsDir = stmt.getConnection().getQueryServices().getProps().get(QueryServices.DYNAMIC_JARS_DIR_KEY);
                    if (dynamicJarsDir == null) {
                        throw new SQLException(QueryServices.DYNAMIC_JARS_DIR_KEY + " is not configured for the listing the jars.");
                    }
                    dynamicJarsDir = dynamicJarsDir.endsWith("/") ? dynamicJarsDir : dynamicJarsDir + '/';
                    Configuration conf = HBaseFactoryProvider.getConfigurationFactory().getConfiguration();
                    Path dynamicJarsDirPath = new Path(dynamicJarsDir);
                    FileSystem fs = dynamicJarsDirPath.getFileSystem(conf);
                    listFiles = fs.listFiles(dynamicJarsDirPath, true);
                    first = false;
                }
                if (listFiles == null || !listFiles.hasNext())
                    return null;
                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
                ParseNodeFactory factory = new ParseNodeFactory();
                LiteralParseNode literal = factory.literal(listFiles.next().getPath().toString());
                LiteralExpression expression = LiteralExpression.newConstant(literal.getValue(), PVarchar.INSTANCE, Determinism.ALWAYS);
                expression.evaluate(null, ptr);
                byte[] rowKey = ByteUtil.copyKeyBytesIfNecessary(ptr);
                Cell cell = CellUtil.createCell(rowKey, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, System.currentTimeMillis(), Type.Put.getCode(), HConstants.EMPTY_BYTE_ARRAY);
                List<Cell> cells = new ArrayList<Cell>(1);
                cells.add(cell);
                return new ResultTuple(Result.create(cells));
            } catch (IOException e) {
                throw new SQLException(e);
            }
        }

        @Override
        public void explain(List<String> planSteps) {
        }
    };
}
Also used : Path(org.apache.hadoop.fs.Path) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Configuration(org.apache.hadoop.conf.Configuration) SQLException(java.sql.SQLException) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) ResultIterator(org.apache.phoenix.iterate.ResultIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) RemoteIterator(org.apache.hadoop.fs.RemoteIterator) FileSystem(org.apache.hadoop.fs.FileSystem) List(java.util.List) ArrayList(java.util.ArrayList) Cell(org.apache.hadoop.hbase.Cell) ParseNodeFactory(org.apache.phoenix.parse.ParseNodeFactory)

Example 23 with LiteralExpression

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

the class CreateIndexCompiler method compile.

public MutationPlan compile(final CreateIndexStatement create) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    final ColumnResolver resolver = FromCompiler.getResolver(create, connection, create.getUdfParseNodes());
    Scan scan = new Scan();
    final StatementContext context = new StatementContext(statement, resolver, scan, new SequenceManager(statement));
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
    List<ParseNode> splitNodes = create.getSplitNodes();
    if (create.getIndexType() == IndexType.LOCAL) {
        if (!splitNodes.isEmpty()) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SPLIT_LOCAL_INDEX).build().buildException();
        }
        List<Pair<String, Object>> list = create.getProps() != null ? create.getProps().get("") : null;
        if (list != null) {
            for (Pair<String, Object> pair : list) {
                if (pair.getFirst().equals(PhoenixDatabaseMetaData.SALT_BUCKETS)) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SALT_LOCAL_INDEX).build().buildException();
                }
            }
        }
    }
    final byte[][] splits = new byte[splitNodes.size()][];
    for (int i = 0; i < splits.length; i++) {
        ParseNode node = splitNodes.get(i);
        if (!node.isStateless()) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.SPLIT_POINT_NOT_CONSTANT).setMessage("Node: " + node).build().buildException();
        }
        LiteralExpression expression = (LiteralExpression) node.accept(expressionCompiler);
        splits[i] = expression.getBytes();
    }
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            return client.createIndex(create, splits);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE INDEX"));
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ParseNode(org.apache.phoenix.parse.ParseNode) Scan(org.apache.hadoop.hbase.client.Scan) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) Pair(org.apache.hadoop.hbase.util.Pair)

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