org.apache.phoenix.query.QueryConstants Java Examples

The following examples show how to use org.apache.phoenix.query.QueryConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpgradeUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static void updateIndexesSequenceIfPresent(PhoenixConnection connection, PTable dataTable)
        throws SQLException {
    PName tenantId = connection.getTenantId();
    PName physicalName = dataTable.getPhysicalName();
    PName oldPhysicalName = PNameFactory.newName(
            physicalName.toString().replace(QueryConstants.NAMESPACE_SEPARATOR, QueryConstants.NAME_SEPARATOR));
    String oldSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(oldPhysicalName, false);
    String newSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(physicalName, true);
    String newSequenceName = MetaDataUtil.getViewIndexSequenceName(physicalName, tenantId, true);
    // create new entry with new schema format
    String upsert = "UPSERT INTO " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " SELECT NULL,\'" + newSchemaName +
        "\',\'" + newSequenceName
            + "\'," + START_WITH + "," + CURRENT_VALUE + "," + INCREMENT_BY + "," + CACHE_SIZE + "," + MIN_VALUE
            + "," + MAX_VALUE + "," + CYCLE_FLAG + "," + LIMIT_REACHED_FLAG + " FROM "
            + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + PhoenixDatabaseMetaData.TENANT_ID
            + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + oldSchemaName + "'";
    connection.createStatement().executeUpdate(upsert);
}
 
Example #2
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testRVCExpressionThroughOr() throws SQLException {
    String tenantId =  "000000000000001";
    String entityId =  "002333333333331";
    String entityId1 = "002333333333330";
    String entityId2 = "002333333333332";
    String query = "select * from atable where (organization_id,entity_id) >= (?,?) and organization_id = ? and  (entity_id = ? or entity_id = ?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, entityId, tenantId, entityId1, entityId2);
    StatementContext context = compileStatement(query, binds);
    Scan scan = context.getScan();
    byte[] expectedStartRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(entityId1));
    byte[] expectedStopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(entityId2), QueryConstants.SEPARATOR_BYTE_ARRAY);
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(expectedStopRow, scan.getStopRow());
    Filter filter = scan.getFilter();
    assertTrue(filter instanceof SkipScanFilter);
    SkipScanFilter skipScanFilter = (SkipScanFilter)filter;
    List<List<KeyRange>> skipScanRanges = Arrays.asList(
            Arrays.asList(KeyRange.getKeyRange(ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(entityId1))),
                          KeyRange.getKeyRange(ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId), PVarchar.INSTANCE.toBytes(entityId2)))));
    assertEquals(skipScanRanges, skipScanFilter.getSlots());
}
 
Example #3
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testBoundaryLessThanOrEqualRound() throws Exception {
    String inst = "a";
    String host = "b";
    Date startDate = DateUtil.parseDate("2012-01-01 00:00:00");
    Date endDate = DateUtil.parseDate("2012-01-02 00:00:00");
    String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<=?";
    List<Object> binds = Arrays.<Object>asList(inst,host,startDate);
    Scan scan = compileStatement(query, binds).getScan();

    assertNull(scan.getFilter());
    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PDate.INSTANCE.toBytes(endDate));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example #4
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testBoundaryLessThanRound() throws Exception {
    String inst = "a";
    String host = "b";
    Date startDate = DateUtil.parseDate("2012-01-01 00:00:00");
    Date endDate = DateUtil.parseDate("2012-01-01 00:00:00");
    String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<?";
    List<Object> binds = Arrays.<Object>asList(inst,host,startDate);
    Scan scan = compileStatement(query, binds).getScan();
    assertNull(scan.getFilter());

    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PDate.INSTANCE.toBytes(endDate));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example #5
Source File: QueryCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public QueryCompiler(PhoenixStatement statement, SelectStatement select, ColumnResolver resolver, List<? extends PDatum> targetColumns, ParallelIteratorFactory parallelIteratorFactory, SequenceManager sequenceManager) throws SQLException {
    this.statement = statement;
    this.select = select;
    this.resolver = resolver;
    this.scan = new Scan();
    this.targetColumns = targetColumns;
    this.parallelIteratorFactory = parallelIteratorFactory;
    this.sequenceManager = sequenceManager;
    this.useSortMergeJoin = select.getHint().hasHint(Hint.USE_SORT_MERGE_JOIN);
    this.noChildParentJoinOptimization = select.getHint().hasHint(Hint.NO_CHILD_PARENT_JOIN_OPTIMIZATION);
    if (statement.getConnection().getQueryServices().getLowestClusterHBaseVersion() >= PhoenixDatabaseMetaData.ESSENTIAL_FAMILY_VERSION_THRESHOLD) {
        this.scan.setAttribute(LOAD_COLUMN_FAMILIES_ON_DEMAND_ATTR, QueryConstants.TRUE);
    }
    if (select.getHint().hasHint(Hint.NO_CACHE)) {
        scan.setCacheBlocks(false);
    }
    scan.setCaching(statement.getFetchSize());

    this.originalScan = ScanUtil.newScan(scan);
}
 
Example #6
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualRound() throws Exception {
    String inst = "a";
    String host = "b";
    Date startDate = DateUtil.parseDate("2012-01-01 00:00:00");
    Date endDate = DateUtil.parseDate("2012-01-02 00:00:00");
    String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')=?";
    List<Object> binds = Arrays.<Object>asList(inst,host,startDate);
    Scan scan = compileStatement(query, binds).getScan();
    assertNull(scan.getFilter());

    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PDate.INSTANCE.toBytes(startDate));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PDate.INSTANCE.toBytes(endDate));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example #7
Source File: DateTimeIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateAddCompareDate() throws Exception {
    String tablename=generateUniqueName();
    String tenantId = getOrganizationId();
    String query = "SELECT feature FROM "+tablename+" WHERE organization_id = ? and date + 1 >= ?";
    Connection conn = DriverManager.getConnection(url);
    try {
        Date startDate = new Date(System.currentTimeMillis());
        Date endDate = new Date(startDate.getTime() + 8 * QueryConstants.MILLIS_IN_DAY);
        initDateTableValues(tablename, tenantId, getSplits(tenantId), startDate);
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        statement.setDate(2, endDate);
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        assertEquals("E", rs.getString(1));
        assertTrue(rs.next());
        assertEquals("F", rs.getString(1));
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example #8
Source File: ParameterizedTransactionIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCommitQuery() throws Exception {
    String transTableName = generateUniqueName();
    String fullTableName = INDEX_DATA_SCHEMA + QueryConstants.NAME_SEPARATOR + transTableName;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute("create table " + fullTableName + TestUtil.TEST_TABLE_SCHEMA + tableDDLOptions + (tableDDLOptions.length() > 0 ? "," : "") + "TRANSACTIONAL=true");
        conn.setAutoCommit(true);
        // verify no rows returned with single table
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM " + fullTableName);
        assertFalse(rs.next());

        // verify no rows returned with multiple tables
        rs = conn.createStatement().executeQuery("SELECT * FROM " + fullTableName + " x JOIN " + fullTableName + " y ON (x.long_pk = y.int_pk)");
        assertFalse(rs.next());
    } 
}
 
Example #9
Source File: TableRef.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public String getColumnDisplayName(ColumnRef ref, boolean cfCaseSensitive, boolean cqCaseSensitive) {
    String cf = null;
    String cq = null;       
    PColumn column = ref.getColumn();
    String name = column.getName().getString();
    boolean isIndex = IndexUtil.isIndexColumn(name);
    if ((table.getType() == PTableType.PROJECTED && TupleProjectionCompiler.PROJECTED_TABLE_SCHEMA.equals(table.getSchemaName()))
            || table.getType() == PTableType.SUBQUERY) {
        cq = name;
    }
    else if (SchemaUtil.isPKColumn(column)) {
        cq = isIndex ? IndexUtil.getDataColumnName(name) : name;
    }
    else {
        String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
        // Translate to the data table column name
        String dataFamilyName = isIndex ? IndexUtil.getDataColumnFamilyName(name) : column.getFamilyName().getString() ;
        cf = (table.getIndexType()==IndexType.LOCAL? IndexUtil.getActualColumnFamilyName(defaultFamilyName):defaultFamilyName).equals(dataFamilyName) ? null : dataFamilyName;
        cq = isIndex ? IndexUtil.getDataColumnName(name) : name;
    }
    
    cf = (cf!=null && cfCaseSensitive) ? "\"" + cf + "\"" : cf;
    cq = cqCaseSensitive ? "\"" + cq + "\"" : cq;
    return SchemaUtil.getColumnDisplayName(cf, cq);
}
 
Example #10
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, QueryConstants.EMPTY_COLUMN_BYTES.length, 0);
	return rowSize;
}
 
Example #11
Source File: DateTimeIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateSubtractionCompareDate() throws Exception {
    String tablename=generateUniqueName();
    String tenantId = getOrganizationId();
    String query = "SELECT feature FROM "+tablename+" WHERE organization_id = ? and date - 1 >= ?";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        Date startDate = new Date(System.currentTimeMillis());
        Date endDate = new Date(startDate.getTime() + 9 * QueryConstants.MILLIS_IN_DAY);
        initDateTableValues(tablename, tenantId, getSplits(tenantId), startDate);
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        statement.setDate(2, endDate);
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        assertEquals("F", rs.getString(1));
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example #12
Source File: WhereCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testRowKeyFilter() throws SQLException {
    String keyPrefix = "foo";
    String query = "select * from atable where substr(entity_id,1,3)=?";
    List<Object> binds = Arrays.<Object>asList(keyPrefix);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    bindParams(pstmt, binds);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();

    assertEquals(
        new RowKeyComparisonFilter(
            constantComparison(CompareOp.EQUAL,
                new SubstrFunction(
                    Arrays.<Expression>asList(
                        new RowKeyColumnExpression(ENTITY_ID,new RowKeyValueAccessor(ATABLE.getPKColumns(),1)),
                        LiteralExpression.newConstant(1),
                        LiteralExpression.newConstant(3))
                    ),
                keyPrefix),
            QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES),
        filter);
}
 
Example #13
Source File: ProductMetricsIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testTruncateNotTraversableToFormScanKey() throws Exception {
    String tablename=generateUniqueName();
    String tenantId = getOrganizationId();
    String query = "SELECT feature FROM "+tablename+" WHERE organization_id = ? and TRUNC(\"DATE\",'DAY') <= ?";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        Date startDate = toDate("2013-01-01 00:00:00");
        initDateTableValues(tablename, tenantId, getSplits(tenantId), startDate, 0.5);
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        statement.setDate(2, new Date(startDate.getTime() + (long)(QueryConstants.MILLIS_IN_DAY * 0.25)));
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        assertEquals("A", rs.getString(1));
        assertTrue(rs.next());
        assertEquals("B", rs.getString(1));
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example #14
Source File: TableResultIterator.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public TableResultIterator(MutationState mutationState, Scan scan, ScanMetricsHolder scanMetricsHolder,
        long renewLeaseThreshold, QueryPlan plan, ParallelScanGrouper scanGrouper,Map<ImmutableBytesPtr,ServerCache> caches) throws SQLException {
    this.scan = scan;
    this.scanMetricsHolder = scanMetricsHolder;
    this.plan = plan;
    PTable table = plan.getTableRef().getTable();
    htable = mutationState.getHTable(table);
    this.scanIterator = UNINITIALIZED_SCANNER;
    this.renewLeaseThreshold = renewLeaseThreshold;
    this.scanGrouper = scanGrouper;
    this.hashCacheClient = new HashCacheClient(plan.getContext().getConnection());
    this.caches = caches;
    this.retry=plan.getContext().getConnection().getQueryServices().getProps()
            .getInt(QueryConstants.HASH_JOIN_CACHE_RETRIES, QueryConstants.DEFAULT_HASH_JOIN_CACHE_RETRIES);
    IndexUtil.setScanAttributesForIndexReadRepair(scan, table, plan.getContext().getConnection());
}
 
Example #15
Source File: ScanUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static byte[] nextKey(byte[] key, RowKeySchema schema, ImmutableBytesWritable ptr) {
    int pos = 0;
    int maxOffset = schema.iterator(key, ptr);
    while (schema.next(ptr, pos, maxOffset) != null) {
        pos++;
    }
    if (!schema.getField(pos-1).getDataType().isFixedWidth()) {
        byte[] newLowerRange = new byte[key.length + 1];
        System.arraycopy(key, 0, newLowerRange, 0, key.length);
        newLowerRange[key.length] = QueryConstants.SEPARATOR_BYTE;
        key = newLowerRange;
    } else {
        key = Arrays.copyOf(key, key.length);
    }
    ByteUtil.nextKey(key, key.length);
    return key;
}
 
Example #16
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column info for the given column for the given table.
 *
 * @param table
 * @param columnName User-specified column name. May be family-qualified or bare.
 * @return columnInfo associated with the column in the table
 * @throws SQLException if parameters are null or if column is not found or if column is ambiguous.
 */
public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    PColumn pColumn = null;
    if (columnName.contains(QueryConstants.NAME_SEPARATOR)) {
        String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX);
        if (tokens.length!=2) {
            throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName));
        }
        String familyName = tokens[0];
        String familyColumn = tokens[1];
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(familyColumn);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return getColumnInfo(pColumn);
}
 
Example #17
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testLessThanOrEqualRound() throws Exception {
    String inst = "a";
    String host = "b";
    Date startDate = DateUtil.parseDate("2012-01-01 01:00:00");
    Date endDate = DateUtil.parseDate("2012-01-02 00:00:00");
    String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<=?";
    List<Object> binds = Arrays.<Object>asList(inst,host,startDate);
    Scan scan = compileStatement(query, binds).getScan();
    assertNull(scan.getFilter());

    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY,
            PDate.INSTANCE.toBytes(endDate));
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example #18
Source File: UpdateCacheConnectionLevelPropIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Test 'Update Cache Frequency' property when it is set at table-level only, and not at
 * connection-level.
 */
@Test
public void testWithTableLevelUCFNoConnLevelUCF() throws Exception {
    final long tableUpdateCacheFrequency = 1000;
    String fullTableName = DEFAULT_SCHEMA_NAME + QueryConstants.NAME_SEPARATOR +
            generateUniqueName();

    // There should only be a single call to getTable() for fetching the table's metadata
    int numExecutions = 2;
    int numExpectedGetTableCalls = 1;
    setUpTableAndConnections(fullTableName, String.valueOf(tableUpdateCacheFrequency), null);
    verifyExpectedGetTableCalls(fullTableName, numExecutions, numExpectedGetTableCalls);

    // Wait for a period of 'tableUpdateCacheFrequency' and verify that there was one new call
    // to getTable() for fetching the table's metadata
    Thread.sleep(tableUpdateCacheFrequency);
    verifyExpectedGetTableCalls(fullTableName, numExecutions, numExpectedGetTableCalls);
}
 
Example #19
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static String getTableNameFromFullName(byte[] tableName) {
    if (tableName == null) {
        return null;
    }
    if (isExistingTableMappedToPhoenixName(Bytes.toString(tableName))) { return Bytes.toString(tableName); }
    int index = indexOf(tableName, QueryConstants.NAME_SEPARATOR_BYTE);
    if (index < 0) {
        index = indexOf(tableName, QueryConstants.NAMESPACE_SEPARATOR_BYTE);
        if (index < 0) { return Bytes.toString(tableName); }
    }
    return Bytes.toString(tableName, index+1, tableName.length - index - 1);
}
 
Example #20
Source File: QueryCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnlyNullInScanKey() throws Exception {
    // Select columns in PK
    String query = "select val from ptsdb where inst is null";
    List<Object> binds = Collections.emptyList();
    Scan scan = compileQuery(query, binds);
    // Projects column family with not null column
    assertEquals(1,scan.getFamilyMap().keySet().size());
    assertArrayEquals(Bytes.toBytes(SchemaUtil.normalizeIdentifier(QueryConstants.DEFAULT_COLUMN_FAMILY)), scan.getFamilyMap().keySet().iterator().next());
}
 
Example #21
Source File: SequenceRegionObserver.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static Result getErrorResult(byte[] row, long timestamp, int errorCode) {
    byte[] errorCodeBuf = new byte[PInteger.INSTANCE.getByteSize()];
    PInteger.INSTANCE.getCodec().encodeInt(errorCode, errorCodeBuf, 0);
    return  Result.create(Collections.singletonList(
            PhoenixKeyValueUtil.newKeyValue(row, 
                    PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, 
                    QueryConstants.EMPTY_COLUMN_BYTES, timestamp, errorCodeBuf)));
}
 
Example #22
Source File: IndexMetadataIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlterIndexWithLowerCaseName() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    conn.setAutoCommit(false);
    String indexName = "\"lowerCaseIndex\"";
    String indexDataTable = generateUniqueName();
    try {
        String fullTableName = INDEX_DATA_SCHEMA + QueryConstants.NAME_SEPARATOR + indexDataTable;
        conn.createStatement().execute("create table " + fullTableName + TestUtil.TEST_TABLE_SCHEMA + "IMMUTABLE_ROWS=true");
        String ddl = "CREATE INDEX " + indexName + " ON " + fullTableName
                + " (char_col1 ASC, int_col2 ASC, long_col2 DESC)"
                + " INCLUDE (int_col1)";
        conn.createStatement().execute(ddl);

        ddl = "ALTER INDEX " + indexName + " ON " + INDEX_DATA_SCHEMA + QueryConstants.NAME_SEPARATOR + indexDataTable + " UNUSABLE";
        conn.createStatement().execute(ddl);
        // Verify the metadata for index is correct.
        ResultSet rs = conn.getMetaData().getTables(null, StringUtil.escapeLike(INDEX_DATA_SCHEMA), "lowerCaseIndex", new String[] {PTableType.INDEX.toString()});
        assertTrue(rs.next());
        assertEquals("lowerCaseIndex", rs.getString(3));
        
        ddl = "DROP INDEX " + indexName + " ON " + INDEX_DATA_SCHEMA + QueryConstants.NAME_SEPARATOR + indexDataTable;
        conn.createStatement().execute(ddl);
        
        // Assert the rows for index table is completely removed.
        rs = conn.getMetaData().getIndexInfo(null, INDEX_DATA_SCHEMA, indexDataTable, false, false);
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example #23
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void writeNewOffsets(byte[] arrayBytes, byte[] newArray, boolean useShortNew,
        boolean useShortPrevious, int newOffsetArrayPosition, int arrayLength, int offsetArrayPosition, int offset,
        int offsetShift, int length) {
    int currentPosition = newOffsetArrayPosition;
    int offsetArrayElementSize = useShortNew ? Bytes.SIZEOF_SHORT : Bytes.SIZEOF_INT;
    if (useShortNew) {
        Bytes.putShort(newArray, currentPosition, (short)(0 - Short.MAX_VALUE));
    } else {
        Bytes.putInt(newArray, currentPosition, 0);
    }

    currentPosition += offsetArrayElementSize;
    boolean nullsAtBeginning = true;
    byte serializationVersion = arrayBytes[offset + length - Bytes.SIZEOF_BYTE];
    for (int arrayIndex = 0; arrayIndex < arrayLength - 1; arrayIndex++) {
        int oldOffset = getOffset(arrayBytes, arrayIndex, useShortPrevious, offsetArrayPosition + offset, serializationVersion);
        if (arrayBytes[offset + oldOffset] == QueryConstants.SEPARATOR_BYTE && nullsAtBeginning) {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset);
            }
        } else {
            if (useShortNew) {
                Bytes.putShort(newArray, currentPosition, (short)(oldOffset + offsetShift - Short.MAX_VALUE));
            } else {
                Bytes.putInt(newArray, currentPosition, oldOffset + offsetShift);
            }
            nullsAtBeginning = false;
        }
        currentPosition += offsetArrayElementSize;
    }

    Bytes.putInt(newArray, currentPosition, newOffsetArrayPosition);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putInt(newArray, currentPosition, useShortNew ? arrayLength : -arrayLength);
    currentPosition += Bytes.SIZEOF_INT;
    Bytes.putByte(newArray, currentPosition, arrayBytes[offset + length - 1]);
}
 
Example #24
Source File: PTableKey.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public PTableKey(PName tenantId, String name) {
    Preconditions.checkNotNull(name);
    this.tenantId = tenantId;
    if (name.indexOf(QueryConstants.NAMESPACE_SEPARATOR) != -1) {
        this.name = name.replace(QueryConstants.NAMESPACE_SEPARATOR, QueryConstants.NAME_SEPARATOR);
    } else {
        this.name = name;
    }
}
 
Example #25
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean hasLocalIndexColumnFamily(TableDescriptor desc) {
    for (ColumnFamilyDescriptor cf : desc.getColumnFamilies()) {
        if (cf.getNameAsString().startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
            return true;
        }
    }
    return false;
}
 
Example #26
Source File: WhereOptimizerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescDecimalRange() throws SQLException {
    String ddl = "create table t (k1 bigint not null, k2 decimal, constraint pk primary key (k1,k2 desc))";
    Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES));
    conn.createStatement().execute(ddl);
    String query = "select * from t where k1 in (1,2) and k2>1.0";
    Scan scan = compileStatement(query).getScan();

    byte[] startRow = ByteUtil.concat(PLong.INSTANCE.toBytes(1), ByteUtil.nextKey(QueryConstants.SEPARATOR_BYTE_ARRAY), QueryConstants.DESC_SEPARATOR_BYTE_ARRAY);
    byte[] upperValue = PDecimal.INSTANCE.toBytes(BigDecimal.valueOf(1.0));
    byte[] stopRow = ByteUtil.concat(PLong.INSTANCE.toBytes(2), SortOrder.invert(upperValue,0,upperValue.length), QueryConstants.DESC_SEPARATOR_BYTE_ARRAY);
    assertTrue(scan.getFilter() instanceof SkipScanFilter);
    assertArrayEquals(startRow, scan.getStartRow());
    assertArrayEquals(stopRow, scan.getStopRow());
}
 
Example #27
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Scan newTableRowsScan(byte[] key, long startTimeStamp, long stopTimeStamp)
        throws IOException {
    Scan scan = new Scan();
    scan.setTimeRange(startTimeStamp, stopTimeStamp);
    scan.setStartRow(key);
    byte[] stopKey = ByteUtil.concat(key, QueryConstants.SEPARATOR_BYTE_ARRAY);
    ByteUtil.nextKey(stopKey, stopKey.length);
    scan.setStopRow(stopKey);
    return scan;
}
 
Example #28
Source File: StringIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryPadding() throws Exception {
    ResultSet rs;
    Connection conn = DriverManager.getConnection(getUrl());
    String tableName = generateUniqueName();
    conn.createStatement().execute("CREATE TABLE " + tableName + " (k BINARY(3) PRIMARY KEY)");
    conn.createStatement().execute("UPSERT INTO " + tableName + " VALUES('a')");
    conn.createStatement().execute("UPSERT INTO " + tableName + " VALUES('ab')");
    conn.commit();
    rs = conn.createStatement().executeQuery("SELECT * FROM " + tableName + " ORDER BY k");
    assertTrue(rs.next());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("a"), QueryConstants.SEPARATOR_BYTE_ARRAY, QueryConstants.SEPARATOR_BYTE_ARRAY), rs.getBytes(1));
    assertTrue(rs.next());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("ab"), QueryConstants.SEPARATOR_BYTE_ARRAY), rs.getBytes(1));
    assertFalse(rs.next());

    String tableNameDesc = generateUniqueName();
    conn.createStatement().execute("CREATE TABLE " +  tableNameDesc + " (k BINARY(3) PRIMARY KEY DESC)");
    conn.createStatement().execute("UPSERT INTO " + tableNameDesc + " VALUES('a')");
    conn.createStatement().execute("UPSERT INTO " + tableNameDesc + " VALUES('ab')");
    conn.commit();
    rs = conn.createStatement().executeQuery("SELECT * FROM " + tableNameDesc + " ORDER BY k DESC");
    assertTrue(rs.next());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("ab"), QueryConstants.SEPARATOR_BYTE_ARRAY), rs.getBytes(1));
    assertTrue(rs.next());
    assertArrayEquals(ByteUtil.concat(Bytes.toBytes("a"), QueryConstants.SEPARATOR_BYTE_ARRAY, QueryConstants.SEPARATOR_BYTE_ARRAY), rs.getBytes(1));
    assertFalse(rs.next());
}
 
Example #29
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static String getEscapedFullColumnName(String fullColumnName) {
    int index = fullColumnName.indexOf(QueryConstants.NAME_SEPARATOR);
    if (index < 0) {
        return getEscapedArgument(fullColumnName); 
    }
    String columnFamily = fullColumnName.substring(0,index);
    String columnName = fullColumnName.substring(index+1);
    return getEscapedArgument(columnFamily) + QueryConstants.NAME_SEPARATOR + getEscapedArgument(columnName) ;
}
 
Example #30
Source File: PhoenixResultSetMetaData.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int getColumnDisplaySize(int column) throws SQLException {
    ColumnProjector projector = rowProjector.getColumnProjector(column-1);
    PDataType type = projector.getExpression().getDataType();
    if (type == null) {
        return QueryConstants.NULL_DISPLAY_TEXT.length();
    }
    if (type.isCoercibleTo(PDate.INSTANCE)) {
        return connection.getDatePattern().length();
    }
    if (projector.getExpression().getMaxLength() != null) {
        return projector.getExpression().getMaxLength();
    }
    return DEFAULT_DISPLAY_WIDTH;
}