@@ -227,14 +227,17 @@ func tableName(schemaName string, tableName string) string {
227227}
228228
229229func orderByString (table * schema.Table ) (string , error ) {
230- info := table .PbTableInfo .ClickhouseTableOptions
231- if info == nil {
232- // Default to ordering by primary key if available, otherwise by block_number
233- if table .PrimaryKey != nil {
234- return fmt .Sprintf ("ORDER BY (%s)" , table .PrimaryKey .Name ), nil
235- }
236- return fmt .Sprintf ("ORDER BY (%s)" , sql2 .DialectFieldBlockNumber ), nil
237- }
230+ var info * pbSchema.ClickhouseTableOptions
231+ if table .PbTableInfo != nil {
232+ info = table .PbTableInfo .ClickhouseTableOptions
233+ }
234+ if info == nil {
235+ // Default to ordering by primary key if available, otherwise by block_number
236+ if table .PrimaryKey != nil {
237+ return fmt .Sprintf ("ORDER BY (%s)" , table .PrimaryKey .Name ), nil
238+ }
239+ return fmt .Sprintf ("ORDER BY (%s)" , sql2 .DialectFieldBlockNumber ), nil
240+ }
238241
239242 if len (info .OrderByFields ) == 0 {
240243 return "" , fmt .Errorf ("clickhouse table options for table %q don't have any 'order_by_fields'. Require at least 1" , table .Name )
@@ -256,11 +259,14 @@ func orderByString(table *schema.Table) (string, error) {
256259}
257260
258261func partitionByString (table * schema.Table ) (string , error ) {
259- info := table .PbTableInfo .ClickhouseTableOptions
260- if info == nil {
261- // Default to partitioning by month based on block timestamp
262- return fmt .Sprintf ("PARTITION BY (%s)" , wrapWithClickhouseFunction (sql2 .DialectFieldBlockTimestamp , pbSchema .Function_toYYYYMM )), nil
263- }
262+ var info * pbSchema.ClickhouseTableOptions
263+ if table .PbTableInfo != nil {
264+ info = table .PbTableInfo .ClickhouseTableOptions
265+ }
266+ if info == nil {
267+ // Default to partitioning by month based on block timestamp
268+ return fmt .Sprintf ("PARTITION BY (%s)" , wrapWithClickhouseFunction (sql2 .DialectFieldBlockTimestamp , pbSchema .Function_toYYYYMM )), nil
269+ }
264270
265271 var parts []string
266272
@@ -288,11 +294,14 @@ func partitionByString(table *schema.Table) (string, error) {
288294}
289295
290296func replacingMergeTreeString (table * schema.Table ) (string , error ) {
291- info := table .PbTableInfo .ClickhouseTableOptions
292- if info == nil {
293- // Default to just using version field when no ClickHouse options are set
294- return fmt .Sprintf ("ReplacingMergeTree(%s)" , sql2 .DialectFieldVersion ), nil
295- }
297+ var info * pbSchema.ClickhouseTableOptions
298+ if table .PbTableInfo != nil {
299+ info = table .PbTableInfo .ClickhouseTableOptions
300+ }
301+ if info == nil {
302+ // Default to just using version field when no ClickHouse options are set
303+ return fmt .Sprintf ("ReplacingMergeTree(%s)" , sql2 .DialectFieldVersion ), nil
304+ }
296305
297306 out := sql2 .DialectFieldVersion
298307 for _ , field := range info .ReplacingFields {
0 commit comments