private PColumn newColumn(int position, ColumnDef def, PrimaryKeyConstraint pkConstraint, String defaultColumnFamily, boolean addingToPK, byte[] columnQualifierBytes, boolean isImmutableRows) throws SQLException { try { ColumnName columnDefName = def.getColumnDefName(); SortOrder sortOrder = def.getSortOrder(); boolean isPK = def.isPK(); boolean isRowTimestamp = def.isRowTimestamp(); if (pkConstraint != null) { Pair<ColumnName, SortOrder> pkSortOrder = pkConstraint.getColumnWithSortOrder(columnDefName); if (isPK && sortOrder == SortOrder.DESC && def.getDataType() == PVarbinary.INSTANCE) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.DESC_VARBINARY_NOT_SUPPORTED) .setColumnName(columnName) if (def.isPK() && !pkConstraint.getColumnNames().isEmpty() ) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_ALREADY_EXISTS) .setColumnName(columnName).build().buildException(); boolean isNull = def.isNull(); if (def.getColumnDefName().getFamilyName() != null) { String family = def.getColumnDefName().getFamilyName(); if (isPK) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_WITH_FAMILY_NAME) .setColumnName(columnName).setFamilyName(family).build().buildException(); } else if (!def.isNull() && !isImmutableRows) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL) .setColumnName(columnName).setFamilyName(family).build().buildException(); if (def.isNull() && def.isNullSet()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.SINGLE_PK_MAY_NOT_BE_NULL)
public ColumnDef columnDef(ColumnName columnDefName, String sqlTypeName, boolean isArray, Integer arrSize, Boolean isNull, Integer maxLength, Integer scale, boolean isPK, SortOrder sortOrder, String expressionStr, boolean isRowTimestamp) { return new ColumnDef(columnDefName, sqlTypeName, isArray, arrSize, isNull, maxLength, scale, isPK, sortOrder, expressionStr, isRowTimestamp); }
private static boolean isPkColumn(PrimaryKeyConstraint pkConstraint, ColumnDef colDef) { return colDef.isPK() || (pkConstraint != null && pkConstraint.contains(colDef.getColumnDefName())); }
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable) throws SQLException { if (!dynColumns.isEmpty()) { List<PColumn> allcolumns = new ArrayList<PColumn>(); List<PColumn> existingColumns = theTable.getColumns(); // Need to skip the salting column, as it's handled in the PTable builder call below allcolumns.addAll(theTable.getBucketNum() == null ? existingColumns : existingColumns.subList(1, existingColumns.size())); // Position still based on with the salting columns int position = existingColumns.size(); PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable)); for (ColumnDef dynColumn : dynColumns) { PName familyName = defaultFamilyName; PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName()); String family = dynColumn.getColumnDefName().getFamilyName(); if (family != null) { theTable.getColumnFamily(family); // Verifies that column family exists familyName = PNameFactory.newName(family); } allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(), dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression(), false, true, Bytes.toBytes(dynColumn.getColumnDefName().getColumnName()), HConstants.LATEST_TIMESTAMP)); position++; } theTable = PTableImpl.builderWithColumns(theTable, allcolumns) .build(); } return theTable; }
state._fsp--; if (state.failed) return ret; if ( state.backtracking==0 ) { if (d.getDataType()!=null) { ret.getFirst().add(d); } ret.getSecond().add(d.getColumnDefName()); } state._fsp--; if (state.failed) return ret; if ( state.backtracking==0 ) { if (d.getDataType()!=null) { ret.getFirst().add(d); } ret.getSecond().add(d.getColumnDefName()); }
String familyName = columnDef.getColumnDefName().getFamilyName(); String columnName = columnDef.getColumnDefName().getColumnName(); if (familyName!=null) { try { short nextKeySeq = SchemaUtil.getMaxKeySeq(table); for( ColumnDef colDef : columnDefs) { if (colDef != null && !colDef.isNull()) { if(colDef.isPK()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.NOT_NULLABLE_COLUMN_IN_ROW_KEY) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); } else if (!willBeImmutableRows) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if (colDef != null && colDef.isPK() && table.getType() == VIEW && table.getViewType() != MAPPED) { throwIfLastPKOfParentIsVariableLength(getParentOfView(table), schemaName, tableName, colDef); if (colDef != null && colDef.isRowTimestamp()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_CREATE_ONLY) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if (!colDef.validateDefault(context, null)) { colDef = new ColumnDef(colDef, null); // Remove DEFAULT as it's not necessary if (!colDef.isPK()) { String colDefFamily = colDef.getColumnDefName().getFamilyName(); String familyName = null; ImmutableStorageScheme storageScheme = table.getImmutableStorageScheme();
public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException { String defaultStr = this.getExpression(); if (defaultStr == null) { return true; new SQLParser(this.getExpression()).parseExpression(); Expression defaultExpression = defaultParseNode.accept(compiler); if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT) .setColumnName(this.getColumnDefName().getColumnName()).build() .buildException(); if (this.isRowTimestamp() || ( pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) { throw new SQLExceptionInfo.Builder( SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP) .setColumnName(this.getColumnDefName().getColumnName()) .build().buildException(); PDataType targetType = this.getDataType(); defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder()); } catch (ConstraintViolationException e) { if (e.getCause() instanceof SQLException) { SQLException sqlE = (SQLException) e.getCause(); throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
private static boolean checkAndValidateRowTimestampCol(ColumnDef colDef, PrimaryKeyConstraint pkConstraint, boolean rowTimeStampColAlreadyFound, PTableType tableType) throws SQLException { ColumnName columnDefName = colDef.getColumnDefName(); if (tableType == VIEW && (pkConstraint.getNumColumnsWithRowTimestamp() > 0 || colDef.isRowTimestamp())) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_NOT_ALLOWED_ON_VIEW) .setColumnName(columnDefName.getColumnName()).build().buildException(); boolean isColumnDeclaredRowTimestamp = colDef.isRowTimestamp() || pkConstraint.isColumnRowTimestamp(columnDefName); if (isColumnDeclaredRowTimestamp) { boolean isColumnPartOfPk = colDef.isPK() || pkConstraint.contains(columnDefName); PDataType dataType = colDef.getDataType(); if (isColumnDeclaredRowTimestamp && (dataType != PLong.INSTANCE && dataType != PUnsignedLong.INSTANCE && !dataType.isCoercibleTo(PTimestamp.INSTANCE))) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_COL_INVALID_TYPE)
PDataType dataType = colDefs.get(autoPartitionColIndex).getDataType(); if (!PLong.INSTANCE.isCastableTo(dataType)) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.SEQUENCE_NOT_CASTABLE_TO_AUTO_PARTITION_ID_COLUMN) for (ColumnDef colDef : colDefs) { rowTimeStampColumnAlreadyFound = checkAndValidateRowTimestampCol(colDef, pkConstraint, rowTimeStampColumnAlreadyFound, tableType); if (colDef.isPK()) { // i.e. the column is declared as CREATE TABLE COLNAME DATATYPE PRIMARY KEY... if (wasPKDefined) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_ALREADY_EXISTS) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if ( !colDef.isNull() && !isImmutableRows && ( wasPKDefined || !isPkColumn(pkConstraint, colDef))) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL) .setSchemaName(schemaName) .setTableName(tableName) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); ColumnName columnDefName = colDef.getColumnDefName(); String colDefFamily = columnDefName.getFamilyName(); boolean isPkColumn = isPkColumn(pkConstraint, colDef); if ((colDef.getDataType() == PVarbinary.INSTANCE || colDef.getDataType().isArrayType()) && SchemaUtil.isPKColumn(column) && pkColumnsIterator.hasNext()) { if (colDef.getColumnDefName().getFamilyName() != null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_WITH_FAMILY_NAME) .setSchemaName(schemaName)
for (int i = 0; i < columnDefs.size(); i++) { ColumnDef columnDef = columnDefs.get(i); if(columnDef.getColumnDefName().getFamilyName()!=null && columnDef.getColumnDefName().getFamilyName().contains(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.UNALLOWED_COLUMN_FAMILY) .build().buildException(); if (!columnDef.validateDefault(context, pkConstraint)) { if (overideColumnDefs == null) { overideColumnDefs = new ArrayList<>(columnDefs); overideColumnDefs.set(i, new ColumnDef(columnDef, null));
if (pkConstraint.contains(columnDef.getColumnDefName())) { columnDef.setIsPK(true);
private static ColumnDef findColumnDefOrNull(List<ColumnDef> colDefs, ColumnName colName) { for (ColumnDef colDef : colDefs) { if (colDef.getColumnDefName().getColumnName().equals(colName.getColumnName())) { return colDef; } } return null; }
@Test public void testParseCreateTableInlinePrimaryKeyWithOrder() throws Exception { for (String order : new String[]{"asc", "desc"}) { String s = "create table core.entity_history_archive (id char(15) primary key ${o})".replace("${o}", order); CreateTableStatement stmt = (CreateTableStatement)new SQLParser((s)).parseStatement(); List<ColumnDef> columnDefs = stmt.getColumnDefs(); assertEquals(1, columnDefs.size()); assertEquals(SortOrder.fromDDLValue(order), columnDefs.iterator().next().getSortOrder()); } }
String familyName = columnDef.getColumnDefName().getFamilyName(); String columnName = columnDef.getColumnDefName().getColumnName(); if (familyName!=null) { try { short nextKeySeq = SchemaUtil.getMaxKeySeq(table); for( ColumnDef colDef : columnDefs) { if (colDef != null && !colDef.isNull()) { if(colDef.isPK()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.NOT_NULLABLE_COLUMN_IN_ROW_KEY) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); } else if (!willBeImmutableRows) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if (colDef != null && colDef.isPK() && table.getType() == VIEW && table.getViewType() != MAPPED) { throwIfLastPKOfParentIsFixedLength(getParentOfView(table), schemaName, tableName, colDef); if (colDef != null && colDef.isRowTimestamp()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_CREATE_ONLY) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if (!colDef.validateDefault(context, null)) { colDef = new ColumnDef(colDef, null); // Remove DEFAULT as it's not necessary if (!colDef.isPK()) { String colDefFamily = colDef.getColumnDefName().getFamilyName(); String familyName = null; ImmutableStorageScheme storageScheme = table.getImmutableStorageScheme();
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable) throws SQLException { if (!dynColumns.isEmpty()) { List<PColumn> allcolumns = new ArrayList<PColumn>(); List<PColumn> existingColumns = theTable.getColumns(); // Need to skip the salting column, as it's added in the makePTable call below allcolumns.addAll(theTable.getBucketNum() == null ? existingColumns : existingColumns.subList(1, existingColumns.size())); // Position still based on with the salting columns int position = existingColumns.size(); PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable)); for (ColumnDef dynColumn : dynColumns) { PName familyName = defaultFamilyName; PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName()); String family = dynColumn.getColumnDefName().getFamilyName(); if (family != null) { theTable.getColumnFamily(family); // Verifies that column family exists familyName = PNameFactory.newName(family); } allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(), dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression(), false, true, Bytes.toBytes(dynColumn.getColumnDefName().getColumnName()))); position++; } theTable = PTableImpl.makePTable(theTable, allcolumns); } return theTable; }
public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException { String defaultStr = this.getExpression(); if (defaultStr == null) { return true; new SQLParser(this.getExpression()).parseExpression(); Expression defaultExpression = defaultParseNode.accept(compiler); if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT) .setColumnName(this.getColumnDefName().getColumnName()).build() .buildException(); if (this.isRowTimestamp() || ( pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) { throw new SQLExceptionInfo.Builder( SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP) .setColumnName(this.getColumnDefName().getColumnName()) .build().buildException(); PDataType targetType = this.getDataType(); defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder()); } catch (ConstraintViolationException e) { if (e.getCause() instanceof SQLException) { SQLException sqlE = (SQLException) e.getCause(); throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
private static boolean checkAndValidateRowTimestampCol(ColumnDef colDef, PrimaryKeyConstraint pkConstraint, boolean rowTimeStampColAlreadyFound, PTableType tableType) throws SQLException { ColumnName columnDefName = colDef.getColumnDefName(); if (tableType == VIEW && (pkConstraint.getNumColumnsWithRowTimestamp() > 0 || colDef.isRowTimestamp())) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_NOT_ALLOWED_ON_VIEW) .setColumnName(columnDefName.getColumnName()).build().buildException(); boolean isColumnDeclaredRowTimestamp = colDef.isRowTimestamp() || pkConstraint.isColumnRowTimestamp(columnDefName); if (isColumnDeclaredRowTimestamp) { boolean isColumnPartOfPk = colDef.isPK() || pkConstraint.contains(columnDefName); PDataType dataType = colDef.getDataType(); if (isColumnDeclaredRowTimestamp && (dataType != PLong.INSTANCE && dataType != PUnsignedLong.INSTANCE && !dataType.isCoercibleTo(PTimestamp.INSTANCE))) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ROWTIMESTAMP_COL_INVALID_TYPE)
PDataType dataType = colDefs.get(autoPartitionColIndex).getDataType(); if (!PLong.INSTANCE.isCastableTo(dataType)) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.SEQUENCE_NOT_CASTABLE_TO_AUTO_PARTITION_ID_COLUMN) for (ColumnDef colDef : colDefs) { rowTimeStampColumnAlreadyFound = checkAndValidateRowTimestampCol(colDef, pkConstraint, rowTimeStampColumnAlreadyFound, tableType); if (colDef.isPK()) { // i.e. the column is declared as CREATE TABLE COLNAME DATATYPE PRIMARY KEY... if (wasPKDefined) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_ALREADY_EXISTS) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); if ( !colDef.isNull() && !isImmutableRows && ( wasPKDefined || !isPkColumn(pkConstraint, colDef))) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL) .setSchemaName(schemaName) .setTableName(tableName) .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException(); ColumnName columnDefName = colDef.getColumnDefName(); String colDefFamily = columnDefName.getFamilyName(); boolean isPkColumn = isPkColumn(pkConstraint, colDef); if ((colDef.getDataType() == PVarbinary.INSTANCE || colDef.getDataType().isArrayType()) && SchemaUtil.isPKColumn(column) && pkColumnsIterator.hasNext()) { if (colDef.getColumnDefName().getFamilyName() != null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_WITH_FAMILY_NAME) .setSchemaName(schemaName)
for (int i = 0; i < columnDefs.size(); i++) { ColumnDef columnDef = columnDefs.get(i); if(columnDef.getColumnDefName().getFamilyName()!=null && columnDef.getColumnDefName().getFamilyName().contains(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.UNALLOWED_COLUMN_FAMILY) .build().buildException(); if (!columnDef.validateDefault(context, pkConstraint)) { if (overideColumnDefs == null) { overideColumnDefs = new ArrayList<>(columnDefs); overideColumnDefs.set(i, new ColumnDef(columnDef, null));
private static boolean isPkColumn(PrimaryKeyConstraint pkConstraint, ColumnDef colDef) { return colDef.isPK() || (pkConstraint != null && pkConstraint.contains(colDef.getColumnDefName())); }