Skip to content

Commit

Permalink
Index NULLS NOT DISTINCT #844
Browse files Browse the repository at this point in the history
+ adding index comment

HOWEVER, migration is still an issue: the JDBC driver doesn't return
comments on indices, and there is no attribute to check NULLS NOT
DISTINCT. Need to devise another fuller solution by querying the comment
directly maybe?

NEED FOLLOW UP!
  • Loading branch information
ldhasson committed Dec 22, 2024
1 parent dd9415b commit 3805ab9
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 64 deletions.
12 changes: 11 additions & 1 deletion Tilda/src/tilda/db/metadata/IndexMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import tilda.db.JDBCHelper;

public class IndexMeta
{

Expand All @@ -32,17 +34,19 @@ protected IndexMeta(ResultSet RS, TableMeta parentTable/*, int indexPos*/)
throws Exception
{
// see https://learn.microsoft.com/en-us/sql/connect/jdbc/reference/getindexinfo-method-sqlserverdatabasemetadata?view=sql-server-ver16
// LOG.debug(JDBCHelper.printResultSet(RS));
// LOG.debug(JDBCHelper.printResultSet(RS));
_Name = RS.getString("INDEX_NAME" );
_Unique = RS.getBoolean("NON_UNIQUE") == false;
_Cluster = RS.getInt("TYPE") == 1;
_FilterCondition = RS.getString("FILTER_CONDITION");
_ParentTable = parentTable;
}

public final String _Name ;
public final boolean _Unique;
public final TableMeta _ParentTable;
public final boolean _Cluster;
public final String _FilterCondition;
public final List<IndexColumnMeta> _Columns = new ArrayList<IndexColumnMeta>();

public String getCleanName()
Expand Down Expand Up @@ -107,6 +111,12 @@ public String getSignature()
Str.append("|" + (ICM._Asc == null || ICM._Asc == true ? "asc" : "desc"));
}

Str.append(_Cluster==true?"|clustered":"|nonclustered");

// This is not viable right now as the database requires the filter clause and we can't compare it afterwards for migration.
// if (_FilterCondition != null)
// Str.append("|").append(_FilterCondition);

return (_Unique ? "u" : "") + "i|" + Str.toString();
}
}
14 changes: 11 additions & 3 deletions Tilda/src/tilda/db/stores/CommonStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,14 @@ else if (IX._Db == false)
Query Q = IX._SubQuery.getQuery(DBType.Postgres);
Out.print(" where " + Q._ClauseStatic);
}
Out.println(";");

if (IX._NullsNotDistinct == true)
Out.print(" NULLS NOT DISTINCT");

Out.print(";");

if (IX._Cluster == true)
Out.println("ALTER TABLE " + IX._Parent.getShortName() + " CLUSTER on " + IX.getName() + ";");
Out.print("ALTER TABLE " + IX._Parent.getShortName() + " CLUSTER on " + IX.getName() + ";");

return OutStr.toString();
}
Expand All @@ -870,7 +874,11 @@ public boolean alterTableAddIndex(Connection Con, Index IX)
if (supportsIndices() == false)
return true;

return Con.executeDDL(IX._Parent._ParentSchema._Name, IX._Parent.getBaseName(), alterTableAddIndexDDL(IX));
String Q = alterTableAddIndexDDL(IX);
if (Con.executeDDL(IX._Parent._ParentSchema._Name, IX._Parent.getBaseName(), Q) == false)
return false;
Q = "COMMENT ON INDEX "+IX._Parent._ParentSchema._Name.toUpperCase()+"."+IX.getName()+" IS E" + TextUtil.escapeSingleQuoteForSQL(Q) + ";";
return Con.executeDDL(IX._Parent._ParentSchema._Name, IX._Parent.getBaseName(), Q);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions Tilda/src/tilda/migration/Migrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,10 @@ else if (IX._Cluster == false && ix._Cluster == true) // removing cluster
boolean Found = false;
String Sig = IX.getSignature();

// LOG.debug("Checking Index: '"+Sig+"'");
for (IndexMeta ix : TMeta._Indices.values())
{
// LOG.debug(" - against index: '"+ix.getSignature()+"'");
if (!ix._Name.toLowerCase().equals(TMeta._TableName.toLowerCase() + "_pkey"))
{
String Sig1 = ix.getSignature();
Expand Down
35 changes: 28 additions & 7 deletions Tilda/src/tilda/parsing/parts/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.gson.annotations.SerializedName;

import tilda.db.stores.DBType;
import tilda.enums.ColumnMode;
import tilda.parsing.ParserSession;
import tilda.parsing.parts.helpers.ValidationHelper;
Expand All @@ -30,13 +31,14 @@
public class Index
{
/*@formatter:off*/
@SerializedName("name" ) public String _Name ;
@SerializedName("columns" ) public String[] _Columns;
@SerializedName("cluster" ) public boolean _Cluster = false;
@SerializedName("orderBy" ) public String[] _OrderBy;
@SerializedName("db" ) public boolean _Db = true;
@SerializedName("subWhere") public String _SubWhere;
@SerializedName("subQuery") public SubWhereClause _SubQuery;
@SerializedName("name" ) public String _Name ;
@SerializedName("columns" ) public String[] _Columns;
@SerializedName("cluster" ) public boolean _Cluster = false;
@SerializedName("orderBy" ) public String[] _OrderBy;
@SerializedName("db" ) public boolean _Db = true;
@SerializedName("nullsNotDistinct") public boolean _NullsNotDistinct = false;
@SerializedName("subWhere" ) public String _SubWhere;
@SerializedName("subQuery" ) public SubWhereClause _SubQuery;
/*@formatter:on*/

public transient List<Column> _ColumnObjs = new ArrayList<Column>();
Expand Down Expand Up @@ -150,6 +152,15 @@ public boolean process(ParserSession PS, Base ParentObject, String What, Column
PS.AddError("Object '" + _Parent.getFullName() + "' is defining unique index '" + _Name + "' with a subQuery, which is not allowed.");
if (TextUtil.isNullOrEmpty(_SubWhere) == false)
PS.AddError("Object '" + _Parent.getFullName() + "' is defining unique index '" + _Name + "' with a subWhere, which is not allowed.");
boolean nullCol = false;
for (Column col : _ColumnObjs)
if (col != null && col._Nullable == true)
{
nullCol = true;
break;
}
if (nullCol == false && _NullsNotDistinct == true)
PS.AddError("Object '" + _Parent.getFullName() + "' is defining unique index '" + _Name + "' with no null columns, yet nullsNotDistinct is set to true.");
}

if (_Cluster == true && _Db == false)
Expand Down Expand Up @@ -186,6 +197,16 @@ public String getSignature()
if (OB._Nulls != null)
Str.append("|").append(OB._Nulls.name().toLowerCase());
}

Str.append(_Cluster==true?"|clustered":"|nonclustered");


// This is not viable right now as the database requires the filter clause and we can't compare it afterwards for migration.
// if (_SubQuery != null)
// {
// Str.append("|").append(_SubWhere);
// }

return (_Unique ? "ui|" : "i|") + Str.toString();
}

Expand Down
Loading

0 comments on commit 3805ab9

Please sign in to comment.