Skip to content

Commit

Permalink
Add facility to handle non null column type (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitvc authored and eolivelli committed Mar 21, 2019
1 parent 6213a41 commit ff5fc84
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 9 deletions.
Expand Up @@ -19,6 +19,7 @@
*/
package herddb.core.system;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -61,14 +62,15 @@ protected Iterable<Record> buildVirtualRecordList() {
int pos = 1;
for (Column c : t.columns) {
boolean pk = t.isPrimaryKeyColumn(c.name);
boolean nonNullCType = pk ? true : ColumnTypes.isNotNullDataType(c.type);
String data_type = ColumnTypes.typeToString(c.type);

result.add(RecordSerializer.makeRecord(
table,
"table_name", t.name,
"column_name", c.name,
"ordinal_position", pos++,
"is_nullable", pk ? 0 : 1,
"is_nullable", nonNullCType ? DatabaseMetaData.columnNoNulls : DatabaseMetaData.columnNullable,
"data_type", data_type,
"auto_increment", (pk && t.auto_increment)?1:0
));
Expand Down
25 changes: 25 additions & 0 deletions herddb-core/src/main/java/herddb/model/ColumnTypes.java
Expand Up @@ -81,4 +81,29 @@ public static boolean isNotNullDataType(int type) {
}
}

/**
* Utility method that takes in type and returns the supported not null equivalent. If the current
* version does not support not null constraints on the type the method throws an exception
* @param type
* @return
* @throws StatementExecutionException
*/
public static int getNonNullTypeForPrimitiveType(int type) throws StatementExecutionException {
switch(type) {
case STRING:
return NOTNULL_STRING;
case INTEGER:
return NOTNULL_INTEGER;
case LONG:
return NOTNULL_LONG;
case BYTEARRAY:
case TIMESTAMP:
case DOUBLE:
case BOOLEAN:
case NULL:
default:
throw new StatementExecutionException("Not null constraints not supported for column type "+ type);
}
}

}
8 changes: 7 additions & 1 deletion herddb-core/src/main/java/herddb/sql/DDLSQLPlanner.java
Expand Up @@ -353,7 +353,6 @@ private Statement buildCreateTableStatement(String defaultTableSpace, CreateTabl
int type;
String dataType = cf.getColDataType().getDataType();
type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList());
tablebuilder.column(columnName, type, position++);

if (cf.getColumnSpecStrings() != null) {
List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecStrings());
Expand All @@ -365,7 +364,14 @@ private Statement buildCreateTableStatement(String defaultTableSpace, CreateTabl
if (auto_increment && primaryKey.contains(cf.getColumnName())) {
tablebuilder.primaryKey(columnName, auto_increment);
}

if(String.join("_",columnSpecs).equals("NOT_NULL")) {
type = ColumnTypes.getNonNullTypeForPrimitiveType(type);
}
}

tablebuilder.column(columnName, type, position++);

}

if (!foundPk) {
Expand Down
Expand Up @@ -112,7 +112,7 @@ public void multipleColumnPrimaryKeyPrefixScanWithAliasTest() throws Exception {
+ " MSG_ID BIGINT NOT NULL,\n"
+ " SID TINYINT NOT NULL, \n"
+ " STATUS INT NOT NULL,\n"
+ " TIMESTAMP TIMESTAMP NOT NULL,\n"
+ " TIMESTAMP TIMESTAMP,\n"
+ " STATUSLINE VARCHAR(2000) NULL,\n"
+ " IDBOUNCECATEGORY SMALLINT NULL,\n"
+ " PRIMARY KEY (MSG_ID, SID)\n"
Expand Down
97 changes: 91 additions & 6 deletions herddb-core/src/test/java/herddb/core/SystemTablesTest.java
Expand Up @@ -24,18 +24,16 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.DatabaseMetaData;
import java.util.Collections;
import java.util.List;

import herddb.model.*;
import org.junit.Test;

import herddb.mem.MemoryCommitLogManager;
import herddb.mem.MemoryDataStorageManager;
import herddb.mem.MemoryMetadataStorageManager;
import herddb.model.DataScanner;
import herddb.model.StatementEvaluationContext;
import herddb.model.TransactionContext;
import herddb.model.Tuple;
import herddb.model.commands.CreateTableSpaceStatement;
import herddb.utils.DataAccessor;
import herddb.utils.RawString;
Expand All @@ -51,7 +49,7 @@ public class SystemTablesTest {
@Test
public void testSysTables() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null);) {
try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null);) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
Expand Down Expand Up @@ -351,4 +349,91 @@ public void testSysTables() throws Exception {
}
}

}
@Test
public void testSystemTablesForNonNullColumnTypes() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null);) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);

execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key auto_increment,n1 int,s1 string not null)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.tsql2 (k1 string primary key auto_increment,n1 int not null,l1 long not null,s1 string not null)", Collections.emptyList());
execute(manager, "CREATE BRIN INDEX index1 on tblspace1.tsql2 (s1,n1)", Collections.emptyList());
execute(manager, "CREATE HASH INDEX index2 on tblspace1.tsql2 (n1)", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.syscolumns", Collections.emptyList());) {
List<DataAccessor> records = scan.consume();
assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql")
&& t.get("column_name").equals("k1")
&& t.get("data_type").equals("string")
&& t.get("auto_increment").equals(1)
).findAny().isPresent());

assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql")
&& t.get("column_name").equals("n1")
&& t.get("data_type").equals("integer")
).findAny().isPresent());

assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql")
&& t.get("column_name").equals("s1")
&& t.get("data_type").equals("string not null")
&& t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)
).findAny().isPresent());


assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql2")
&& t.get("column_name").equals("k1")
&& t.get("data_type").equals("string")
&& t.get("auto_increment").equals(1)
).findAny().isPresent());

assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql2")
&& t.get("column_name").equals("n1")
&& t.get("data_type").equals("integer not null")
&& t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)
).findAny().isPresent());

assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql2")
&& t.get("column_name").equals("s1")
&& t.get("data_type").equals("string not null")
&& t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)
).findAny().isPresent());

assertTrue(records.stream()
.filter(t
-> t.get("table_name").equals("tsql2")
&& t.get("column_name").equals("l1")
&& t.get("data_type").equals("long not null")
&& t.get("is_nullable").equals(DatabaseMetaData.columnNoNulls)
).findAny().isPresent());
}
}
}

@Test(expected = StatementExecutionException.class)
public void testForNonSupportedTypesWithNotNullConstraints() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null);) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);

execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key auto_increment,n1 int,d1 double not null)", Collections.emptyList());
}

}
}

0 comments on commit ff5fc84

Please sign in to comment.