Search in sources :

Example 1 with SQLParser

use of org.apache.phoenix.parse.SQLParser in project phoenix by apache.

the class JoinQueryCompilerTest method getJoinTable.

private static JoinTable getJoinTable(String query, PhoenixConnection connection) throws SQLException {
    SQLParser parser = new SQLParser(query);
    SelectStatement select = SubselectRewriter.flatten(parser.parseQuery(), connection);
    ColumnResolver resolver = FromCompiler.getResolverForQuery(select, connection);
    select = StatementNormalizer.normalize(select, resolver);
    SelectStatement transformedSelect = SubqueryRewriter.transform(select, resolver, connection);
    if (transformedSelect != select) {
        resolver = FromCompiler.getResolverForQuery(transformedSelect, connection);
        select = StatementNormalizer.normalize(transformedSelect, resolver);
    }
    PhoenixStatement stmt = connection.createStatement().unwrap(PhoenixStatement.class);
    return JoinCompiler.compile(stmt, select, resolver);
}
Also used : SelectStatement(org.apache.phoenix.parse.SelectStatement) SQLParser(org.apache.phoenix.parse.SQLParser) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement)

Example 2 with SQLParser

use of org.apache.phoenix.parse.SQLParser in project phoenix by apache.

the class BaseConnectionlessQueryTest method assertRoundtrip.

protected static void assertRoundtrip(String sql) throws SQLException {
    SQLParser parser = new SQLParser(sql);
    BindableStatement stmt = null;
    stmt = parser.parseStatement();
    if (stmt.getOperation() != Operation.QUERY) {
        return;
    }
    String newSQL = stmt.toString();
    SQLParser newParser = new SQLParser(newSQL);
    BindableStatement newStmt = null;
    try {
        newStmt = newParser.parseStatement();
    } catch (SQLException e) {
        fail("Unable to parse new:\n" + newSQL);
    }
    assertEquals("Expected equality:\n" + sql + "\n" + newSQL, stmt, newStmt);
}
Also used : SQLParser(org.apache.phoenix.parse.SQLParser) SQLException(java.sql.SQLException) BindableStatement(org.apache.phoenix.parse.BindableStatement)

Example 3 with SQLParser

use of org.apache.phoenix.parse.SQLParser in project phoenix by apache.

the class IndexUtil method rewriteViewStatement.

/**
 * Rewrite a view statement to be valid against an index
 * @param conn
 * @param index
 * @param table
 * @return
 * @throws SQLException
 */
public static String rewriteViewStatement(PhoenixConnection conn, PTable index, PTable table, String viewStatement) throws SQLException {
    if (viewStatement == null) {
        return null;
    }
    SelectStatement select = new SQLParser(viewStatement).parseQuery();
    ColumnResolver resolver = FromCompiler.getResolver(new TableRef(table));
    SelectStatement translatedSelect = IndexStatementRewriter.translate(select, resolver);
    ParseNode whereNode = translatedSelect.getWhere();
    PhoenixStatement statement = new PhoenixStatement(conn);
    TableRef indexTableRef = new TableRef(index) {

        @Override
        public String getColumnDisplayName(ColumnRef ref, boolean schemaNameCaseSensitive, boolean colNameCaseSensitive) {
            return '"' + ref.getColumn().getName().getString() + '"';
        }
    };
    ColumnResolver indexResolver = FromCompiler.getResolver(indexTableRef);
    StatementContext context = new StatementContext(statement, indexResolver);
    // Compile to ensure validity
    WhereCompiler.compile(context, whereNode);
    StringBuilder buf = new StringBuilder();
    whereNode.toSQL(indexResolver, buf);
    return QueryUtil.getViewStatement(index.getSchemaName().getString(), index.getTableName().getString(), buf.toString());
}
Also used : SelectStatement(org.apache.phoenix.parse.SelectStatement) SQLParser(org.apache.phoenix.parse.SQLParser) ParseNode(org.apache.phoenix.parse.ParseNode) ColumnRef(org.apache.phoenix.schema.ColumnRef) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) ColumnResolver(org.apache.phoenix.compile.ColumnResolver) TableRef(org.apache.phoenix.schema.TableRef) StatementContext(org.apache.phoenix.compile.StatementContext)

Example 4 with SQLParser

use of org.apache.phoenix.parse.SQLParser in project phoenix by apache.

the class MetaDataEndpointImpl method dropColumnsFromChildViews.

private MetaDataMutationResult dropColumnsFromChildViews(Region region, PTable basePhysicalTable, List<RowLock> locks, List<Mutation> tableMetadata, List<Mutation> mutationsForAddingColumnsToViews, byte[] schemaName, byte[] tableName, List<ImmutableBytesPtr> invalidateList, long clientTimeStamp, TableViewFinder childViewsResult, List<byte[]> tableNamesToDelete, List<SharedTableState> sharedTablesToDelete, int clientVersion) throws IOException, SQLException {
    List<Delete> columnDeletesForBaseTable = new ArrayList<>(tableMetadata.size());
    // are being added.
    for (Mutation m : tableMetadata) {
        if (m instanceof Delete) {
            byte[][] rkmd = new byte[5][];
            int pkCount = getVarChars(m.getRow(), rkmd);
            if (pkCount > COLUMN_NAME_INDEX && Bytes.compareTo(schemaName, rkmd[SCHEMA_NAME_INDEX]) == 0 && Bytes.compareTo(tableName, rkmd[TABLE_NAME_INDEX]) == 0) {
                columnDeletesForBaseTable.add((Delete) m);
            }
        }
    }
    for (ViewInfo viewInfo : childViewsResult.getViewInfoList()) {
        short numColsDeleted = 0;
        byte[] viewTenantId = viewInfo.getTenantId();
        byte[] viewSchemaName = viewInfo.getSchemaName();
        byte[] viewName = viewInfo.getViewName();
        byte[] viewKey = SchemaUtil.getTableKey(viewTenantId, viewSchemaName, viewName);
        // lock the rows corresponding to views so that no other thread can modify the view
        // meta-data
        RowLock viewRowLock = acquireLock(region, viewKey, locks);
        PTable view = doGetTable(viewKey, clientTimeStamp, viewRowLock, clientVersion);
        ColumnOrdinalPositionUpdateList ordinalPositionList = new ColumnOrdinalPositionUpdateList();
        int numCols = view.getColumns().size();
        int minDroppedColOrdinalPos = Integer.MAX_VALUE;
        for (Delete columnDeleteForBaseTable : columnDeletesForBaseTable) {
            PColumn existingViewColumn = null;
            byte[][] rkmd = new byte[5][];
            getVarChars(columnDeleteForBaseTable.getRow(), rkmd);
            String columnName = Bytes.toString(rkmd[COLUMN_NAME_INDEX]);
            String columnFamily = rkmd[FAMILY_NAME_INDEX] == null ? null : Bytes.toString(rkmd[FAMILY_NAME_INDEX]);
            byte[] columnKey = getColumnKey(viewKey, columnName, columnFamily);
            try {
                existingViewColumn = columnFamily == null ? view.getColumnForColumnName(columnName) : view.getColumnFamily(columnFamily).getPColumnForColumnName(columnName);
            } catch (ColumnFamilyNotFoundException e) {
            // ignore since it means that the column family is not present for the column to
            // be added.
            } catch (ColumnNotFoundException e) {
            // ignore since it means the column is not present in the view
            }
            // it
            if (existingViewColumn != null && view.getViewStatement() != null) {
                ParseNode viewWhere = new SQLParser(view.getViewStatement()).parseQuery().getWhere();
                PhoenixConnection conn = null;
                try {
                    conn = QueryUtil.getConnectionOnServer(env.getConfiguration()).unwrap(PhoenixConnection.class);
                } catch (ClassNotFoundException e) {
                }
                PhoenixStatement statement = new PhoenixStatement(conn);
                TableRef baseTableRef = new TableRef(basePhysicalTable);
                ColumnResolver columnResolver = FromCompiler.getResolver(baseTableRef);
                StatementContext context = new StatementContext(statement, columnResolver);
                Expression whereExpression = WhereCompiler.compile(context, viewWhere);
                Expression colExpression = new ColumnRef(baseTableRef, existingViewColumn.getPosition()).newColumnExpression();
                ColumnFinder columnFinder = new ColumnFinder(colExpression);
                whereExpression.accept(columnFinder);
                if (columnFinder.getColumnFound()) {
                    return new MetaDataMutationResult(MutationCode.UNALLOWED_TABLE_MUTATION, EnvironmentEdgeManager.currentTimeMillis(), basePhysicalTable);
                }
            }
            minDroppedColOrdinalPos = Math.min(getOrdinalPosition(view, existingViewColumn), minDroppedColOrdinalPos);
            if (existingViewColumn != null) {
                --numColsDeleted;
                if (ordinalPositionList.size() == 0) {
                    ordinalPositionList.setOffset(view.getBucketNum() == null ? 1 : 0);
                    for (PColumn col : view.getColumns()) {
                        ordinalPositionList.addColumn(getColumnKey(viewKey, col));
                    }
                }
                ordinalPositionList.dropColumn(columnKey);
                Delete viewColumnDelete = new Delete(columnKey, clientTimeStamp);
                mutationsForAddingColumnsToViews.add(viewColumnDelete);
                // drop any view indexes that need this column
                dropIndexes(view, region, invalidateList, locks, clientTimeStamp, schemaName, view.getName().getBytes(), mutationsForAddingColumnsToViews, existingViewColumn, tableNamesToDelete, sharedTablesToDelete, clientVersion);
            }
        }
        updateViewHeaderRow(basePhysicalTable, tableMetadata, mutationsForAddingColumnsToViews, invalidateList, clientTimeStamp, numColsDeleted, numColsDeleted, viewKey, view, ordinalPositionList, numCols, true);
    }
    return null;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) PTable(org.apache.phoenix.schema.PTable) StatementContext(org.apache.phoenix.compile.StatementContext) PColumn(org.apache.phoenix.schema.PColumn) ParseNode(org.apache.phoenix.parse.ParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) ColumnResolver(org.apache.phoenix.compile.ColumnResolver) RowLock(org.apache.hadoop.hbase.regionserver.Region.RowLock) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) SQLParser(org.apache.phoenix.parse.SQLParser) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) Mutation(org.apache.hadoop.hbase.client.Mutation) ColumnRef(org.apache.phoenix.schema.ColumnRef) TableRef(org.apache.phoenix.schema.TableRef)

Example 5 with SQLParser

use of org.apache.phoenix.parse.SQLParser in project phoenix by apache.

the class MetaDataClient method getParentOfView.

private PTable getParentOfView(PTable view) throws SQLException {
    // TODO just use view.getParentName().getString() after implementing https://issues.apache.org/jira/browse/PHOENIX-2114
    SelectStatement select = new SQLParser(view.getViewStatement()).parseQuery();
    String parentName = SchemaUtil.normalizeFullTableName(select.getFrom().toString().trim());
    return connection.getTable(new PTableKey(view.getTenantId(), parentName));
}
Also used : SelectStatement(org.apache.phoenix.parse.SelectStatement) SQLParser(org.apache.phoenix.parse.SQLParser)

Aggregations

SQLParser (org.apache.phoenix.parse.SQLParser)11 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)7 ParseNode (org.apache.phoenix.parse.ParseNode)7 Expression (org.apache.phoenix.expression.Expression)6 StatementContext (org.apache.phoenix.compile.StatementContext)5 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)5 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)5 SelectStatement (org.apache.phoenix.parse.SelectStatement)5 TableRef (org.apache.phoenix.schema.TableRef)5 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)4 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)4 PTable (org.apache.phoenix.schema.PTable)4 ColumnResolver (org.apache.phoenix.compile.ColumnResolver)3 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)3 ByteString (com.google.protobuf.ByteString)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Delete (org.apache.hadoop.hbase.client.Delete)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2