|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.phoenix.expression.function; |
| 19 | + |
| 20 | +import org.apache.hadoop.hbase.Cell; |
| 21 | +import org.apache.hadoop.hbase.CellUtil; |
| 22 | +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; |
| 23 | +import org.apache.phoenix.expression.Determinism; |
| 24 | +import org.apache.phoenix.expression.Expression; |
| 25 | +import org.apache.phoenix.expression.KeyValueColumnExpression; |
| 26 | +import org.apache.phoenix.parse.FunctionParseNode; |
| 27 | +import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction; |
| 28 | +import org.apache.phoenix.parse.DecodeViewIndexIdParseNode; |
| 29 | +import org.apache.phoenix.parse.PhoenixRowTimestampParseNode; |
| 30 | +import org.apache.phoenix.schema.SortOrder; |
| 31 | +import org.apache.phoenix.schema.tuple.Tuple; |
| 32 | +import org.apache.phoenix.schema.types.PDataType; |
| 33 | +import org.apache.phoenix.schema.types.PInteger; |
| 34 | +import org.apache.phoenix.schema.types.PLong; |
| 35 | +import org.apache.phoenix.schema.types.PSmallint; |
| 36 | + |
| 37 | +import java.sql.Types; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +import static org.apache.phoenix.util.ViewIndexIdRetrieveUtil.NULL_DATA_TYPE_VALUE; |
| 41 | +import static org.apache.phoenix.util.ViewIndexIdRetrieveUtil.VIEW_INDEX_ID_BIGINT_TYPE_PTR_LEN; |
| 42 | + |
| 43 | +/** |
| 44 | + * Function to return the timestamp of the empty column which functions as the row timestamp. The |
| 45 | + * result returned can be used for debugging(eg. using HBase shell), logging etc. |
| 46 | + * Can also be used in sql predicates. |
| 47 | + */ |
| 48 | +@BuiltInFunction(name = DecodeViewIndexIdFunction.NAME, |
| 49 | + nodeClass= DecodeViewIndexIdParseNode.class, |
| 50 | + args = {@FunctionParseNode.Argument(allowedTypes = { PLong.class}), |
| 51 | + @FunctionParseNode.Argument(allowedTypes = { PInteger.class}) |
| 52 | + }) |
| 53 | +public class DecodeViewIndexIdFunction extends ScalarFunction { |
| 54 | + |
| 55 | + public static final String NAME = "DECODE_VIEW_INDEX_ID"; |
| 56 | + |
| 57 | + public DecodeViewIndexIdFunction() { |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param children An EMPTY_COLUMN key value expression injected thru |
| 62 | + * {@link PhoenixRowTimestampParseNode#create create} |
| 63 | + * will cause the empty column key value to be evaluated during scan filter processing. |
| 64 | + */ |
| 65 | + public DecodeViewIndexIdFunction(List<Expression> children) { |
| 66 | + super(children); |
| 67 | + |
| 68 | + // It takes 2 parameters - VIEW_INDEX_ID, VIEW_INDEX_ID_DATA_TYPE. |
| 69 | + if ((children.size() != 2) || !children.get(0).getClass().isAssignableFrom( |
| 70 | + KeyValueColumnExpression.class) || !children.get(1).getClass().isAssignableFrom( |
| 71 | + KeyValueColumnExpression.class)) { |
| 72 | + throw new IllegalArgumentException( |
| 73 | + "DecodeViewIndexIdFunction should only have a " |
| 74 | + + "VIEW_INDEX_ID and a VIEW_INDEX_ID_DATA_TYPE key value expression." |
| 75 | + ); |
| 76 | + } |
| 77 | + if (!(children.get(0).getDataType().equals(PLong.INSTANCE))) { |
| 78 | + throw new IllegalArgumentException( |
| 79 | + "DecodeViewIndexIdFunction should have an " |
| 80 | + + "VIEW_INDEX_ID key value expression of type PLong" |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (!(children.get(1).getDataType().equals(PInteger.INSTANCE))) { |
| 85 | + throw new IllegalArgumentException( |
| 86 | + "DecodeViewIndexIdFunction should have an " |
| 87 | + + "VIEW_INDEX_ID_DATA_TYPE key value expression of type PLong" |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getName() { |
| 94 | + return NAME; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { |
| 99 | + if (tuple == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + byte[] viewIndexIdCF = ((KeyValueColumnExpression) children.get(0)).getColumnFamily(); |
| 104 | + byte[] viewIndexIdCQ = ((KeyValueColumnExpression) children.get(0)).getColumnQualifier(); |
| 105 | + byte[] viewIndexIdTypeCF = ((KeyValueColumnExpression) children.get(1)).getColumnFamily(); |
| 106 | + byte[] viewIndexIdTypeCQ = ((KeyValueColumnExpression) children.get(1)).getColumnQualifier(); |
| 107 | + |
| 108 | + Cell viewIndexIdCell = tuple.getValue(viewIndexIdCF, viewIndexIdCQ); |
| 109 | + Cell viewIndexIdDataTypeCell = tuple.getValue(viewIndexIdTypeCF, viewIndexIdTypeCQ); |
| 110 | + |
| 111 | + |
| 112 | + /* |
| 113 | + This is combination of diff client created view index looks like: |
| 114 | + client VIEW_INDEX_ID(Cell number of bytes) VIEW_INDEX_ID_DATA_TYPE |
| 115 | + pre-4.15 2 bytes NULL |
| 116 | + post-4.15[config smallint] 2 bytes 5(smallint) |
| 117 | + post-4.15[config bigint] 8 bytes -5(bigint) |
| 118 | +
|
| 119 | + VIEW_INDEX_ID_DATA_TYPE, VIEW_INDEX_ID(Cell representation of the data) |
| 120 | + NULL, SMALLINT -> RETRIEVE AND CONVERT TO BIGINT |
| 121 | + SMALLINT, SMALLINT -> RETRIEVE AND CONVERT TO BIGINT |
| 122 | + BIGINT, BIGINT -> DO NOT CONVERT |
| 123 | +
|
| 124 | + */ |
| 125 | + |
| 126 | + if (viewIndexIdCell != null) { |
| 127 | + int type = NULL_DATA_TYPE_VALUE; |
| 128 | + if (viewIndexIdDataTypeCell != null) { |
| 129 | + type = (Integer) PInteger.INSTANCE.toObject( |
| 130 | + viewIndexIdDataTypeCell.getValueArray(), |
| 131 | + viewIndexIdDataTypeCell.getValueOffset(), |
| 132 | + viewIndexIdDataTypeCell.getValueLength(), |
| 133 | + PInteger.INSTANCE, |
| 134 | + SortOrder.ASC); |
| 135 | + } |
| 136 | + |
| 137 | + System.out.println("DecodeViewIndexIdFunction: Type: " + type); |
| 138 | + ImmutableBytesWritable columnValue = |
| 139 | + new ImmutableBytesWritable(CellUtil.cloneValue(viewIndexIdCell)); |
| 140 | + if ((type == NULL_DATA_TYPE_VALUE || type == Types.SMALLINT) && (viewIndexIdCell.getValueLength() < |
| 141 | + VIEW_INDEX_ID_BIGINT_TYPE_PTR_LEN)) { |
| 142 | + byte[] newBytes = PLong.INSTANCE.toBytes(PSmallint.INSTANCE.toObject(columnValue.get())); |
| 143 | + ptr.set(newBytes, 0, newBytes.length); |
| 144 | + } else { |
| 145 | + ptr.set(columnValue.get(), columnValue.getOffset(), columnValue.getLength()); |
| 146 | + } |
| 147 | + } |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public PDataType getDataType() { |
| 153 | + return PLong.INSTANCE; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean isStateless() { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Determinism getDeterminism() { |
| 163 | + return Determinism.PER_ROW; |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments