Package org.apache.phoenix.schema

Examples of org.apache.phoenix.schema.ColumnRef


            super(context, resolveViewConstants);
        }

        @Override
        public Expression visit(ColumnParseNode node) throws SQLException {
            ColumnRef ref = resolveColumn(node);
            TableRef tableRef = ref.getTableRef();
            if (tableRef.equals(context.getCurrentTable()) && !SchemaUtil.isPKColumn(ref.getColumn())) {
                // track the where condition columns. Later we need to ensure the Scan in HRS scans these column CFs
                context.addWhereCoditionColumn(ref.getColumn().getFamilyName().getBytes(), ref.getColumn().getName()
                        .getBytes());
            }
            return ref.newColumnExpression();
        }
View Full Code Here


            return ref.newColumnExpression();
        }

        @Override
        protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
            ColumnRef ref = super.resolveColumn(node);
            PTable table = ref.getTable();
            // Track if we need to compare KeyValue during filter evaluation
            // using column family. If the column qualifier is enough, we
            // just use that.
            try {
                if (!SchemaUtil.isPKColumn(ref.getColumn())) {
                    table.getColumn(ref.getColumn().getName().getString());
                }
            } catch (AmbiguousColumnException e) {
                disambiguateWithFamily = true;
            }
            return ref;
View Full Code Here

    public ColumnRef resolveColumn(String schemaName, String tableName,
        String colName) throws SQLException {
      String name = getProjectedColumnName(schemaName, tableName, colName);
      try {
        PColumn column = tableRef.getTable().getColumn(name);
        return new ColumnRef(tableRef, column.getPosition());
      } catch (ColumnNotFoundException e) {
        List<String> names = table.getMappedColumnName(name);
        if (names.size() == 1) {
          PColumn column = tableRef.getTable().getColumn(names.get(0));
          return new ColumnRef(tableRef, column.getPosition());         
        }
       
        if (names.size() > 1) {
          throw new AmbiguousColumnException(name);
        }
View Full Code Here

            return true;
        }

        @Override
        public Void visit(ColumnParseNode node) throws SQLException {
            ColumnRef ref = context.getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
            boolean isAggregateColumn = groupBy.getExpressions().indexOf(ref.newColumnExpression()) >= 0;
            if (hasOnlyAggregateColumns == null) {
                hasOnlyAggregateColumns = isAggregateColumn;
            } else {
                hasOnlyAggregateColumns &= isAggregateColumn;
            }
View Full Code Here

                                column, PNameFactory.newName(TupleProjector.VALUE_COLUMN_FAMILY), hasSaltingColumn);
                    }
                }
            } else {
                for (Map.Entry<ColumnRef, ColumnRefType> e : columnRefs.entrySet()) {
                    ColumnRef columnRef = e.getKey();
                    if (e.getValue() != ColumnRefType.PREFILTER
                            && columnRef.getTableRef().equals(tableRef)
                            && (!retainPKColumns || !SchemaUtil.isPKColumn(columnRef.getColumn()))) {
                        PColumn column = columnRef.getColumn();
                        addProjectedColumn(projectedColumns, sourceExpressions, columnNameMap,
                                column, PNameFactory.newName(TupleProjector.VALUE_COLUMN_FAMILY), hasSaltingColumn);
                    }
                }              
            }
View Full Code Here

           
            PName name = PNameFactory.newName(aliasedName);
            PColumnImpl column = new PColumnImpl(name, familyName, sourceColumn.getDataType(),
                    sourceColumn.getMaxLength(), sourceColumn.getScale(), sourceColumn.isNullable(),
                    position, sourceColumn.getSortOrder(), sourceColumn.getArraySize(), sourceColumn.getViewConstant(), sourceColumn.isViewReferenced());
            Expression sourceExpression = new ColumnRef(tableRef, sourceColumn.getPosition()).newColumnExpression();
            projectedColumns.add(column);
            sourceExpressions.add(sourceExpression);
        }
View Full Code Here

            this.columnRefMap.clear();
        }
       
        @Override
        public Void visit(ColumnParseNode node) throws SQLException {
            ColumnRef columnRef = resolver.resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
            columnRefMap.put(columnRef, node);
            tableRefSet.add(columnRef.getTableRef());
            return null;               
        }
View Full Code Here

                        pkColumnsSet.set(pkSlotIndexesToBe[i] = i + posOffset);
                        targetColumns.set(i, indexIdColumn);
                        i++;
                    }
                    for (ColumnName colName : columnNodes) {
                        ColumnRef ref = resolver.resolveColumn(null, colName.getFamilyName(), colName.getColumnName());
                        PColumn column = ref.getColumn();
                        if (IndexUtil.getViewConstantValue(column, ptr)) {
                            if (overlapViewColumnsToBe.isEmpty()) {
                                overlapViewColumnsToBe = Sets.newHashSetWithExpectedSize(addViewColumnsToBe.size());
                            }
                            nColumnsToSet--;
                            overlapViewColumnsToBe.add(column);
                            addViewColumnsToBe.remove(column);
                        }
                        columnIndexesToBe[i] = ref.getColumnPosition();
                        targetColumns.set(i, column);
                        if (SchemaUtil.isPKColumn(column)) {
                            pkColumnsSet.set(pkSlotIndexesToBe[i] = ref.getPKSlotPosition());
                        }
                        i++;
                    }
                    for (PColumn column : addViewColumnsToBe) {
                        columnIndexesToBe[i] = column.getPosition();
                        targetColumns.set(i, column);
                        if (SchemaUtil.isPKColumn(column)) {
                            pkColumnsSet.set(pkSlotIndexesToBe[i] = SchemaUtil.getPKPosition(table, column));
                        }
                        i++;
                    }
                    for (i = posOffset; i < table.getPKColumns().size(); i++) {
                        PColumn pkCol = table.getPKColumns().get(i);
                        if (!pkColumnsSet.get(i)) {
                            if (!pkCol.isNullable()) {
                                throw new ConstraintViolationException(table.getName().getString() + "." + pkCol.getName().getString() + " may not be null");
                            }
                        }
                    }
                }
                boolean isAutoCommit = connection.getAutoCommit();
                if (valueNodes == null) {
                    SelectStatement select = upsert.getSelect();
                    assert(select != null);
                    select = SubselectRewriter.flatten(select, connection);
                    ColumnResolver selectResolver = FromCompiler.getResolverForQuery(select, connection);
                    select = StatementNormalizer.normalize(select, selectResolver);
                    select = prependTenantAndViewConstants(table, select, tenantId, addViewColumnsToBe);
                    sameTable = select.getFrom().size() == 1
                        && tableRefToBe.equals(selectResolver.getTables().get(0));
                    /* We can run the upsert in a coprocessor if:
                     * 1) from has only 1 table and the into table matches from table
                     * 2) the select query isn't doing aggregation (which requires a client-side final merge)
                     * 3) autoCommit is on
                     * 4) the table is not immutable with indexes, as the client is the one that figures out the additional
                     *    puts for index tables.
                     * 5) no limit clause, as the limit clause requires client-side post processing
                     * 6) no sequences, as sequences imply that the order of upsert must match the order of
                     *    selection.
                     * Otherwise, run the query to pull the data from the server
                     * and populate the MutationState (upto a limit).
                    */           
                    if (! (select.isAggregate() || select.isDistinct() || select.getLimit() != null || select.hasSequence()) ) {
                        // We can pipeline the upsert select instead of spooling everything to disk first,
                        // if we don't have any post processing that's required.
                        parallelIteratorFactoryToBe = new UpsertingParallelIteratorFactory(connection, tableRefToBe);
                        // If we're in the else, then it's not an aggregate, distinct, limted, or sequence using query,
                        // so we might be able to run it entirely on the server side.
                        runOnServer = sameTable && isAutoCommit && !(table.isImmutableRows() && !table.getIndexes().isEmpty());
                    }
                    // If we may be able to run on the server, add a hint that favors using the data table
                    // if all else is equal.
                    // TODO: it'd be nice if we could figure out in advance if the PK is potentially changing,
                    // as this would disallow running on the server. We currently use the row projector we
                    // get back to figure this out.
                    HintNode hint = upsert.getHint();
                    if (!upsert.getHint().hasHint(Hint.USE_INDEX_OVER_DATA_TABLE)) {
                        hint = HintNode.create(hint, Hint.USE_DATA_OVER_INDEX_TABLE);
                    }
                    select = SelectStatement.create(select, hint);
                    // Pass scan through if same table in upsert and select so that projection is computed correctly
                    // Use optimizer to choose the best plan
                    try {
                        QueryCompiler compiler = new QueryCompiler(statement, select, selectResolver, targetColumns, parallelIteratorFactoryToBe, new SequenceManager(statement));
                        queryPlanToBe = compiler.compile();
                    } catch (MetaDataEntityNotFoundException e) {
                        retryOnce = false; // don't retry if select clause has meta data entities that aren't found, as we already updated the cache
                        throw e;
                    }
                    nValuesToSet = queryPlanToBe.getProjector().getColumnCount();
                    // Cannot auto commit if doing aggregation or topN or salted
                    // Salted causes problems because the row may end up living on a different region
                } else {
                    nValuesToSet = valueNodes.size() + addViewColumnsToBe.size() + (isTenantSpecific ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
                }
                // Resize down to allow a subset of columns to be specifiable
                if (columnNodes.isEmpty() && columnIndexesToBe.length >= nValuesToSet) {
                    nColumnsToSet = nValuesToSet;
                    columnIndexesToBe = Arrays.copyOf(columnIndexesToBe, nValuesToSet);
                    pkSlotIndexesToBe = Arrays.copyOf(pkSlotIndexesToBe, nValuesToSet);
                }
               
                if (nValuesToSet != nColumnsToSet) {
                    // We might have added columns, so refresh cache and try again if stale.
                    // Note that this check is not really sufficient, as a column could have
                    // been removed and the added back and we wouldn't detect that here.
                    if (retryOnce) {
                        retryOnce = false;
                        if (new MetaDataClient(connection).updateCache(schemaName, tableName).wasUpdated()) {
                            continue;
                        }
                    }
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.UPSERT_COLUMN_NUMBERS_MISMATCH)
                        .setMessage("Numbers of columns: " + nColumnsToSet + ". Number of values: " + nValuesToSet)
                        .build().buildException();
                }
            } catch (MetaDataEntityNotFoundException e) {
                // Catch column/column family not found exception, as our meta data may
                // be out of sync. Update the cache once and retry if we were out of sync.
                // Otherwise throw, as we'll just get the same error next time.
                if (retryOnce) {
                    retryOnce = false;
                    if (new MetaDataClient(connection).updateCache(schemaName, tableName).wasUpdated()) {
                        continue;
                    }
                }
                throw e;
            }
            break;
        }
       
        RowProjector projectorToBe = null;
        // Optimize only after all checks have been performed
        if (valueNodes == null) {
            queryPlanToBe = new QueryOptimizer(services).optimize(queryPlanToBe, statement, targetColumns, parallelIteratorFactoryToBe);
            projectorToBe = queryPlanToBe.getProjector();
            runOnServer &= queryPlanToBe.getTableRef().equals(tableRefToBe);
        }
        final List<PColumn> allColumns = allColumnsToBe;
        final RowProjector projector = projectorToBe;
        final QueryPlan queryPlan = queryPlanToBe;
        final TableRef tableRef = tableRefToBe;
        final int[] columnIndexes = columnIndexesToBe;
        final int[] pkSlotIndexes = pkSlotIndexesToBe;
        final Set<PColumn> addViewColumns = addViewColumnsToBe;
        final Set<PColumn> overlapViewColumns = overlapViewColumnsToBe;
        final UpsertingParallelIteratorFactory parallelIteratorFactory = parallelIteratorFactoryToBe;
       
        // TODO: break this up into multiple functions
        ////////////////////////////////////////////////////////////////////
        // UPSERT SELECT
        /////////////////////////////////////////////////////////////////////
        if (valueNodes == null) {
            // Before we re-order, check that for updatable view columns
            // the projected expression either matches the column name or
            // is a constant with the same required value.
            throwIfNotUpdatable(tableRef, overlapViewColumnsToBe, targetColumns, projector, sameTable);
           
            ////////////////////////////////////////////////////////////////////
            // UPSERT SELECT run server-side (maybe)
            /////////////////////////////////////////////////////////////////////
            if (runOnServer) {
                // At most this array will grow bigger by the number of PK columns
                int[] allColumnsIndexes = Arrays.copyOf(columnIndexes, columnIndexes.length + nValuesToSet);
                int[] reverseColumnIndexes = new int[table.getColumns().size()];
                List<Expression> projectedExpressions = Lists.newArrayListWithExpectedSize(reverseColumnIndexes.length);
                Arrays.fill(reverseColumnIndexes, -1);
                for (int i =0; i < nValuesToSet; i++) {
                    projectedExpressions.add(projector.getColumnProjector(i).getExpression());
                    reverseColumnIndexes[columnIndexes[i]] = i;
                }
                /*
                 * Order projected columns and projected expressions with PK columns
                 * leading order by slot position
                 */
                int offset = table.getBucketNum() == null ? 0 : 1;
                for (int i = 0; i < table.getPKColumns().size() - offset; i++) {
                    PColumn column = table.getPKColumns().get(i + offset);
                    int pos = reverseColumnIndexes[column.getPosition()];
                    if (pos == -1) {
                        // Last PK column may be fixed width and nullable
                        // We don't want to insert a null expression b/c
                        // it's not valid to set a fixed width type to null.
                        if (column.getDataType().isFixedWidth()) {
                            continue;
                        }
                        // Add literal null for missing PK columns
                        pos = projectedExpressions.size();
                        Expression literalNull = LiteralExpression.newConstant(null, column.getDataType(), true);
                        projectedExpressions.add(literalNull);
                        allColumnsIndexes[pos] = column.getPosition();
                    }
                    // Swap select expression at pos with i
                    Collections.swap(projectedExpressions, i, pos);
                    // Swap column indexes and reverse column indexes too
                    int tempPos = allColumnsIndexes[i];
                    allColumnsIndexes[i] = allColumnsIndexes[pos];
                    allColumnsIndexes[pos] = tempPos;
                    reverseColumnIndexes[tempPos] = reverseColumnIndexes[i];
                    reverseColumnIndexes[i] = i;
                }
                // If any pk slots are changing, be conservative and don't run this server side.
                // If the row ends up living in a different region, we'll get an error otherwise.
                for (int i = 0; i < table.getPKColumns().size(); i++) {
                    PColumn column = table.getPKColumns().get(i);
                    Expression source = projectedExpressions.get(i);
                    if (source == null || !source.equals(new ColumnRef(tableRef, column.getPosition()).newColumnExpression())) {
                        // TODO: we could check the region boundaries to see if the pk will still be in it.
                        runOnServer = false; // bail on running server side, since PK may be changing
                        break;
                    }
                }
View Full Code Here

        return rewrite(statement, new IndexStatementRewriter(dataResolver, multiTableRewriteMap));
    }

    @Override
    public ParseNode visit(ColumnParseNode node) throws SQLException {
        ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
        PColumn dataCol = dataColRef.getColumn();
        TableRef dataTableRef = dataColRef.getTableRef();
        // Rewrite view constants as literals, as they won't be in the schema for
        // an index on the view. Our view may be READ_ONLY yet still have inherited
        // view constants if based on an UPDATABLE view
        if (dataCol.getViewConstant() != null) {
            byte[] viewConstant = dataCol.getViewConstant();
            // Ignore last byte, as it's only there so we can have a way to differentiate null
            // from the absence of a value.
            ptr.set(viewConstant, 0, viewConstant.length-1);
            Object literal = dataCol.getDataType().toObject(ptr);
            return new LiteralParseNode(literal, dataCol.getDataType());
        }
        TableName tName = getReplacedTableName(dataTableRef);
        if (multiTableRewriteMap != null && tName == null)
            return node;

        String indexColName = IndexUtil.getIndexColumnName(dataCol);
        // Same alias as before, but use the index column name instead of the data column name
        ParseNode indexColNode = new ColumnParseNode(tName, node.isCaseSensitive() ? '"' + indexColName + '"' : indexColName, node.getAlias());
        PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
        PDataType dataColType = dataColRef.getColumn().getDataType();

        // Coerce index column reference back to same type as data column so that
        // expression behave exactly the same. No need to invert, as this will be done
        // automatically as needed. If node is used at the top level, do not convert, as
        // otherwise the wrapper gets in the way in the group by clause. For example,
View Full Code Here

                        theColumnPosition = column.getPosition();
                    } catch (ColumnNotFoundException e) {

                    }
                }
                if (theTableRef != null) { return new ColumnRef(theTableRef, theColumnPosition); }
                throw new ColumnNotFoundException(colName);
            } else {
                try {
                    TableRef tableRef = resolveTable(schemaName, tableName);
                    PColumn column = tableRef.getTable().getColumn(colName);
                    return new ColumnRef(tableRef, column.getPosition());
                } catch (TableNotFoundException e) {
                    // Try using the tableName as a columnFamily reference instead
                    ColumnFamilyRef cfRef = resolveColumnFamily(schemaName, tableName);
                    PColumn column = cfRef.getFamily().getColumn(colName);
                    return new ColumnRef(cfRef.getTableRef(), column.getPosition());
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.phoenix.schema.ColumnRef

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.