forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract JDBC write-related configuration to separate class
- Loading branch information
Showing
11 changed files
with
230 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/JdbcWriteConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.jdbc; | ||
|
||
import io.airlift.configuration.Config; | ||
import io.airlift.configuration.ConfigDescription; | ||
|
||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
|
||
public class JdbcWriteConfig | ||
{ | ||
static final int MAX_ALLOWED_WRITE_BATCH_SIZE = 1_000_000; | ||
|
||
private int writeBatchSize = 1000; | ||
|
||
// Do not create temporary table during insert. | ||
// This means that the write operation can fail and leave the table in an inconsistent state. | ||
private boolean nonTransactionalInsert; | ||
|
||
@Min(1) | ||
@Max(MAX_ALLOWED_WRITE_BATCH_SIZE) | ||
public int getWriteBatchSize() | ||
{ | ||
return writeBatchSize; | ||
} | ||
|
||
@Config("write.batch-size") | ||
@ConfigDescription("Maximum number of rows to write in a single batch") | ||
public JdbcWriteConfig setWriteBatchSize(int writeBatchSize) | ||
{ | ||
this.writeBatchSize = writeBatchSize; | ||
return this; | ||
} | ||
|
||
public boolean isNonTransactionalInsert() | ||
{ | ||
return nonTransactionalInsert; | ||
} | ||
|
||
@Config("insert.non-transactional-insert.enabled") | ||
@ConfigDescription("Do not create temporary table during insert. " + | ||
"This means that the write operation can fail and leave the table in an inconsistent state.") | ||
public JdbcWriteConfig setNonTransactionalInsert(boolean nonTransactionalInsert) | ||
{ | ||
this.nonTransactionalInsert = nonTransactionalInsert; | ||
return this; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/JdbcWriteSessionProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.jdbc; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.plugin.base.session.SessionPropertiesProvider; | ||
import io.trino.spi.TrinoException; | ||
import io.trino.spi.connector.ConnectorSession; | ||
import io.trino.spi.session.PropertyMetadata; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.util.List; | ||
|
||
import static io.trino.plugin.jdbc.JdbcWriteConfig.MAX_ALLOWED_WRITE_BATCH_SIZE; | ||
import static io.trino.spi.StandardErrorCode.INVALID_SESSION_PROPERTY; | ||
import static io.trino.spi.session.PropertyMetadata.booleanProperty; | ||
import static io.trino.spi.session.PropertyMetadata.integerProperty; | ||
import static java.lang.String.format; | ||
|
||
public class JdbcWriteSessionProperties | ||
implements SessionPropertiesProvider | ||
{ | ||
public static final String WRITE_BATCH_SIZE = "write_batch_size"; | ||
public static final String NON_TRANSACTIONAL_INSERT = "non_transactional_insert"; | ||
|
||
private final List<PropertyMetadata<?>> properties; | ||
|
||
@Inject | ||
public JdbcWriteSessionProperties(JdbcWriteConfig writeConfig) | ||
{ | ||
properties = ImmutableList.<PropertyMetadata<?>>builder() | ||
.add(integerProperty( | ||
WRITE_BATCH_SIZE, | ||
"Maximum number of rows to write in a single batch", | ||
writeConfig.getWriteBatchSize(), | ||
JdbcWriteSessionProperties::validateWriteBatchSize, | ||
false)) | ||
.add(booleanProperty( | ||
NON_TRANSACTIONAL_INSERT, | ||
"Do not use temporary table on insert to table", | ||
writeConfig.isNonTransactionalInsert(), | ||
false)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public List<PropertyMetadata<?>> getSessionProperties() | ||
{ | ||
return properties; | ||
} | ||
|
||
public static int getWriteBatchSize(ConnectorSession session) | ||
{ | ||
return session.getProperty(WRITE_BATCH_SIZE, Integer.class); | ||
} | ||
|
||
public static boolean isNonTransactionalInsert(ConnectorSession session) | ||
{ | ||
return session.getProperty(NON_TRANSACTIONAL_INSERT, Boolean.class); | ||
} | ||
|
||
private static void validateWriteBatchSize(int maxBatchSize) | ||
{ | ||
if (maxBatchSize < 1) { | ||
throw new TrinoException(INVALID_SESSION_PROPERTY, format("%s must be greater than 0: %s", WRITE_BATCH_SIZE, maxBatchSize)); | ||
} | ||
if (maxBatchSize > MAX_ALLOWED_WRITE_BATCH_SIZE) { | ||
throw new TrinoException(INVALID_SESSION_PROPERTY, format("%s cannot exceed %s: %s", WRITE_BATCH_SIZE, MAX_ALLOWED_WRITE_BATCH_SIZE, maxBatchSize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.