Skip to content

Support primitive type arrays contains #3300

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -261,10 +261,23 @@ private static LogicalOperator generateCorrelatedFieldAccess(@Nonnull Expression
() -> String.format(Locale.ROOT, "join correlation can occur only on column of repeated type, not %s type", expression.getDataType()));
final var explode = new ExplodeExpression(expression.getUnderlying());
final var resultingQuantifier = Quantifier.forEach(Reference.of(explode));
final var outputAttributes = Expressions.of(convertToExpressions(resultingQuantifier));

Expressions outputAttributes;
if (resultingQuantifier.getFlowedObjectType().isPrimitive()) {
outputAttributes = Expressions.of(convertPrimitiveArrayToExpressions(resultingQuantifier, explode, alias));
} else {
outputAttributes = Expressions.of(convertToExpressions(resultingQuantifier));
}
return LogicalOperator.newOperator(alias, outputAttributes, resultingQuantifier);
}

@Nonnull
private static Expressions convertPrimitiveArrayToExpressions(@Nonnull Quantifier quantifier, @Nonnull ExplodeExpression explodeExpression, @Nonnull Optional<Identifier> alias) {
final ImmutableList.Builder<Expression> attributesBuilder = ImmutableList.builder();
attributesBuilder.add(new Expression(alias, DataTypeUtils.toRelationalType(explodeExpression.getResultValue().getResultType()), quantifier.getFlowedObjectValue()));
return Expressions.of(attributesBuilder.build());
}

@Nonnull
public static Expressions convertToExpressions(@Nonnull Quantifier quantifier) {
final ImmutableList.Builder<Expression> attributesBuilder = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ void simpleSelect() throws Exception {

@Test
void simpleSelectWithNonNullableArrays() throws Exception {
// var ddl = Ddl.builder().database(URI.create("/TEST/QT")).relationalExtension(relationalExtension).schemaTemplate(schemaTemplateWithNonNullableArrays).build();
try (var ddl = Ddl.builder().database(URI.create("/TEST/QT")).relationalExtension(relationalExtension).schemaTemplate(schemaTemplateWithNonNullableArrays).build()) {
try (var statement = ddl.setSchemaAndGetConnection().createStatement()) {
var insertedRecord = insertRestaurantComplexRecord(statement);
Expand Down Expand Up @@ -1360,6 +1359,61 @@ void unionIsNotSupported() throws Exception {
}
}

@Test
void structArrayContains() throws Exception {
final String schemaTemplate = "CREATE TYPE AS STRUCT A(col2 string, col3 bigint, col4 bigint) " +
"CREATE TABLE T1(col1 bigint, a A Array, col5 bigint, primary key(col1))";
try (var ddl = Ddl.builder().database(URI.create("/TEST/QT")).relationalExtension(relationalExtension).schemaTemplate(schemaTemplate).build()) {
try (var statement = ddl.setSchemaAndGetConnection().createStatement()) {
statement.executeUpdate("insert into t1 values (42, [('Apple', 1, 100), ('Orange', 2, 200)], 142), (44, [('Grape', 3, 300), ('Pear', 4, 400)], 144)");
Assertions.assertTrue(statement.execute("SELECT T1.col5, X.col2, X.col3 FROM T1, (SELECT col2, col3 FROM T1.A) X where X.col2 = 'Grape'"));
try (final RelationalResultSet resultSet = statement.getResultSet()) {
ResultSetAssert.assertThat(resultSet).hasNextRow()
.isRowExactly(144L, "Grape", 3L)
.hasNoNextRow();
}
Assertions.assertTrue(statement.execute("SELECT T1.col5 FROM T1 where exists (SELECT col2 FROM T1.A where col2 = 'Grape')"));
try (final RelationalResultSet resultSet = statement.getResultSet()) {
ResultSetAssert.assertThat(resultSet).hasNextRow()
.isRowExactly(144L)
.hasNoNextRow();
}
Assertions.assertTrue(statement.execute("SELECT T1.col5, X.col2, X.col3 FROM T1, (SELECT col2, col3 FROM T1.A) X where X.col2 in ('Grape', 'Orange')"));
try (final RelationalResultSet resultSet = statement.getResultSet()) {
ResultSetAssert.assertThat(resultSet).hasNextRow()
.isRowExactly(144L, "Grape", 3L)
.hasNextRow()
.isRowExactly(142L, "Orange", 2L)
.hasNoNextRow();
}
}
}
}

@Test
void primitiveArrayContains() throws Exception {
final String schemaTemplate = "CREATE TABLE T1(col1 bigint, a string Array, primary key(col1))";
try (var ddl = Ddl.builder().database(URI.create("/TEST/QT")).relationalExtension(relationalExtension).schemaTemplate(schemaTemplate).build()) {
try (var statement = ddl.setSchemaAndGetConnection().createStatement()) {
statement.executeUpdate("insert into t1 values (42, ['Apple', 'Orange']), (44, ['Grape', 'Pear'])");
Assertions.assertTrue(statement.execute("SELECT * FROM T1 where exists (SELECT 1 FROM T1.A r where r = 'Grape')"));
try (final RelationalResultSet resultSet = statement.getResultSet()) {
ResultSetAssert.assertThat(resultSet).hasNextRow()
.isRowExactly(44L, EmbeddedRelationalArray.newBuilder().addString("Grape").addString("Pear").build())
.hasNoNextRow();
}
Assertions.assertTrue(statement.execute("SELECT * FROM T1 where exists (SELECT 1 FROM T1.A r where r in ('Grape', 'Orange'))"));
try (final RelationalResultSet resultSet = statement.getResultSet()) {
ResultSetAssert.assertThat(resultSet).hasNextRow()
.isRowExactly(42L, EmbeddedRelationalArray.newBuilder().addString("Apple").addString("Orange").build())
.hasNextRow()
.isRowExactly(44L, EmbeddedRelationalArray.newBuilder().addString("Grape").addString("Pear").build())
.hasNoNextRow();
}
}
}
}

@Test
void cteWorksCorrectly() throws Exception {
final String schemaTemplate = "CREATE TABLE T1(pk bigint, a bigint, b bigint, c bigint, PRIMARY KEY(pk))";
Expand Down
Loading