Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit ad3cf0b

Browse files
authored
Fix broken _ids (#1409)
<img width="1717" alt="image" src="https://github.com/user-attachments/assets/67850d0c-d4f6-47ce-bb91-01cd018767e2" />
1 parent b2b4202 commit ad3cf0b

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

platform/frontend_connectors/schema_transformer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ func (s *SchemaCheckPass) applyTimestampField(indexSchema schema.Schema, query *
656656
if column, ok := indexSchema.Fields[model.TimestampFieldName]; ok {
657657
timestampColumnName = column.InternalPropertyName.AsString()
658658
}
659+
table, ok := s.tableDiscovery.TableDefinitions().Load(query.TableName)
660+
if !ok {
661+
return nil, fmt.Errorf("table %s not found", query.TableName)
662+
}
663+
if table.DiscoveredTimestampFieldName != nil {
664+
timestampColumnName = *table.DiscoveredTimestampFieldName
665+
}
659666

660667
// if not found, check if the table has a timestamp field discovered somehow
661668
// This is commented out for now.

platform/frontend_connectors/schema_transformer_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,92 @@ func (f fixedTableProvider) TableDefinitions() map[string]schema.Table {
2525
func (f fixedTableProvider) AutodiscoveryEnabled() bool { return false }
2626
func (f fixedTableProvider) RegisterTablesReloadListener(chan<- types.ReloadMessage) {}
2727

28+
func TestApplyTimestampField(t *testing.T) {
29+
indexConfig := map[string]config.IndexConfiguration{
30+
"test": {},
31+
}
32+
33+
fields := map[schema.FieldName]schema.Field{
34+
"@timestamp": {PropertyName: "@timestamp", InternalPropertyName: "@timestamp", InternalPropertyType: "DateTime64", Type: schema.QuesmaTypeDate},
35+
"other_field": {PropertyName: "other_field", InternalPropertyName: "other_field", InternalPropertyType: "String", Type: schema.QuesmaTypeText},
36+
}
37+
38+
indexSchema := schema.Schema{
39+
Fields: fields,
40+
}
41+
42+
tableMap := clickhouse.NewTableMap()
43+
tableDiscovery := clickhouse.NewEmptyTableDiscovery()
44+
tableDiscovery.TableMap = tableMap
45+
46+
tableMap.Store("test", &clickhouse.Table{
47+
Name: "test",
48+
DiscoveredTimestampFieldName: func() *string {
49+
field := "discovered_timestamp"
50+
return &field
51+
}(),
52+
})
53+
54+
transform := NewSchemaCheckPass(&config.QuesmaConfiguration{IndexConfig: indexConfig}, tableDiscovery, defaultSearchAfterStrategy)
55+
56+
tests := []struct {
57+
name string
58+
query *model.Query
59+
expected *model.Query
60+
}{
61+
{
62+
name: "replace @timestamp with discovered timestamp",
63+
query: &model.Query{
64+
TableName: "test",
65+
SelectCommand: model.SelectCommand{
66+
Columns: []model.Expr{
67+
model.NewColumnRef("@timestamp"),
68+
},
69+
},
70+
},
71+
expected: &model.Query{
72+
TableName: "test",
73+
SelectCommand: model.SelectCommand{
74+
Columns: []model.Expr{
75+
model.NewColumnRef("discovered_timestamp"),
76+
},
77+
},
78+
},
79+
},
80+
{
81+
name: "no replacement needed",
82+
query: &model.Query{
83+
TableName: "test",
84+
SelectCommand: model.SelectCommand{
85+
Columns: []model.Expr{
86+
model.NewColumnRef("other_field"),
87+
},
88+
},
89+
},
90+
expected: &model.Query{
91+
TableName: "test",
92+
SelectCommand: model.SelectCommand{
93+
Columns: []model.Expr{
94+
model.NewColumnRef("other_field"),
95+
},
96+
},
97+
},
98+
},
99+
}
100+
101+
for i, tt := range tests {
102+
t.Run(util.PrettyTestName(tt.name, i), func(t *testing.T) {
103+
tt.query.Schema = indexSchema
104+
tt.query.Indexes = []string{tt.query.TableName}
105+
106+
actual, err := transform.applyTimestampField(indexSchema, tt.query)
107+
assert.NoError(t, err)
108+
109+
assert.Equal(t, model.AsString(tt.expected.SelectCommand), model.AsString(actual.SelectCommand))
110+
})
111+
}
112+
}
113+
28114
func Test_ipRangeTransform(t *testing.T) {
29115
const isIPAddressInRangePrimitive = "isIPAddressInRange"
30116
const CASTPrimitive = "CAST"

platform/parsers/elastic_query_dsl/query_parser.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,14 @@ func (cw *ClickhouseQueryTranslator) parseIds(queryMap QueryMap) model.SimpleQue
347347
}
348348
}
349349

350-
// TODO replace with cw.Schema
351350
var idToSql func(string) (model.Expr, error)
352-
timestampColumnName := model.TimestampFieldName
351+
var timestampColumnName string
352+
if cw.Table.DiscoveredTimestampFieldName != nil {
353+
timestampColumnName = *cw.Table.DiscoveredTimestampFieldName
354+
} else {
355+
timestampColumnName = model.TimestampFieldName
356+
}
357+
353358
if column, ok := cw.Table.Cols[timestampColumnName]; ok {
354359
switch column.Type.String() {
355360
case clickhouse.DateTime64.String():

0 commit comments

Comments
 (0)