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

Cast table properties based on its type in Faker connector #24597

Open
wants to merge 1 commit into
base: master
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 @@ -57,7 +57,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -256,13 +255,18 @@ public synchronized void setTableProperties(ConnectorSession session, ConnectorT
SchemaTableName tableName = handle.schemaTableName();

TableInfo oldInfo = tables.get(tableName);
Map<String, Object> newProperties = Stream.concat(
oldInfo.properties().entrySet().stream()
.filter(entry -> !properties.containsKey(entry.getKey())),
properties.entrySet().stream()
.filter(entry -> entry.getValue().isPresent()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties should already have correct types - would it be enough to add a get() here? Like:

.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get()));

tables.put(tableName, oldInfo.withProperties(newProperties));
ImmutableMap.Builder updatedProperties = ImmutableMap.<String, Object>builder().putAll(oldInfo.properties());
if (properties.containsKey(TableInfo.DEFAULT_LIMIT_PROPERTY)) {
long defaultLimit = (long) properties.get(TableInfo.DEFAULT_LIMIT_PROPERTY)
.orElseThrow(() -> new IllegalArgumentException("The default_limit property cannot be empty"));
updatedProperties.put(TableInfo.DEFAULT_LIMIT_PROPERTY, defaultLimit);
}
if (properties.containsKey(TableInfo.NULL_PROBABILITY_PROPERTY)) {
double nullProbability = (double) properties.get(TableInfo.NULL_PROBABILITY_PROPERTY)
.orElseThrow(() -> new IllegalArgumentException("The null_probability property cannot be empty"));
updatedProperties.put(TableInfo.NULL_PROBABILITY_PROPERTY, nullProbability);
}
tables.put(tableName, oldInfo.withProperties(updatedProperties.buildOrThrow()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,15 @@ String columnSchema()
return "%s %s NOT NULL%s".formatted(name, type, propertiesSchema.isEmpty() ? "" : " WITH (%s)".formatted(propertiesSchema));
}
}

@Test
void testSetTableProperties()
{
try (TestTable table = new TestTable(getQueryRunner()::execute, "set_table_properties", "(id INTEGER, name VARCHAR)")) {
assertUpdate("ALTER TABLE %s SET PROPERTIES default_limit = 100".formatted(table.getName()));
assertQueryFails("ALTER TABLE %s SET PROPERTIES invalid_property = true".formatted(table.getName()), "(?s).*Catalog 'faker' table property 'invalid_property' does not exist");
assertThat((String) computeScalar("SHOW CREATE TABLE " + table.getName()))
.contains("default_limit = 100");
}
}
}
Loading