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

[FreeSql.Provider.ClickHouse] Select.WithSql() => Syntax error: ('@') #1904

Open
ktb-dev opened this issue Oct 25, 2024 · 6 comments
Open

[FreeSql.Provider.ClickHouse] Select.WithSql() => Syntax error: ('@') #1904

ktb-dev opened this issue Oct 25, 2024 · 6 comments

Comments

@ktb-dev
Copy link

ktb-dev commented Oct 25, 2024

问题描述及重现代码:

var query = _freeSql.GetRepository<MyClass>().Select.WithSql(SqlQuery(), new Dictionary<string, object>
{
    { "@prop1", prop1 },
    { "@prop2", prop2 },
    { "@prop3", prop3 }
}).AsQueryable();

数据库版本

ClickHouse: 23.3.2.37

安装的Nuget包

"FreeSql.Provider.ClickHouse" Version="3.2.833"

.net framework/. net core? 及具体版本

net8.0

Error message:

Code: 62. DB::Exception: Syntax error: failed at position 785 ('@') (line 17, col 38): @Prop1
AND toDate(received) >= @Prop1
. Expected one of: CAST operator, ANY, ALL, NOT, INTERVAL, CASE, DATE, TIMESTAMP, tuple, collection of literals, array, number, literal, NULL, Bool, true, false, string literal, asterisk, qualified asterisk, compound identifier, list of elements, identifier, COLUMNS matcher, COLUMNS, qualified COLUMNS matcher, substitution, MySQL-style global variable. (SYNTAX_ERROR) (version 23.3.2.37 (official build))


To fix the bug, need to remove the "@" symbol from the parameter name:

DbParameter ret = new ClickHouseDbParameter { ParameterName = $"@{name}", Value = value };

DbParameter ret = new ClickHouseDbParameter { ParameterName = $"{name}", Value = value };
@2881099
Copy link
Collaborator

2881099 commented Oct 28, 2024

ToLower

@ktb-dev
Copy link
Author

ktb-dev commented Oct 28, 2024

ToLower

ah, no. My parameter name is written with a lowercase letter, everywhere it appears in the code.

The thing is that the github editor read @Prop1 as a nickname, and that's why my error message showed the name 'prop1' with an uppercase letter.

@2881099
Copy link
Collaborator

2881099 commented Oct 29, 2024

{
{ "prop1", prop1 },
{ "prop2", prop2 },
{ "prop3", prop3 }
}

@2881099
Copy link
Collaborator

2881099 commented Oct 29, 2024

My clickhouse server is not available here, but I tested the code using SQL Server and it worked fine.

var result1 = fsql.Select<object>().WithSql("select @Prop1 as1", new { Prop1 = 1 }).ToList();
var result2 = fsql.Select<object>().WithSql("select @Prop1 as1", new Dictionary<string, object> { { "Prop1", 1 } }).ToList();

@ktb-dev
Copy link
Author

ktb-dev commented Oct 29, 2024

var result1 = fsql.Select<object>().WithSql("select @Prop1 as1", new { Prop1 = 1 }).ToList();
var result2 = fsql.Select<object>().WithSql("select @Prop1 as1", new Dictionary<string, object> { { "Prop1", 1 } }).ToList();

debug screenshot with an exception
1111111111111111

FreeSql config:

 builder.Services.AddSingleton((provider) =>
     { 
         var connectionString = GetConnectionString(provider);
         return new FreeSqlBuilder()
             .UseConnectionString(DataType.ClickHouse, connectionString)
             .Build();
     }) ;

An exception was also thrown here:

var result3 = await _freeSql.Ado.QueryAsync<object>("select @Prop1 as1, @prop2 as2", new { Prop1 = 1, prop2 = 2 });

But with such a select, everything was successful:

ClickHouseDbParameter[] dbParams = [
    DbParameter("Prop1", 1), 
    DbParameter("prop2", 2)
];
var result4 = await _freeSql.Ado.QueryAsync<object>(System.Data.CommandType.Text, "select @Prop1 as1, @prop2 as2", dbParams);

@2881099, doesn't that seem strange to you?

@ktb-dev
Copy link
Author

ktb-dev commented Oct 29, 2024

but I tested the code using SQL Server and it worked fine.

the issue arises precisely when executing a select with parameters via the clickhouse client

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