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

Fix duplicate results and failures of queries with indexes and query parameters and improve index scan generation [HZ-3014] [HZ-3013] #25527

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ static IndexIterationPointer[] indexFilterToPointers(
boolean descending,
ExpressionEvalContext evalContext
) {
ArrayList<IndexIterationPointer> result = new ArrayList<>();
List<IndexIterationPointer> result = new ArrayList<>();
createFromIndexFilterInt(indexFilter, compositeIndex, descending, evalContext, result);
result = IndexIterationPointer.normalizePointers(result, descending);
return result.toArray(new IndexIterationPointer[0]);
}

Expand Down Expand Up @@ -173,6 +174,8 @@ private static void createFromIndexFilterInt(
} else if (indexFilter instanceof IndexEqualsFilter) {
IndexEqualsFilter equalsFilter = (IndexEqualsFilter) indexFilter;
Comparable<?> value = equalsFilter.getComparable(evalContext);
// Note: this branch is also used for IS NULL, but null value in IndexEqualsFilter
// is mapped to NULL by getComparable, so we can easily use it in the same way as ordinary values.
result.add(IndexIterationPointer.create(value, true, value, true, descending, null));
} else if (indexFilter instanceof IndexCompositeFilter) {
IndexCompositeFilter inFilter = (IndexCompositeFilter) indexFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hazelcast.jet.sql.impl.connector.map.index;

import com.google.common.base.Predicates;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.hazelcast.config.IndexConfig;
Expand Down Expand Up @@ -43,7 +44,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -148,7 +148,6 @@ public void test() {
}

@Test
@Ignore("HZ-3013")
public void testDisjunctionSameValue() {
// Test for index scans with disjunctions that match the same row.
// SQL query must not return duplicate rows in such case.
Expand Down Expand Up @@ -185,6 +184,19 @@ public void testDisjunctionOverlappingRange() {
);
}

@Test
public void testConjunctionOfRanges() {
///// Conjunction of ranges open on the same end
check(query("field1>? and field1>?", f1.valueFrom(), f1.valueTo()),
c_sorted(), gt(f1.valueTo()));
check(query("field1>=? and field1>=?", f1.valueFrom(), f1.valueTo()),
c_sorted(), gte(f1.valueTo()));
check(query("field1<? and field1<?", f1.valueFrom(), f1.valueTo()),
c_sorted(), lt(f1.valueFrom()));
check(query("field1<=? and field1<=?", f1.valueFrom(), f1.valueTo()),
c_sorted(), lte(f1.valueFrom()));
}

@Test
public void testOrderBy() {
check0(query("1=1 order by field1"), c_sorted(), v -> true);
Expand Down Expand Up @@ -232,6 +244,12 @@ private void checkFirstColumn() {
check(query("?=field1 or ?=field1", f1.valueFrom(), f1.valueTo()),
c_notHashComposite(), or(eq(f1.valueFrom()), eq(f1.valueTo())));

// WHERE f1=? and f1=? - contradictory values
check(query("field1=? and field1=?", f1.valueTo(), f1.valueFrom()),
c_notHashComposite(), Predicates.alwaysFalse());
check(query("?=field1 and ?=field1", f1.valueFrom(), f1.valueTo()),
c_notHashComposite(), Predicates.alwaysFalse());

// WHERE f1=? or f1 is null
check(query("field1=? or field1 is null", f1.valueFrom()),
c_notHashComposite(), or(eq(f1.valueFrom()), isNull()));
Expand Down Expand Up @@ -325,27 +343,47 @@ private void checkFirstColumn() {
c_sorted(),
and(gt(f1.valueFrom()), lt(f1.valueTo()))
);
check(
query("field1>? AND field1<?", f1.valueTo(), f1.valueFrom()),
c_sorted(),
Predicates.alwaysFalse()
);

// WHERE f1>? AND f1<=?
check(
query("field1>? AND field1<=?", f1.valueFrom(), f1.valueTo()),
c_sorted(),
and(gt(f1.valueFrom()), lte(f1.valueTo()))
);
check(
query("field1>? AND field1<=?", f1.valueTo(), f1.valueFrom()),
c_sorted(),
Predicates.alwaysFalse()
);

// WHERE f1>=? AND f1<?
check(
query("field1>=? AND field1<?", f1.valueFrom(), f1.valueTo()),
c_sorted(),
and(gte(f1.valueFrom()), lt(f1.valueTo()))
);
check(
query("field1>=? AND field1<?", f1.valueTo(), f1.valueFrom()),
c_sorted(),
Predicates.alwaysFalse()
);

// WHERE f1>=? AND f1<=?
check(
query("field1>=? AND field1<=?", f1.valueFrom(), f1.valueTo()),
c_sorted(),
and(gte(f1.valueFrom()), lte(f1.valueTo()))
);
check(
query("field1>=? AND field1<=?", f1.valueTo(), f1.valueFrom()),
c_sorted(),
Predicates.alwaysFalse()
);

///// 2 disjoint unlimited ranges

Expand Down Expand Up @@ -596,17 +634,10 @@ private void check(Query query, boolean expectedUseIndex, boolean expectedUseInd
// Run query with OR, no index should be used
check0(queryWithOr, false, expectedKeysPredicateWithOr);

// TODO: enable after fixing HZ-3012 and HZ-3013
// TODO: remove `or field1 is null` condition after those fixes as well
// Sorting is so costly that index should be preferred regardless of predicates
// For hash index sorting does not use index, but scan still can use it.
if (!query.sql.toLowerCase(Locale.ROOT).contains("or field1 is null") || !c_sorted()) {
check0(queryWithOrderBy, expectedUseIndex || c_sorted(), expectedKeysPredicate);
if (!c_sorted()) {
// TODO: DESC works correctly only for HASH, will be enabled globally after fixing HZ-3012 and HZ-3013
check0(queryWithOrderByDesc, expectedUseIndex || c_sorted(), expectedKeysPredicate);
}
}
check0(queryWithOrderBy, expectedUseIndex || c_sorted(), expectedKeysPredicate);
check0(queryWithOrderByDesc, expectedUseIndex || c_sorted(), expectedKeysPredicate);
}

private void check0(Query query, boolean expectedUseIndex, Predicate<ExpressionValue> expectedKeysPredicate) {
Expand Down