Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AUTO_INCREMENT support for SQL #521

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class ExportedColumn {
public final String physicalColumnName;
public final int physicalPosition;
public final boolean primary;
public final boolean autoIncrement;


public String getDisplayType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public void dropGraph( Context context, AllocationGraph allocation ) {
@Override
public List<PhysicalEntity> createCollection( Context context, LogicalCollection logical, AllocationCollection allocation ) {
PhysicalTable physical = Scannable.createSubstitutionTable( modifiable, context, logical, allocation, "_doc_", List.of(
new ColumnContext( DocumentType.DOCUMENT_ID, null, PolyType.TEXT, false ),
new ColumnContext( DocumentType.DOCUMENT_DATA, null, PolyType.TEXT, true ) ), 1 );
new ColumnContext( DocumentType.DOCUMENT_ID, null, PolyType.TEXT, false, false ),
new ColumnContext( DocumentType.DOCUMENT_DATA, null, PolyType.TEXT, true, false ) ), 1 );
catalog.addPhysical( allocation, physical );

return List.of( physical );
Expand Down
32 changes: 16 additions & 16 deletions core/src/main/java/org/polypheny/db/adapter/Scannable.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static PhysicalEntity createSubstitutionEntity( Scannable scannable, Context con

int i = 0;
for ( ColumnContext col : columnsInformations ) {
LogicalColumn column = new LogicalColumn( builder.getNewFieldId(), col.name, table.id, table.namespaceId, i, col.type, null, col.precision, null, null, null, col.nullable, Collation.getDefaultCollation(), null );
LogicalColumn column = new LogicalColumn( builder.getNewFieldId(), col.name, table.id, table.namespaceId, i, col.type, null, col.precision, null, null, null, col.nullable, Collation.getDefaultCollation(), null, col.autoIncrement );
columns.add( column );
i++;
}
Expand Down Expand Up @@ -170,24 +170,24 @@ static AlgNode getDocumentScanSubstitute( Scannable scannable, long allocId, Alg

static List<PhysicalEntity> createGraphSubstitute( Scannable scannable, Context context, LogicalGraph logical, AllocationGraph allocation ) {
PhysicalEntity node = createSubstitutionEntity( scannable, context, logical, allocation, "_node_", List.of(
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false ),
new ColumnContext( "label", null, PolyType.TEXT, false ) ), 2 );
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false, false ),
new ColumnContext( "label", null, PolyType.TEXT, false, false ) ), 2 );

PhysicalEntity nProperties = createSubstitutionEntity( scannable, context, logical, allocation, "_nProperties_", List.of(
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false ),
new ColumnContext( "key", null, PolyType.TEXT, false ),
new ColumnContext( "value", null, PolyType.TEXT, true ) ), 2 );
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false, false ),
new ColumnContext( "key", null, PolyType.TEXT, false, false ),
new ColumnContext( "value", null, PolyType.TEXT, true, false ) ), 2 );

PhysicalEntity edge = createSubstitutionEntity( scannable, context, logical, allocation, "_edge_", List.of(
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false ),
new ColumnContext( "label", null, PolyType.TEXT, true ),
new ColumnContext( "_l_id_", GraphType.ID_SIZE, PolyType.VARCHAR, true ),
new ColumnContext( "_r_id_", GraphType.ID_SIZE, PolyType.VARCHAR, true ) ), 1 );
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false, false ),
new ColumnContext( "label", null, PolyType.TEXT, true, false ),
new ColumnContext( "_l_id_", GraphType.ID_SIZE, PolyType.VARCHAR, true, false ),
new ColumnContext( "_r_id_", GraphType.ID_SIZE, PolyType.VARCHAR, true, false ) ), 1 );

PhysicalEntity eProperties = createSubstitutionEntity( scannable, context, logical, allocation, "_eProperties_", List.of(
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false ),
new ColumnContext( "key", null, PolyType.TEXT, false ),
new ColumnContext( "value", null, PolyType.TEXT, true ) ), 2 );
new ColumnContext( "id", GraphType.ID_SIZE, PolyType.VARCHAR, false, false ),
new ColumnContext( "key", null, PolyType.TEXT, false, false ),
new ColumnContext( "value", null, PolyType.TEXT, true, false ) ), 2 );

scannable.getCatalog().addPhysical( allocation, node, nProperties, edge, eProperties );
return List.of( node, nProperties, edge, eProperties );
Expand Down Expand Up @@ -222,8 +222,8 @@ static void dropGraphSubstitute( Scannable scannable, Context context, Allocatio

static List<PhysicalEntity> createCollectionSubstitute( Scannable scannable, Context context, LogicalCollection logical, AllocationCollection allocation ) {
PhysicalEntity doc = createSubstitutionEntity( scannable, context, logical, allocation, "_doc_", List.of(
new ColumnContext( DocumentType.DOCUMENT_ID, null, PolyType.TEXT, false ),
new ColumnContext( DocumentType.DOCUMENT_DATA, null, PolyType.TEXT, false ) ), 1 );
new ColumnContext( DocumentType.DOCUMENT_ID, null, PolyType.TEXT, false, false ),
new ColumnContext( DocumentType.DOCUMENT_DATA, null, PolyType.TEXT, false, false ) ), 1 );

scannable.getCatalog().addPhysical( allocation, doc );
return List.of( doc );
Expand All @@ -250,7 +250,7 @@ static void dropCollectionSubstitute( Scannable scannable, Context context, Allo
void renameLogicalColumn( long id, String newColumnName );


record ColumnContext( String name, Integer precision, PolyType type, boolean nullable ) {
record ColumnContext( String name, Integer precision, PolyType type, boolean nullable, boolean autoIncrement ) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ default AlgDataType asGraph() {
*/
boolean isNullable();

boolean isAutoIncrement();

/**
* Gets the component type if this type is a collection, otherwise null.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@ public boolean isNullable() {
return nullable;
}

@Override
public boolean isAutoIncrement() {
return false;
}


@Override
public AlgDataTypeFamily getFamily() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public boolean isNullable() {
return false;
}

@Override
public boolean isAutoIncrement() {
return false;
}


@Override
public Charset getCharset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public boolean isNullable() {
return false;
}

@Override
public boolean isAutoIncrement() {
return false;
}


@Override
public AlgDataType getComponentType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public boolean isNullable() {
return false;
}

@Override
public boolean isAutoIncrement() {
return false;
}


@Override
public AlgDataType getComponentType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public interface LogicalRelationalCatalog extends LogicalCatalog {
* @param collation The collation of the field (if applicable, else null)
* @return The id of the inserted column
*/
LogicalColumn addColumn( String name, long tableId, int position, PolyType type, PolyType collectionsType, Integer length, Integer scale, Integer dimension, Integer cardinality, boolean nullable, Collation collation );
LogicalColumn addColumn( String name, long tableId, int position, PolyType type, PolyType collectionsType, Integer length, Integer scale, Integer dimension, Integer cardinality, boolean nullable, Collation collation, boolean autoIncrement );


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.activej.serializer.annotations.Serialize;
import io.activej.serializer.annotations.SerializeNullable;
import java.io.Serial;
import java.util.concurrent.atomic.AtomicLong;
import lombok.NonNull;
import lombok.Value;
import lombok.experimental.NonFinal;
Expand Down Expand Up @@ -91,6 +92,12 @@ public class LogicalColumn implements PolyObject, Comparable<LogicalColumn> {
@JsonProperty
public boolean nullable;

@Serialize
@JsonProperty
public boolean autoIncrement;

public AtomicLong currentValue = new AtomicLong(0l);

@Serialize
@JsonProperty
public @SerializeNullable Collation collation;
Expand All @@ -117,7 +124,8 @@ public LogicalColumn(
@Deserialize("cardinality") final Integer cardinality,
@Deserialize("nullable") final boolean nullable,
@Deserialize("collation") final Collation collation,
@Deserialize("defaultValue") final LogicalDefaultValue defaultValue ) {
@Deserialize("defaultValue") final LogicalDefaultValue defaultValue,
@Deserialize("autoIncrement") final boolean autoIncrement) {
this.id = id;
this.name = name;
this.tableId = tableId;
Expand All @@ -132,6 +140,7 @@ public LogicalColumn(
this.nullable = nullable;
this.collation = collation;
this.defaultValue = defaultValue;
this.autoIncrement = autoIncrement;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public void deleteKey( long id ) {


@Override
public LogicalColumn addColumn( String name, long tableId, int position, PolyType type, PolyType collectionsType, Integer length, Integer scale, Integer dimension, Integer cardinality, boolean nullable, Collation collation ) {
public LogicalColumn addColumn( String name, long tableId, int position, PolyType type, PolyType collectionsType, Integer length, Integer scale, Integer dimension, Integer cardinality, boolean nullable, Collation collation, boolean autoIncrement ) {
long id = idBuilder.getNewFieldId();
LogicalColumn column = new LogicalColumn( id, name, tableId, logicalNamespace.id, position, type, collectionsType, length, scale, dimension, cardinality, nullable, collation, null );
LogicalColumn column = new LogicalColumn( id, name, tableId, logicalNamespace.id, position, type, collectionsType, length, scale, dimension, cardinality, nullable, collation, null, autoIncrement );
columns.put( id, column );
change( CatalogEvent.LOGICAL_REL_FIELD_CREATED, null, id );
return column;
Expand Down
22 changes: 18 additions & 4 deletions core/src/main/java/org/polypheny/db/ddl/DdlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static DdlManager getInstance() {
* @param defaultValue a default value for the column; can be null
* @param statement the query statement
*/
public abstract void createColumn( String columnName, LogicalTable table, String beforeColumnName, String afterColumnName, ColumnTypeInformation type, boolean nullable, PolyValue defaultValue, Statement statement );
public abstract void createColumn( String columnName, LogicalTable table, String beforeColumnName, String afterColumnName, ColumnTypeInformation type, boolean nullable, PolyValue defaultValue, Statement statement, boolean autoIncrement );

/**
* Add a foreign key to a table
Expand Down Expand Up @@ -605,7 +605,7 @@ public ConstraintInformation( String name, ConstraintType type, List<String> col
* decoupled from the used query language
*/

public record ColumnTypeInformation( PolyType type, @Nullable PolyType collectionType, Integer precision, Integer scale, Integer dimension, Integer cardinality, Boolean nullable ) {
public record ColumnTypeInformation( PolyType type, @Nullable PolyType collectionType, Integer precision, Integer scale, Integer dimension, Integer cardinality, Boolean nullable, Boolean autoIncrement ) {

public ColumnTypeInformation(
PolyType type,
Expand All @@ -614,14 +614,27 @@ public ColumnTypeInformation(
Integer scale,
Integer dimension,
Integer cardinality,
Boolean nullable ) {
Boolean nullable,
Boolean autoIncrement ) {
this.type = type;
this.collectionType = collectionType == type ? null : collectionType;
this.precision = precision == null || precision == -1 ? null : precision;
this.scale = scale == null || scale == -1 || scale == Integer.MIN_VALUE ? null : scale;
this.dimension = dimension == null || dimension == -1 ? null : dimension;
this.cardinality = cardinality == null || cardinality == -1 ? null : cardinality;
this.nullable = nullable;
this.autoIncrement = autoIncrement == null ? false : autoIncrement;
}

public ColumnTypeInformation(
PolyType type,
@Nullable PolyType collectionType,
Integer precision,
Integer scale,
Integer dimension,
Integer cardinality,
Boolean nullable ) {
this(type, collectionType, precision, scale, dimension, cardinality, nullable, false);
}


Expand All @@ -633,7 +646,8 @@ public static ColumnTypeInformation fromDataTypeSpec( DataTypeSpec sqlDataType )
sqlDataType.getScale(),
sqlDataType.getDimension(),
sqlDataType.getCardinality(),
sqlDataType.getNullable() );
sqlDataType.getNullable(),
sqlDataType.getAutoIncrement());
}

}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/polypheny/db/nodes/DataTypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface DataTypeSpec extends Visitable {

Boolean getNullable();

Boolean getAutoIncrement();

Identifier getCollectionsTypeName();

PolyType getCollectionsType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class AbstractPolyType extends AlgDataTypeImpl implements Clonea

protected final PolyType typeName;
protected boolean isNullable;
protected boolean isAutoIncrement;


/**
Expand Down Expand Up @@ -79,6 +80,11 @@ public boolean isNullable() {
return isNullable;
}

@Override
public boolean isAutoIncrement() {
return isAutoIncrement;
}


// implement RelDataType
@Override
Expand Down
23 changes: 15 additions & 8 deletions dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public void createSource( String uniqueName, String adapterName, long namespace,
exportedColumn.dimension,
exportedColumn.cardinality,
exportedColumn.nullable,
Collation.getDefaultCollation() );
Collation.getDefaultCollation(),
exportedColumn.autoIncrement);

AllocationColumn allocationColumn = catalog.getAllocRel( namespace ).addColumn(
placement.id,
Expand Down Expand Up @@ -408,7 +409,8 @@ public void addColumnToSourceTable( LogicalTable table, String columnPhysicalNam
exportedColumn.dimension,
exportedColumn.cardinality,
exportedColumn.nullable,
Collation.getDefaultCollation()
Collation.getDefaultCollation(),
exportedColumn.autoIncrement
);

// Add default value
Expand Down Expand Up @@ -455,7 +457,7 @@ private void updateColumnPosition( LogicalTable table, LogicalColumn column, int


@Override
public void createColumn( String columnName, LogicalTable table, String beforeColumnName, String afterColumnName, ColumnTypeInformation type, boolean nullable, PolyValue defaultValue, Statement statement ) {
public void createColumn( String columnName, LogicalTable table, String beforeColumnName, String afterColumnName, ColumnTypeInformation type, boolean nullable, PolyValue defaultValue, Statement statement, boolean autoIncrement ) {
columnName = adjustNameIfNeeded( columnName, table.namespaceId );
// Check if the column either allows null values or has a default value defined.
if ( defaultValue == null && !nullable ) {
Expand Down Expand Up @@ -484,7 +486,8 @@ public void createColumn( String columnName, LogicalTable table, String beforeCo
type.dimension(),
type.cardinality(),
nullable,
Collation.getDefaultCollation()
Collation.getDefaultCollation(),
autoIncrement
);

// Add default value
Expand Down Expand Up @@ -1683,7 +1686,8 @@ public void createView( String viewName, long namespaceId, AlgNode algNode, AlgC
column.typeInformation().dimension(),
column.typeInformation().cardinality(),
column.typeInformation().nullable(),
column.collation() );
column.collation(),
column.typeInformation().autoIncrement());
}

catalog.updateSnapshot();
Expand Down Expand Up @@ -1973,7 +1977,8 @@ private List<FieldInformation> getViewColumnInformation( List<String> projectedC
type.getScale(),
alg.getType().getPolyType() == PolyType.ARRAY ? (int) ((ArrayType) alg.getType()).getDimension() : -1,
alg.getType().getPolyType() == PolyType.ARRAY ? (int) ((ArrayType) alg.getType()).getCardinality() : -1,
alg.getType().isNullable() ),
alg.getType().isNullable(),
alg.getType().isAutoIncrement()),
Collation.getDefaultCollation(),
null,
position ) );
Expand All @@ -1992,7 +1997,8 @@ private List<FieldInformation> getViewColumnInformation( List<String> projectedC
-1,
-1,
-1,
false ),
false,
false),
Collation.getDefaultCollation(),
null,
position ) );
Expand Down Expand Up @@ -2762,7 +2768,8 @@ private LogicalColumn addColumn( long namespaceId, String columnName, ColumnType
typeInformation.dimension(),
typeInformation.cardinality(),
typeInformation.nullable(),
collation
collation,
typeInformation.autoIncrement()
);

// Add default value
Expand Down
Loading