Search in sources :

Example 61 with Column

use of herddb.model.Column in project herddb by diennea.

the class TableManager method validateAlterTable.

@Override
public void validateAlterTable(Table table, StatementEvaluationContext context) throws StatementExecutionException {
    List<String> columnsChangedFromNullToNotNull = new ArrayList<>();
    for (Column c : this.table.columns) {
        Column newColumnSpecs = table.getColumn(c.name);
        if (newColumnSpecs == null) {
            // dropped column
            LOGGER.log(Level.INFO, "Table {0}.{1} dropping column {2}", new Object[] { table.tablespace, table.name, c.name });
        } else if (newColumnSpecs.type == c.type) {
        // no data type change
        } else if (ColumnTypes.isNotNullToNullConversion(c.type, newColumnSpecs.type)) {
            LOGGER.log(Level.INFO, "Table {0}.{1} making column {2} NULLABLE", new Object[] { table.tablespace, table.name, newColumnSpecs.name });
        } else if (ColumnTypes.isNullToNotNullConversion(c.type, newColumnSpecs.type)) {
            LOGGER.log(Level.INFO, "Table {0}.{1} making column {2} NOT NULL", new Object[] { table.tablespace, table.name, newColumnSpecs.name });
            columnsChangedFromNullToNotNull.add(c.name);
        }
    }
    for (final String column : columnsChangedFromNullToNotNull) {
        LOGGER.log(Level.INFO, "Table {0}.{1} validating column {2}, check for NULL values", new Object[] { table.tablespace, table.name, column });
        ScanStatement scan = new ScanStatement(this.table.tablespace, this.table, new Predicate() {

            @Override
            public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
                return record.getDataAccessor(table).get(column) == null;
            }
        });
        // fast fail
        scan.setLimits(new ScanLimitsImpl(1, 0));
        boolean foundOneNull = false;
        try (DataScanner scanner = this.scan(scan, context, null, false, false)) {
            foundOneNull = scanner.hasNext();
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
        if (foundOneNull) {
            throw new StatementExecutionException("Found a record in table " + table.name + " that contains a NULL value for column " + column + " ALTER command is not possible");
        }
    }
    // if we are adding new FK we have to check that the FK is not violated
    if (table.foreignKeys != null) {
        List<ForeignKeyDef> newForeignKeys;
        if (this.table.foreignKeys == null) {
            newForeignKeys = Arrays.asList(table.foreignKeys);
        } else {
            Set<String> currentKfs = Stream.of(this.table.foreignKeys).map(f -> f.name.toLowerCase()).collect(Collectors.toSet());
            newForeignKeys = Stream.of(table.foreignKeys).filter(fk -> !currentKfs.contains(fk.name)).collect(Collectors.toList());
        }
        for (ForeignKeyDef newFk : newForeignKeys) {
            validateForeignKeyConsistency(newFk, context, null);
        }
    }
}
Also used : Arrays(java.util.Arrays) NullLockManager(herddb.utils.NullLockManager) Table(herddb.model.Table) TruncateTableStatement(herddb.model.commands.TruncateTableStatement) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) TableStatus(herddb.storage.TableStatus) Map(java.util.Map) DataAccessor(herddb.utils.DataAccessor) LogNotAvailableException(herddb.log.LogNotAvailableException) CommitLogResult(herddb.log.CommitLogResult) LogSequenceNumber(herddb.log.LogSequenceNumber) UniqueIndexContraintViolationException(herddb.model.UniqueIndexContraintViolationException) Set(java.util.Set) RecordSerializer(herddb.codec.RecordSerializer) JSQLParserPlanner.delimit(herddb.sql.JSQLParserPlanner.delimit) DataPageMetaData(herddb.core.PageSet.DataPageMetaData) ScanStatement(herddb.model.commands.ScanStatement) Stream(java.util.stream.Stream) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) Bytes(herddb.utils.Bytes) Holder(herddb.utils.Holder) LockHandle(herddb.utils.LockHandle) ForeignKeyViolationException(herddb.model.ForeignKeyViolationException) LogEntry(herddb.log.LogEntry) ArrayList(java.util.ArrayList) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Projection(herddb.model.Projection) ForeignKeyDef(herddb.model.ForeignKeyDef) EnsureLongIncrementAccumulator(herddb.utils.EnsureLongIncrementAccumulator) LogEntryType(herddb.log.LogEntryType) Record(herddb.model.Record) LogEntryFactory(herddb.log.LogEntryFactory) KeyToPageIndex(herddb.index.KeyToPageIndex) DataStorageManager(herddb.storage.DataStorageManager) ColumnTypes(herddb.model.ColumnTypes) ILocalLockManager(herddb.utils.ILocalLockManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) IndexOperation(herddb.index.IndexOperation) Column(herddb.model.Column) StampedLock(java.util.concurrent.locks.StampedLock) UpdateStatement(herddb.model.commands.UpdateStatement) ScanLimitsImpl(herddb.model.ScanLimitsImpl) TupleComparator(herddb.model.TupleComparator) ServerConfiguration(herddb.server.ServerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordTooBigException(herddb.model.RecordTooBigException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) LocalLockManager(herddb.utils.LocalLockManager) Futures(herddb.utils.Futures) DataStorageManagerException(herddb.storage.DataStorageManagerException) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) RecordFunction(herddb.model.RecordFunction) StatementExecutionException(herddb.model.StatementExecutionException) TableContext(herddb.model.TableContext) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) GetStatement(herddb.model.commands.GetStatement) Entry(java.util.Map.Entry) Statement(herddb.model.Statement) LongAdder(java.util.concurrent.atomic.LongAdder) DataScannerException(herddb.model.DataScannerException) GetResult(herddb.model.GetResult) PrimaryIndexSeek(herddb.index.PrimaryIndexSeek) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) BatchOrderedExecutor(herddb.utils.BatchOrderedExecutor) ConcurrentMap(java.util.concurrent.ConcurrentMap) Level(java.util.logging.Level) HashSet(java.util.HashSet) BooleanHolder(herddb.utils.BooleanHolder) ScanLimits(herddb.model.ScanLimits) DeleteStatement(herddb.model.commands.DeleteStatement) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Semaphore(java.util.concurrent.Semaphore) DataPageDoesNotExistException(herddb.storage.DataPageDoesNotExistException) Counter(org.apache.bookkeeper.stats.Counter) StatementExecutionResult(herddb.model.StatementExecutionResult) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CommitLog(herddb.log.CommitLog) AbstractMap(java.util.AbstractMap) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement) Predicate(herddb.model.Predicate) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Comparator(java.util.Comparator) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TableManagerStats(herddb.core.stats.TableManagerStats) SystemProperties(herddb.utils.SystemProperties) ArrayList(java.util.ArrayList) ScanLimitsImpl(herddb.model.ScanLimitsImpl) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(herddb.model.Predicate) DataScanner(herddb.model.DataScanner) Column(herddb.model.Column) Record(herddb.model.Record) StatementEvaluationContext(herddb.model.StatementEvaluationContext) ForeignKeyDef(herddb.model.ForeignKeyDef) ScanStatement(herddb.model.commands.ScanStatement) DataScannerException(herddb.model.DataScannerException)

Example 62 with Column

use of herddb.model.Column in project herddb by diennea.

the class DataAccessorForFullRecord method forEach.

@Override
public void forEach(BiConsumer<String, Object> consumer) {
    // best case
    if (table.physicalLayoutLikeLogicalLayout) {
        // no need to create a Map
        if (table.primaryKey.length == 1) {
            String pkField = table.primaryKey[0];
            Object value = RecordSerializer.deserialize(record.key, table.getColumn(pkField).type);
            consumer.accept(pkField, value);
            if (value instanceof RawString) {
                ((RawString) value).recycle();
            }
        } else {
            try (final ByteArrayCursor din = record.key.newCursor()) {
                for (String primaryKeyColumn : table.primaryKey) {
                    Bytes value = din.readBytesNoCopy();
                    Object theValue = RecordSerializer.deserialize(value, table.getColumn(primaryKeyColumn).type);
                    consumer.accept(primaryKeyColumn, theValue);
                    if (theValue instanceof RawString) {
                        ((RawString) theValue).recycle();
                    }
                }
            } catch (IOException err) {
                throw new IllegalStateException("bad data:" + err, err);
            }
        }
        try (ByteArrayCursor din = record.value.newCursor()) {
            while (!din.isEof()) {
                int serialPosition;
                serialPosition = din.readVIntNoEOFException();
                if (din.isEof()) {
                    break;
                }
                Column col = table.getColumnBySerialPosition(serialPosition);
                if (col != null) {
                    Object value = RecordSerializer.deserializeTypeAndValue(din);
                    consumer.accept(col.name, value);
                } else {
                    // we have to deserialize always the value, even the column is no more present
                    RecordSerializer.skipTypeAndValue(din);
                }
            }
        } catch (IOException err) {
            throw new IllegalStateException("bad data:" + err, err);
        }
    } else {
        // bad case
        for (int i = 0; i < table.columnNames.length; i++) {
            String columnName = table.columnNames[i];
            Object value = get(i);
            consumer.accept(columnName, value);
            if (value instanceof RawString) {
                ((RawString) value).recycle();
            }
        }
    }
}
Also used : Bytes(herddb.utils.Bytes) RawString(herddb.utils.RawString) Column(herddb.model.Column) RawString(herddb.utils.RawString) IOException(java.io.IOException) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Example 63 with Column

use of herddb.model.Column in project herddb by diennea.

the class RecordSerializer method accessRawDataFromValue.

static Object accessRawDataFromValue(int index, Bytes value, Table table) throws IOException {
    Column column = table.getColumn(index);
    try (ByteArrayCursor din = value.newCursor()) {
        while (!din.isEof()) {
            int serialPosition;
            serialPosition = din.readVIntNoEOFException();
            if (din.isEof()) {
                return null;
            }
            Column col = table.getColumnBySerialPosition(serialPosition);
            if (col != null && col.serialPosition == column.serialPosition) {
                return deserializeTypeAndValue(din);
            } else {
                // we have to deserialize always the value, even the column is no more present
                skipTypeAndValue(din);
            }
        }
        return null;
    }
}
Also used : Column(herddb.model.Column) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Example 64 with Column

use of herddb.model.Column in project herddb by diennea.

the class RecordSerializer method serializeIndexKey.

public static Bytes serializeIndexKey(DataAccessor record, ColumnsList index, String[] columns) {
    String[] indexedColumnsList = index.getPrimaryKey();
    if (indexedColumnsList.length == 1) {
        String pkColumn = indexedColumnsList[0];
        if (columns.length != 1 && !columns[0].equals(pkColumn)) {
            throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(pkColumn));
        }
        Column c = index.getColumn(pkColumn);
        Object v = record.get(c.name);
        if (v == null) {
            if (index.allowNullsForIndexedValues()) {
                return null;
            }
            throw new IllegalArgumentException("key field " + pkColumn + " cannot be null. Record data: " + record);
        }
        byte[] fieldValue = serialize(v, c.type);
        return Bytes.from_array(fieldValue);
    } else {
        VisibleByteArrayOutputStream key = new VisibleByteArrayOutputStream(columns.length * Long.BYTES);
        // beware that sometime we serialize only a part of the PK, for instance of a prefix index scan
        try (ExtendedDataOutputStream doo_key = new ExtendedDataOutputStream(key)) {
            int i = 0;
            for (String indexedColumn : columns) {
                if (!indexedColumn.equals(indexedColumnsList[i])) {
                    throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(indexedColumnsList));
                }
                Column c = index.getColumn(indexedColumn);
                Object v = record.get(c.name);
                if (v == null) {
                    if (!index.allowNullsForIndexedValues()) {
                        throw new IllegalArgumentException("key field " + indexedColumn + " cannot be null. Record data: " + record);
                    }
                    if (i == 0) {
                        // if the first column is null than we do not index the record at all
                        return null;
                    } else {
                        // we stop serializing the value at the first null
                        return Bytes.from_array(key.getBuffer(), 0, key.size());
                    }
                }
                serializeTo(v, c.type, doo_key);
                i++;
            }
        } catch (IOException err) {
            throw new RuntimeException(err);
        }
        return Bytes.from_array(key.getBuffer(), 0, key.size());
    }
}
Also used : Column(herddb.model.Column) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) RawString(herddb.utils.RawString) IOException(java.io.IOException) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 65 with Column

use of herddb.model.Column in project herddb by diennea.

the class RecordSerializer method serializeValueRaw.

public static byte[] serializeValueRaw(Map<String, Object> record, Table table, int expectedSize) {
    VisibleByteArrayOutputStream value = new VisibleByteArrayOutputStream(expectedSize <= 0 ? INITIAL_BUFFER_SIZE : expectedSize);
    try (ExtendedDataOutputStream doo = new ExtendedDataOutputStream(value)) {
        for (Column c : table.columns) {
            Object v = record.get(c.name);
            if (v != null && !table.isPrimaryKeyColumn(c.name)) {
                doo.writeVInt(c.serialPosition);
                serializeTypeAndValue(v, c.type, doo);
            }
        }
    } catch (IOException err) {
        throw new RuntimeException(err);
    }
    return value.toByteArrayNoCopy();
}
Also used : Column(herddb.model.Column) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Aggregations

Column (herddb.model.Column)68 StatementExecutionException (herddb.model.StatementExecutionException)25 ArrayList (java.util.ArrayList)24 Table (herddb.model.Table)23 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)19 DataAccessor (herddb.utils.DataAccessor)18 HashMap (java.util.HashMap)14 PlannerOp (herddb.model.planner.PlannerOp)12 List (java.util.List)12 Bytes (herddb.utils.Bytes)10 AbstractTableManager (herddb.core.AbstractTableManager)9 RecordFunction (herddb.model.RecordFunction)9 RawString (herddb.utils.RawString)9 IOException (java.io.IOException)9 HashSet (java.util.HashSet)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 Test (org.junit.Test)9 DataScanner (herddb.model.DataScanner)8 Tuple (herddb.model.Tuple)8 InsertStatement (herddb.model.commands.InsertStatement)8