Skip to content

Handle nullable columns #233

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 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
5b5a0e4
[WIP] Handle nullable columns
hariso Dec 25, 2024
44db32b
improve integration test
hariso Dec 25, 2024
df71d71
improve tests
hariso Dec 25, 2024
74e3d99
get table info
hariso Dec 26, 2024
5924975
fixing tests
hariso Dec 26, 2024
cc1ecf8
rename
hariso Dec 26, 2024
0d1533b
updated integration test
hariso Dec 27, 2024
e961a17
first validate, then refresh tables
hariso Dec 27, 2024
afbe851
linter
hariso Dec 27, 2024
5a8913a
Merge branch 'main' into haris/handle-nullable-columns
hariso Dec 27, 2024
53b79f6
support timestamps
hariso Dec 27, 2024
ec5d5f0
Merge branch 'main' into haris/handle-nullable-columns
hariso Jan 13, 2025
321f149
Merge changes for big.Rat
hariso May 29, 2025
752982f
uncomment numeric, fix stuff
hariso May 29, 2025
d79dc99
table_info: support capital case in table name
hariso May 29, 2025
830eb6f
better way to take a pointer
hariso May 29, 2025
b7b60c9
fix types_test
hariso Jun 13, 2025
98a0a3c
minor change
hariso Jun 13, 2025
cf60d50
add more types to source integration test
hariso Jun 13, 2025
74275b4
update avro_integration_test
hariso Jun 13, 2025
2f9de01
fix tests
hariso Jun 13, 2025
84d8a20
more tests fixed
hariso Jun 13, 2025
28ed11b
fix fetch worker int test
hariso Jun 16, 2025
dcf70c4
updates
hariso Jun 16, 2025
4297c35
fix usage of pointers in tests
hariso Jun 17, 2025
0027279
fix usage of pointers in tests
hariso Jun 17, 2025
d396f70
Merge branch 'main' into haris/handle-nullable-columns
hariso Jun 17, 2025
6b79643
fixes
hariso Jun 17, 2025
b40a8ef
fix cdc_test
hariso Jun 17, 2025
5aa9fd2
fix source_integration_test
hariso Jun 17, 2025
aef7582
fix source_integration_test
hariso Jun 17, 2025
7bc7d76
sync with main
hariso Jun 17, 2025
7d70122
add comment
hariso Jun 17, 2025
b2c5210
better source_integration_test
hariso Jun 17, 2025
40c7476
more checks in source_integration_test
hariso Jun 17, 2025
06f4114
more checks in source_integration_test
hariso Jun 17, 2025
d372fe7
fix source_integration_test
hariso Jun 18, 2025
6647017
format, fixes
hariso Jun 18, 2025
9c0cc47
move method
hariso Jun 18, 2025
1ce91bf
move method
hariso Jun 18, 2025
5ea78ab
move method
hariso Jun 18, 2025
f1f44b6
destination from main
hariso Jun 18, 2025
0bbc863
fix date
hariso Jun 18, 2025
2447721
lint
hariso Jun 18, 2025
8327705
finally done with source_integration_test
hariso Jun 18, 2025
aa732dd
simplify method
hariso Jun 18, 2025
e00e218
col_bytea
hariso Jun 18, 2025
edbfe3c
simplify method
hariso Jun 18, 2025
ee8f150
simplify col_date
hariso Jun 18, 2025
49c99ed
no normalization needed
hariso Jun 18, 2025
19cabba
less type casting
hariso Jun 18, 2025
66b66d3
simplify test
hariso Jun 18, 2025
99f0030
add comment
hariso Jun 18, 2025
42ecfd5
add time zone offsets
hariso Jun 18, 2025
fa095eb
comments
hariso Jun 18, 2025
0eb72c5
move table_info
hariso Jun 18, 2025
0fb14ac
fix uuid test
hariso Jun 18, 2025
0db44e4
remove comment
hariso Jun 18, 2025
4406396
add test for delete
hariso Jun 20, 2025
59a0f5d
refactor integration test, add note about delete test
hariso Jun 20, 2025
dc9f97d
refactor integration test, add note about delete test
hariso Jun 20, 2025
09ac652
refactor tests
hariso Jun 20, 2025
2826c91
rename file
hariso Jun 20, 2025
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 destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Destination struct {
getTableName destination.TableFn

conn *pgx.Conn
dbInfo *internal.DbInfo
dbInfo *internal.NumericScaleInfo
stmtBuilder sq.StatementBuilderType
}

Expand Down Expand Up @@ -347,7 +347,7 @@ func (d *Destination) formatBigRat(ctx context.Context, table string, column str

// we need to get the scale of the column so we that we can properly
// round the result of dividing the input big.Rat's numerator and denominator.
scale, err := d.dbInfo.GetNumericColumnScale(ctx, table, column)
scale, err := d.dbInfo.Get(ctx, table, column)
if err != nil {
return "", fmt.Errorf("failed getting scale of numeric column: %w", err)
}
Expand Down
36 changes: 22 additions & 14 deletions destination_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDestination_Write(t *testing.T) {

// tables with capital letters should be quoted
tableName := strings.ToUpper(test.RandomIdentifier(t))
test.SetupTestTableWithName(ctx, t, conn, tableName)
test.SetupTableWithName(ctx, t, conn, tableName)

d := NewDestination()
err := sdk.Util.ParseConfig(
Expand Down Expand Up @@ -70,10 +70,11 @@ func TestDestination_Write(t *testing.T) {
Key: opencdc.StructuredData{"id": 5000},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("123"),
"column1": "foo",
"column2": 123,
"column3": true,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 222,
},
},
Expand All @@ -88,10 +89,11 @@ func TestDestination_Write(t *testing.T) {
Key: opencdc.StructuredData{"id": 5},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("234"),
"column1": "foo",
"column2": 456,
"column3": false,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 333,
},
},
Expand All @@ -106,10 +108,11 @@ func TestDestination_Write(t *testing.T) {
Key: opencdc.StructuredData{"id": 6},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("345"),
"column1": "bar",
"column2": 567,
"column3": true,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 444,
},
},
Expand All @@ -124,10 +127,11 @@ func TestDestination_Write(t *testing.T) {
Key: opencdc.StructuredData{"id": 1},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("456"),
"column1": "foobar",
"column2": 567,
"column3": true,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 555,
},
},
Expand All @@ -151,6 +155,7 @@ func TestDestination_Write(t *testing.T) {
Key: opencdc.StructuredData{"id": 123},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("567"),
"column1": "abcdef",
"column2": 567,
"column3": true,
Expand Down Expand Up @@ -179,9 +184,7 @@ func TestDestination_Write(t *testing.T) {
cmp.Diff(
tt.record.Payload.After,
got,
cmp.Comparer(func(x, y *big.Rat) bool {
return x.Cmp(y) == 0
}),
test.BigRatComparer,
),
) // -want, +got
case opencdc.OperationDelete:
Expand All @@ -197,7 +200,7 @@ func TestDestination_Batch(t *testing.T) {
conn := test.ConnectSimple(ctx, t, test.RegularConnString)

tableName := strings.ToUpper(test.RandomIdentifier(t))
test.SetupTestTableWithName(ctx, t, conn, tableName)
test.SetupTableWithName(ctx, t, conn, tableName)

d := NewDestination()

Expand All @@ -223,10 +226,11 @@ func TestDestination_Batch(t *testing.T) {
Key: opencdc.StructuredData{"id": 5},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("123"),
"column1": "foo1",
"column2": 1,
"column3": false,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 111,
},
},
Expand All @@ -237,10 +241,11 @@ func TestDestination_Batch(t *testing.T) {
Key: opencdc.StructuredData{"id": 6},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("234"),
"column1": "foo2",
"column2": 2,
"column3": true,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 222,
},
},
Expand All @@ -251,10 +256,11 @@ func TestDestination_Batch(t *testing.T) {
Key: opencdc.StructuredData{"id": 7},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"key": []uint8("345"),
"column1": "foo3",
"column2": 3,
"column3": false,
"column4": nil,
"column4": big.NewRat(123, 10),
"UppercaseColumn1": 333,
},
},
Expand All @@ -275,19 +281,20 @@ func TestDestination_Batch(t *testing.T) {
func queryTestTable(ctx context.Context, conn test.Querier, tableName string, id any) (opencdc.StructuredData, error) {
row := conn.QueryRow(
ctx,
fmt.Sprintf(`SELECT column1, column2, column3, column4, "UppercaseColumn1" FROM %q WHERE id = $1`, tableName),
fmt.Sprintf(`SELECT key, column1, column2, column3, column4, "UppercaseColumn1" FROM %q WHERE id = $1`, tableName),
id,
)

var (
key []uint8
col1 string
col2 int
col3 bool
col4Str *string
uppercaseCol1 int
)

err := row.Scan(&col1, &col2, &col3, &col4Str, &uppercaseCol1)
err := row.Scan(&key, &col1, &col2, &col3, &col4Str, &uppercaseCol1)
if err != nil {
return nil, err
}
Expand All @@ -301,6 +308,7 @@ func queryTestTable(ctx context.Context, conn test.Querier, tableName string, id
}

return opencdc.StructuredData{
"key": key,
"column1": col1,
"column2": col2,
"column3": col3,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/conduitio/evolviconf/evolviyaml v0.1.0 // indirect
github.com/conduitio/yaml/v3 v3.3.0 // indirect
github.com/curioswitch/go-reassign v0.3.0 // indirect
github.com/daixiang0/gci v0.13.6 // indirect
github.com/daixiang0/gci v0.13.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denis-tingaikin/go-header v0.5.0 // indirect
github.com/ettle/strcase v0.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/curioswitch/go-reassign v0.3.0 h1:dh3kpQHuADL3cobV/sSGETA8DOv457dwl+fbBAhrQPs=
github.com/curioswitch/go-reassign v0.3.0/go.mod h1:nApPCCTtqLJN/s8HfItCcKV0jIPwluBOvZP+dsJGA88=
github.com/daixiang0/gci v0.13.6 h1:RKuEOSkGpSadkGbvZ6hJ4ddItT3cVZ9Vn9Rybk6xjl8=
github.com/daixiang0/gci v0.13.6/go.mod h1:12etP2OniiIdP4q+kjUGrC/rUagga7ODbqsom5Eo5Yk=
github.com/daixiang0/gci v0.13.5 h1:kThgmH1yBmZSBCh1EJVxQ7JsHpm5Oms0AMed/0LaH4c=
github.com/daixiang0/gci v0.13.5/go.mod h1:12etP2OniiIdP4q+kjUGrC/rUagga7ODbqsom5Eo5Yk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down
26 changes: 13 additions & 13 deletions internal/db_info.go → internal/numeric_scale_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,52 @@ import (
"github.com/jackc/pgx/v5"
)

// DbInfo provides information about tables in a database.
type DbInfo struct {
// NumericScaleInfo provides information about the scale of numeric columns
// in a database.
type NumericScaleInfo struct {
conn *pgx.Conn
cache map[string]*tableCache
}

// tableCache stores information about a table.
// The information is cached and refreshed every 'cacheExpiration'.
type tableCache struct {
columns map[string]int
}

func NewDbInfo(conn *pgx.Conn) *DbInfo {
return &DbInfo{
func NewDbInfo(conn *pgx.Conn) *NumericScaleInfo {
return &NumericScaleInfo{
conn: conn,
cache: map[string]*tableCache{},
}
}

func (d *DbInfo) GetNumericColumnScale(ctx context.Context, table string, column string) (int, error) {
// Check if table exists in cache and is not expired
tableInfo, ok := d.cache[table]
func (i *NumericScaleInfo) Get(ctx context.Context, table string, column string) (int, error) {
// Check if table exists in cache
tableInfo, ok := i.cache[table]
if ok {
scale, ok := tableInfo.columns[column]
if ok {
return scale, nil
}
} else {
// Table info has expired, refresh the cache
d.cache[table] = &tableCache{
i.cache[table] = &tableCache{
columns: map[string]int{},
}
}

// Fetch scale from database
scale, err := d.numericScaleFromDb(ctx, table, column)
scale, err := i.fetchFromDB(ctx, table, column)
if err != nil {
return 0, err
}

d.cache[table].columns[column] = scale
i.cache[table].columns[column] = scale

return scale, nil
}

func (d *DbInfo) numericScaleFromDb(ctx context.Context, table string, column string) (int, error) {
func (i *NumericScaleInfo) fetchFromDB(ctx context.Context, table string, column string) (int, error) {
// Query to get the column type and numeric scale
query := `
SELECT
Expand All @@ -83,7 +83,7 @@ func (d *DbInfo) numericScaleFromDb(ctx context.Context, table string, column st
var dataType string
var numericScale *int

err := d.conn.QueryRow(ctx, query, table, column).Scan(&dataType, &numericScale)
err := i.conn.QueryRow(ctx, query, table, column).Scan(&dataType, &numericScale)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, fmt.Errorf("column %s not found in table %s", column, table)
Expand Down
114 changes: 114 additions & 0 deletions internal/table_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright © 2024 Meroxa, Inc.
//
// 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 internal

import (
"context"
"fmt"

sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/jackc/pgx/v5/pgxpool"
)

type TableInfo struct {
Name string
Columns map[string]*ColumnInfo
}

func NewTableInfo(tableName string) *TableInfo {
return &TableInfo{
Name: tableName,
Columns: make(map[string]*ColumnInfo),
}
}

type ColumnInfo struct {
IsNotNull bool
}

type TableInfoFetcher struct {
connPool *pgxpool.Pool
tableInfo map[string]*TableInfo
}

func NewTableInfoFetcher(connPool *pgxpool.Pool) *TableInfoFetcher {
return &TableInfoFetcher{
connPool: connPool,
tableInfo: make(map[string]*TableInfo),
}
}

func (i TableInfoFetcher) Refresh(ctx context.Context, tableName string) error {
tx, err := i.connPool.Begin(ctx)
if err != nil {
return fmt.Errorf("failed to start tx for getting table info: %w", err)
}
defer func() {
if err := tx.Rollback(ctx); err != nil {
sdk.Logger(ctx).Warn().
Err(err).
Msgf("error on tx rollback for getting table info")
}
}()

query := `
SELECT a.attname as column_name, a.attnotnull as is_not_null
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = $1::regclass
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum;
`

rows, err := tx.Query(ctx, query, WrapSQLIdent(tableName))
if err != nil {
sdk.Logger(ctx).
Err(err).
Str("query", query).
Msgf("failed to execute table info query")

return fmt.Errorf("failed to get table info: %w", err)
}
defer rows.Close()

ti := NewTableInfo(tableName)
for rows.Next() {
var columnName string
var isNotNull bool

err := rows.Scan(&columnName, &isNotNull)
if err != nil {
return fmt.Errorf("failed to scan table info row: %w", err)
}

ci := ti.Columns[columnName]
if ci == nil {
ci = &ColumnInfo{}
ti.Columns[columnName] = ci
}
ci.IsNotNull = isNotNull
}

if err := rows.Err(); err != nil {
return fmt.Errorf("failed to get table info rows: %w", err)
}

i.tableInfo[tableName] = ti
return nil
}

func (i TableInfoFetcher) GetTable(name string) *TableInfo {
return i.tableInfo[name]
}
Loading