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

Commit 185c831

Browse files
committed
Introducing datetime doris transformation
1 parent cb30918 commit 185c831

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed

platform/database_common/log_manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type LogManagerIFace interface {
7777
CountMultiple(ctx context.Context, tables ...*Table) (int64, error)
7878
Count(ctx context.Context, table *Table) (int64, error)
7979
GetTableDefinitions() (TableMap, error)
80+
GetBackendConnector() quesma_api.BackendConnector
8081
}
8182

8283
func NewTableMap() *TableMap {
@@ -427,3 +428,7 @@ func (c *ChTableConfig) GetAttributes() []Attribute {
427428
func (l *LogManager) IsInTransparentProxyMode() bool {
428429
return l.cfg.TransparentProxy
429430
}
431+
432+
func (l *LogManager) GetBackendConnector() quesma_api.BackendConnector {
433+
return l.chDb
434+
}

platform/frontend_connectors/schema_transformer.go

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/QuesmaOrg/quesma/platform/parsers/elastic_query_dsl"
1515
"github.com/QuesmaOrg/quesma/platform/schema"
1616
"github.com/QuesmaOrg/quesma/platform/transformations"
17+
quesma_api "github.com/QuesmaOrg/quesma/platform/v2/core"
1718
"sort"
1819
"strconv"
1920
"strings"
@@ -901,7 +902,6 @@ func (s *SchemaCheckPass) applyRuntimeMappings(indexSchema schema.Schema, query
901902
func (s *SchemaCheckPass) convertQueryDateTimeFunctionToClickhouse(indexSchema schema.Schema, query *model.Query) (*model.Query, error) {
902903

903904
visitor := model.NewBaseVisitor()
904-
905905
visitor.OverrideVisitFunction = func(b *model.BaseExprVisitor, e model.FunctionExpr) interface{} {
906906

907907
switch e.Name {
@@ -926,7 +926,36 @@ func (s *SchemaCheckPass) convertQueryDateTimeFunctionToClickhouse(indexSchema s
926926
query.SelectCommand = *expr.(*model.SelectCommand)
927927
}
928928
return query, nil
929+
}
930+
931+
// it convers out internal date time related fuction to clickhouse functions
932+
func (s *SchemaCheckPass) convertQueryDateTimeFunctionToDoris(indexSchema schema.Schema, query *model.Query) (*model.Query, error) {
933+
934+
visitor := model.NewBaseVisitor()
935+
visitor.OverrideVisitFunction = func(b *model.BaseExprVisitor, e model.FunctionExpr) interface{} {
936+
937+
switch e.Name {
938+
939+
case model.DateHourFunction:
940+
if len(e.Args) != 1 {
941+
return e
942+
}
943+
return model.NewFunction("HOUR", e.Args[0].Accept(b).(model.Expr))
929944

945+
// TODO this is a place for over date/time related functions
946+
// add more
947+
948+
default:
949+
return visitFunction(b, e)
950+
}
951+
}
952+
953+
expr := query.SelectCommand.Accept(visitor)
954+
955+
if _, ok := expr.(*model.SelectCommand); ok {
956+
query.SelectCommand = *expr.(*model.SelectCommand)
957+
}
958+
return query, nil
930959
}
931960

932961
func (s *SchemaCheckPass) checkAggOverUnsupportedType(indexSchema schema.Schema, query *model.Query) (*model.Query, error) {
@@ -1116,20 +1145,38 @@ func (s *SchemaCheckPass) Transform(plan *model.ExecutionPlan) (*model.Execution
11161145
{TransformationName: "FullTextFieldTransformation", Transformation: s.applyFullTextField},
11171146
{TransformationName: "TimestampFieldTransformation", Transformation: s.applyTimestampField},
11181147
{TransformationName: "ApplySearchAfterParameter", Transformation: s.applySearchAfterParameter},
1119-
1120-
// Section 3: clickhouse specific transformations
1121-
{TransformationName: "QuesmaDateFunctions", Transformation: s.convertQueryDateTimeFunctionToClickhouse},
1122-
{TransformationName: "IpTransformation", Transformation: s.applyIpTransformations},
1123-
{TransformationName: "GeoTransformation", Transformation: s.applyGeoTransformations},
1124-
{TransformationName: "ArrayTransformation", Transformation: s.applyArrayTransformations},
1125-
{TransformationName: "MapTransformation", Transformation: s.applyMapTransformations},
1126-
{TransformationName: "MatchOperatorTransformation", Transformation: s.applyMatchOperator},
1127-
{TransformationName: "AggOverUnsupportedType", Transformation: s.checkAggOverUnsupportedType},
1128-
{TransformationName: "ApplySelectFromCluster", Transformation: s.ApplySelectFromCluster},
1129-
1130-
// Section 4: compensations and checks
1131-
{TransformationName: "BooleanLiteralTransformation", Transformation: s.applyBooleanLiteralLowering},
11321148
}
1149+
// Section 3: backend specific transformations
1150+
if plan.BackendConnector.GetId() == quesma_api.ClickHouseSQLBackend {
1151+
transformationChain = append(transformationChain, struct {
1152+
TransformationName string
1153+
Transformation func(schema.Schema, *model.Query) (*model.Query, error)
1154+
}{TransformationName: "QuesmaDateFunctions", Transformation: s.convertQueryDateTimeFunctionToClickhouse})
1155+
}
1156+
1157+
if plan.BackendConnector.GetId() == quesma_api.DorisSQLBackend {
1158+
transformationChain = append(transformationChain, struct {
1159+
TransformationName string
1160+
Transformation func(schema.Schema, *model.Query) (*model.Query, error)
1161+
}{TransformationName: "QuesmaDateFunctions", Transformation: s.convertQueryDateTimeFunctionToDoris})
1162+
1163+
}
1164+
1165+
transformationChain = append(transformationChain,
1166+
[]struct {
1167+
TransformationName string
1168+
Transformation func(schema.Schema, *model.Query) (*model.Query, error)
1169+
}{
1170+
{TransformationName: "IpTransformation", Transformation: s.applyIpTransformations},
1171+
{TransformationName: "GeoTransformation", Transformation: s.applyGeoTransformations},
1172+
{TransformationName: "ArrayTransformation", Transformation: s.applyArrayTransformations},
1173+
{TransformationName: "MapTransformation", Transformation: s.applyMapTransformations},
1174+
{TransformationName: "MatchOperatorTransformation", Transformation: s.applyMatchOperator},
1175+
{TransformationName: "AggOverUnsupportedType", Transformation: s.checkAggOverUnsupportedType},
1176+
{TransformationName: "ApplySelectFromCluster", Transformation: s.ApplySelectFromCluster},
1177+
{TransformationName: "BooleanLiteralTransformation", Transformation: s.applyBooleanLiteralLowering},
1178+
}...,
1179+
)
11331180

11341181
for k, query := range plan.Queries {
11351182
var err error

platform/frontend_connectors/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin
521521
logger.InfoWithCtx(ctx).Msgf("Input SQL query %d: %s", i, query.SelectCommand.String())
522522
}
523523
}
524+
plan.BackendConnector = q.logManager.GetBackendConnector()
524525
err = q.transformQueries(plan)
525526

526527
if err != nil {

platform/model/query.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package model
55
import (
66
"github.com/QuesmaOrg/quesma/platform/parsers/painful"
77
"github.com/QuesmaOrg/quesma/platform/schema"
8+
quesma_api "github.com/QuesmaOrg/quesma/platform/v2/core"
89
"time"
910
)
1011

@@ -120,6 +121,8 @@ type ExecutionPlan struct {
120121
MergeSiblingResults func(plan *ExecutionPlan, results [][]QueryResultRow) (*ExecutionPlan, [][]QueryResultRow)
121122

122123
SiblingQueries map[int][]int // Map of query IDs to their sibling query IDs
124+
125+
BackendConnector quesma_api.BackendConnector // Backend connector used for executing the queries
123126
}
124127

125128
// NewExecutionPlan creates a new instance of model.ExecutionPlan

platform/parsers/elastic_query_dsl/aggregation_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func (cw *ClickhouseQueryTranslator) parseFieldFromScriptField(queryMap QueryMap
406406
wantedRegex := regexp.MustCompile(`^doc\['(\w+)']\.value\.(?:getHour\(\)|hourOfDay)$`)
407407
matches := wantedRegex.FindStringSubmatch(source)
408408
if len(matches) == 2 {
409-
return model.NewFunction("toHour", model.NewColumnRef(matches[1])), true
409+
return model.NewFunction(model.DateHourFunction, model.NewColumnRef(matches[1])), true
410410
}
411411

412412
// b) source: "if (doc['field_name_1'].value == doc['field_name_2'].value") { return 1; } else { return 0; }"

0 commit comments

Comments
 (0)