mirror of
https://github.com/pikami/cosmium.git
synced 2024-11-28 00:17:37 +00:00
Compare commits
4 Commits
18edb925bf
...
398584368f
Author | SHA1 | Date | |
---|---|---|---|
|
398584368f | ||
|
5b66828bd0 | ||
|
6ed74688ca | ||
|
b72bba86c8 |
14
.github/ISSUE_TEMPLATE.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
#### Summary
|
||||
Bug report in one concise sentence
|
||||
|
||||
#### Steps to reproduce
|
||||
How can we reproduce the issue (what version are you using?)
|
||||
|
||||
#### Expected behavior
|
||||
Describe your issue in detail
|
||||
|
||||
#### Observed behavior (that appears unintentional)
|
||||
What did you see happen? Please include relevant error messages.
|
||||
|
||||
#### Possible fixes
|
||||
If you can, link to the line of code that might be responsible for the problem
|
18
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
18
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
<!-- Thank you for contributing a pull request! Here are a few tips to help you:
|
||||
|
||||
1. If applicable, please check if unit tests are added for new features
|
||||
2. Read the contribution guide lines https://github.com/pikami/cosmium/docs/CONTRIBUTING.md
|
||||
|
||||
-->
|
||||
|
||||
#### Summary
|
||||
<!--
|
||||
A description of what this pull request does, as well as QA test steps (if applicable).
|
||||
-->
|
||||
|
||||
#### Ticket Link
|
||||
<!--
|
||||
If applicable, please include a link to the GitHub issue:
|
||||
|
||||
Fixes https://github.com/pikami/cosmium/issues/XXX
|
||||
-->
|
@ -1,6 +1,6 @@
|
||||
# Cosmium
|
||||
|
||||
Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline.
|
||||
Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline. Read more about compatibility [here](docs/compatibility.md).
|
||||
|
||||
One of Cosmium's notable features is its ability to save and load state to a single JSON file. This feature makes it easy to load different test cases or share state with other developers, enhancing collaboration and efficiency in development workflows.
|
||||
|
||||
|
125
docs/CODE_OF_CONDUCT.md
Normal file
125
docs/CODE_OF_CONDUCT.md
Normal file
@ -0,0 +1,125 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, caste, color, religion, or sexual
|
||||
identity and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the overall
|
||||
community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or advances of
|
||||
any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email address,
|
||||
without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official email address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
cosmium@pikami.org.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series of
|
||||
actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or permanent
|
||||
ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within the
|
||||
community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.1, available at
|
||||
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
||||
|
||||
Community Impact Guidelines were inspired by
|
||||
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
[https://www.contributor-covenant.org/faq][FAQ].
|
203
docs/COMPATIBILITY.md
Normal file
203
docs/COMPATIBILITY.md
Normal file
@ -0,0 +1,203 @@
|
||||
# Cosmium Compatibility with Cosmos DB
|
||||
|
||||
## Introduction
|
||||
|
||||
Cosmium is designed to emulate the functionality of Cosmos DB, providing developers with a local development environment that closely mimics the behavior of Cosmos DB. While Cosmium aims to be compatible with Cosmos DB, there are certain differences and limitations to be aware of. This document provides an overview of Cosmium's compatibility with Cosmos DB and highlights areas where deviations may occur.
|
||||
|
||||
## Supported Features
|
||||
|
||||
Cosmium strives to support the core features of Cosmos DB, including:
|
||||
|
||||
- REST API
|
||||
- SQL-like query language
|
||||
- Document-based data model
|
||||
|
||||
## Compatibility Matrix
|
||||
|
||||
### Features
|
||||
| Feature | Implemented |
|
||||
|-------------------------------|-------------|
|
||||
| Subqueries | No |
|
||||
| Joins | No |
|
||||
| Computed properties | No |
|
||||
| Coalesce operators | No |
|
||||
| Bitwise operators | No |
|
||||
| GeoJSON location data | No |
|
||||
| Parameterized queries | Yes |
|
||||
| Stored procedures | No |
|
||||
| Triggers | No |
|
||||
| User-defined functions (UDFs) | No |
|
||||
|
||||
### Clauses
|
||||
| Clause | Implemented |
|
||||
|--------------|-------------|
|
||||
| SELECT | Yes |
|
||||
| FROM | Yes |
|
||||
| WHERE | Yes |
|
||||
| ORDER BY | Yes |
|
||||
| GROUP BY | Yes |
|
||||
| OFFSET LIMIT | Yes |
|
||||
|
||||
### Keywords
|
||||
| Keyword | Implemented |
|
||||
|----------|-------------|
|
||||
| BETWEEN | No |
|
||||
| DISTINCT | Yes |
|
||||
| LIKE | No |
|
||||
| IN | Yes |
|
||||
| TOP | Yes |
|
||||
|
||||
### Aggregate Functions
|
||||
| Function | Implemented |
|
||||
|----------|-------------|
|
||||
| AVG | Yes |
|
||||
| COUNT | Yes |
|
||||
| MAX | Yes |
|
||||
| MIN | Yes |
|
||||
| SUM | Yes |
|
||||
|
||||
### Array Functions
|
||||
| Function | Implemented |
|
||||
|----------------|-------------|
|
||||
| ARRAY_CONCAT | Yes |
|
||||
| ARRAY_CONTAINS | No |
|
||||
| ARRAY_LENGTH | Yes |
|
||||
| ARRAY_SLICE | Yes |
|
||||
| CHOOSE | No |
|
||||
| ObjectToArray | No |
|
||||
| SetIntersect | Yes |
|
||||
| SetUnion | Yes |
|
||||
|
||||
### Conditional Functions
|
||||
| Function | Implemented |
|
||||
|----------|-------------|
|
||||
| IIF | No |
|
||||
|
||||
### Date and time Functions
|
||||
| Function | Implemented |
|
||||
|---------------------------|-------------|
|
||||
| DateTimeAdd | No |
|
||||
| DateTimeBin | No |
|
||||
| DateTimeDiff | No |
|
||||
| DateTimeFromParts | No |
|
||||
| DateTimePart | No |
|
||||
| DateTimeToTicks | No |
|
||||
| DateTimeToTimestamp | No |
|
||||
| GetCurrentDateTime | No |
|
||||
| GetCurrentDateTimeStatic | No |
|
||||
| GetCurrentTicks | No |
|
||||
| GetCurrentTicksStatic | No |
|
||||
| GetCurrentTimestamp | No |
|
||||
| GetCurrentTimestampStatic | No |
|
||||
| TicksToDateTime | No |
|
||||
| TimestampToDateTime | No |
|
||||
|
||||
### Item Functions
|
||||
| Function | Implemented |
|
||||
|------------|-------------|
|
||||
| DocumentId | No |
|
||||
|
||||
### Mathematical Functions
|
||||
| Function | Implemented |
|
||||
|------------------|-------------|
|
||||
| ABS | No |
|
||||
| ACOS | No |
|
||||
| ASIN | No |
|
||||
| ATAN | No |
|
||||
| ATN2 | No |
|
||||
| CEILING | No |
|
||||
| COS | No |
|
||||
| COT | No |
|
||||
| DEGREES | No |
|
||||
| EXP | No |
|
||||
| FLOOR | No |
|
||||
| IntAdd | No |
|
||||
| IntBitAnd | No |
|
||||
| IntBitLeftShift | No |
|
||||
| IntBitNot | No |
|
||||
| IntBitOr | No |
|
||||
| IntBitRightShift | No |
|
||||
| IntBitXor | No |
|
||||
| IntDiv | No |
|
||||
| IntMod | No |
|
||||
| IntMul | No |
|
||||
| IntSub | No |
|
||||
| LOG | No |
|
||||
| LOG10 | No |
|
||||
| NumberBin | No |
|
||||
| PI | No |
|
||||
| POWER | No |
|
||||
| RADIANS | No |
|
||||
| RAND | No |
|
||||
| ROUND | No |
|
||||
| SIGN | No |
|
||||
| SIN | No |
|
||||
| SQRT | No |
|
||||
| SQUARE | No |
|
||||
| TAN | No |
|
||||
| TRUNC | No |
|
||||
|
||||
### Spatial Functions
|
||||
| Function | Implemented |
|
||||
|--------------------|-------------|
|
||||
| ST_AREA | No |
|
||||
| ST_DISTANCE | No |
|
||||
| ST_WITHIN | No |
|
||||
| ST_INTERSECTS | No |
|
||||
| ST_ISVALID | No |
|
||||
| ST_ISVALIDDETAILED | No |
|
||||
|
||||
### String Functions
|
||||
| Function | Implemented |
|
||||
|-----------------|-------------|
|
||||
| CONCAT | Yes |
|
||||
| CONTAINS | Yes |
|
||||
| ENDSWITH | Yes |
|
||||
| INDEX_OF | Yes |
|
||||
| LEFT | Yes |
|
||||
| LENGTH | Yes |
|
||||
| LOWER | Yes |
|
||||
| LTRIM | Yes |
|
||||
| REGEXMATCH | No |
|
||||
| REPLACE | Yes |
|
||||
| REPLICATE | Yes |
|
||||
| REVERSE | Yes |
|
||||
| RIGHT | Yes |
|
||||
| RTRIM | Yes |
|
||||
| STARTSWITH | Yes |
|
||||
| STRINGEQUALS | Yes |
|
||||
| StringToArray | No |
|
||||
| StringToBoolean | No |
|
||||
| StringToNull | No |
|
||||
| StringToNumber | No |
|
||||
| StringToObject | No |
|
||||
| SUBSTRING | Yes |
|
||||
| ToString | Yes |
|
||||
| TRIM | Yes |
|
||||
| UPPER | Yes |
|
||||
|
||||
### Type checking Functions
|
||||
| Function | Implemented |
|
||||
|------------------|-------------|
|
||||
| IS_ARRAY | Yes |
|
||||
| IS_BOOL | Yes |
|
||||
| IS_DEFINED | Yes |
|
||||
| IS_FINITE_NUMBER | Yes |
|
||||
| IS_INTEGER | Yes |
|
||||
| IS_NULL | Yes |
|
||||
| IS_NUMBER | Yes |
|
||||
| IS_OBJECT | Yes |
|
||||
| IS_PRIMITIVE | Yes |
|
||||
| IS_STRING | Yes |
|
||||
|
||||
## Known Differences
|
||||
|
||||
While Cosmium aims to replicate the behavior of Cosmos DB as closely as possible, there are certain differences and limitations to be aware of:
|
||||
|
||||
1. **Performance**: Cosmium may exhibit different performance characteristics compared to Cosmos DB, especially under heavy load or large datasets.
|
||||
2. **Consistency Levels**: The consistency model in Cosmium may differ slightly from Cosmos DB.
|
||||
3. **Features**: Some advanced features or functionalities of Cosmos DB may not be fully supported or available in Cosmium.
|
||||
|
||||
## Future Development
|
||||
|
||||
Cosmium is actively developed and maintained, with ongoing efforts to improve compatibility with Cosmos DB and enhance its features and capabilities. Future updates may address known differences and limitations, as well as introduce new functionality to bring Cosmium closer to feature parity with Cosmos DB.
|
37
docs/CONTRIBUTING.md
Normal file
37
docs/CONTRIBUTING.md
Normal file
@ -0,0 +1,37 @@
|
||||
# Contributing to Cosmium
|
||||
|
||||
Thank you for considering contributing to Cosmium! We appreciate your interest in helping to improve our project.
|
||||
|
||||
Please note that by participating in this project, you agree to abide by our [Code of Conduct](/docs/CODE_OF_CONDUCT.md). We expect all contributors to uphold the principles of respect, inclusivity, and professionalism.
|
||||
|
||||
If you have any questions or need assistance with the contribution process, feel free to reach out to us by opening an issue or contacting the maintainers directly.
|
||||
|
||||
We look forward to your contributions! 🚀
|
||||
|
||||
## Finding ways to contribute
|
||||
|
||||
A great way to contribute is to scan the [Compatibility Matrix](/docs/compatibility.md) for unsupported features and improving compatibility with CosmosDB.
|
||||
|
||||
A part from that, the [Issues page](https://github.com/pikami/cosmium/issues) might contain issues registered by other users. Fixing reported issues is a great way to contribute.
|
||||
|
||||
## How to Contribute
|
||||
|
||||
1. **Create an Issue**: Before starting work on a new feature or bug fix, please create an issue or look for existing ones on the [Issues page](https://github.com/pikami/cosmium/issues) to discuss your proposed changes. This allows us to provide feedback and ensure that your contribution aligns with the project goals.
|
||||
|
||||
2. **Fork the Repository**: Once you have identified an issue to work on, fork the repository to your own GitHub account.
|
||||
|
||||
3. **Create a Branch**: Create a new branch for your changes using a descriptive name that reflects the issue you are addressing.
|
||||
|
||||
4. **Commit Changes**: Commit your changes with clear and descriptive commit messages. Reference the issue number in the commit message. **Please write unit tests for your implemented feature!**
|
||||
|
||||
5. **Create a Pull Request**: Once your changes are ready, create a pull request from your forked repository to the main repository. Be sure to include a detailed description of your changes and reference the relevant issue.
|
||||
|
||||
6. **Review and Collaborate**: Participate in the code review process by addressing any feedback or comments from maintainers. Collaboration and constructive feedback help ensure the quality of contributions.
|
||||
|
||||
## Example Commits
|
||||
|
||||
To get an idea of how to implement new query functions, you can review the following example commits:
|
||||
|
||||
* [Implement IN function](https://github.com/pikami/cosmium/commit/f37c664c1aef39ee820106eaec1a3708ee7a93c8)
|
||||
* [Implement ToString function](https://github.com/pikami/cosmium/commit/16f41a547956f54481605f0ce035eee978a5e74b)
|
||||
* [Implement ARRAY_CONCAT, ARRAY_LENGTH, ARRAY_SLICE, SetIntersect, SetUnion functions](https://github.com/pikami/cosmium/commit/1c5e5ce85d70ed91e4b9be9e8f76d59e6eafc1b5)
|
@ -6,8 +6,10 @@ type SelectStmt struct {
|
||||
Filters interface{}
|
||||
Distinct bool
|
||||
Count int
|
||||
Offset int
|
||||
Parameters map[string]interface{}
|
||||
OrderExpressions []OrderExpression
|
||||
GroupBy []SelectItem
|
||||
}
|
||||
|
||||
type Table struct {
|
||||
@ -118,9 +120,23 @@ const (
|
||||
FunctionCallSetIntersect FunctionCallType = "SetIntersect"
|
||||
FunctionCallSetUnion FunctionCallType = "SetUnion"
|
||||
|
||||
FunctionCallAggregateAvg FunctionCallType = "AggregateAvg"
|
||||
FunctionCallAggregateCount FunctionCallType = "AggregateCount"
|
||||
FunctionCallAggregateMax FunctionCallType = "AggregateMax"
|
||||
FunctionCallAggregateMin FunctionCallType = "AggregateMin"
|
||||
FunctionCallAggregateSum FunctionCallType = "AggregateSum"
|
||||
|
||||
FunctionCallIn FunctionCallType = "In"
|
||||
)
|
||||
|
||||
var AggregateFunctions = []FunctionCallType{
|
||||
FunctionCallAggregateAvg,
|
||||
FunctionCallAggregateCount,
|
||||
FunctionCallAggregateMax,
|
||||
FunctionCallAggregateMin,
|
||||
FunctionCallAggregateSum,
|
||||
}
|
||||
|
||||
type FunctionCall struct {
|
||||
Arguments []interface{}
|
||||
Type FunctionCallType
|
||||
|
130
parsers/nosql/aggregate_functions_test.go
Normal file
130
parsers/nosql/aggregate_functions_test.go
Normal file
@ -0,0 +1,130 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
|
||||
t.Run("Should parse function AVG()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT AVG(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COUNT()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT COUNT(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateCount,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MAX()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MAX(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MIN()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MIN(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SUM()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT SUM(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateSum,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
@ -63,6 +63,24 @@ func Test_Parse(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT with GROUP BY", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, c["pk"] FROM c GROUP BY c.id, c.pk`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN function", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -6,7 +6,8 @@ import "github.com/pikami/cosmium/parsers"
|
||||
func makeSelectStmt(
|
||||
columns, table,
|
||||
whereClause interface{}, distinctClause interface{},
|
||||
count interface{}, orderList interface{},
|
||||
count interface{}, groupByClause interface{}, orderList interface{},
|
||||
offsetClause interface{},
|
||||
) (parsers.SelectStmt, error) {
|
||||
selectStmt := parsers.SelectStmt{
|
||||
SelectItems: columns.([]parsers.SelectItem),
|
||||
@ -26,10 +27,24 @@ func makeSelectStmt(
|
||||
selectStmt.Count = n
|
||||
}
|
||||
|
||||
if offsetArr, ok := offsetClause.([]interface{}); ok && len(offsetArr) == 2 {
|
||||
if n, ok := offsetArr[0].(int); ok {
|
||||
selectStmt.Offset = n
|
||||
}
|
||||
|
||||
if n, ok := offsetArr[1].(int); ok {
|
||||
selectStmt.Count = n
|
||||
}
|
||||
}
|
||||
|
||||
if orderExpressions, ok := orderList.([]parsers.OrderExpression); ok {
|
||||
selectStmt.OrderExpressions = orderExpressions
|
||||
}
|
||||
|
||||
if groupByClause != nil {
|
||||
selectStmt.GroupBy = groupByClause.([]parsers.SelectItem)
|
||||
}
|
||||
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
@ -149,8 +164,11 @@ SelectStmt <- Select ws
|
||||
topClause:TopClause? ws columns:Selection ws
|
||||
From ws table:TableName ws
|
||||
whereClause:(ws Where ws condition:Condition { return condition, nil })?
|
||||
orderByClause:OrderByClause? {
|
||||
return makeSelectStmt(columns, table, whereClause, distinctClause, topClause, orderByClause)
|
||||
groupByClause:(ws GroupBy ws columns:ColumnList { return columns, nil })?
|
||||
orderByClause:OrderByClause?
|
||||
offsetClause:OffsetClause? {
|
||||
return makeSelectStmt(columns, table, whereClause,
|
||||
distinctClause, topClause, groupByClause, orderByClause, offsetClause)
|
||||
}
|
||||
|
||||
DistinctClause <- "DISTINCT"i
|
||||
@ -159,6 +177,10 @@ TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
OffsetClause <- "OFFSET"i ws offset:IntegerLiteral ws "LIMIT"i ws limit:IntegerLiteral {
|
||||
return []interface{}{offset.(parsers.Constant).Value, limit.(parsers.Constant).Value}, nil
|
||||
}
|
||||
|
||||
Selection <- SelectValueSpec / ColumnList / SelectAsterisk
|
||||
|
||||
SelectAsterisk <- "*" {
|
||||
@ -285,6 +307,8 @@ And <- "AND"i
|
||||
|
||||
Or <- "OR"i
|
||||
|
||||
GroupBy <- "GROUP"i ws "BY"i
|
||||
|
||||
OrderBy <- "ORDER"i ws "BY"i
|
||||
|
||||
ComparisonOperator <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {
|
||||
@ -319,6 +343,7 @@ FunctionCall <- StringFunctions
|
||||
/ TypeCheckingFunctions
|
||||
/ ArrayFunctions
|
||||
/ InFunction
|
||||
/ AggregateFunctions
|
||||
|
||||
StringFunctions <- StringEqualsExpression
|
||||
/ ToStringExpression
|
||||
@ -348,6 +373,12 @@ TypeCheckingFunctions <- IsDefined
|
||||
/ IsPrimitive
|
||||
/ IsString
|
||||
|
||||
AggregateFunctions <- AvgAggregateExpression
|
||||
/ CountAggregateExpression
|
||||
/ MaxAggregateExpression
|
||||
/ MinAggregateExpression
|
||||
/ SumAggregateExpression
|
||||
|
||||
ArrayFunctions <- ArrayConcatExpression
|
||||
/ ArrayLengthExpression
|
||||
/ ArraySliceExpression
|
||||
@ -501,6 +532,26 @@ InFunction <- ex1:SelectProperty ws "IN"i ws "(" ws ex2:SelectItem others:(ws ",
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
}
|
||||
|
||||
AvgAggregateExpression <- "AVG"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateAvg, []interface{}{ex})
|
||||
}
|
||||
|
||||
CountAggregateExpression <- "COUNT"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateCount, []interface{}{ex})
|
||||
}
|
||||
|
||||
MaxAggregateExpression <- "MAX"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMax, []interface{}{ex})
|
||||
}
|
||||
|
||||
MinAggregateExpression <- "MIN"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMin, []interface{}{ex})
|
||||
}
|
||||
|
||||
SumAggregateExpression <- "SUM"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateSum, []interface{}{ex})
|
||||
}
|
||||
|
||||
Integer <- [0-9]+ {
|
||||
return strconv.Atoi(string(c.text))
|
||||
}
|
||||
|
@ -50,6 +50,21 @@ func Test_Parse_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT OFFSET", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c OFFSET 3 LIMIT 5`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Count: 5,
|
||||
Offset: 3,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT VALUE", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
|
131
query_executors/memory_executor/aggregate_functions.go
Normal file
131
query_executors/memory_executor/aggregate_functions.go
Normal file
@ -0,0 +1,131 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Avg(arguments []interface{}, row RowType) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
sum := 0.0
|
||||
count := 0
|
||||
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
for _, item := range array {
|
||||
value := c.getFieldValue(selectExpression, item)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return sum / float64(count)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Count(arguments []interface{}, row RowType) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
count := 0
|
||||
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
for _, item := range array {
|
||||
value := c.getFieldValue(selectExpression, item)
|
||||
if value != nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Max(arguments []interface{}, row RowType) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
max := 0.0
|
||||
count := 0
|
||||
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
for _, item := range array {
|
||||
value := c.getFieldValue(selectExpression, item)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue > max {
|
||||
max = numericValue
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) > max {
|
||||
max = float64(numericValue)
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return max
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Min(arguments []interface{}, row RowType) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
min := math.MaxFloat64
|
||||
count := 0
|
||||
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
for _, item := range array {
|
||||
value := c.getFieldValue(selectExpression, item)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue < min {
|
||||
min = numericValue
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) < min {
|
||||
min = float64(numericValue)
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return min
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Sum(arguments []interface{}, row RowType) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
sum := 0.0
|
||||
count := 0
|
||||
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
for _, item := range array {
|
||||
value := c.getFieldValue(selectExpression, item)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return sum
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
210
query_executors/memory_executor/aggregate_functions_test.go
Normal file
210
query_executors/memory_executor/aggregate_functions_test.go
Normal file
@ -0,0 +1,210 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "number": 123, "key": "a"},
|
||||
map[string]interface{}{"id": "456", "number": 456, "key": "a"},
|
||||
map[string]interface{}{"id": "789", "number": 789, "key": "b"},
|
||||
map[string]interface{}{"id": "no-number", "key": "b"},
|
||||
}
|
||||
|
||||
t.Run("Should execute function AVG()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "avg",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "avg": 289.5},
|
||||
map[string]interface{}{"key": "b", "avg": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function AVG() without GROUP BY clause", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "avg",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"avg": 456.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COUNT()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "cnt",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateCount,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "cnt": 2},
|
||||
map[string]interface{}{"key": "b", "cnt": 1},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function MAX()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "max",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "max": 456.0},
|
||||
map[string]interface{}{"key": "b", "max": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function MIN()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "min",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "min": 123.0},
|
||||
map[string]interface{}{"key": "b", "min": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function SUM()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "sum",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateSum,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "sum": 579.0},
|
||||
map[string]interface{}{"key": "b", "sum": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type RowType interface{}
|
||||
@ -36,12 +37,27 @@ func Execute(query parsers.SelectStmt, data []RowType) []RowType {
|
||||
ctx.orderBy(query.OrderExpressions, result)
|
||||
}
|
||||
|
||||
// Apply group
|
||||
isGroupSelect := query.GroupBy != nil && len(query.GroupBy) > 0
|
||||
if isGroupSelect {
|
||||
result = ctx.groupBy(query, result)
|
||||
}
|
||||
|
||||
// Apply select
|
||||
if !isGroupSelect {
|
||||
selectedData := make([]RowType, 0)
|
||||
if hasAggregateFunctions(query.SelectItems) {
|
||||
// When can have aggregate functions without GROUP BY clause,
|
||||
// we should aggregate all rows in that case
|
||||
selectedData = append(selectedData, ctx.selectRow(query.SelectItems, result))
|
||||
} else {
|
||||
for _, row := range result {
|
||||
selectedData = append(selectedData, ctx.selectRow(query.SelectItems, row))
|
||||
}
|
||||
}
|
||||
|
||||
result = selectedData
|
||||
}
|
||||
|
||||
// Apply distinct
|
||||
if query.Distinct {
|
||||
@ -182,6 +198,11 @@ func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowTy
|
||||
return typedValue.Value
|
||||
}
|
||||
|
||||
rowValue := row
|
||||
if array, isArray := row.([]RowType); isArray {
|
||||
rowValue = array[0]
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeFunctionCall {
|
||||
var typedValue parsers.FunctionCall
|
||||
var ok bool
|
||||
@ -192,82 +213,94 @@ func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowTy
|
||||
|
||||
switch typedValue.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return c.strings_StringEquals(typedValue.Arguments, row)
|
||||
return c.strings_StringEquals(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallContains:
|
||||
return c.strings_Contains(typedValue.Arguments, row)
|
||||
return c.strings_Contains(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallEndsWith:
|
||||
return c.strings_EndsWith(typedValue.Arguments, row)
|
||||
return c.strings_EndsWith(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallStartsWith:
|
||||
return c.strings_StartsWith(typedValue.Arguments, row)
|
||||
return c.strings_StartsWith(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallConcat:
|
||||
return c.strings_Concat(typedValue.Arguments, row)
|
||||
return c.strings_Concat(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIndexOf:
|
||||
return c.strings_IndexOf(typedValue.Arguments, row)
|
||||
return c.strings_IndexOf(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallToString:
|
||||
return c.strings_ToString(typedValue.Arguments, row)
|
||||
return c.strings_ToString(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallUpper:
|
||||
return c.strings_Upper(typedValue.Arguments, row)
|
||||
return c.strings_Upper(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLower:
|
||||
return c.strings_Lower(typedValue.Arguments, row)
|
||||
return c.strings_Lower(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLeft:
|
||||
return c.strings_Left(typedValue.Arguments, row)
|
||||
return c.strings_Left(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLength:
|
||||
return c.strings_Length(typedValue.Arguments, row)
|
||||
return c.strings_Length(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLTrim:
|
||||
return c.strings_LTrim(typedValue.Arguments, row)
|
||||
return c.strings_LTrim(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReplace:
|
||||
return c.strings_Replace(typedValue.Arguments, row)
|
||||
return c.strings_Replace(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReplicate:
|
||||
return c.strings_Replicate(typedValue.Arguments, row)
|
||||
return c.strings_Replicate(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReverse:
|
||||
return c.strings_Reverse(typedValue.Arguments, row)
|
||||
return c.strings_Reverse(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallRight:
|
||||
return c.strings_Right(typedValue.Arguments, row)
|
||||
return c.strings_Right(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallRTrim:
|
||||
return c.strings_RTrim(typedValue.Arguments, row)
|
||||
return c.strings_RTrim(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSubstring:
|
||||
return c.strings_Substring(typedValue.Arguments, row)
|
||||
return c.strings_Substring(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallTrim:
|
||||
return c.strings_Trim(typedValue.Arguments, row)
|
||||
return c.strings_Trim(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return c.typeChecking_IsDefined(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsDefined(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsArray:
|
||||
return c.typeChecking_IsArray(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsArray(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsBool:
|
||||
return c.typeChecking_IsBool(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsBool(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsFiniteNumber:
|
||||
return c.typeChecking_IsFiniteNumber(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsFiniteNumber(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsInteger:
|
||||
return c.typeChecking_IsInteger(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsInteger(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsNull:
|
||||
return c.typeChecking_IsNull(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsNull(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsNumber:
|
||||
return c.typeChecking_IsNumber(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsNumber(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsObject:
|
||||
return c.typeChecking_IsObject(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsObject(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsPrimitive:
|
||||
return c.typeChecking_IsPrimitive(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsPrimitive(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsString:
|
||||
return c.typeChecking_IsString(typedValue.Arguments, row)
|
||||
return c.typeChecking_IsString(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallArrayConcat:
|
||||
return c.array_Concat(typedValue.Arguments, row)
|
||||
return c.array_Concat(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallArrayLength:
|
||||
return c.array_Length(typedValue.Arguments, row)
|
||||
return c.array_Length(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallArraySlice:
|
||||
return c.array_Slice(typedValue.Arguments, row)
|
||||
return c.array_Slice(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSetIntersect:
|
||||
return c.set_Intersect(typedValue.Arguments, row)
|
||||
return c.set_Intersect(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSetUnion:
|
||||
return c.set_Union(typedValue.Arguments, row)
|
||||
return c.set_Union(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallAggregateAvg:
|
||||
return c.aggregate_Avg(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallAggregateCount:
|
||||
return c.aggregate_Count(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallAggregateMax:
|
||||
return c.aggregate_Max(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallAggregateMin:
|
||||
return c.aggregate_Min(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallAggregateSum:
|
||||
return c.aggregate_Sum(typedValue.Arguments, row)
|
||||
|
||||
case parsers.FunctionCallIn:
|
||||
return c.misc_In(typedValue.Arguments, row)
|
||||
return c.misc_In(typedValue.Arguments, rowValue)
|
||||
}
|
||||
}
|
||||
|
||||
value := row
|
||||
value := rowValue
|
||||
|
||||
if len(field.Path) > 1 {
|
||||
for _, pathSegment := range field.Path[1:] {
|
||||
if nestedValue, ok := value.(map[string]interface{}); ok {
|
||||
@ -314,6 +347,47 @@ func (c memoryExecutorContext) orderBy(orderBy []parsers.OrderExpression, data [
|
||||
sort.SliceStable(data, less)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) groupBy(selectStmt parsers.SelectStmt, data []RowType) []RowType {
|
||||
groupedRows := make(map[string][]RowType)
|
||||
groupedKeys := make([]string, 0)
|
||||
|
||||
// Group rows by group by columns
|
||||
for _, row := range data {
|
||||
key := c.generateGroupKey(selectStmt.GroupBy, row)
|
||||
if _, ok := groupedRows[key]; !ok {
|
||||
groupedKeys = append(groupedKeys, key)
|
||||
}
|
||||
groupedRows[key] = append(groupedRows[key], row)
|
||||
}
|
||||
|
||||
// Aggregate each group
|
||||
aggregatedRows := make([]RowType, 0)
|
||||
for _, key := range groupedKeys {
|
||||
groupRows := groupedRows[key]
|
||||
aggregatedRow := c.aggregateGroup(selectStmt, groupRows)
|
||||
aggregatedRows = append(aggregatedRows, aggregatedRow)
|
||||
}
|
||||
|
||||
return aggregatedRows
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) generateGroupKey(groupByFields []parsers.SelectItem, row RowType) string {
|
||||
var keyBuilder strings.Builder
|
||||
for _, column := range groupByFields {
|
||||
fieldValue := c.getFieldValue(column, row)
|
||||
keyBuilder.WriteString(fmt.Sprintf("%v", fieldValue))
|
||||
keyBuilder.WriteString(":")
|
||||
}
|
||||
|
||||
return keyBuilder.String()
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregateGroup(selectStmt parsers.SelectStmt, groupRows []RowType) RowType {
|
||||
aggregatedRow := c.selectRow(selectStmt.SelectItems, groupRows)
|
||||
|
||||
return aggregatedRow
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
if reflect.TypeOf(val1) != reflect.TypeOf(val2) {
|
||||
return 1
|
||||
@ -376,3 +450,23 @@ func deduplicate(slice []RowType) []RowType {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func hasAggregateFunctions(selectItems []parsers.SelectItem) bool {
|
||||
if selectItems == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, selectItem := range selectItems {
|
||||
if selectItem.Type == parsers.SelectItemTypeFunctionCall {
|
||||
if typedValue, ok := selectItem.Value.(parsers.FunctionCall); ok && slices.Contains[[]parsers.FunctionCallType](parsers.AggregateFunctions, typedValue.Type) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if hasAggregateFunctions(selectItem.SelectItems) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -59,6 +59,26 @@ func Test_Execute(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT with GROUP BY", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"pk": 123},
|
||||
map[string]interface{}{"pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN function", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
|
@ -71,6 +71,32 @@ func Test_Execute_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT OFFSET", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Count: 2,
|
||||
Offset: 1,
|
||||
OrderExpressions: []parsers.OrderExpression{
|
||||
{
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Direction: parsers.OrderDirectionDesc,
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "67890", "pk": 456},
|
||||
map[string]interface{}{"id": "456", "pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT VALUE", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
|
Loading…
Reference in New Issue
Block a user