Skip to content

Commit afbea5b

Browse files
authored
Merge pull request #388 from alvarotuso/master
Improve external table support
2 parents e862cd0 + 4d6034f commit afbea5b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

clickhouse_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,9 @@ func Test_Select_External_Tables(t *testing.T) {
959959
?
960960
)
961961
`
962-
query = `SELECT COUNT(*) FROM clickhouse_test_select_external_tables WHERE string1 IN ? AND string2 IN ? AND string1 NOT IN ?`
963-
queryNamed = `SELECT COUNT(*) FROM clickhouse_test_select_external_tables WHERE string1 IN @e1 AND string2 IN @e2 AND string1 NOT IN @e3`
962+
query = `SELECT COUNT(*) FROM clickhouse_test_select_external_tables WHERE string1 IN ? AND string2 IN ? AND string1 NOT IN (SELECT c1 FROM ?)`
963+
queryNamed = `SELECT COUNT(*) FROM clickhouse_test_select_external_tables WHERE string1 IN @e1 AND string2 IN @e2 AND string1 NOT IN (SELECT c1 FROM @e3)`
964+
queryJoin = `SELECT COUNT(*) FROM clickhouse_test_select_external_tables AS ctset JOIN ? AS ext ON ctset.string1 = ext.c1`
964965
)
965966
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) && assert.NoError(t, connect.Ping()) {
966967
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_select_external_tables"); assert.NoError(t, err) {
@@ -1034,6 +1035,16 @@ func Test_Select_External_Tables(t *testing.T) {
10341035
}
10351036
assert.Equal(t, 1, count)
10361037
}
1038+
if rows, err := connect.Query(queryJoin, externalTable1); assert.NoError(t, err) {
1039+
var count int
1040+
for rows.Next() {
1041+
err := rows.Scan(&count)
1042+
if !assert.NoError(t, err) {
1043+
return
1044+
}
1045+
}
1046+
assert.Equal(t, 2, count)
1047+
}
10371048
}
10381049
}
10391050
}

helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func numInput(query string) int {
2727
between = newMatcher("between")
2828
in = newMatcher("in")
2929
and = newMatcher("and")
30+
from = newMatcher("from")
31+
join = newMatcher("join")
3032
)
3133
for {
3234
if char, _, err := reader.ReadRune(); err == nil {
@@ -71,7 +73,8 @@ func numInput(query string) int {
7173
char == '%':
7274
keyword = true
7375
default:
74-
if limit.matchRune(char) || offset.matchRune(char) || like.matchRune(char) || in.matchRune(char) {
76+
if limit.matchRune(char) || offset.matchRune(char) || like.matchRune(char) ||
77+
in.matchRune(char) || from.matchRune(char) || join.matchRune(char) {
7578
keyword = true
7679
} else if between.matchRune(char) {
7780
keyword = true

stmt.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func (stmt *stmt) bind(args []driver.NamedValue) (string, []ExternalTable) {
125125
between = newMatcher("between")
126126
and = newMatcher("and")
127127
in = newMatcher("in")
128+
from = newMatcher("from")
129+
join = newMatcher("join")
128130
externalTables = make([]ExternalTable, 0)
129131
)
130132
switch {
@@ -175,7 +177,8 @@ func (stmt *stmt) bind(args []driver.NamedValue) (string, []ExternalTable) {
175177
char == '[':
176178
keyword = true
177179
default:
178-
if limit.matchRune(char) || offset.matchRune(char) || like.matchRune(char) || in.matchRune(char) {
180+
if limit.matchRune(char) || offset.matchRune(char) || like.matchRune(char) ||
181+
in.matchRune(char) || from.matchRune(char) || join.matchRune(char) {
179182
keyword = true
180183
} else if between.matchRune(char) {
181184
keyword = true

0 commit comments

Comments
 (0)