-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC N index -> 1 table mapping (#532)
This PR adds new configuration settings to map input/source indexes to internal database table (that's proposal, I'm open to suggestions) ``` indexMappings: big_kibana_common_table: sourceIndexes: ["kibana_sample_data_flights", "kibana_sample_data_logs"] ``` It then uses this information to rewrite all table references to specified common database table. Rewriting table references in from clause should be sufficient for our current needs
- Loading branch information
Showing
7 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type IndexMappingsConfiguration struct { | ||
Name string `koanf:"name"` | ||
Mappings []string `koanf:"sourceIndexes"` | ||
} | ||
|
||
func (imc IndexMappingsConfiguration) String() string { | ||
var str = fmt.Sprintf("\n\t\t%s", | ||
imc.Name, | ||
) | ||
if len(imc.Mappings) > 0 { | ||
str = fmt.Sprintf("%s <- %s", str, strings.Join(imc.Mappings, ", ")) | ||
} | ||
return str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
|
||
package quesma | ||
|
||
import ( | ||
"quesma/model" | ||
"quesma/quesma/config" | ||
) | ||
|
||
func makeSourceToDestMappings(indexMappings map[string]config.IndexMappingsConfiguration) map[string]string { | ||
sourceToDestMapping := make(map[string]string) | ||
for _, indexMapping := range indexMappings { | ||
for _, sourceIndex := range indexMapping.Mappings { | ||
destIndex := indexMapping.Name | ||
sourceToDestMapping[sourceIndex] = destIndex | ||
} | ||
} | ||
return sourceToDestMapping | ||
} | ||
|
||
func (s *SchemaCheckPass) applyIndexMappingTransformations(query *model.Query) (*model.Query, error) { | ||
sourceToDestMapping := makeSourceToDestMappings(s.indexMappings) | ||
|
||
visitor := model.NewBaseVisitor() | ||
|
||
// For now, we only rewrite the table refs | ||
// as it seems to be sufficient for the current use case | ||
// as there will be only one table ref in the query | ||
// we don't need to worry about the other expressions | ||
visitor.OverrideVisitTableRef = func(b *model.BaseExprVisitor, e model.TableRef) interface{} { | ||
if destIndex, ok := sourceToDestMapping[e.Name]; ok { | ||
return model.NewTableRef(destIndex) | ||
} | ||
return model.NewTableRef(e.Name) | ||
} | ||
expr := query.SelectCommand.Accept(visitor) | ||
if _, ok := expr.(*model.SelectCommand); ok { | ||
query.SelectCommand = *expr.(*model.SelectCommand) | ||
} | ||
return query, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package quesma | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"quesma/clickhouse" | ||
"quesma/model" | ||
"quesma/quesma/config" | ||
"quesma/schema" | ||
"testing" | ||
) | ||
|
||
func Test_index_table_mapping(t *testing.T) { | ||
// Below mapping says that two indexes are mapped to a single db table called | ||
// CommonKibanaTable, so queries from clauses should be rewritten to use | ||
// CommonKibanaTable | ||
mappingsConfig := map[string]config.IndexMappingsConfiguration{ | ||
"CommonKibanaTable": { | ||
Name: "CommonKibanaTable", | ||
Mappings: []string{"kibana_sample_data_logs", "kibana_sample_data_flights"}, | ||
}, | ||
} | ||
|
||
// expectedQueries expects to have CommonKibanaTable as the table name | ||
expectedQueries := []*model.Query{ | ||
{ | ||
TableName: "kibana_sample_data_logs", | ||
SelectCommand: model.SelectCommand{ | ||
FromClause: model.NewTableRef("CommonKibanaTable"), | ||
}, | ||
}, | ||
|
||
{ | ||
TableName: "kibana_sample_data_flights", | ||
SelectCommand: model.SelectCommand{ | ||
FromClause: model.NewTableRef("CommonKibanaTable"), | ||
}, | ||
}, | ||
} | ||
|
||
// input queries always use the original index names | ||
queries := [][]*model.Query{ | ||
{ | ||
{ | ||
TableName: "kibana_sample_data_logs", | ||
SelectCommand: model.SelectCommand{ | ||
FromClause: model.NewTableRef("kibana_sample_data_logs"), | ||
}}, | ||
}, | ||
|
||
{ | ||
{ | ||
TableName: "kibana_sample_data_flights", | ||
SelectCommand: model.SelectCommand{ | ||
FromClause: model.NewTableRef("kibana_sample_data_flights"), | ||
}}, | ||
}, | ||
} | ||
|
||
indexConfig := map[string]config.IndexConfiguration{ | ||
"kibana_sample_data_logs": { | ||
Name: "kibana_sample_data_logs", | ||
Enabled: true, | ||
}, | ||
} | ||
|
||
cfg := config.QuesmaConfiguration{ | ||
IndexConfig: indexConfig, | ||
} | ||
|
||
tableDiscovery := | ||
fixedTableProvider{tables: map[string]schema.Table{ | ||
"kibana_sample_data_flights": {Columns: map[string]schema.Column{}}, | ||
}} | ||
|
||
s := schema.NewSchemaRegistry(tableDiscovery, cfg, clickhouse.SchemaTypeAdapter{}) | ||
|
||
transform := &SchemaCheckPass{cfg: indexConfig, schemaRegistry: s, logManager: clickhouse.NewLogManagerEmpty(), indexMappings: mappingsConfig} | ||
|
||
for k := range queries { | ||
resultQueries, err := transform.Transform(queries[k]) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedQueries[k].SelectCommand.String(), resultQueries[0].SelectCommand.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters