Skip to content
Merged
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
3 changes: 2 additions & 1 deletion flow/connectors/clickhouse/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ func (c *ClickHouseConnector) generateCreateTableSQLForNormalizedTable(
if err != nil {
return nil, fmt.Errorf("error while converting column type to ClickHouse type: %w", err)
}
} else if (tableSchema.NullableEnabled || columnNullableEnabled) && column.Nullable && !colType.IsArray() {
} else if (tableSchema.NullableEnabled || columnNullableEnabled) && column.Nullable && !colType.IsArray() &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

afaiu tableSchema.NullableEnabled is kind of a global flag whether we want to have nullable fields at the destination at all (regardless of source column nullability)
so, I think it should be tableSchema.NullableEnabled && columnNullableEnabled, no?
if tableSchema.NullableEnabled is false, it means that none of the destination columns can be nullable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ah
we have schema.NullableEnabled and column.NullableEnabled
seems like we can put false on the schema lvl and override on the column lvl..
so OR is correct here I guess

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

seems like we can put false on the schema lvl and override on the column lvl..

Exactly, at least this is how I interpret it.

!strings.HasPrefix(clickHouseType, "Nullable(") {
clickHouseType = fmt.Sprintf("Nullable(%s)", clickHouseType)
}

Expand Down
5 changes: 5 additions & 0 deletions flow/connectors/clickhouse/normalize_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func (t *NormalizeQueryGenerator) BuildQuery(ctx context.Context) (string, error
if err != nil {
return "", fmt.Errorf("error while converting column type to clickhouse type: %w", err)
}
} else if (schema.NullableEnabled || columnNullableEnabled) && column.Nullable && !colType.IsArray() &&
!strings.HasPrefix(clickHouseType, "Nullable(") {
// mirror the table DDL: a nullable-enabled column created as Nullable(...) must also be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we move it to the helper then?
if it's exactly the same condition as in flow/connectors/clickhouse/normalize.go.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's the same condition today but JSON extraction function and DDL are different enough IMO as to maybe make us stumble upon different conditions in future changes.

// extracted as Nullable(...), or JSON nulls turn into the type's default value
clickHouseType = fmt.Sprintf("Nullable(%s)", clickHouseType)
}

switch clickHouseType {
Expand Down
43 changes: 43 additions & 0 deletions flow/connectors/clickhouse/normalize_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,53 @@ import (
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/stretchr/testify/require"

"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/pkg/testutil"
"github.com/PeerDB-io/peerdb/flow/shared/types"
)

// TestBuildQueryNullableDestinationTypeOverride checks the normalize query agrees with the table DDL on
// nullability for `destination_type` overrides: with nullability enabled, the DDL creates the column as
// Nullable(<type>) so the query has to extract Nullable(<type>) too, or JSON nulls turn into the type's
// default value. An override already spelled Nullable(...) is used as is on both sides.
func TestBuildQueryNullableDestinationTypeOverride(t *testing.T) {
schema := &protos.TableSchema{
TableIdentifier: "src.t1",
PrimaryKeyColumns: []string{"id"},
System: protos.TypeSystem_Q,
NullableEnabled: true,
Columns: []*protos.FieldDescription{
{Name: "id", Type: string(types.QValueKindString), TypeModifier: -1},
{Name: "num", Type: string(types.QValueKindInt64), TypeModifier: -1, Nullable: true},
{Name: "tag", Type: string(types.QValueKindString), TypeModifier: -1, Nullable: true},
},
}
tableMapping := &protos.TableMapping{
SourceTableIdentifier: "src.t1",
DestinationTableIdentifier: "t1_dst",
Columns: []*protos.ColumnSetting{
{SourceName: "num", DestinationType: "Int64"},
{SourceName: "tag", DestinationType: "Nullable(String)"},
},
}

query, err := NewNormalizeQueryGenerator(
"t1_dst",
map[string]*protos.TableSchema{"t1_dst": schema},
[]*protos.TableMapping{tableMapping},
1, 0,
false, false,
nil, "_peerdb_raw_t1", nil, false, "", 0, nil,
).BuildQuery(t.Context())
require.NoError(t, err)

// nullable-enabled override extracts as Nullable, matching the Nullable(Int64) column the DDL creates
require.Contains(t, query, `JSONExtract(_peerdb_data, 'num', 'Nullable(Int64)') AS `+"`num`")
// an already-Nullable override is used verbatim, not double wrapped
require.Contains(t, query, `JSONExtract(_peerdb_data, 'tag', 'Nullable(String)') AS `+"`tag`")
require.NotContains(t, query, "Nullable(Nullable(")
}

func TestExtendedTimeToDateTime(t *testing.T) {
ctx := context.Background()
addr := fmt.Sprintf("%s:%d", testutil.ClickHouseTestHost(), testutil.ClickHouseTestPort())
Expand Down
Loading