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 @@ -156,10 +156,12 @@ private static String nullToEmpty(String s) {

/**
* {@link DialectVersion} view of {@link #databaseMajorVersion} /
* {@link #databaseMinorVersion}.
* {@link #databaseMinorVersion}, with the patch of {@link #productVersion} where that names the same major and
* minor: a check of a patch ({@code 5.7.8}, {@code 8.0.19}) took every version of its minor for its {@code .0}
* before.
*/
public DialectVersion version() {
return new DialectVersion(databaseMajorVersion, databaseMinorVersion);
return DialectVersion.of(databaseMajorVersion, databaseMinorVersion, productVersion);
}

/** Returns a copy with a different {@link IdentifierQuotingPolicy}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public int compareTo(DialectVersion o) {
return ORDER.compare(this, o);
}

/**
* The version of a major and a minor, with the patch of {@code productVersion} where that is a version of the
* same major and minor ({@code 8.0.44} of 8.0: 8.0.44); else the patch is 0, as of a product version that names
* another version ({@code 5.5.5-10.11.6-MariaDB} of 10.11). No major and no minor is {@link #UNKNOWN}.
*/
static DialectVersion of(int major, int minor, String productVersion) {
if (major <= 0 && minor <= 0) {
return UNKNOWN;
}
boolean same = productVersion != null && productVersion.startsWith(major + "." + minor + ".");
return new DialectVersion(major, minor, same ? parsePatch(productVersion) : 0);
}

private static int parsePatch(String productVersion) {
if (productVersion == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.common;

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

import java.util.Set;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.DialectVersion;
import org.eclipse.daanse.sql.dialect.api.IdentifierQuotingPolicy;
import org.junit.jupiter.api.Test;

/**
* The version a dialect checks carries the patch of the product version, where that names the same major and minor:
* a check of a patch ({@code 5.7.8}, {@code 8.0.19}) took every version of its minor for its {@code .0} before.
*/
class DialectInitDataVersionTest {

private static DialectInitData init(String productVersion, int major, int minor) {
return new DialectInitData("`", "MySQL", productVersion, major, minor, Set.of(), true, 0, Set.of(),
IdentifierQuotingPolicy.ALWAYS);
}

@Test
void the_patch_of_the_product_version() {
assertThat(init("8.0.44", 8, 0).version()).isEqualTo(new DialectVersion(8, 0, 44));
assertThat(init("5.7.44-log", 5, 7).version()).isEqualTo(new DialectVersion(5, 7, 44));
assertThat(init("11.4.2-MariaDB-ubu2404", 11, 4).version()).isEqualTo(new DialectVersion(11, 4, 2));
assertThat(init("16.4 (Debian 16.4-1.pgdg120+2)", 16, 4).version()).isEqualTo(new DialectVersion(16, 4, 0));
}

@Test
void a_check_of_a_patch() {
DialectVersion v8018 = init("8.0.18", 8, 0).version();
DialectVersion v8019 = init("8.0.19", 8, 0).version();
assertThat(v8018.atLeast(new DialectVersion(8, 0, 19))).isFalse();
assertThat(v8019.atLeast(new DialectVersion(8, 0, 19))).isTrue();
assertThat(init("5.7.7", 5, 7).version().atLeast(new DialectVersion(5, 7, 8))).isFalse();
assertThat(init("5.7.44", 5, 7).version().atLeast(new DialectVersion(5, 7, 8))).isTrue();
assertThat(v8019.atLeast(8, 0)).as("a check of a minor as before").isTrue();
}

@Test
void no_patch_of_a_product_version_that_names_another_version() {
assertThat(init("5.5.5-10.11.6-MariaDB", 10, 11).version()).isEqualTo(new DialectVersion(10, 11, 0));
assertThat(init("8.0.44", 8, 0).withVersion(5, 7).version()).as("withVersion names another version")
.isEqualTo(new DialectVersion(5, 7, 0));
}

@Test
void no_version_is_unknown() {
assertThat(DialectInitData.ansiDefaults().version()).isEqualTo(DialectVersion.UNKNOWN);
assertThat(init("8.0.44", 0, 0).version()).isEqualTo(DialectVersion.UNKNOWN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ protected boolean supportsNativeRenameColumn() {
return dialectVersion.isUnknownOrAtLeast(10, 5);
}

/**
* MariaDB's {@code RENAME INDEX} landed in 10.5.2, with {@code RENAME COLUMN}: not MySQL's 5.7, which every
* MariaDB 10 and 11 would pass. A dialect made without a connection takes such a version.
*/
@Override
public boolean supportsRenameIndex() {
return org.eclipse.daanse.sql.dialect.api.DialectVersion.UNKNOWN.equals(dialectVersion)
|| dialectVersion.atLeast(new org.eclipse.daanse.sql.dialect.api.DialectVersion(10, 5, 2));
}

@Override
public boolean supportsRenameSequence() {
return dialectVersion.isUnknownOrAtLeast(10, 5);
Expand Down Expand Up @@ -196,4 +206,13 @@ public boolean supportsDescendingIndexColumns() {
protected boolean createOrReplaceRoutine() {
return true;
}

/**
* MariaDB has no alias of the row an upsert would insert: its update columns read it with
* {@code VALUES(col)}, on every version.
*/
@Override
protected boolean upsertRowAlias() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ void twoDigitMajorIsAtLeast57ForOrderByAlias() {
assertTrue(dialect(10, 6).requiresOrderByAlias());
}

/**
* MariaDB has the percentiles as window functions only, with an OVER clause; as an aggregate of a group, as the
* dialect writes them, it refuses them on every version (MariaDBPercentileTest runs them against the server).
*/
@Test
void twoDigitMajorSupportsPercentiles() {
assertTrue(dialect(11, 4).supportsPercentileDisc());
assertTrue(dialect(11, 4).supportsPercentileCont());
assertTrue(dialect(10, 6).supportsPercentileDisc());
void noPercentilesOfAGroupOnAnyVersion() {
assertFalse(dialect(11, 4).supportsPercentileDisc());
assertFalse(dialect(11, 4).supportsPercentileCont());
assertFalse(dialect(10, 6).supportsPercentileDisc());
assertTrue(dialect(11, 4).generatePercentileCont(0.5, false, "t", "v").isEmpty());
}

@Test
Expand All @@ -49,7 +54,5 @@ void twoDigitMajorAllowsFromQuery() {
void oldMySqlStyleVersionsStillGateCorrectly() {
assertFalse(dialect(5, 6).requiresOrderByAlias());
assertTrue(dialect(5, 7).requiresOrderByAlias());
assertFalse(dialect(5, 7).supportsPercentileDisc());
assertTrue(dialect(8, 0).supportsPercentileDisc());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.mariadb.container;

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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.db.mariadb.MariaDBDialect;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.MariaDBContainer;

/**
* MariaDB has {@code PERCENTILE_DISC} and {@code PERCENTILE_CONT … WITHIN GROUP} as window functions, with an
* {@code OVER} clause, from 10.3.3. The dialect writes a percentile as an aggregate of a group, without one, and is
* built from a live connection: where it offers a percentile, the server must compute it as such an aggregate, per
* group too; where the server takes it as a window function only, as every version here does, the dialect must offer
* none and write none.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class MariaDBPercentileTest {

/** Group a: 10, 20, 30, 40 and a NULL that every aggregate leaves out; group b: 5. */
private static final List<String> SETUP = List.of("CREATE TABLE nums(grp VARCHAR(1), v INT)",
"INSERT INTO nums VALUES ('a', 30), ('a', 10), ('a', NULL), ('a', 40), ('a', 20), ('b', 5)");

@ParameterizedTest(name = "mariadb:{0} -> percentiles {1}")
@CsvSource({ "10.4, false", "10.6, false", "10.11, false", "11.4, false" })
void percentile(String tag, boolean expected) throws Exception {
try (MariaDBContainer<?> db = new MariaDBContainer<>("mariadb:" + tag).withDatabaseName("rt")
.withUsername("rt").withPassword("rt")) {
db.start();
try (Connection c = db.createConnection(""); Statement st = c.createStatement()) {
for (String sql : SETUP) {
st.execute(sql);
}
MariaDBDialect dialect = new MariaDBDialect(DialectInitData.fromConnection(c));

assertThat(rows(st, "SELECT DISTINCT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY v)"
+ " OVER (PARTITION BY grp) FROM nums WHERE grp = 'a'"))
.as("MariaDB has the percentile as a window function").containsExactly("25");

assertThat(dialect.supportsPercentileDisc()).as("supportsPercentileDisc").isEqualTo(expected);
assertThat(dialect.supportsPercentileCont()).as("supportsPercentileCont").isEqualTo(expected);

if (!expected) {
assertThat(dialect.generatePercentileDisc(0.5, false, "nums", "v")).isEmpty();
assertThat(dialect.generatePercentileCont(0.5, false, "nums", "v")).isEmpty();
assertThatThrownBy(() -> rows(st,
"SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY v) FROM nums WHERE grp = 'a'"))
.as("not as an aggregate of a group").isInstanceOf(SQLException.class);
assertThatThrownBy(() -> rows(st,
"SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY v) FROM nums WHERE grp = 'a'"))
.as("not as an aggregate of a group").isInstanceOf(SQLException.class);
return;
}

String disc = dialect.generatePercentileDisc(0.5, false, "nums", "v").orElseThrow();
String cont = dialect.generatePercentileCont(0.5, false, "nums", "v").orElseThrow();
assertThat(rows(st, "SELECT " + disc + " FROM nums WHERE grp = 'a'")).containsExactly("20");
assertThat(rows(st, "SELECT " + cont + " FROM nums WHERE grp = 'a'")).containsExactly("25");
assertThat(rows(st, "SELECT CONCAT(grp, '=', " + cont + ") FROM nums GROUP BY grp ORDER BY grp"))
.as("per group").containsExactly("a=25", "b=5");
}
}
}

/** The first column of each row; a number without the zeros of its fraction. */
private static List<String> rows(Statement st, String sql) throws SQLException {
List<String> out = new ArrayList<>();
try (ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
out.add(Optional.ofNullable(rs.getString(1))
.map(s -> s.contains(".") ? s.replaceAll("0+$", "").replaceAll("\\.$", "") : s).orElse(null));
}
}
return out;
}
}
Loading
Loading