-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support timestamp in iso 8601 format (#210)
Why: - our prospect got many failed queries into that trap - Elastic and Opensearch support [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) - Clickhouse also support it as part of https://clickhouse.com/docs/en/sql-reference/functions/type-conversion-functions#parsedatetimebesteffort - Go native support of ISO 8601 is poor, but there is good library for that
- Loading branch information
Showing
8 changed files
with
148 additions
and
86 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
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,91 @@ | ||
package queryparser | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"mitmproxy/quesma/clickhouse" | ||
"mitmproxy/quesma/concurrent" | ||
"mitmproxy/quesma/quesma/config" | ||
"testing" | ||
) | ||
|
||
type parseRangeTest struct { | ||
name string | ||
rangePartOfQuery QueryMap | ||
createTableQuery string | ||
expectedWhere string | ||
} | ||
|
||
var parseRangeTests = []parseRangeTest{ | ||
{ | ||
"DateTime64", | ||
QueryMap{ | ||
"timestamp": QueryMap{ | ||
"format": "strict_date_optional_time", | ||
"gte": "2024-02-02T13:47:16.029Z", | ||
"lte": "2024-02-09T13:47:16.029Z", | ||
}, | ||
}, | ||
`CREATE TABLE ` + tableName + ` | ||
( "message" String, "timestamp" DateTime64(3, 'UTC') ) | ||
ENGINE = Memory`, | ||
`"timestamp">=parseDateTime64BestEffort('2024-02-02T13:47:16.029Z') AND "timestamp"<=parseDateTime64BestEffort('2024-02-09T13:47:16.029Z')`, | ||
}, | ||
{ | ||
"parseDateTimeBestEffort", | ||
QueryMap{ | ||
"timestamp": QueryMap{ | ||
"format": "strict_date_optional_time", | ||
"gte": "2024-02-02T13:47:16.029Z", | ||
"lte": "2024-02-09T13:47:16.029Z", | ||
}, | ||
}, | ||
`CREATE TABLE ` + tableName + ` | ||
( "message" String, "timestamp" DateTime ) | ||
ENGINE = Memory`, | ||
`"timestamp">=parseDateTimeBestEffort('2024-02-02T13:47:16.029Z') AND "timestamp"<=parseDateTimeBestEffort('2024-02-09T13:47:16.029Z')`, | ||
}, | ||
{ | ||
"numeric range", | ||
QueryMap{ | ||
"time_taken": QueryMap{ | ||
"gt": "100", | ||
}, | ||
}, | ||
`CREATE TABLE ` + tableName + ` | ||
( "message" String, "timestamp" DateTime, "time_taken" UInt32 ) | ||
ENGINE = Memory`, | ||
`"time_taken">100`, | ||
}, | ||
{ | ||
"DateTime64", | ||
QueryMap{ | ||
"timestamp": QueryMap{ | ||
"format": "strict_date_optional_time", | ||
"gte": "2024-02-02T13:47:16", | ||
"lte": "2024-02-09T13:47:16", | ||
}, | ||
}, | ||
`CREATE TABLE ` + tableName + ` | ||
( "message" String, "timestamp" DateTime64(3, 'UTC') ) | ||
ENGINE = Memory`, | ||
`"timestamp">=parseDateTime64BestEffort('2024-02-02T13:47:16') AND "timestamp"<=parseDateTime64BestEffort('2024-02-09T13:47:16')`, | ||
}, | ||
} | ||
|
||
func Test_parseRange(t *testing.T) { | ||
for _, test := range parseRangeTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
table, err := clickhouse.NewTable(test.createTableQuery, clickhouse.NewNoTimestampOnlyStringAttrCHConfig()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.NoError(t, err) | ||
lm := clickhouse.NewLogManager(concurrent.NewMapWith(tableName, table), config.QuesmaConfiguration{}) | ||
cw := ClickhouseQueryTranslator{ClickhouseLM: lm, Table: table, Ctx: context.Background()} | ||
|
||
whereClause := cw.parseRange(test.rangePartOfQuery).Sql.Stmt | ||
assert.Equal(t, test.expectedWhere, whereClause) | ||
}) | ||
} | ||
} |
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
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