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
22 changes: 4 additions & 18 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,14 @@ func Context(parent context.Context, options ...QueryOption) context.Context {

// queryOptions returns a mutable copy of the QueryOptions struct within the given context.
// If ClickHouse context was not provided, an empty struct with a valid Settings map is returned.
// If the context has a deadline greater than 1s then max_execution_time setting is appended.
func queryOptions(ctx context.Context) QueryOptions {
var opt QueryOptions

if ctxOpt, ok := ctx.Value(_contextOptionKey).(QueryOptions); ok {
opt = ctxOpt.clone()
} else {
opt = QueryOptions{
settings: make(Settings),
}
}

deadline, ok := ctx.Deadline()
if !ok {
return opt
if opt, ok := ctx.Value(_contextOptionKey).(QueryOptions); ok {
return opt.clone()
}

if sec := time.Until(deadline).Seconds(); sec > 1 {
opt.settings["max_execution_time"] = int(sec + 5)
return QueryOptions{
settings: make(Settings),
}

return opt
}

// queryOptionsAsync returns the AsyncOptions struct within the given context's QueryOptions.
Expand Down
24 changes: 24 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,27 @@ func TestWithoutProfileEvents(t *testing.T) {
require.Equal(t, 0, opts.settings["send_profile_events"])
})
}

func TestQueryOptionsMaxExecutionTime(t *testing.T) {
tests := []struct {
name string
settings Settings
}{
{name: "not set is not added", settings: nil},
{name: "set is kept", settings: Settings{"max_execution_time": 600}},
{name: "zero is kept", settings: Settings{"max_execution_time": 0}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(Context(context.Background(), WithSettings(tt.settings)), time.Minute)
t.Cleanup(cancel)

opts := queryOptions(ctx)
value, ok := opts.settings["max_execution_time"]
want, set := tt.settings["max_execution_time"]
require.Equal(t, set, ok)
require.Equal(t, want, value)
})
}
}
4 changes: 2 additions & 2 deletions docs/config-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ ClientInfo: clickhouse.ClientInfo{

| Option | Type | Default | DSN param | Per-query | Description | Best practice | When misconfigured |
|--------|------|---------|-----------|-----------|-------------|---------------|-------------------|
| `Settings` | `map[string]any` | `nil` | Any unrecognized param (e.g. `?max_execution_time=60`) | Yes (`WithSettings`, context wins on conflict) | ClickHouse server settings applied to every query. DSN conversion: `"true"`→`1`, `"false"`→`0`, numeric→`int`. | Set common limits at connection level, override per-query via context. | Typos: silently ignored or error by version. Wrong types: `"Cannot parse string 'abc' as Int64"`. `max_execution_time=0` + no deadline: queries run forever. |
| `Settings` | `map[string]any` | `nil` | Any unrecognized param (e.g. `?max_execution_time=60`) | Yes (`WithSettings`, context wins on conflict) | ClickHouse server settings applied to every query. DSN conversion: `"true"`→`1`, `"false"`→`0`, numeric→`int`. | Set common limits at connection level, override per-query via context. | Typos: silently ignored or error by version. Wrong types: `"Cannot parse string 'abc' as Int64"`. `max_execution_time=0`: the server never stops the query; a context deadline only stops the client. |
| `CustomSetting` | `CustomSetting{Value string}` | — | — | Yes (via `WithSettings`) | Marks a setting as "custom" (non-important) for Native protocol. Won't error if server doesn't recognize it. HTTP treats all settings as custom by default. | Use for experimental or version-specific settings. | Marking important settings as custom: silently ignored if unsupported. |

**Common settings:**
Expand Down Expand Up @@ -313,7 +313,7 @@ ctx := clickhouse.Context(context.Background(),
<Info>
**Context deadline behavior**

If the context has a deadline > 1s, `max_execution_time` is automatically set to `seconds_remaining + 5`. This overrides any manually set value.
The context deadline only limits how long the client waits. The driver does not derive `max_execution_time` from it. To limit server processing time, set `max_execution_time` on the connection or with `WithSettings`.
</Info>

| Option | Type | Default | Protocol | Description | Best practice | When misconfigured |
Expand Down
55 changes: 55 additions & 0 deletions tests/issues/issue_2100_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package issues

import (
"context"
"fmt"
"maps"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
)

// Test2100 verifies that the context deadline does not change the `max_execution_time` the
// server uses.
// See https://github.com/ClickHouse/clickhouse-go/issues/2100.
func Test2100(t *testing.T) {
testEnv, err := clickhouse_tests.GetTestEnvironment("issues")
require.NoError(t, err)

limit := clickhouse.Settings{"max_execution_time": 2}
cases := []struct {
name string
ctxSettings clickhouse.Settings
connSettings clickhouse.Settings
}{
{name: "not set"},
{name: "context", ctxSettings: limit},
{name: "connection", connSettings: limit},
}

for _, protocol := range []clickhouse.Protocol{clickhouse.Native, clickhouse.HTTP} {
for _, tc := range cases {
t.Run(fmt.Sprintf("%v/%s", protocol, tc.name), func(t *testing.T) {
settings := clickhouse_tests.TestClientDefaultSettings(testEnv)
maps.Copy(settings, tc.connSettings)
conn, err := clickhouse_tests.GetConnection("issues", t, protocol, settings, nil, nil)
require.NoError(t, err)
t.Cleanup(func() { conn.Close() })

ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(tc.ctxSettings))
deadlineCtx, cancel := context.WithTimeout(ctx, time.Minute)
t.Cleanup(cancel)

const query = "SELECT toString(getSetting('max_execution_time'))"
var want, got string
require.NoError(t, conn.QueryRow(ctx, query).Scan(&want))
require.NoError(t, conn.QueryRow(deadlineCtx, query).Scan(&got))
require.Equal(t, want, got)
})
}
}
}
Loading