Skip to content
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

Question mark (and ternary operators) in queries #878

Closed
Denvormine opened this issue Jan 25, 2023 · 2 comments
Closed

Question mark (and ternary operators) in queries #878

Denvormine opened this issue Jan 25, 2023 · 2 comments

Comments

@Denvormine
Copy link

Denvormine commented Jan 25, 2023

Hi! We just changed from v1 to v2 and we started to face problems with our queries which use ternary operators. Are they supported at all? If we use them with numeric/named parameters then we get MixedParams error (escaping them will result syntax error, they appear as ? in queries). If we use them with positional params then we must escape ternary question mark with backslash. So it appears that ternary operator only works if we use them with positional params and escape character. It's kind of strange behavior and I couldn't find any info about that. Could you please clarify this problem: maybe we do something wrong and there's a change needed to be made when going from v1 to v2 in order to ternary operator to work?

P. S. question mark outside of scope of positional params and ternary operators (maybe in constant string) also kinda broken. It always considered as positional arg when not escaped and the only way to put it in query is to escape it, but then it will appear as escaped question mark.

@jkaflik
Copy link
Contributor

jkaflik commented Jan 27, 2023

Hi @Denvormine

Bind functionality is deprecated since the 2.5.0 version and support for native query parametersa has been introduced.

If you use native query parameters, the bind feature will not be run and no issue with the ternary operator should arise.

A simple test to confirm:

package issues

import (
	"context"
	"fmt"
	"github.com/ClickHouse/clickhouse-go/v2"
	clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"testing"
)

func Test878(t *testing.T) {
	var (
		conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
			"max_execution_time": 60,
			"flatten_nested":     0,
		}, nil, &clickhouse.Compression{
			Method: clickhouse.CompressionLZ4,
		})
	)

	require.NoError(t, err)

	if !clickhouse_tests.CheckMinServerServerVersion(conn, 22, 8, 0) {
		t.Skip(fmt.Errorf("unsupported clickhouse version"))
		return
	}

	queryCtx := clickhouse.Context(context.Background(), clickhouse.WithParameters(clickhouse.Parameters{
		"intVar": "1",
	}))

	row := conn.QueryRow(queryCtx, "SELECT {intVar:Int64} = 1 ? 5 : 8")
	assert.NoError(t, row.Err())

	var actualVar uint8

	assert.NoError(t, row.Scan(&actualVar))
	assert.Equal(t, uint8(5), actualVar)
}

@Denvormine
Copy link
Author

Okay, thank you for answer! We'll change our code to native then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants