Skip to content
Merged
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 @@ -405,6 +405,82 @@ default String createIndex(String indexName, TableReference table, List<String>
if (columnNames == null || columnNames.isEmpty()) {
throw new IllegalArgumentException("columnNames must not be empty for CREATE INDEX");
}
return indexStatement(indexName, table, columnNames.stream().map(IndexColumn::ascending).toList(), unique,
ifNotExists);
}

/** A column of an index and the order the index sorts it in. */
record IndexColumn(String name, boolean descending) {

public IndexColumn {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("index column name must not be blank");
}
}

/** A column the index sorts ascending, the default order. */
public static IndexColumn ascending(String name) {
return new IndexColumn(name, false);
}

/** A column the index sorts descending. */
public static IndexColumn descending(String name) {
return new IndexColumn(name, true);
}
}

/**
* An index: its name, its table, its columns in order with the order each sorts in, whether it
* is unique, and the condition on its rows for a partial index, or {@code null}.
*/
record IndexDefinition(String name, TableReference table, List<IndexColumn> columns, boolean unique,
String condition) {

public IndexDefinition {
if (columns == null || columns.isEmpty()) {
throw new IllegalArgumentException("columns must not be empty for CREATE INDEX");
}
columns = List.copyOf(columns);
}
}

/**
* {@code CREATE [UNIQUE] INDEX [IF NOT EXISTS] idx ON schema.table (col1 DESC, col2, …) [WHERE condition]}:
* an index whose columns each sort ascending or descending. An ascending column is written
* without an order, as SQL sorts by default.
*
* @return empty for a descending column where the database keeps no order in an index
* ({@link #supportsDescendingIndexColumns()} is false), and for a condition where it has
* no partial indexes ({@link #supportsFilteredIndexes()} is false)
*/
default Optional<String> createIndex(IndexDefinition index, boolean ifNotExists) {
if (index.columns().stream().noneMatch(IndexColumn::descending)) {
return createIndex(index.name(), index.table(),
index.columns().stream().map(IndexColumn::name).toList(), index.unique(), index.condition(),
ifNotExists);
}
if (!supportsDescendingIndexColumns()) {
return Optional.empty();
}
String statement = indexStatement(index.name(), index.table(), index.columns(), index.unique(),
ifNotExists);
if (index.condition() == null || index.condition().isBlank()) {
return Optional.of(statement);
}
return supportsFilteredIndexes() ? Optional.of(statement + " WHERE " + index.condition()) : Optional.empty();
}

/**
* @return true if an index keeps the order of a column it sorts descending
* ({@code CREATE INDEX … (col DESC)}); false where the database has no index DDL or takes
* {@code DESC} and ignores it
*/
default boolean supportsDescendingIndexColumns() {
return false;
}

private String indexStatement(String indexName, TableReference table, List<IndexColumn> columns, boolean unique,
boolean ifNotExists) {
StringBuilder sb = new StringBuilder("CREATE ");
if (unique)
sb.append("UNIQUE ");
Expand All @@ -415,10 +491,12 @@ default String createIndex(String indexName, TableReference table, List<String>
sb.append(quoteIdentifier(indexName));
sb.append(" ON ").append(qualified(table));
sb.append(" (");
for (int i = 0; i < columnNames.size(); i++) {
for (int i = 0; i < columns.size(); i++) {
if (i > 0)
sb.append(", ");
sb.append(quoteIdentifier(columnNames.get(i)));
sb.append(quoteIdentifier(columns.get(i).name()));
if (columns.get(i).descending())
sb.append(" DESC");
}
sb.append(")");
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.dialect.db.consistencytest;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.eclipse.daanse.sql.dialect.api.Dialect;
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator.IndexColumn;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator.IndexDefinition;
import org.eclipse.daanse.sql.dialect.db.clickhouse.ClickHouseDialect;
import org.eclipse.daanse.sql.dialect.db.common.AnsiDialect;
import org.eclipse.daanse.sql.dialect.db.derby.DerbyDialect;
import org.eclipse.daanse.sql.dialect.db.duckdb.DuckDbDialect;
import org.eclipse.daanse.sql.dialect.db.h2.H2Dialect;
import org.eclipse.daanse.sql.dialect.db.hive.HiveDialect;
import org.eclipse.daanse.sql.dialect.db.impala.ImpalaDialect;
import org.eclipse.daanse.sql.dialect.db.mariadb.MariaDBDialect;
import org.eclipse.daanse.sql.dialect.db.mssqlserver.MicrosoftSqlServerDialect;
import org.eclipse.daanse.sql.dialect.db.mysql.MySqlDialect;
import org.eclipse.daanse.sql.dialect.db.oracle.OracleDialect;
import org.eclipse.daanse.sql.dialect.db.postgresql.PostgreSqlDialect;
import org.eclipse.daanse.sql.dialect.db.sqlite.SqliteDialect;
import org.eclipse.daanse.sql.model.schema.SchemaReference;
import org.eclipse.daanse.sql.model.schema.TableReference;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* What each dialect writes for the index {@code ix} on {@code s.t (a DESC, b)}. A dialect whose
* database keeps no order in an index writes nothing for it and says so by
* {@link DdlGenerator#supportsDescendingIndexColumns()}; an index of ascending columns is the plain
* index of every dialect.
*/
class DescendingIndexStatementsTest {

private static final TableReference TABLE = new TableReference(
Optional.of(new SchemaReference(Optional.empty(), "s")), "t");

private static final IndexDefinition DESCENDING = new IndexDefinition("ix", TABLE,
List.of(IndexColumn.descending("a"), IndexColumn.ascending("b")), false, null);

private static final IndexDefinition ASCENDING = new IndexDefinition("ix", TABLE,
List.of(IndexColumn.ascending("a"), IndexColumn.ascending("b")), false, null);

static Stream<Arguments> dialects() {
String quoted = "CREATE INDEX \"ix\" ON \"s\".\"t\" (\"a\" DESC, \"b\")";
String backticked = "CREATE INDEX `ix` ON `s`.`t` (`a` DESC, `b`)";
return Stream.of(of("postgresql", new PostgreSqlDialect(), quoted),
of("oracle", new OracleDialect(), quoted),
of("mssql", new MicrosoftSqlServerDialect(), quoted),
of("mysql", new MySqlDialect(), backticked),
of("mysql 5.7", new MySqlDialect(version(5, 7)), null),
of("mariadb", new MariaDBDialect(), backticked),
of("mariadb 10.6", new MariaDBDialect(version(10, 6)), null),
of("h2", new H2Dialect(), quoted),
of("sqlite", new SqliteDialect(), quoted),
of("derby", new DerbyDialect(), quoted),
of("duckdb", new DuckDbDialect(), null),
of("clickhouse", new ClickHouseDialect(), null),
of("hive", new HiveDialect(), null),
of("impala", new ImpalaDialect(), null),
of("ansi", new AnsiDialect(), null));
}

@ParameterizedTest(name = "{0}")
@MethodSource("dialects")
void anIndexWithADescendingColumn(Dialect dialect, String expected) {
DdlGenerator ddl = dialect.ddlGenerator();

assertThat(ddl.supportsDescendingIndexColumns()).as("supportsDescendingIndexColumns")
.isEqualTo(expected != null);
assertThat(ddl.createIndex(DESCENDING, false)).isEqualTo(Optional.ofNullable(expected));
assertThat(ddl.createIndex(ASCENDING, false))
.contains(ddl.createIndex("ix", TABLE, List.of("a", "b"), false, false));
}

private static Arguments of(String name, Dialect dialect, String expected) {
return Arguments.of(Named.of(name, dialect), expected);
}

private static DialectInitData version(int major, int minor) {
return DialectInitData.ansiDefaults().withVersion(major, minor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,10 @@ public java.util.Optional<String> commentOnColumn(TableReference table, String c
org.eclipse.daanse.sql.model.schema.ColumnMetaData currentMeta) {
return java.util.Optional.empty();
}

/** Derby keeps the order of each column of an index. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,10 @@ public Optional<String> createUser(String userName, boolean ifNotExists) {
return Optional.of("CREATE USER " + (ifNotExists ? "IF NOT EXISTS " : "") + quoteIdentifier(userName)
+ " SALT X'' HASH X''");
}

/** H2 keeps the order of each column of an index. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,13 @@ public boolean supportsUsers() {
return org.eclipse.daanse.sql.dialect.api.DialectVersion.UNKNOWN.equals(dialectVersion)
|| dialectVersion.atLeast(new org.eclipse.daanse.sql.dialect.api.DialectVersion(10, 4, 2));
}

/**
* MariaDB keeps a descending index column from 10.8; before, it took {@code DESC} and ignored it.
* A dialect made without a connection takes such a version.
*/
@Override
public boolean supportsDescendingIndexColumns() {
return dialectVersion.isUnknownOrAtLeast(10, 8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.PaginationGenerator.PreparedPaging;
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
import org.eclipse.daanse.sql.jdbc.metadata.MariaDbMetadataProvider;
Expand Down Expand Up @@ -126,4 +127,11 @@ void user_is_created_given_a_role_and_dropped() throws SQLException {
DatabasePrincipal.KIND_USER, users, "rt_user", "rt_role");
}
}

/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new MariaDbMetadataProvider(), users,
"ix_users_desc", "name", "id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,10 @@ private static String principalNamed(String name) {
public boolean supportsFilteredIndexes() {
return true;
}

/** SQL Server keeps the order of each key column of an index. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.PaginationGenerator.PreparedPaging;
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.meta.IndexInfo;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
Expand Down Expand Up @@ -190,4 +191,11 @@ void filtered_index_is_created_and_read_back() throws SQLException {
s.execute("DELETE FROM [dbo].[users] WHERE name = 'same'");
}
}

/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new MicrosoftSqlServerMetadataProvider(),
users, "ix_users_desc", "name", "id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -878,4 +878,13 @@ public Optional<String> createUser(String userName, boolean ifNotExists) {
return Optional.of("CREATE USER " + (ifNotExists ? "IF NOT EXISTS " : "") + quoteIdentifier(userName)
+ " ACCOUNT LOCK");
}

/**
* MySQL keeps a descending index column from 8.0; before, it took {@code DESC} and ignored it. A
* dialect made without a connection takes such a version.
*/
@Override
public boolean supportsDescendingIndexColumns() {
return dialectVersion.isUnknownOrAtLeast(8, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.PaginationGenerator.PreparedPaging;
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
import org.eclipse.daanse.sql.jdbc.metadata.MySqlMetadataProvider;
Expand Down Expand Up @@ -133,4 +134,11 @@ void user_is_created_given_a_role_and_dropped() throws SQLException {
DatabasePrincipal.KIND_UNKNOWN, users, "rt_user", "rt_role");
}
}

/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new MySqlMetadataProvider(), users,
"ix_users_desc", "name", "id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,10 @@ public Optional<String> createMaterializedView(TableReference view, String selec
public Optional<String> dropMaterializedView(TableReference view, boolean ifExists) {
return super.dropMaterializedView(view, ifExists && dialectVersion.atLeast(23, 0));
}

/** Oracle keeps a descending column as a function-based index column. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.PaginationGenerator.PreparedPaging;
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
import org.eclipse.daanse.sql.jdbc.metadata.OracleMetadataProvider;
Expand Down Expand Up @@ -171,4 +172,11 @@ void materialized_view_is_created_read_back_and_dropped() throws SQLException {
.noneMatch(mv -> mv.view().name().equals("MV_USERS"));
}
}

/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new OracleMetadataProvider(), users,
"IX_USERS_DESC", "NAME", "ID");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,10 @@ public boolean supportsMaterializedViews() {
public boolean supportsFilteredIndexes() {
return true;
}

/** PostgreSQL keeps the order of each column of a B-tree index. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.PaginationGenerator.PreparedPaging;
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.meta.IndexInfo;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
Expand Down Expand Up @@ -174,4 +175,11 @@ void filtered_index_is_created_and_read_back() throws SQLException {
s.execute("DELETE FROM \"public\".\"users\" WHERE name = 'same'");
}
}

/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new PostgreSqlMetadataProvider(), USERS,
"ix_users_desc", "name", "id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,10 @@ public java.util.Optional<String> commentOnColumn(TableReference table, String c
public boolean supportsFilteredIndexes() {
return dialectVersion.isUnknownOrAtLeast(3, 8);
}

/** SQLite keeps the order of each column of an index. */
@Override
public boolean supportsDescendingIndexColumns() {
return true;
}
}
Loading
Loading