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

refactor numeric code and handle typmod better #2091

Open
wants to merge 6 commits 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
4 changes: 2 additions & 2 deletions flow/connectors/bigquery/qrep.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"cloud.google.com/go/bigquery"

"github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model"
"github.com/PeerDB-io/peer-flow/model/qvalue"
Expand Down Expand Up @@ -39,6 +38,7 @@ func (c *BigQueryConnector) SyncQRepRecords(
tblMetadata, stream, config.SyncedAtColName, config.SoftDeleteColName)
}

// TODO: consider removing this codepath entirely
func (c *BigQueryConnector) replayTableSchemaDeltasQRep(
ctx context.Context,
config *protos.QRepConfig,
Expand Down Expand Up @@ -74,7 +74,7 @@ func (c *BigQueryConnector) replayTableSchemaDeltasQRep(
tableSchemaDelta.AddedColumns = append(tableSchemaDelta.AddedColumns, &protos.FieldDescription{
Name: col.Name,
Type: string(col.Type),
TypeModifier: datatypes.MakeNumericTypmod(int32(col.Precision), int32(col.Scale)),
TypeModifier: col.ParsedNumericTypmod.ToTypmod(),
},
)
}
Expand Down
6 changes: 3 additions & 3 deletions flow/connectors/bigquery/qrep_avro_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cloud.google.com/go/bigquery"

avro "github.com/PeerDB-io/peer-flow/connectors/utils/avro"
"github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model"
"github.com/PeerDB-io/peer-flow/model/qvalue"
Expand Down Expand Up @@ -268,9 +269,6 @@ func DefineAvroSchema(dstTableName string,
}

func GetAvroType(bqField *bigquery.FieldSchema) (interface{}, error) {
avroNumericPrecision, avroNumericScale := qvalue.DetermineNumericSettingForDWH(
int16(bqField.Precision), int16(bqField.Scale), protos.DBType_BIGQUERY)

considerRepeated := func(typ string, repeated bool) interface{} {
if repeated {
return qvalue.AvroSchemaArray{
Expand Down Expand Up @@ -341,6 +339,8 @@ func GetAvroType(bqField *bigquery.FieldSchema) (interface{}, error) {
},
}, nil
case bigquery.BigNumericFieldType:
avroNumericPrecision, avroNumericScale := datatypes.NewConstrainedNumericTypmod(int16(bqField.Precision),
int16(bqField.Scale)).ToDWHNumericConstraints(protos.DBType_BIGQUERY)
return qvalue.AvroSchemaNumeric{
Type: "bytes",
LogicalType: "decimal",
Expand Down
17 changes: 11 additions & 6 deletions flow/connectors/bigquery/qvalue_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func qValueKindToBigQueryType(columnDescription *protos.FieldDescription, nullab
case qvalue.QValueKindFloat32, qvalue.QValueKindFloat64:
bqField.Type = bigquery.FloatFieldType
case qvalue.QValueKindNumeric:
precision, scale := datatypes.GetNumericTypeForWarehouse(columnDescription.TypeModifier, datatypes.BigQueryNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(columnDescription.TypeModifier).
ToDWHNumericConstraints(protos.DBType_BIGQUERY)
bqField.Type = bigquery.BigNumericFieldType
bqField.Precision = int64(precision)
bqField.Scale = int64(scale)
Expand Down Expand Up @@ -150,11 +151,15 @@ func qValueKindToBigQueryTypeString(columnDescription *protos.FieldDescription,
}

func BigQueryFieldToQField(bqField *bigquery.FieldSchema) qvalue.QField {
var parsedNumericTypmod *datatypes.NumericTypmod
if BigQueryTypeToQValueKind(bqField) == qvalue.QValueKindNumeric {
parsedNumericTypmod = datatypes.NewConstrainedNumericTypmod(int16(bqField.Precision),
int16(bqField.Scale))
}
return qvalue.QField{
Name: bqField.Name,
Type: BigQueryTypeToQValueKind(bqField),
Precision: int16(bqField.Precision),
Scale: int16(bqField.Scale),
Nullable: !bqField.Required,
Name: bqField.Name,
Type: BigQueryTypeToQValueKind(bqField),
Nullable: !bqField.Required,
ParsedNumericTypmod: parsedNumericTypmod,
}
}
6 changes: 4 additions & 2 deletions flow/connectors/clickhouse/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func generateCreateTableSQLForNormalizedTable(
}

if colType == qvalue.QValueKindNumeric {
precision, scale := datatypes.GetNumericTypeForWarehouse(column.TypeModifier, datatypes.ClickHouseNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(column.TypeModifier).
ToDWHNumericConstraints(protos.DBType_CLICKHOUSE)
if column.Nullable {
stmtBuilder.WriteString(fmt.Sprintf("`%s` Nullable(DECIMAL(%d, %d)), ", dstColName, precision, scale))
} else {
Expand Down Expand Up @@ -301,7 +302,8 @@ func (c *ClickhouseConnector) NormalizeRecords(
colSelector.WriteString(fmt.Sprintf("`%s`,", dstColName))
if clickhouseType == "" {
if colType == qvalue.QValueKindNumeric {
precision, scale := datatypes.GetNumericTypeForWarehouse(column.TypeModifier, datatypes.ClickHouseNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(column.TypeModifier).
ToDWHNumericConstraints(protos.DBType_CLICKHOUSE)
clickhouseType = fmt.Sprintf("Decimal(%d, %d)", precision, scale)
} else {
var err error
Expand Down
4 changes: 2 additions & 2 deletions flow/connectors/postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/lib/pq/oid"

"github.com/PeerDB-io/peer-flow/connectors/utils"
numeric "github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model"
"github.com/PeerDB-io/peer-flow/shared"
Expand Down Expand Up @@ -463,7 +463,7 @@ func generateCreateTableSQLForNormalizedTable(
pgColumnType = qValueKindToPostgresType(pgColumnType)
}
if column.Type == "numeric" && column.TypeModifier != -1 {
precision, scale := numeric.ParseNumericTypmod(column.TypeModifier)
precision, scale := datatypes.NewParsedNumericTypmod(column.TypeModifier).PrecisionAndScale()
pgColumnType = fmt.Sprintf("numeric(%d,%d)", precision, scale)
}
var notNull string
Expand Down
10 changes: 4 additions & 6 deletions flow/connectors/postgres/qrep_query_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ func (qe *QRepQueryExecutor) fieldDescriptionsToSchema(fds []pgconn.FieldDescrip
// TODO fix this.
cnullable := true
if ctype == qvalue.QValueKindNumeric {
precision, scale := datatypes.ParseNumericTypmod(fd.TypeModifier)
qfields[i] = qvalue.QField{
Name: cname,
Type: ctype,
Nullable: cnullable,
Precision: precision,
Scale: scale,
Name: cname,
Type: ctype,
Nullable: cnullable,
ParsedNumericTypmod: datatypes.NewParsedNumericTypmod(fd.TypeModifier),
}
} else {
qfields[i] = qvalue.QField{
Expand Down
11 changes: 8 additions & 3 deletions flow/connectors/snowflake/get_schema_for_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ func (c *SnowflakeConnector) getTableSchemaForTable(ctx context.Context, tableNa
genericColType = qvalue.QValueKindString
}

colFields = append(colFields, &protos.FieldDescription{
colField := &protos.FieldDescription{
Name: columns[i].ColumnName,
Type: string(genericColType),
TypeModifier: datatypes.MakeNumericTypmod(sfColumn.NumericPrecision, sfColumn.NumericScale),
})
TypeModifier: -1,
}
if genericColType == qvalue.QValueKindNumeric {
colField.TypeModifier = datatypes.NewConstrainedNumericTypmod(int16(sfColumn.NumericPrecision),
int16(sfColumn.NumericScale)).ToTypmod()
}
colFields = append(colFields, colField)
}

return &protos.TableSchema{
Expand Down
5 changes: 3 additions & 2 deletions flow/connectors/snowflake/merge_stmt_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/PeerDB-io/peer-flow/connectors/utils"
numeric "github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model/qvalue"
"github.com/PeerDB-io/peer-flow/shared"
Expand Down Expand Up @@ -62,7 +62,8 @@ func (m *mergeStmtGenerator) generateMergeStmt(dstTable string) (string, error)
// "Microseconds*1000) "+
// "AS %s", toVariantColumnName, columnName, columnName))
case qvalue.QValueKindNumeric:
precision, scale := numeric.GetNumericTypeForWarehouse(column.TypeModifier, numeric.SnowflakeNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(column.TypeModifier).
ToDWHNumericConstraints(protos.DBType_SNOWFLAKE)
numericType := fmt.Sprintf("NUMERIC(%d,%d)", precision, scale)
flattenedCastsSQLArray = append(flattenedCastsSQLArray,
fmt.Sprintf("TRY_CAST((%s:\"%s\")::text AS %s) AS %s",
Expand Down
8 changes: 5 additions & 3 deletions flow/connectors/snowflake/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

metadataStore "github.com/PeerDB-io/peer-flow/connectors/external_metadata"
"github.com/PeerDB-io/peer-flow/connectors/utils"
numeric "github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/datatypes"
"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/logger"
"github.com/PeerDB-io/peer-flow/model"
Expand Down Expand Up @@ -383,7 +383,8 @@ func (c *SnowflakeConnector) ReplayTableSchemaDeltas(
}

if addedColumn.Type == string(qvalue.QValueKindNumeric) {
precision, scale := numeric.GetNumericTypeForWarehouse(addedColumn.TypeModifier, numeric.SnowflakeNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(addedColumn.TypeModifier).
ToDWHNumericConstraints(protos.DBType_SNOWFLAKE)
sfColtype = fmt.Sprintf("NUMERIC(%d,%d)", precision, scale)
}

Expand Down Expand Up @@ -686,7 +687,8 @@ func generateCreateTableSQLForNormalizedTable(
}

if genericColumnType == "numeric" {
precision, scale := numeric.GetNumericTypeForWarehouse(column.TypeModifier, numeric.SnowflakeNumericCompatibility{})
precision, scale := datatypes.NewParsedNumericTypmod(column.TypeModifier).
ToDWHNumericConstraints(protos.DBType_SNOWFLAKE)
sfColType = fmt.Sprintf("NUMERIC(%d,%d)", precision, scale)
}

Expand Down
Loading
Loading