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
8 changes: 8 additions & 0 deletions runtime/drivers/clickhouse/model_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func (c *Connection) validateAndApplyDefaults(opts *drivers.ModelExecuteOptions,
ip.SQL = ip.SQL + " SETTINGS " + op.QuerySettings
}

if ip != nil {
ip.PostExec = strings.TrimSpace(ip.PostExec)
ip.PreExec = strings.TrimSpace(ip.PreExec)
}
if op != nil {
op.PostExec = strings.TrimSpace(op.PostExec)
op.PreExec = strings.TrimSpace(op.PreExec)
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/drivers/duckdb/model_executor_localfile_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (e *localFileToSelfExecutor) Execute(ctx context.Context, opts *drivers.Mod
}
warnings = append(warnings, fmt.Sprintf("Undefined fields %q in output properties. Will be ignored.", strings.Join(unused, ", ")))
}
if err := outputProps.validateAndApplyDefaults(opts, &ModelInputProperties{}, outputProps); err != nil {
if err := outputProps.validateAndApplyDefaults(opts, &ModelInputProperties{}); err != nil {
return nil, fmt.Errorf("invalid output properties: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/drivers/duckdb/model_executor_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *selfToSelfExecutor) Execute(ctx context.Context, opts *drivers.ModelExe
}
warnings = append(warnings, fmt.Sprintf("Undefined fields %q in input properties. Will be ignored.", strings.Join(unused, ", ")))
}
if err := inputProps.Validate(); err != nil {
if err := inputProps.ValidateAndApplyDefaults(); err != nil {
return nil, fmt.Errorf("invalid input properties: %w", err)
}

Expand All @@ -69,7 +69,7 @@ func (e *selfToSelfExecutor) Execute(ctx context.Context, opts *drivers.ModelExe
}
warnings = append(warnings, fmt.Sprintf("Undefined fields %q in output properties. Will be ignored.", strings.Join(unused, ", ")))
}
if err := outputProps.validateAndApplyDefaults(opts, inputProps, outputProps); err != nil {
if err := outputProps.validateAndApplyDefaults(opts, inputProps); err != nil {
return nil, fmt.Errorf("invalid output properties: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/drivers/duckdb/model_executor_self_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (e *selfToFileExecutor) Execute(ctx context.Context, opts *drivers.ModelExe
}
warnings = append(warnings, fmt.Sprintf("Undefined fields %q in input properties. Will be ignored.", strings.Join(unused, ", ")))
}
if err := inputProps.Validate(); err != nil {
if err := inputProps.ValidateAndApplyDefaults(); err != nil {
return nil, fmt.Errorf("invalid input properties: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/drivers/duckdb/model_executor_self_objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (e *selfToObjectStoreExecutor) Execute(ctx context.Context, opts *drivers.M
}
warnings = append(warnings, fmt.Sprintf("Undefined fields %q in input properties. Will be ignored.", strings.Join(unused, ", ")))
}
if err := inputProps.Validate(); err != nil {
if err := inputProps.ValidateAndApplyDefaults(); err != nil {
return nil, fmt.Errorf("invalid input properties: %w", err)
}

Expand Down
23 changes: 14 additions & 9 deletions runtime/drivers/duckdb/model_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ type ModelInputProperties struct {
InternalDropSecretSQL string `mapstructure:"internal_drop_secret_sql"`
}

func (p *ModelInputProperties) Validate() error {
func (p *ModelInputProperties) ValidateAndApplyDefaults() error {
p.SQL = strings.TrimSpace(p.SQL)
p.PreExec = strings.TrimSpace(p.PreExec)
p.PostExec = strings.TrimSpace(p.PostExec)
if p.SQL == "" {
return fmt.Errorf("missing property 'sql'")
}
Expand All @@ -47,7 +50,7 @@ type ModelOutputProperties struct {
CreateSecretsFromConnectors []string `mapstructure:"create_secrets_from_connectors"`
}

func (p *ModelOutputProperties) validateAndApplyDefaults(opts *drivers.ModelExecuteOptions, ip *ModelInputProperties, op *ModelOutputProperties) error {
func (p *ModelOutputProperties) validateAndApplyDefaults(opts *drivers.ModelExecuteOptions, ip *ModelInputProperties) error {
if opts.Incremental || opts.PartitionRun {
if p.Materialize != nil && !*p.Materialize {
return fmt.Errorf("incremental or partitioned models must be materialized")
Expand Down Expand Up @@ -78,21 +81,23 @@ func (p *ModelOutputProperties) validateAndApplyDefaults(opts *drivers.ModelExec

// We want to use partition_overwrite as the default incremental strategy for models with partitions.
// This requires us to inject the partition key into the SQL query, so this only works for SQL models.
if op.IncrementalStrategy == drivers.IncrementalStrategyUnspecified {
if len(op.UniqueKey) > 0 {
op.IncrementalStrategy = drivers.IncrementalStrategyMerge
if p.IncrementalStrategy == drivers.IncrementalStrategyUnspecified {
if len(p.UniqueKey) > 0 {
p.IncrementalStrategy = drivers.IncrementalStrategyMerge
} else if opts.PartitionRun && ip != nil && ip.SQL != "" {
ip.SQL = fmt.Sprintf("SELECT %s AS __rill_partition, * FROM (%s\n)", safeSQLString(opts.PartitionKey), ip.SQL)
op.IncrementalStrategy = drivers.IncrementalStrategyPartitionOverwrite
op.PartitionBy = "__rill_partition"
p.IncrementalStrategy = drivers.IncrementalStrategyPartitionOverwrite
p.PartitionBy = "__rill_partition"
}
}

// If we failed to apply a better incremental strategy, fall back to append.
if op.IncrementalStrategy == drivers.IncrementalStrategyUnspecified {
op.IncrementalStrategy = drivers.IncrementalStrategyAppend
if p.IncrementalStrategy == drivers.IncrementalStrategyUnspecified {
p.IncrementalStrategy = drivers.IncrementalStrategyAppend
}

p.PreExec = strings.TrimSpace(p.PreExec)
p.PostExec = strings.TrimSpace(p.PostExec)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/reconcilers/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ sql: SELECT 1 AS num
}

func TestPartitionedIncrementalPostExecSeesIncrementalFlag(t *testing.T) {
rt, instanceID := testruntime.NewInstance(t)
rt, instanceID := testruntime.NewInstanceWithOptions(t, testruntime.InstanceOptions{StageChanges: true})

testruntime.PutFiles(t, rt, instanceID, map[string]string{
"rill.yaml": ``,
Expand Down
Loading