Search in sources :

Example 91 with Type

use of com.google.spanner.v1.Type in project pgadapter by GoogleCloudPlatform.

the class MutationWriter method buildMutationList.

/**
 * Build mutation to add to mutations list with data contained within a CopyData payload
 */
public void buildMutationList(ConnectionHandler connectionHandler) throws Exception {
    List<CSVRecord> records = parsePayloadData(this.payload.toByteArray());
    for (CSVRecord record : records) {
        // Check that the number of columns in a record matches the number of columns in the table
        if (record.size() != this.tableColumns.keySet().size()) {
            handleError(connectionHandler);
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid COPY data: Row length mismatched. Expected " + this.tableColumns.keySet().size() + " columns, but only found " + record.size());
        }
        WriteBuilder builder = Mutation.newInsertBuilder(this.tableName);
        // Iterate through all table column to copy into
        for (String columnName : this.tableColumns.keySet()) {
            TypeCode columnType = this.tableColumns.get(columnName);
            String recordValue = "";
            try {
                recordValue = record.get(columnName).trim();
                switch(columnType) {
                    case STRING:
                        builder.set(columnName).to(recordValue);
                        break;
                    case BOOL:
                        builder.set(columnName).to(Boolean.parseBoolean(recordValue));
                        break;
                    case INT64:
                        builder.set(columnName).to(Long.parseLong(recordValue));
                        break;
                    case FLOAT64:
                        builder.set(columnName).to(Double.parseDouble(recordValue));
                        break;
                    case BYTES:
                        builder.set(columnName).to(Byte.parseByte(recordValue));
                        break;
                    case TIMESTAMP:
                        builder.set(columnName).to(com.google.cloud.Timestamp.parseTimestamp(recordValue));
                        break;
                }
            } catch (NumberFormatException | DateTimeParseException e) {
                handleError(connectionHandler);
                throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid input syntax for type " + columnType.toString() + ":" + "\"" + recordValue + "\"");
            } catch (IllegalArgumentException e) {
                handleError(connectionHandler);
                throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid input syntax for column \"" + columnName + "\"");
            } catch (Exception e) {
                handleError(connectionHandler);
                throw e;
            }
        }
        // Add write builder to mutation list
        this.mutations.add(builder.build());
        // Increment the number of mutations being added
        this.mutationCount += record.size();
        // Increment the number of COPY rows by one
        this.rowCount++;
    }
    if (!mutationCountIsWithinLimit()) {
        handleError(connectionHandler);
        throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "Mutation count: " + mutationCount + " has exceeded the limit: " + MUTATION_LIMIT);
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TypeCode(com.google.spanner.v1.TypeCode) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException) DateTimeParseException(java.time.format.DateTimeParseException)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)22 Type (edu.stanford.CVC4.Type)14 ArrayList (java.util.ArrayList)14 ByteString (com.google.protobuf.ByteString)13 Type (com.google.spanner.v1.Type)12 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 MapType (com.google.api.expr.v1alpha1.Type.MapType)10 Type (org.apache.xbean.asm9.Type)10 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 FormulaType (org.sosy_lab.java_smt.api.FormulaType)8 ListValue (com.google.protobuf.ListValue)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7 CheckerEnv.isObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType)7