mirror of
https://github.com/pikami/cosmium.git
synced 2026-01-09 04:26:57 +00:00
Compare commits
17 Commits
e623a563f4
...
v0.1.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4659d90a9 | ||
|
|
503e6bb8ad | ||
|
|
e5ddc143f0 | ||
|
|
66ea859f34 | ||
|
|
3584f9b5ce | ||
|
|
c7d01b4593 | ||
|
|
2834f3f641 | ||
|
|
a6b5d32ff7 | ||
|
|
0e98e3481a | ||
|
|
827046f634 | ||
|
|
475d586dc5 | ||
|
|
9abef691d6 | ||
|
|
62dcbc1f2b | ||
|
|
2f42651fb7 | ||
|
|
20af73ee9c | ||
|
|
3bdff9b643 | ||
|
|
b808e97c72 |
@@ -9,11 +9,6 @@ builds:
|
||||
- arm64
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
ignore:
|
||||
- goos: linux
|
||||
goarch: arm64
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
|
||||
release:
|
||||
prerelease: auto
|
||||
@@ -32,11 +27,14 @@ brews:
|
||||
email: git@pikami.org
|
||||
|
||||
dockers:
|
||||
- image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest"
|
||||
- id: docker-linux-amd64
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
dockerfile: Dockerfile
|
||||
use: docker
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/amd64"
|
||||
- "--pull"
|
||||
@@ -47,6 +45,38 @@ dockers:
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
- id: docker-linux-arm64
|
||||
goos: linux
|
||||
goarch: arm64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64v8"
|
||||
dockerfile: Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/arm64"
|
||||
- "--pull"
|
||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
||||
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
|
||||
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
|
||||
docker_manifests:
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:latest'
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64v8"
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}'
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
|
||||
|
||||
checksum:
|
||||
name_template: 'checksums.txt'
|
||||
|
||||
@@ -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. Read more about compatibility [here](./docs/compatibility.md).
|
||||
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.
|
||||
|
||||
@@ -56,7 +56,7 @@ If you wan to run the application using docker, configure it using environment v
|
||||
|
||||
```sh
|
||||
docker run --rm \
|
||||
-e Persist=/save.json \
|
||||
-e COSMIUM_PERSIST=/save.json \
|
||||
-v ./save.json:/save.json \
|
||||
-p 8081:8081 \
|
||||
ghcr.io/pikami/cosmium
|
||||
|
||||
@@ -8,8 +8,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
||||
EnvPrefix = "COSMIUM_"
|
||||
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
||||
EnvPrefix = "COSMIUM_"
|
||||
ExplorerBaseUrlLocation = "/_explorer"
|
||||
)
|
||||
|
||||
var Config = ServerConfig{}
|
||||
@@ -45,6 +46,7 @@ func ParseFlags() {
|
||||
Config.DatabaseDomain = Config.Host
|
||||
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
||||
Config.AccountKey = *accountKey
|
||||
Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||
}
|
||||
|
||||
func setFlagsFromEnvironment() (err error) {
|
||||
|
||||
@@ -6,14 +6,15 @@ type ServerConfig struct {
|
||||
DatabaseEndpoint string
|
||||
AccountKey string
|
||||
|
||||
ExplorerPath string
|
||||
Port int
|
||||
Host string
|
||||
TLS_CertificatePath string
|
||||
TLS_CertificateKey string
|
||||
InitialDataFilePath string
|
||||
PersistDataFilePath string
|
||||
DisableAuth bool
|
||||
DisableTls bool
|
||||
Debug bool
|
||||
ExplorerPath string
|
||||
Port int
|
||||
Host string
|
||||
TLS_CertificatePath string
|
||||
TLS_CertificateKey string
|
||||
InitialDataFilePath string
|
||||
PersistDataFilePath string
|
||||
DisableAuth bool
|
||||
DisableTls bool
|
||||
Debug bool
|
||||
ExplorerBaseUrlLocation string
|
||||
}
|
||||
|
||||
@@ -8,5 +8,11 @@ import (
|
||||
)
|
||||
|
||||
func CosmiumExport(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, repositories.GetState())
|
||||
repositoryState, err := repositories.GetState()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(http.StatusOK, "application/json", []byte(repositoryState))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -223,6 +224,11 @@ func DocumentsPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
||||
if isUpsert {
|
||||
repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func RegisterExplorerHandlers(router *gin.Engine) {
|
||||
explorer := router.Group("/_explorer")
|
||||
explorer := router.Group(config.Config.ExplorerBaseUrlLocation)
|
||||
{
|
||||
explorer.Use(func(ctx *gin.Context) {
|
||||
if ctx.Param("filepath") == "/config.json" {
|
||||
|
||||
@@ -14,7 +14,7 @@ func Authentication() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestUrl := c.Request.URL.String()
|
||||
if config.Config.DisableAuth ||
|
||||
strings.HasPrefix(requestUrl, "/_explorer") ||
|
||||
strings.HasPrefix(requestUrl, config.Config.ExplorerBaseUrlLocation) ||
|
||||
strings.HasPrefix(requestUrl, "/cosmium") {
|
||||
return
|
||||
}
|
||||
|
||||
21
api/handlers/middleware/strip_trailing_slashes.go
Normal file
21
api/handlers/middleware/strip_trailing_slashes.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.Config.ExplorerBaseUrlLocation) {
|
||||
c.Request.URL.Path = path[:len(path)-1]
|
||||
r.HandleContext(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,15 @@ import (
|
||||
)
|
||||
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
router := gin.Default(func(e *gin.Engine) {
|
||||
e.RedirectTrailingSlash = false
|
||||
})
|
||||
|
||||
if config.Config.Debug {
|
||||
router.Use(middleware.RequestLogger())
|
||||
}
|
||||
|
||||
router.Use(middleware.StripTrailingSlashes(router))
|
||||
router.Use(middleware.Authentication())
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
func runTestServer() *httptest.Server {
|
||||
config.Config.AccountKey = config.DefaultAccountKey
|
||||
config.Config.ExplorerPath = "/tmp/nothing"
|
||||
config.Config.ExplorerBaseUrlLocation = config.ExplorerBaseUrlLocation
|
||||
|
||||
return httptest.NewServer(api.CreateRouter())
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
@@ -65,8 +67,8 @@ func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.Container
|
||||
Paths: []string{"/pk"},
|
||||
},
|
||||
})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||
|
||||
ts := runTestServer()
|
||||
|
||||
@@ -146,6 +148,72 @@ func Test_Documents(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should query array accessor", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
`SELECT c.id,
|
||||
c["arr"][0] AS arr0,
|
||||
c["arr"][1] AS arr1,
|
||||
c["arr"][2] AS arr2,
|
||||
c["arr"][3] AS arr3
|
||||
FROM c ORDER BY c.id`,
|
||||
nil,
|
||||
[]interface{}{
|
||||
map[string]interface{}{"id": "12345", "arr0": 1.0, "arr1": 2.0, "arr2": 3.0, "arr3": nil},
|
||||
map[string]interface{}{"id": "67890", "arr0": 6.0, "arr1": 7.0, "arr2": 8.0, "arr3": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle parallel writes", func(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
rutineCount := 100
|
||||
results := make(chan error, rutineCount)
|
||||
|
||||
createCall := func(i int) {
|
||||
defer wg.Done()
|
||||
item := map[string]interface{}{
|
||||
"id": fmt.Sprintf("id-%d", i),
|
||||
"pk": fmt.Sprintf("pk-%d", i),
|
||||
"val": i,
|
||||
}
|
||||
bytes, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = collectionClient.CreateItem(
|
||||
ctx,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
results <- err
|
||||
|
||||
collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
|
||||
collectionClient.DeleteItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
|
||||
}
|
||||
|
||||
for i := 0; i < rutineCount; i++ {
|
||||
wg.Add(1)
|
||||
go createCall(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
for err := range results {
|
||||
if err != nil {
|
||||
t.Errorf("Error creating item: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Documents_Patch(t *testing.T) {
|
||||
@@ -204,4 +272,92 @@ func Test_Documents_Patch(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateItem", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{
|
||||
"Id": "6789011",
|
||||
"pk": "456",
|
||||
"newField": "newValue2",
|
||||
}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.CreateItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
t.Run("CreateItem that already exists", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err := collectionClient.CreateItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusConflict, respErr.StatusCode)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpsertItem new", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "123456", "pk": "1234", "isCool": false, "arr": []int{1, 2, 3}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.UpsertItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
t.Run("UpsertItem that already exists", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3, 4}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.UpsertItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
41
api/tests/documents_trailingslash_test.go
Normal file
41
api/tests/documents_trailingslash_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/authentication"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Request document with trailing slash like python cosmosdb client does.
|
||||
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
|
||||
ts, _ := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
|
||||
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
|
||||
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
|
||||
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
|
||||
testUrl := ts.URL + "/" + path + "/"
|
||||
date := time.Now().Format(time.RFC1123)
|
||||
signature := authentication.GenerateSignature("GET", "docs", path, date, config.Config.AccountKey)
|
||||
httpClient := &http.Client{}
|
||||
req, _ := http.NewRequest("GET", testUrl, nil)
|
||||
req.Header.Add("x-ms-date", date)
|
||||
req.Header.Add("authorization", "sig="+url.QueryEscape(signature))
|
||||
res, err := httpClient.Do(req)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
if res != nil {
|
||||
defer res.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode, "Expected HTTP status 200 OK")
|
||||
} else {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -15,10 +15,11 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
## Compatibility Matrix
|
||||
|
||||
### Features
|
||||
|
||||
| Feature | Implemented |
|
||||
|-------------------------------|-------------|
|
||||
| Subqueries | No |
|
||||
| Joins | No |
|
||||
| ----------------------------- | ----------- |
|
||||
| Subqueries | Yes |
|
||||
| Joins | Yes |
|
||||
| Computed properties | No |
|
||||
| Coalesce operators | No |
|
||||
| Bitwise operators | No |
|
||||
@@ -29,8 +30,9 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| User-defined functions (UDFs) | No |
|
||||
|
||||
### Clauses
|
||||
|
||||
| Clause | Implemented |
|
||||
|--------------|-------------|
|
||||
| ------------ | ----------- |
|
||||
| SELECT | Yes |
|
||||
| FROM | Yes |
|
||||
| WHERE | Yes |
|
||||
@@ -39,8 +41,9 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| OFFSET LIMIT | Yes |
|
||||
|
||||
### Keywords
|
||||
|
||||
| Keyword | Implemented |
|
||||
|----------|-------------|
|
||||
| -------- | ----------- |
|
||||
| BETWEEN | No |
|
||||
| DISTINCT | Yes |
|
||||
| LIKE | No |
|
||||
@@ -48,8 +51,9 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| TOP | Yes |
|
||||
|
||||
### Aggregate Functions
|
||||
|
||||
| Function | Implemented |
|
||||
|----------|-------------|
|
||||
| -------- | ----------- |
|
||||
| AVG | Yes |
|
||||
| COUNT | Yes |
|
||||
| MAX | Yes |
|
||||
@@ -57,25 +61,30 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| 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 |
|
||||
|
||||
| Function | Implemented |
|
||||
| ------------------ | ----------- |
|
||||
| ARRAY_CONCAT | Yes |
|
||||
| ARRAY_CONTAINS | No |
|
||||
| ARRAY_CONTAINS_ANY | No |
|
||||
| ARRAY_CONTAINS_ALL | 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 |
|
||||
@@ -93,53 +102,56 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| 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 |
|
||||
| ---------------- | ----------- |
|
||||
| ABS | Yes |
|
||||
| ACOS | Yes |
|
||||
| ASIN | Yes |
|
||||
| ATAN | Yes |
|
||||
| ATN2 | Yes |
|
||||
| CEILING | Yes |
|
||||
| COS | Yes |
|
||||
| COT | Yes |
|
||||
| DEGREES | Yes |
|
||||
| EXP | Yes |
|
||||
| FLOOR | Yes |
|
||||
| IntAdd | Yes |
|
||||
| IntBitAnd | Yes |
|
||||
| IntBitLeftShift | Yes |
|
||||
| IntBitNot | Yes |
|
||||
| IntBitOr | Yes |
|
||||
| IntBitRightShift | Yes |
|
||||
| IntBitXor | Yes |
|
||||
| IntDiv | Yes |
|
||||
| IntMod | Yes |
|
||||
| IntMul | Yes |
|
||||
| IntSub | Yes |
|
||||
| LOG | Yes |
|
||||
| LOG10 | Yes |
|
||||
| NumberBin | Yes |
|
||||
| PI | Yes |
|
||||
| POWER | Yes |
|
||||
| RADIANS | Yes |
|
||||
| RAND | Yes |
|
||||
| ROUND | Yes |
|
||||
| SIGN | Yes |
|
||||
| SIN | Yes |
|
||||
| SQRT | Yes |
|
||||
| SQUARE | Yes |
|
||||
| TAN | Yes |
|
||||
| TRUNC | Yes |
|
||||
|
||||
### Spatial Functions
|
||||
|
||||
| Function | Implemented |
|
||||
|--------------------|-------------|
|
||||
| ------------------ | ----------- |
|
||||
| ST_AREA | No |
|
||||
| ST_DISTANCE | No |
|
||||
| ST_WITHIN | No |
|
||||
@@ -148,8 +160,9 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| ST_ISVALIDDETAILED | No |
|
||||
|
||||
### String Functions
|
||||
|
||||
| Function | Implemented |
|
||||
|-----------------|-------------|
|
||||
| --------------- | ----------- |
|
||||
| CONCAT | Yes |
|
||||
| CONTAINS | Yes |
|
||||
| ENDSWITH | Yes |
|
||||
@@ -177,8 +190,9 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| UPPER | Yes |
|
||||
|
||||
### Type checking Functions
|
||||
|
||||
| Function | Implemented |
|
||||
|------------------|-------------|
|
||||
| ---------------- | ----------- |
|
||||
| IS_ARRAY | Yes |
|
||||
| IS_BOOL | Yes |
|
||||
| IS_DEFINED | Yes |
|
||||
|
||||
@@ -12,6 +12,9 @@ import (
|
||||
)
|
||||
|
||||
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -20,6 +23,9 @@ func GetAllCollections(databaseId string) ([]repositorymodels.Collection, reposi
|
||||
}
|
||||
|
||||
func GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -32,6 +38,9 @@ func GetCollection(databaseId string, collectionId string) (repositorymodels.Col
|
||||
}
|
||||
|
||||
func DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -46,6 +55,9 @@ func DeleteCollection(databaseId string, collectionId string) repositorymodels.R
|
||||
}
|
||||
|
||||
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||
|
||||
@@ -11,10 +11,16 @@ import (
|
||||
)
|
||||
|
||||
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
return maps.Values(storeState.Databases), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
if database, ok := storeState.Databases[id]; ok {
|
||||
return database, repositorymodels.StatusOk
|
||||
}
|
||||
@@ -23,6 +29,9 @@ func GetDatabase(id string) (repositorymodels.Database, repositorymodels.Reposit
|
||||
}
|
||||
|
||||
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
if _, ok := storeState.Databases[id]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -33,6 +42,9 @@ func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||
}
|
||||
|
||||
func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
if _, ok := storeState.Databases[newDatabase.ID]; ok {
|
||||
return repositorymodels.Database{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@ import (
|
||||
)
|
||||
|
||||
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -27,6 +30,9 @@ func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels
|
||||
}
|
||||
|
||||
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -43,6 +49,9 @@ func GetDocument(databaseId string, collectionId string, documentId string) (rep
|
||||
}
|
||||
|
||||
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
@@ -61,6 +70,9 @@ func DeleteDocument(databaseId string, collectionId string, documentId string) r
|
||||
}
|
||||
|
||||
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
storeState.Lock()
|
||||
defer storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var documentId string
|
||||
var database repositorymodels.Database
|
||||
@@ -111,7 +123,7 @@ func ExecuteQueryDocuments(databaseId string, collectionId string, query string,
|
||||
|
||||
if typedQuery, ok := parsedQuery.(parsers.SelectStmt); ok {
|
||||
typedQuery.Parameters = queryParameters
|
||||
return memoryexecutor.Execute(typedQuery, covDocs), repositorymodels.StatusOk
|
||||
return memoryexecutor.ExecuteQuery(typedQuery, covDocs), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return nil, repositorymodels.BadRequest
|
||||
|
||||
@@ -10,6 +10,9 @@ import (
|
||||
|
||||
// I have no idea what this is tbh
|
||||
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
databaseRid := databaseId
|
||||
collectionRid := collectionId
|
||||
var timestamp int64 = 0
|
||||
|
||||
@@ -66,6 +66,9 @@ func LoadStateFS(filePath string) {
|
||||
}
|
||||
|
||||
func SaveStateFS(filePath string) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
data, err := json.MarshalIndent(storeState, "", "\t")
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to save state: %v\n", err)
|
||||
@@ -80,8 +83,17 @@ func SaveStateFS(filePath string) {
|
||||
logger.Infof("Documents: %d\n", getLength(storeState.Documents))
|
||||
}
|
||||
|
||||
func GetState() repositorymodels.State {
|
||||
return storeState
|
||||
func GetState() (string, error) {
|
||||
storeState.RLock()
|
||||
defer storeState.RUnlock()
|
||||
|
||||
data, err := json.MarshalIndent(storeState, "", "\t")
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to serialize state: %v\n", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func getLength(v interface{}) int {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package repositorymodels
|
||||
|
||||
import "sync"
|
||||
|
||||
type Database struct {
|
||||
ID string `json:"id"`
|
||||
TimeStamp int64 `json:"_ts"`
|
||||
@@ -101,6 +103,8 @@ type PartitionKeyRange struct {
|
||||
}
|
||||
|
||||
type State struct {
|
||||
sync.RWMutex
|
||||
|
||||
// Map databaseId -> Database
|
||||
Databases map[string]Database `json:"databases"`
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@ package parsers
|
||||
type SelectStmt struct {
|
||||
SelectItems []SelectItem
|
||||
Table Table
|
||||
JoinItems []JoinItem
|
||||
Filters interface{}
|
||||
Exists bool
|
||||
Distinct bool
|
||||
Count int
|
||||
Offset int
|
||||
@@ -13,7 +15,13 @@ type SelectStmt struct {
|
||||
}
|
||||
|
||||
type Table struct {
|
||||
Value string
|
||||
Value string
|
||||
SelectItem SelectItem
|
||||
}
|
||||
|
||||
type JoinItem struct {
|
||||
Table Table
|
||||
SelectItem SelectItem
|
||||
}
|
||||
|
||||
type SelectItemType int
|
||||
@@ -24,6 +32,7 @@ const (
|
||||
SelectItemTypeArray
|
||||
SelectItemTypeConstant
|
||||
SelectItemTypeFunctionCall
|
||||
SelectItemTypeSubQuery
|
||||
)
|
||||
|
||||
type SelectItem struct {
|
||||
@@ -120,6 +129,43 @@ const (
|
||||
FunctionCallSetIntersect FunctionCallType = "SetIntersect"
|
||||
FunctionCallSetUnion FunctionCallType = "SetUnion"
|
||||
|
||||
FunctionCallMathAbs FunctionCallType = "MathAbs"
|
||||
FunctionCallMathAcos FunctionCallType = "MathAcos"
|
||||
FunctionCallMathAsin FunctionCallType = "MathAsin"
|
||||
FunctionCallMathAtan FunctionCallType = "MathAtan"
|
||||
FunctionCallMathAtn2 FunctionCallType = "MathAtn2"
|
||||
FunctionCallMathCeiling FunctionCallType = "MathCeiling"
|
||||
FunctionCallMathCos FunctionCallType = "MathCos"
|
||||
FunctionCallMathCot FunctionCallType = "MathCot"
|
||||
FunctionCallMathDegrees FunctionCallType = "MathDegrees"
|
||||
FunctionCallMathExp FunctionCallType = "MathExp"
|
||||
FunctionCallMathFloor FunctionCallType = "MathFloor"
|
||||
FunctionCallMathIntAdd FunctionCallType = "MathIntAdd"
|
||||
FunctionCallMathIntBitAnd FunctionCallType = "MathIntBitAnd"
|
||||
FunctionCallMathIntBitLeftShift FunctionCallType = "MathIntBitLeftShift"
|
||||
FunctionCallMathIntBitNot FunctionCallType = "MathIntBitNot"
|
||||
FunctionCallMathIntBitOr FunctionCallType = "MathIntBitOr"
|
||||
FunctionCallMathIntBitRightShift FunctionCallType = "MathIntBitRightShift"
|
||||
FunctionCallMathIntBitXor FunctionCallType = "MathIntBitXor"
|
||||
FunctionCallMathIntDiv FunctionCallType = "MathIntDiv"
|
||||
FunctionCallMathIntMod FunctionCallType = "MathIntMod"
|
||||
FunctionCallMathIntMul FunctionCallType = "MathIntMul"
|
||||
FunctionCallMathIntSub FunctionCallType = "MathIntSub"
|
||||
FunctionCallMathLog FunctionCallType = "MathLog"
|
||||
FunctionCallMathLog10 FunctionCallType = "MathLog10"
|
||||
FunctionCallMathNumberBin FunctionCallType = "MathNumberBin"
|
||||
FunctionCallMathPi FunctionCallType = "MathPi"
|
||||
FunctionCallMathPower FunctionCallType = "MathPower"
|
||||
FunctionCallMathRadians FunctionCallType = "MathRadians"
|
||||
FunctionCallMathRand FunctionCallType = "MathRand"
|
||||
FunctionCallMathRound FunctionCallType = "MathRound"
|
||||
FunctionCallMathSign FunctionCallType = "MathSign"
|
||||
FunctionCallMathSin FunctionCallType = "MathSin"
|
||||
FunctionCallMathSqrt FunctionCallType = "MathSqrt"
|
||||
FunctionCallMathSquare FunctionCallType = "MathSquare"
|
||||
FunctionCallMathTan FunctionCallType = "MathTan"
|
||||
FunctionCallMathTrunc FunctionCallType = "MathTrunc"
|
||||
|
||||
FunctionCallAggregateAvg FunctionCallType = "AggregateAvg"
|
||||
FunctionCallAggregateCount FunctionCallType = "AggregateCount"
|
||||
FunctionCallAggregateMax FunctionCallType = "AggregateMax"
|
||||
|
||||
57
parsers/nosql/join_test.go
Normal file
57
parsers/nosql/join_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_Join(t *testing.T) {
|
||||
|
||||
t.Run("Should parse simple JOIN", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, c["pk"] FROM c JOIN cc IN c["tags"]`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN VALUE", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT VALUE cc FROM c JOIN cc IN c["tags"]`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
650
parsers/nosql/math_functions_test.go
Normal file
650
parsers/nosql/math_functions_test.go
Normal file
@@ -0,0 +1,650 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
t.Run("Should parse function ABS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ABS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAbs,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ACOS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ACOS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAcos,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ASIN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ASIN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAsin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ATAN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ATAN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAtan,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function CEILING(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT CEILING(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCeiling,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT COS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCos,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COT(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT COT(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCot,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function DEGREES(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT DEGREES(c.value) FROM c`,
|
||||
parsers.FunctionCallMathDegrees,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function EXP(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT EXP(c.value) FROM c`,
|
||||
parsers.FunctionCallMathExp,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function FLOOR(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT FLOOR(c.value) FROM c`,
|
||||
parsers.FunctionCallMathFloor,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitNot(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitNot(c.value) FROM c`,
|
||||
parsers.FunctionCallMathIntBitNot,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG10(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG10(c.value) FROM c`,
|
||||
parsers.FunctionCallMathLog10,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function RADIANS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT RADIANS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathRadians,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ROUND(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ROUND(c.value) FROM c`,
|
||||
parsers.FunctionCallMathRound,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SIGN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SIGN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSign,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SIN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SIN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SQRT(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SQRT(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSqrt,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SQUARE(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SQUARE(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSquare,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function TAN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT TAN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathTan,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function TRUNC(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT TRUNC(c.value) FROM c`,
|
||||
parsers.FunctionCallMathTrunc,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ATN2(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ATN2(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathAtn2,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntAdd(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntAdd(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntAdd,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitAnd(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitAnd(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitAnd,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitLeftShift(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitLeftShift(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitLeftShift,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitOr(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitOr(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitOr,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitRightShift(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitRightShift(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitRightShift,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitXor(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitXor(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitXor,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntDiv(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntDiv(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntDiv,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntMod(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntMod(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntMod,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntMul(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntMul(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntMul,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntSub(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntSub(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntSub,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function POWER(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT POWER(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathPower,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG(c.value) FROM c`,
|
||||
parsers.FunctionCallMathLog,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathLog,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function NumberBin(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT NumberBin(c.value) FROM c`,
|
||||
parsers.FunctionCallMathNumberBin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function NumberBin(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT NumberBin(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathNumberBin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function PI()", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT PI() FROM c`,
|
||||
parsers.FunctionCallMathPi,
|
||||
[]interface{}{},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function RAND()", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT RAND() FROM c`,
|
||||
parsers.FunctionCallMathRand,
|
||||
[]interface{}{},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func testMathFunctionParse(
|
||||
t *testing.T,
|
||||
query string,
|
||||
expectedFunctionType parsers.FunctionCallType,
|
||||
expectedArguments []interface{},
|
||||
expectedTable string,
|
||||
) {
|
||||
testQueryParse(
|
||||
t,
|
||||
query,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: expectedFunctionType,
|
||||
Arguments: expectedArguments,
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: expectedTable},
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -122,4 +122,25 @@ func Test_Parse(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN selector", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c IN c.tags`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,14 +4,24 @@ package nosql
|
||||
import "github.com/pikami/cosmium/parsers"
|
||||
|
||||
func makeSelectStmt(
|
||||
columns, table,
|
||||
columns, fromClause, joinItems,
|
||||
whereClause interface{}, distinctClause interface{},
|
||||
count interface{}, groupByClause interface{}, orderList interface{},
|
||||
offsetClause interface{},
|
||||
) (parsers.SelectStmt, error) {
|
||||
selectStmt := parsers.SelectStmt{
|
||||
SelectItems: columns.([]parsers.SelectItem),
|
||||
Table: table.(parsers.Table),
|
||||
}
|
||||
|
||||
if fromTable, ok := fromClause.(parsers.Table); ok {
|
||||
selectStmt.Table = fromTable
|
||||
}
|
||||
|
||||
if joinItemsArray, ok := joinItems.([]interface{}); ok && len(joinItemsArray) > 0 {
|
||||
selectStmt.JoinItems = make([]parsers.JoinItem, len(joinItemsArray))
|
||||
for i, joinItem := range joinItemsArray {
|
||||
selectStmt.JoinItems[i] = joinItem.(parsers.JoinItem)
|
||||
}
|
||||
}
|
||||
|
||||
switch v := whereClause.(type) {
|
||||
@@ -48,6 +58,21 @@ func makeSelectStmt(
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
func makeJoin(table interface{}, column interface{}) (parsers.JoinItem, error) {
|
||||
joinItem := parsers.JoinItem{}
|
||||
|
||||
if selectItem, isSelectItem := column.(parsers.SelectItem); isSelectItem {
|
||||
joinItem.SelectItem = selectItem
|
||||
joinItem.Table.Value = selectItem.Alias
|
||||
}
|
||||
|
||||
if tableTyped, isTable := table.(parsers.Table); isTable {
|
||||
joinItem.Table = tableTyped
|
||||
}
|
||||
|
||||
return joinItem, nil
|
||||
}
|
||||
|
||||
func makeSelectItem(name interface{}, path interface{}, selectItemType parsers.SelectItemType) (parsers.SelectItem, error) {
|
||||
ps := path.([]interface{})
|
||||
|
||||
@@ -161,13 +186,15 @@ Input <- selectStmt:SelectStmt {
|
||||
|
||||
SelectStmt <- Select ws
|
||||
distinctClause:DistinctClause? ws
|
||||
topClause:TopClause? ws columns:Selection ws
|
||||
From ws table:TableName ws
|
||||
topClause:TopClause? ws
|
||||
columns:Selection ws
|
||||
fromClause:FromClause? ws
|
||||
joinClauses:(ws join:JoinClause { return join, nil })* ws
|
||||
whereClause:(ws Where ws condition:Condition { return condition, nil })?
|
||||
groupByClause:(ws GroupBy ws columns:ColumnList { return columns, nil })?
|
||||
orderByClause:OrderByClause?
|
||||
offsetClause:OffsetClause? {
|
||||
return makeSelectStmt(columns, table, whereClause,
|
||||
orderByClause:(ws order:OrderByClause { return order, nil })?
|
||||
offsetClause:(ws offset:OffsetClause { return offset, nil })? {
|
||||
return makeSelectStmt(columns, fromClause, joinClauses, whereClause,
|
||||
distinctClause, topClause, groupByClause, orderByClause, offsetClause)
|
||||
}
|
||||
|
||||
@@ -177,6 +204,51 @@ TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
FromClause <- From ws table:TableName selectItem:(ws "IN"i ws column:SelectItem { return column, nil })? {
|
||||
tableTyped := table.(parsers.Table)
|
||||
|
||||
if selectItem != nil {
|
||||
tableTyped.SelectItem = selectItem.(parsers.SelectItem)
|
||||
}
|
||||
|
||||
return tableTyped, nil
|
||||
} / From ws subQuery:SubQuerySelectItem {
|
||||
subQueryTyped := subQuery.(parsers.SelectItem)
|
||||
table := parsers.Table{
|
||||
Value: subQueryTyped.Alias,
|
||||
SelectItem: subQueryTyped,
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
||||
SubQuery <- exists:(exists:Exists ws { return exists, nil })? "(" ws selectStmt:SelectStmt ws ")" {
|
||||
if selectStatement, isGoodValue := selectStmt.(parsers.SelectStmt); isGoodValue {
|
||||
selectStatement.Exists = exists != nil
|
||||
return selectStatement, nil
|
||||
}
|
||||
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return alias, nil })? {
|
||||
selectItem := parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: subQuery,
|
||||
}
|
||||
|
||||
if tableName, isString := asClause.(string); isString {
|
||||
selectItem.Alias = tableName
|
||||
}
|
||||
|
||||
return selectItem, nil
|
||||
}
|
||||
|
||||
JoinClause <- Join ws table:TableName ws "IN"i ws column:SelectItem {
|
||||
return makeJoin(table, column)
|
||||
} / Join ws subQuery:SubQuerySelectItem {
|
||||
return makeJoin(nil, subQuery)
|
||||
}
|
||||
|
||||
OffsetClause <- "OFFSET"i ws offset:IntegerLiteral ws "LIMIT"i ws limit:IntegerLiteral {
|
||||
return []interface{}{offset.(parsers.Constant).Value, limit.(parsers.Constant).Value}, nil
|
||||
}
|
||||
@@ -221,7 +293,7 @@ SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
|
||||
return makeSelectItem(name, path, parsers.SelectItemTypeField)
|
||||
}
|
||||
|
||||
SelectItem <- selectItem:(Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
var itemResult parsers.SelectItem
|
||||
switch typedValue := selectItem.(type) {
|
||||
case parsers.SelectItem:
|
||||
@@ -251,9 +323,8 @@ DotFieldAccess <- "." id:Identifier {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
|
||||
return id, nil
|
||||
}
|
||||
ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil }
|
||||
/ "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil }
|
||||
|
||||
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
|
||||
return string(c.text), nil
|
||||
@@ -301,11 +372,15 @@ As <- "AS"i
|
||||
|
||||
From <- "FROM"i
|
||||
|
||||
Join <- "JOIN"i
|
||||
|
||||
Exists <- "EXISTS"i
|
||||
|
||||
Where <- "WHERE"i
|
||||
|
||||
And <- "AND"i
|
||||
|
||||
Or <- "OR"i
|
||||
Or <- "OR"i wss
|
||||
|
||||
GroupBy <- "GROUP"i ws "BY"i
|
||||
|
||||
@@ -344,6 +419,7 @@ FunctionCall <- StringFunctions
|
||||
/ ArrayFunctions
|
||||
/ InFunction
|
||||
/ AggregateFunctions
|
||||
/ MathFunctions
|
||||
|
||||
StringFunctions <- StringEqualsExpression
|
||||
/ ToStringExpression
|
||||
@@ -385,6 +461,43 @@ ArrayFunctions <- ArrayConcatExpression
|
||||
/ SetIntersectExpression
|
||||
/ SetUnionExpression
|
||||
|
||||
MathFunctions <- MathAbsExpression
|
||||
/ MathAcosExpression
|
||||
/ MathAsinExpression
|
||||
/ MathAtanExpression
|
||||
/ MathCeilingExpression
|
||||
/ MathCosExpression
|
||||
/ MathCotExpression
|
||||
/ MathDegreesExpression
|
||||
/ MathExpExpression
|
||||
/ MathFloorExpression
|
||||
/ MathIntBitNotExpression
|
||||
/ MathLog10Expression
|
||||
/ MathRadiansExpression
|
||||
/ MathRoundExpression
|
||||
/ MathSignExpression
|
||||
/ MathSinExpression
|
||||
/ MathSqrtExpression
|
||||
/ MathSquareExpression
|
||||
/ MathTanExpression
|
||||
/ MathTruncExpression
|
||||
/ MathAtn2Expression
|
||||
/ MathIntAddExpression
|
||||
/ MathIntBitAndExpression
|
||||
/ MathIntBitLeftShiftExpression
|
||||
/ MathIntBitOrExpression
|
||||
/ MathIntBitRightShiftExpression
|
||||
/ MathIntBitXorExpression
|
||||
/ MathIntDivExpression
|
||||
/ MathIntModExpression
|
||||
/ MathIntMulExpression
|
||||
/ MathIntSubExpression
|
||||
/ MathPowerExpression
|
||||
/ MathLogExpression
|
||||
/ MathNumberBinExpression
|
||||
/ MathPiExpression
|
||||
/ MathRandExpression
|
||||
|
||||
UpperExpression <- "UPPER"i ws "(" ex:SelectItem ")" {
|
||||
return createFunctionCall(parsers.FunctionCallUpper, []interface{}{ex})
|
||||
}
|
||||
@@ -528,6 +641,49 @@ SetUnionExpression <- "SetUnion"i ws "(" ws set1:SelectItem ws "," ws set2:Selec
|
||||
return createFunctionCall(parsers.FunctionCallSetUnion, []interface{}{set1, set2})
|
||||
}
|
||||
|
||||
MathAbsExpression <- "ABS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAbs, []interface{}{ex}) }
|
||||
MathAcosExpression <- "ACOS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAcos, []interface{}{ex}) }
|
||||
MathAsinExpression <- "ASIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAsin, []interface{}{ex}) }
|
||||
MathAtanExpression <- "ATAN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAtan, []interface{}{ex}) }
|
||||
MathCeilingExpression <- "CEILING"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCeiling, []interface{}{ex}) }
|
||||
MathCosExpression <- "COS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCos, []interface{}{ex}) }
|
||||
MathCotExpression <- "COT"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCot, []interface{}{ex}) }
|
||||
MathDegreesExpression <- "DEGREES"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathDegrees, []interface{}{ex}) }
|
||||
MathExpExpression <- "EXP"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathExp, []interface{}{ex}) }
|
||||
MathFloorExpression <- "FLOOR"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathFloor, []interface{}{ex}) }
|
||||
MathIntBitNotExpression <- "IntBitNot"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitNot, []interface{}{ex}) }
|
||||
MathLog10Expression <- "LOG10"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathLog10, []interface{}{ex}) }
|
||||
MathRadiansExpression <- "RADIANS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathRadians, []interface{}{ex}) }
|
||||
MathRoundExpression <- "ROUND"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathRound, []interface{}{ex}) }
|
||||
MathSignExpression <- "SIGN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSign, []interface{}{ex}) }
|
||||
MathSinExpression <- "SIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSin, []interface{}{ex}) }
|
||||
MathSqrtExpression <- "SQRT"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSqrt, []interface{}{ex}) }
|
||||
MathSquareExpression <- "SQUARE"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSquare, []interface{}{ex}) }
|
||||
MathTanExpression <- "TAN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathTan, []interface{}{ex}) }
|
||||
MathTruncExpression <- "TRUNC"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathTrunc, []interface{}{ex}) }
|
||||
|
||||
MathAtn2Expression <- "ATN2"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAtn2, []interface{}{set1, set2}) }
|
||||
MathIntAddExpression <- "IntAdd"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntAdd, []interface{}{set1, set2}) }
|
||||
MathIntBitAndExpression <- "IntBitAnd"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitAnd, []interface{}{set1, set2}) }
|
||||
MathIntBitLeftShiftExpression <- "IntBitLeftShift"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitLeftShift, []interface{}{set1, set2}) }
|
||||
MathIntBitOrExpression <- "IntBitOr"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitOr, []interface{}{set1, set2}) }
|
||||
MathIntBitRightShiftExpression <- "IntBitRightShift"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitRightShift, []interface{}{set1, set2}) }
|
||||
MathIntBitXorExpression <- "IntBitXor"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitXor, []interface{}{set1, set2}) }
|
||||
MathIntDivExpression <- "IntDiv"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntDiv, []interface{}{set1, set2}) }
|
||||
MathIntModExpression <- "IntMod"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntMod, []interface{}{set1, set2}) }
|
||||
MathIntMulExpression <- "IntMul"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntMul, []interface{}{set1, set2}) }
|
||||
MathIntSubExpression <- "IntSub"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntSub, []interface{}{set1, set2}) }
|
||||
MathPowerExpression <- "POWER"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathPower, []interface{}{set1, set2}) }
|
||||
|
||||
MathLogExpression <- "LOG"i ws "(" ws ex1:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallMathLog, append([]interface{}{ex1}, others.([]interface{})...))
|
||||
}
|
||||
MathNumberBinExpression <- "NumberBin"i ws "(" ws ex1:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallMathNumberBin, append([]interface{}{ex1}, others.([]interface{})...))
|
||||
}
|
||||
MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) }
|
||||
MathRandExpression <- "RAND"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathRand, []interface{}{}) }
|
||||
|
||||
InFunction <- ex1:SelectProperty ws "IN"i ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
}
|
||||
@@ -575,4 +731,6 @@ non_escape_character <- !(escape_character) char:.
|
||||
|
||||
ws <- [ \t\n\r]*
|
||||
|
||||
wss <- [ \t\n\r]+
|
||||
|
||||
EOF <- !.
|
||||
|
||||
125
parsers/nosql/subquery_test.go
Normal file
125
parsers/nosql/subquery_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_SubQuery(t *testing.T) {
|
||||
|
||||
t.Run("Should parse FROM subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM (SELECT VALUE cc["info"] FROM cc) AS c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, cc.name FROM c JOIN (SELECT tag.name FROM tag IN c.tags) AS cc`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "cc",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN EXISTS subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id
|
||||
FROM c
|
||||
JOIN (
|
||||
SELECT VALUE EXISTS(SELECT tag.name FROM tag IN c.tags)
|
||||
) AS hasTags
|
||||
WHERE hasTags`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{Value: "hasTags"},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "hasTags",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
IsTopLevel: true,
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
Exists: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"hasTags"},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -6,21 +6,19 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Avg(arguments []interface{}, row RowType) interface{} {
|
||||
func (r rowContext) aggregate_Avg(arguments []interface{}) 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++
|
||||
}
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,41 +29,37 @@ func (c memoryExecutorContext) aggregate_Avg(arguments []interface{}, row RowTyp
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Count(arguments []interface{}, row RowType) interface{} {
|
||||
func (r rowContext) aggregate_Count(arguments []interface{}) 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++
|
||||
}
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if value != nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Max(arguments []interface{}, row RowType) interface{} {
|
||||
func (r rowContext) aggregate_Max(arguments []interface{}) 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++
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
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++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,25 +70,23 @@ func (c memoryExecutorContext) aggregate_Max(arguments []interface{}, row RowTyp
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Min(arguments []interface{}, row RowType) interface{} {
|
||||
func (r rowContext) aggregate_Min(arguments []interface{}) 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++
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
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++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,21 +97,19 @@ func (c memoryExecutorContext) aggregate_Min(arguments []interface{}, row RowTyp
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregate_Sum(arguments []interface{}, row RowType) interface{} {
|
||||
func (r rowContext) aggregate_Sum(arguments []interface{}) 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++
|
||||
}
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,17 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) array_Concat(arguments []interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) array_Concat(arguments []interface{}) []interface{} {
|
||||
var result []interface{}
|
||||
for _, arg := range arguments {
|
||||
array := c.parseArray(arg, row)
|
||||
array := r.parseArray(arg)
|
||||
result = append(result, array...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) array_Length(arguments []interface{}, row RowType) int {
|
||||
array := c.parseArray(arguments[0], row)
|
||||
func (r rowContext) array_Length(arguments []interface{}) int {
|
||||
array := r.parseArray(arguments[0])
|
||||
if array == nil {
|
||||
return 0
|
||||
}
|
||||
@@ -25,15 +25,15 @@ func (c memoryExecutorContext) array_Length(arguments []interface{}, row RowType
|
||||
return len(array)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) array_Slice(arguments []interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) array_Slice(arguments []interface{}) []interface{} {
|
||||
var ok bool
|
||||
var start int
|
||||
var length int
|
||||
array := c.parseArray(arguments[0], row)
|
||||
startEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
array := r.parseArray(arguments[0])
|
||||
startEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if arguments[2] != nil {
|
||||
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
||||
lengthEx := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
logger.Error("array_Slice - got length parameters of wrong type")
|
||||
@@ -65,9 +65,9 @@ func (c memoryExecutorContext) array_Slice(arguments []interface{}, row RowType)
|
||||
return array[start:end]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) set_Intersect(arguments []interface{}, row RowType) []interface{} {
|
||||
set1 := c.parseArray(arguments[0], row)
|
||||
set2 := c.parseArray(arguments[1], row)
|
||||
func (r rowContext) set_Intersect(arguments []interface{}) []interface{} {
|
||||
set1 := r.parseArray(arguments[0])
|
||||
set2 := r.parseArray(arguments[1])
|
||||
|
||||
intersection := make(map[interface{}]struct{})
|
||||
if set1 == nil || set2 == nil {
|
||||
@@ -88,9 +88,9 @@ func (c memoryExecutorContext) set_Intersect(arguments []interface{}, row RowTyp
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) set_Union(arguments []interface{}, row RowType) []interface{} {
|
||||
set1 := c.parseArray(arguments[0], row)
|
||||
set2 := c.parseArray(arguments[1], row)
|
||||
func (r rowContext) set_Union(arguments []interface{}) []interface{} {
|
||||
set1 := r.parseArray(arguments[0])
|
||||
set2 := r.parseArray(arguments[1])
|
||||
|
||||
var result []interface{}
|
||||
union := make(map[interface{}]struct{})
|
||||
@@ -111,9 +111,9 @@ func (c memoryExecutorContext) set_Union(arguments []interface{}, row RowType) [
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) parseArray(argument interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) parseArray(argument interface{}) []interface{} {
|
||||
exItem := argument.(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
arrValue := reflect.ValueOf(ex)
|
||||
if arrValue.Kind() != reflect.Slice {
|
||||
|
||||
86
query_executors/memory_executor/join_test.go
Normal file
86
query_executors/memory_executor/join_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_Joins(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{
|
||||
"id": 1,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "a"},
|
||||
{"name": "b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": 2,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "b"},
|
||||
{"name": "c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute JOIN on 'tags'", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "name": "a"},
|
||||
map[string]interface{}{"id": 1, "name": "b"},
|
||||
map[string]interface{}{"id": 2, "name": "b"},
|
||||
map[string]interface{}{"id": 2, "name": "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN VALUE on 'tags'", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "a"},
|
||||
map[string]interface{}{"name": "b"},
|
||||
map[string]interface{}{"name": "b"},
|
||||
map[string]interface{}{"name": "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
615
query_executors/memory_executor/math_functions.go
Normal file
615
query_executors/memory_executor/math_functions.go
Normal file
@@ -0,0 +1,615 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (r rowContext) math_Abs(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Abs(val)
|
||||
case int:
|
||||
if val < 0 {
|
||||
return -val
|
||||
}
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Abs - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Acos(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Acos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.Debug("math_Acos - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Acos(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Asin(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Asin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.Debug("math_Asin - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Asin(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Atan(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Atan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Ceiling(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Ceil(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Ceiling - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Cos(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Cos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Cos(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Cot(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Cot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val == 0 {
|
||||
logger.Debug("math_Cot - cotangent undefined for zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return 1 / math.Tan(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Degrees(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Degrees - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (180 / math.Pi)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Exp(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Exp - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Exp(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Floor(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Floor(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Floor - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitNot(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case int:
|
||||
return ^val
|
||||
default:
|
||||
logger.Debug("math_IntBitNot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Log10(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Log10 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val <= 0 {
|
||||
logger.Debug("math_Log10 - value must be greater than 0")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log10(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Radians(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Radians - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (math.Pi / 180.0)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Round(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Round(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.Debug("math_Round - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sign(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
case int:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
default:
|
||||
logger.Debug("math_Sign - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sin(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Sin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sin(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sqrt(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Sqrt - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sqrt(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Square(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Square - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(val, 2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Tan(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.Debug("math_Tan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Tan(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Trunc(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Trunc(val)
|
||||
case int:
|
||||
return float64(val)
|
||||
default:
|
||||
logger.Debug("math_Trunc - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Atn2(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
y, yIsNumber := numToFloat64(ex1)
|
||||
x, xIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !yIsNumber || !xIsNumber {
|
||||
logger.Debug("math_Atn2 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan2(y, x)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntAdd(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
ex1Number, ex1IsNumber := numToInt(ex1)
|
||||
ex2Number, ex2IsNumber := numToInt(ex2)
|
||||
|
||||
if !ex1IsNumber || !ex2IsNumber {
|
||||
logger.Debug("math_IntAdd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Number + ex2Number
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitAnd(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
ex1Int, ex1IsInt := numToInt(ex1)
|
||||
ex2Int, ex2IsInt := numToInt(ex2)
|
||||
|
||||
if !ex1IsInt || !ex2IsInt {
|
||||
logger.Debug("math_IntBitAnd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Int & ex2Int
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitLeftShift(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitLeftShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 << uint(num2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitOr(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitOr - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 | num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitRightShift(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitRightShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 >> uint(num2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitXor(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntBitXor - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 ^ num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntDiv(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.Debug("math_IntDiv - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 / num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntMul(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntMul - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 * num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntSub(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.Debug("math_IntSub - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 - num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntMod(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.Debug("math_IntMod - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 % num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_Power(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
base, baseIsNumber := numToFloat64(ex1)
|
||||
exponent, exponentIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !baseIsNumber || !exponentIsNumber {
|
||||
logger.Debug("math_Power - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(base, exponent)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Log(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem1)
|
||||
|
||||
var base float64 = math.E
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
baseValueObject := r.resolveSelectItem(exItem2)
|
||||
baseValue, baseValueIsNumber := numToFloat64(baseValueObject)
|
||||
|
||||
if !baseValueIsNumber {
|
||||
logger.Debug("math_Log - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if baseValue > 0 && baseValue != 1 {
|
||||
base = baseValue
|
||||
} else {
|
||||
logger.Debug("math_Log - base must be greater than 0 and not equal to 1")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber || num <= 0 {
|
||||
logger.Debug("math_Log - parameter must be a positive numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log(num) / math.Log(base)
|
||||
}
|
||||
|
||||
func (r rowContext) math_NumberBin(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem1)
|
||||
|
||||
binSize := 1.0
|
||||
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
binSizeValueObject := r.resolveSelectItem(exItem2)
|
||||
binSizeValue, binSizeValueIsNumber := numToFloat64(binSizeValueObject)
|
||||
|
||||
if !binSizeValueIsNumber {
|
||||
logger.Debug("math_NumberBin - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if binSizeValue != 0 {
|
||||
binSize = binSizeValue
|
||||
} else {
|
||||
logger.Debug("math_NumberBin - base must not be equal to 0")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber {
|
||||
logger.Debug("math_NumberBin - parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Floor(num/binSize) * binSize
|
||||
}
|
||||
|
||||
func (r rowContext) math_Pi() interface{} {
|
||||
return math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Rand() interface{} {
|
||||
return rand.Float64()
|
||||
}
|
||||
|
||||
func numToInt(ex interface{}) (int, bool) {
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return int(val), true
|
||||
case int:
|
||||
return val, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func numToFloat64(num interface{}) (float64, bool) {
|
||||
switch val := num.(type) {
|
||||
case float64:
|
||||
return val, true
|
||||
case int:
|
||||
return float64(val), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
269
query_executors/memory_executor/math_functions_test.go
Normal file
269
query_executors/memory_executor/math_functions_test.go
Normal file
@@ -0,0 +1,269 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": 0.0},
|
||||
map[string]interface{}{"id": 2, "value": 1.0},
|
||||
map[string]interface{}{"id": 3, "value": -1.0},
|
||||
map[string]interface{}{"id": 4, "value": 0.5},
|
||||
map[string]interface{}{"id": 5, "value": -0.5},
|
||||
map[string]interface{}{"id": 6, "value": 0.707},
|
||||
map[string]interface{}{"id": 7, "value": -0.707},
|
||||
map[string]interface{}{"id": 8, "value": 0.866},
|
||||
map[string]interface{}{"id": 9, "value": -0.866},
|
||||
}
|
||||
|
||||
mockDataInts := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": -1},
|
||||
map[string]interface{}{"id": 2, "value": 0},
|
||||
map[string]interface{}{"id": 3, "value": 1},
|
||||
map[string]interface{}{"id": 4, "value": 5},
|
||||
}
|
||||
|
||||
t.Run("Should execute function ABS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAbs,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": -1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": -0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": -0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866},
|
||||
map[string]interface{}{"value": -0.866, "result": 0.866},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ACOS(cosine)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAcos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Acos(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Acos(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Acos(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Acos(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Acos(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Acos(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Acos(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Acos(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Acos(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ASIN(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAsin,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Asin(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Asin(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Asin(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Asin(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Asin(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Asin(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Asin(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Asin(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Asin(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ATAN(tangent)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAtan,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Atan(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Atan(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Atan(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Atan(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Atan(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Atan(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Atan(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Atan(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Atan(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Cos(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Cos(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Cos(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Cos(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Cos(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Cos(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Cos(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Cos(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Cos(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COT(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCot,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": 1 / math.Tan(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": 1 / math.Tan(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": 1 / math.Tan(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": 1 / math.Tan(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": 1 / math.Tan(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": 1 / math.Tan(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": 1 / math.Tan(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": 1 / math.Tan(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function Degrees(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathDegrees,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -1.0, "result": -1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.5, "result": -0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.707, "result": -0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.866, "result": -0.866 * (180 / math.Pi)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function EXP(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathExp,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Exp(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Exp(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Exp(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Exp(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Exp(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Exp(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Exp(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Exp(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Exp(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function FLOOR(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathFloor,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Floor(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Floor(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Floor(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Floor(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Floor(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Floor(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Floor(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Floor(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Floor(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function IntBitNot(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathIntBitNot,
|
||||
mockDataInts,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": -1, "result": ^-1},
|
||||
map[string]interface{}{"value": 0, "result": ^0},
|
||||
map[string]interface{}{"value": 1, "result": ^1},
|
||||
map[string]interface{}{"value": 5, "result": ^5},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function LOG10(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathLog10,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Log10(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": nil},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Log10(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": nil},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Log10(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": nil},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Log10(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func testMathFunctionExecute(
|
||||
t *testing.T,
|
||||
functionCallType parsers.FunctionCallType,
|
||||
data []memoryexecutor.RowType,
|
||||
expectedData []memoryexecutor.RowType,
|
||||
) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "result",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: functionCallType,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
data,
|
||||
expectedData,
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
@@ -12,326 +13,220 @@ import (
|
||||
)
|
||||
|
||||
type RowType interface{}
|
||||
type ExpressionType interface{}
|
||||
|
||||
type memoryExecutorContext struct {
|
||||
parameters map[string]interface{}
|
||||
type rowContext struct {
|
||||
tables map[string]RowType
|
||||
parameters map[string]interface{}
|
||||
grouppedRows []rowContext
|
||||
}
|
||||
|
||||
func Execute(query parsers.SelectStmt, data []RowType) []RowType {
|
||||
ctx := memoryExecutorContext{
|
||||
parameters: query.Parameters,
|
||||
func ExecuteQuery(query parsers.SelectStmt, documents []RowType) []RowType {
|
||||
currentDocuments := make([]rowContext, 0)
|
||||
for _, doc := range documents {
|
||||
currentDocuments = append(currentDocuments, resolveFrom(query, doc)...)
|
||||
}
|
||||
|
||||
result := make([]RowType, 0)
|
||||
// Handle JOINS
|
||||
nextDocuments := make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
rowContexts := currentDocument.handleJoin(query)
|
||||
nextDocuments = append(nextDocuments, rowContexts...)
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
|
||||
// Apply Filter
|
||||
for _, row := range data {
|
||||
if ctx.evaluateFilters(query.Filters, row) {
|
||||
result = append(result, row)
|
||||
// Apply filters
|
||||
nextDocuments = make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
if currentDocument.applyFilters(query.Filters) {
|
||||
nextDocuments = append(nextDocuments, currentDocument)
|
||||
}
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
|
||||
// Apply order
|
||||
if query.OrderExpressions != nil && len(query.OrderExpressions) > 0 {
|
||||
ctx.orderBy(query.OrderExpressions, result)
|
||||
if len(query.OrderExpressions) > 0 {
|
||||
applyOrder(currentDocuments, query.OrderExpressions)
|
||||
}
|
||||
|
||||
// Apply group
|
||||
isGroupSelect := query.GroupBy != nil && len(query.GroupBy) > 0
|
||||
if isGroupSelect {
|
||||
result = ctx.groupBy(query, result)
|
||||
// Apply group by
|
||||
if len(query.GroupBy) > 0 {
|
||||
currentDocuments = applyGroupBy(currentDocuments, query.GroupBy)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
projectedDocuments := applyProjection(currentDocuments, query.SelectItems, query.GroupBy)
|
||||
|
||||
// Apply distinct
|
||||
if query.Distinct {
|
||||
result = deduplicate(result)
|
||||
projectedDocuments = deduplicate(projectedDocuments)
|
||||
}
|
||||
|
||||
// Apply result limit
|
||||
if query.Count > 0 {
|
||||
count := func() int {
|
||||
if len(result) < query.Count {
|
||||
return len(result)
|
||||
}
|
||||
return query.Count
|
||||
}()
|
||||
result = result[:count]
|
||||
if query.Count > 0 && len(projectedDocuments) > query.Count {
|
||||
projectedDocuments = projectedDocuments[:query.Count]
|
||||
}
|
||||
|
||||
return result
|
||||
return projectedDocuments
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) selectRow(selectItems []parsers.SelectItem, row RowType) interface{} {
|
||||
// When the first value is top level, select it instead
|
||||
if len(selectItems) > 0 && selectItems[0].IsTopLevel {
|
||||
return c.getFieldValue(selectItems[0], row)
|
||||
}
|
||||
|
||||
// Construct a new row based on the selected columns
|
||||
newRow := make(map[string]interface{})
|
||||
for index, column := range selectItems {
|
||||
destinationName := column.Alias
|
||||
if destinationName == "" {
|
||||
if len(column.Path) > 0 {
|
||||
destinationName = column.Path[len(column.Path)-1]
|
||||
} else {
|
||||
destinationName = fmt.Sprintf("$%d", index+1)
|
||||
}
|
||||
func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
||||
initialRow, gotParentContext := doc.(rowContext)
|
||||
if !gotParentContext {
|
||||
var initialTableName string
|
||||
if query.Table.SelectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
initialTableName = query.Table.SelectItem.Value.(parsers.SelectStmt).Table.Value
|
||||
}
|
||||
|
||||
newRow[destinationName] = c.getFieldValue(column, row)
|
||||
if initialTableName == "" {
|
||||
initialTableName = query.Table.Value
|
||||
}
|
||||
|
||||
initialRow = rowContext{
|
||||
parameters: query.Parameters,
|
||||
tables: map[string]RowType{
|
||||
initialTableName: doc,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return newRow
|
||||
if query.Table.SelectItem.Path != nil || query.Table.SelectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
destinationTableName := query.Table.SelectItem.Alias
|
||||
if destinationTableName == "" {
|
||||
destinationTableName = query.Table.Value
|
||||
}
|
||||
|
||||
selectValue := initialRow.parseArray(query.Table.SelectItem)
|
||||
rowContexts := make([]rowContext, len(selectValue))
|
||||
for i, newRowData := range selectValue {
|
||||
rowContexts[i].parameters = initialRow.parameters
|
||||
rowContexts[i].tables = copyMap(initialRow.tables)
|
||||
rowContexts[i].tables[destinationTableName] = newRowData
|
||||
}
|
||||
return rowContexts
|
||||
}
|
||||
|
||||
return []rowContext{initialRow}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) evaluateFilters(expr ExpressionType, row RowType) bool {
|
||||
if expr == nil {
|
||||
func (r rowContext) handleJoin(query parsers.SelectStmt) []rowContext {
|
||||
currentDocuments := []rowContext{r}
|
||||
|
||||
for _, joinItem := range query.JoinItems {
|
||||
nextDocuments := make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
joinedItems := currentDocument.resolveJoinItemSelect(joinItem.SelectItem)
|
||||
for _, joinedItem := range joinedItems {
|
||||
tablesCopy := copyMap(currentDocument.tables)
|
||||
tablesCopy[joinItem.Table.Value] = joinedItem
|
||||
nextDocuments = append(nextDocuments, rowContext{
|
||||
parameters: currentDocument.parameters,
|
||||
tables: tablesCopy,
|
||||
})
|
||||
}
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
}
|
||||
|
||||
return currentDocuments
|
||||
}
|
||||
|
||||
func (r rowContext) resolveJoinItemSelect(selectItem parsers.SelectItem) []RowType {
|
||||
if selectItem.Path != nil || selectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
selectValue := r.parseArray(selectItem)
|
||||
documents := make([]RowType, len(selectValue))
|
||||
for i, newRowData := range selectValue {
|
||||
documents[i] = newRowData
|
||||
}
|
||||
return documents
|
||||
}
|
||||
|
||||
return []RowType{}
|
||||
}
|
||||
|
||||
func (r rowContext) applyFilters(filters interface{}) bool {
|
||||
if filters == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
switch typedValue := expr.(type) {
|
||||
switch typedFilters := filters.(type) {
|
||||
case parsers.ComparisonExpression:
|
||||
leftValue := c.getExpressionParameterValue(typedValue.Left, row)
|
||||
rightValue := c.getExpressionParameterValue(typedValue.Right, row)
|
||||
|
||||
cmp := compareValues(leftValue, rightValue)
|
||||
switch typedValue.Operation {
|
||||
case "=":
|
||||
return cmp == 0
|
||||
case "!=":
|
||||
return cmp != 0
|
||||
case "<":
|
||||
return cmp < 0
|
||||
case ">":
|
||||
return cmp > 0
|
||||
case "<=":
|
||||
return cmp <= 0
|
||||
case ">=":
|
||||
return cmp >= 0
|
||||
}
|
||||
return r.filters_ComparisonExpression(typedFilters)
|
||||
case parsers.LogicalExpression:
|
||||
var result bool
|
||||
for i, expression := range typedValue.Expressions {
|
||||
expressionResult := c.evaluateFilters(expression, row)
|
||||
if i == 0 {
|
||||
result = expressionResult
|
||||
}
|
||||
|
||||
switch typedValue.Operation {
|
||||
case parsers.LogicalExpressionTypeAnd:
|
||||
result = result && expressionResult
|
||||
if !result {
|
||||
return false
|
||||
}
|
||||
case parsers.LogicalExpressionTypeOr:
|
||||
result = result || expressionResult
|
||||
if result {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
return r.filters_LogicalExpression(typedFilters)
|
||||
case parsers.Constant:
|
||||
if value, ok := typedValue.Value.(bool); ok {
|
||||
if value, ok := typedFilters.Value.(bool); ok {
|
||||
return value
|
||||
}
|
||||
return false
|
||||
case parsers.SelectItem:
|
||||
resolvedValue := c.getFieldValue(typedValue, row)
|
||||
resolvedValue := r.resolveSelectItem(typedFilters)
|
||||
if value, ok := resolvedValue.(bool); ok {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowType) interface{} {
|
||||
if field.Type == parsers.SelectItemTypeArray {
|
||||
arrayValue := make([]interface{}, 0)
|
||||
for _, selectItem := range field.SelectItems {
|
||||
arrayValue = append(arrayValue, c.getFieldValue(selectItem, row))
|
||||
}
|
||||
return arrayValue
|
||||
func (r rowContext) filters_ComparisonExpression(expression parsers.ComparisonExpression) bool {
|
||||
leftExpression, leftExpressionOk := expression.Left.(parsers.SelectItem)
|
||||
rightExpression, rightExpressionOk := expression.Right.(parsers.SelectItem)
|
||||
|
||||
if !leftExpressionOk || !rightExpressionOk {
|
||||
logger.Error("ComparisonExpression has incorrect Left or Right type")
|
||||
return false
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeObject {
|
||||
objectValue := make(map[string]interface{})
|
||||
for _, selectItem := range field.SelectItems {
|
||||
objectValue[selectItem.Alias] = c.getFieldValue(selectItem, row)
|
||||
}
|
||||
return objectValue
|
||||
leftValue := r.resolveSelectItem(leftExpression)
|
||||
rightValue := r.resolveSelectItem(rightExpression)
|
||||
|
||||
cmp := compareValues(leftValue, rightValue)
|
||||
switch expression.Operation {
|
||||
case "=":
|
||||
return cmp == 0
|
||||
case "!=":
|
||||
return cmp != 0
|
||||
case "<":
|
||||
return cmp < 0
|
||||
case ">":
|
||||
return cmp > 0
|
||||
case "<=":
|
||||
return cmp <= 0
|
||||
case ">=":
|
||||
return cmp >= 0
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeConstant {
|
||||
var typedValue parsers.Constant
|
||||
var ok bool
|
||||
if typedValue, ok = field.Value.(parsers.Constant); !ok {
|
||||
// TODO: Handle error
|
||||
logger.Error("parsers.Constant has incorrect Value type")
|
||||
return false
|
||||
}
|
||||
|
||||
func (r rowContext) filters_LogicalExpression(expression parsers.LogicalExpression) bool {
|
||||
var result bool
|
||||
for i, subExpression := range expression.Expressions {
|
||||
expressionResult := r.applyFilters(subExpression)
|
||||
if i == 0 {
|
||||
result = expressionResult
|
||||
}
|
||||
|
||||
if typedValue.Type == parsers.ConstantTypeParameterConstant &&
|
||||
c.parameters != nil {
|
||||
if key, ok := typedValue.Value.(string); ok {
|
||||
return c.parameters[key]
|
||||
switch expression.Operation {
|
||||
case parsers.LogicalExpressionTypeAnd:
|
||||
result = result && expressionResult
|
||||
if !result {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if typedValue, ok = field.Value.(parsers.FunctionCall); !ok {
|
||||
// TODO: Handle error
|
||||
logger.Error("parsers.Constant has incorrect Value type")
|
||||
}
|
||||
|
||||
switch typedValue.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return c.strings_StringEquals(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallContains:
|
||||
return c.strings_Contains(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallEndsWith:
|
||||
return c.strings_EndsWith(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallStartsWith:
|
||||
return c.strings_StartsWith(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallConcat:
|
||||
return c.strings_Concat(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIndexOf:
|
||||
return c.strings_IndexOf(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallToString:
|
||||
return c.strings_ToString(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallUpper:
|
||||
return c.strings_Upper(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLower:
|
||||
return c.strings_Lower(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLeft:
|
||||
return c.strings_Left(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLength:
|
||||
return c.strings_Length(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallLTrim:
|
||||
return c.strings_LTrim(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReplace:
|
||||
return c.strings_Replace(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReplicate:
|
||||
return c.strings_Replicate(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallReverse:
|
||||
return c.strings_Reverse(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallRight:
|
||||
return c.strings_Right(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallRTrim:
|
||||
return c.strings_RTrim(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSubstring:
|
||||
return c.strings_Substring(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallTrim:
|
||||
return c.strings_Trim(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return c.typeChecking_IsDefined(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsArray:
|
||||
return c.typeChecking_IsArray(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsBool:
|
||||
return c.typeChecking_IsBool(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsFiniteNumber:
|
||||
return c.typeChecking_IsFiniteNumber(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsInteger:
|
||||
return c.typeChecking_IsInteger(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsNull:
|
||||
return c.typeChecking_IsNull(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsNumber:
|
||||
return c.typeChecking_IsNumber(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsObject:
|
||||
return c.typeChecking_IsObject(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsPrimitive:
|
||||
return c.typeChecking_IsPrimitive(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallIsString:
|
||||
return c.typeChecking_IsString(typedValue.Arguments, rowValue)
|
||||
|
||||
case parsers.FunctionCallArrayConcat:
|
||||
return c.array_Concat(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallArrayLength:
|
||||
return c.array_Length(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallArraySlice:
|
||||
return c.array_Slice(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSetIntersect:
|
||||
return c.set_Intersect(typedValue.Arguments, rowValue)
|
||||
case parsers.FunctionCallSetUnion:
|
||||
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, rowValue)
|
||||
}
|
||||
}
|
||||
|
||||
value := rowValue
|
||||
|
||||
if len(field.Path) > 1 {
|
||||
for _, pathSegment := range field.Path[1:] {
|
||||
if nestedValue, ok := value.(map[string]interface{}); ok {
|
||||
value = nestedValue[pathSegment]
|
||||
} else {
|
||||
return nil
|
||||
case parsers.LogicalExpressionTypeOr:
|
||||
result = result || expressionResult
|
||||
if result {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return value
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getExpressionParameterValue(
|
||||
parameter interface{},
|
||||
row RowType,
|
||||
) interface{} {
|
||||
switch typedParameter := parameter.(type) {
|
||||
case parsers.SelectItem:
|
||||
return c.getFieldValue(typedParameter, row)
|
||||
}
|
||||
|
||||
logger.Error("getExpressionParameterValue - got incorrect parameter type")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) orderBy(orderBy []parsers.OrderExpression, data []RowType) {
|
||||
func applyOrder(documents []rowContext, orderExpressions []parsers.OrderExpression) {
|
||||
less := func(i, j int) bool {
|
||||
for _, order := range orderBy {
|
||||
val1 := c.getFieldValue(order.SelectItem, data[i])
|
||||
val2 := c.getFieldValue(order.SelectItem, data[j])
|
||||
for _, order := range orderExpressions {
|
||||
val1 := documents[i].resolveSelectItem(order.SelectItem)
|
||||
val2 := documents[j].resolveSelectItem(order.SelectItem)
|
||||
|
||||
cmp := compareValues(val1, val2)
|
||||
if cmp != 0 {
|
||||
@@ -344,48 +239,379 @@ func (c memoryExecutorContext) orderBy(orderBy []parsers.OrderExpression, data [
|
||||
return i < j
|
||||
}
|
||||
|
||||
sort.SliceStable(data, less)
|
||||
sort.SliceStable(documents, less)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) groupBy(selectStmt parsers.SelectStmt, data []RowType) []RowType {
|
||||
groupedRows := make(map[string][]RowType)
|
||||
func applyGroupBy(documents []rowContext, groupBy []parsers.SelectItem) []rowContext {
|
||||
groupedRows := make(map[string][]rowContext)
|
||||
groupedKeys := make([]string, 0)
|
||||
|
||||
// Group rows by group by columns
|
||||
for _, row := range data {
|
||||
key := c.generateGroupKey(selectStmt.GroupBy, row)
|
||||
for _, row := range documents {
|
||||
key := row.generateGroupByKey(groupBy)
|
||||
if _, ok := groupedRows[key]; !ok {
|
||||
groupedKeys = append(groupedKeys, key)
|
||||
}
|
||||
groupedRows[key] = append(groupedRows[key], row)
|
||||
}
|
||||
|
||||
// Aggregate each group
|
||||
aggregatedRows := make([]RowType, 0)
|
||||
grouppedRows := make([]rowContext, 0)
|
||||
for _, key := range groupedKeys {
|
||||
groupRows := groupedRows[key]
|
||||
aggregatedRow := c.aggregateGroup(selectStmt, groupRows)
|
||||
aggregatedRows = append(aggregatedRows, aggregatedRow)
|
||||
grouppedRowContext := rowContext{
|
||||
tables: groupedRows[key][0].tables,
|
||||
parameters: groupedRows[key][0].parameters,
|
||||
grouppedRows: groupedRows[key],
|
||||
}
|
||||
grouppedRows = append(grouppedRows, grouppedRowContext)
|
||||
}
|
||||
|
||||
return aggregatedRows
|
||||
return grouppedRows
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) generateGroupKey(groupByFields []parsers.SelectItem, row RowType) string {
|
||||
func (r rowContext) generateGroupByKey(groupBy []parsers.SelectItem) string {
|
||||
var keyBuilder strings.Builder
|
||||
for _, column := range groupByFields {
|
||||
fieldValue := c.getFieldValue(column, row)
|
||||
keyBuilder.WriteString(fmt.Sprintf("%v", fieldValue))
|
||||
for _, selectItem := range groupBy {
|
||||
value := r.resolveSelectItem(selectItem)
|
||||
keyBuilder.WriteString(fmt.Sprintf("%v", value))
|
||||
keyBuilder.WriteString(":")
|
||||
}
|
||||
|
||||
return keyBuilder.String()
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) aggregateGroup(selectStmt parsers.SelectStmt, groupRows []RowType) RowType {
|
||||
aggregatedRow := c.selectRow(selectStmt.SelectItems, groupRows)
|
||||
func applyProjection(documents []rowContext, selectItems []parsers.SelectItem, groupBy []parsers.SelectItem) []RowType {
|
||||
if len(documents) == 0 {
|
||||
return []RowType{}
|
||||
}
|
||||
|
||||
return aggregatedRow
|
||||
if hasAggregateFunctions(selectItems) && len(groupBy) == 0 {
|
||||
// When can have aggregate functions without GROUP BY clause,
|
||||
// we should aggregate all rows in that case
|
||||
rowContext := rowContext{
|
||||
tables: documents[0].tables,
|
||||
parameters: documents[0].parameters,
|
||||
grouppedRows: documents,
|
||||
}
|
||||
return []RowType{rowContext.applyProjection(selectItems)}
|
||||
}
|
||||
|
||||
projectedDocuments := make([]RowType, len(documents))
|
||||
for index, row := range documents {
|
||||
projectedDocuments[index] = row.applyProjection(selectItems)
|
||||
}
|
||||
|
||||
return projectedDocuments
|
||||
}
|
||||
|
||||
func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
||||
// When the first value is top level, select it instead
|
||||
if len(selectItems) > 0 && selectItems[0].IsTopLevel {
|
||||
return r.resolveSelectItem(selectItems[0])
|
||||
}
|
||||
|
||||
// Construct a new row based on the selected columns
|
||||
row := make(map[string]interface{})
|
||||
for index, selectItem := range selectItems {
|
||||
destinationName := selectItem.Alias
|
||||
if destinationName == "" {
|
||||
if len(selectItem.Path) > 0 {
|
||||
destinationName = selectItem.Path[len(selectItem.Path)-1]
|
||||
} else {
|
||||
destinationName = fmt.Sprintf("$%d", index+1)
|
||||
}
|
||||
}
|
||||
|
||||
row[destinationName] = r.resolveSelectItem(selectItem)
|
||||
}
|
||||
|
||||
return row
|
||||
}
|
||||
|
||||
func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{} {
|
||||
if selectItem.Type == parsers.SelectItemTypeArray {
|
||||
return r.selectItem_SelectItemTypeArray(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeObject {
|
||||
return r.selectItem_SelectItemTypeObject(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeConstant {
|
||||
return r.selectItem_SelectItemTypeConstant(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
return r.selectItem_SelectItemTypeSubQuery(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeFunctionCall {
|
||||
if typedFunctionCall, ok := selectItem.Value.(parsers.FunctionCall); ok {
|
||||
return r.selectItem_SelectItemTypeFunctionCall(typedFunctionCall)
|
||||
}
|
||||
|
||||
logger.Error("parsers.SelectItem has incorrect Value type (expected parsers.FunctionCall)")
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.selectItem_SelectItemTypeField(selectItem)
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeArray(selectItem parsers.SelectItem) interface{} {
|
||||
arrayValue := make([]interface{}, 0)
|
||||
for _, subSelectItem := range selectItem.SelectItems {
|
||||
arrayValue = append(arrayValue, r.resolveSelectItem(subSelectItem))
|
||||
}
|
||||
return arrayValue
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeObject(selectItem parsers.SelectItem) interface{} {
|
||||
objectValue := make(map[string]interface{})
|
||||
for _, subSelectItem := range selectItem.SelectItems {
|
||||
objectValue[subSelectItem.Alias] = r.resolveSelectItem(subSelectItem)
|
||||
}
|
||||
return objectValue
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeConstant(selectItem parsers.SelectItem) interface{} {
|
||||
var typedValue parsers.Constant
|
||||
var ok bool
|
||||
if typedValue, ok = selectItem.Value.(parsers.Constant); !ok {
|
||||
// TODO: Handle error
|
||||
logger.Error("parsers.Constant has incorrect Value type")
|
||||
}
|
||||
|
||||
if typedValue.Type == parsers.ConstantTypeParameterConstant &&
|
||||
r.parameters != nil {
|
||||
if key, ok := typedValue.Value.(string); ok {
|
||||
return r.parameters[key]
|
||||
}
|
||||
}
|
||||
|
||||
return typedValue.Value
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeSubQuery(selectItem parsers.SelectItem) interface{} {
|
||||
subQuery := selectItem.Value.(parsers.SelectStmt)
|
||||
subQueryResult := ExecuteQuery(
|
||||
subQuery,
|
||||
[]RowType{r},
|
||||
)
|
||||
|
||||
if subQuery.Exists {
|
||||
return len(subQueryResult) > 0
|
||||
}
|
||||
|
||||
return subQueryResult
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeFunctionCall(functionCall parsers.FunctionCall) interface{} {
|
||||
switch functionCall.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return r.strings_StringEquals(functionCall.Arguments)
|
||||
case parsers.FunctionCallContains:
|
||||
return r.strings_Contains(functionCall.Arguments)
|
||||
case parsers.FunctionCallEndsWith:
|
||||
return r.strings_EndsWith(functionCall.Arguments)
|
||||
case parsers.FunctionCallStartsWith:
|
||||
return r.strings_StartsWith(functionCall.Arguments)
|
||||
case parsers.FunctionCallConcat:
|
||||
return r.strings_Concat(functionCall.Arguments)
|
||||
case parsers.FunctionCallIndexOf:
|
||||
return r.strings_IndexOf(functionCall.Arguments)
|
||||
case parsers.FunctionCallToString:
|
||||
return r.strings_ToString(functionCall.Arguments)
|
||||
case parsers.FunctionCallUpper:
|
||||
return r.strings_Upper(functionCall.Arguments)
|
||||
case parsers.FunctionCallLower:
|
||||
return r.strings_Lower(functionCall.Arguments)
|
||||
case parsers.FunctionCallLeft:
|
||||
return r.strings_Left(functionCall.Arguments)
|
||||
case parsers.FunctionCallLength:
|
||||
return r.strings_Length(functionCall.Arguments)
|
||||
case parsers.FunctionCallLTrim:
|
||||
return r.strings_LTrim(functionCall.Arguments)
|
||||
case parsers.FunctionCallReplace:
|
||||
return r.strings_Replace(functionCall.Arguments)
|
||||
case parsers.FunctionCallReplicate:
|
||||
return r.strings_Replicate(functionCall.Arguments)
|
||||
case parsers.FunctionCallReverse:
|
||||
return r.strings_Reverse(functionCall.Arguments)
|
||||
case parsers.FunctionCallRight:
|
||||
return r.strings_Right(functionCall.Arguments)
|
||||
case parsers.FunctionCallRTrim:
|
||||
return r.strings_RTrim(functionCall.Arguments)
|
||||
case parsers.FunctionCallSubstring:
|
||||
return r.strings_Substring(functionCall.Arguments)
|
||||
case parsers.FunctionCallTrim:
|
||||
return r.strings_Trim(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return r.typeChecking_IsDefined(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsArray:
|
||||
return r.typeChecking_IsArray(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsBool:
|
||||
return r.typeChecking_IsBool(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsFiniteNumber:
|
||||
return r.typeChecking_IsFiniteNumber(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsInteger:
|
||||
return r.typeChecking_IsInteger(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsNull:
|
||||
return r.typeChecking_IsNull(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsNumber:
|
||||
return r.typeChecking_IsNumber(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsObject:
|
||||
return r.typeChecking_IsObject(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsPrimitive:
|
||||
return r.typeChecking_IsPrimitive(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsString:
|
||||
return r.typeChecking_IsString(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallArrayConcat:
|
||||
return r.array_Concat(functionCall.Arguments)
|
||||
case parsers.FunctionCallArrayLength:
|
||||
return r.array_Length(functionCall.Arguments)
|
||||
case parsers.FunctionCallArraySlice:
|
||||
return r.array_Slice(functionCall.Arguments)
|
||||
case parsers.FunctionCallSetIntersect:
|
||||
return r.set_Intersect(functionCall.Arguments)
|
||||
case parsers.FunctionCallSetUnion:
|
||||
return r.set_Union(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallMathAbs:
|
||||
return r.math_Abs(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAcos:
|
||||
return r.math_Acos(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAsin:
|
||||
return r.math_Asin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAtan:
|
||||
return r.math_Atan(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCeiling:
|
||||
return r.math_Ceiling(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCos:
|
||||
return r.math_Cos(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCot:
|
||||
return r.math_Cot(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathDegrees:
|
||||
return r.math_Degrees(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathExp:
|
||||
return r.math_Exp(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathFloor:
|
||||
return r.math_Floor(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitNot:
|
||||
return r.math_IntBitNot(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathLog10:
|
||||
return r.math_Log10(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathRadians:
|
||||
return r.math_Radians(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathRound:
|
||||
return r.math_Round(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSign:
|
||||
return r.math_Sign(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSin:
|
||||
return r.math_Sin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSqrt:
|
||||
return r.math_Sqrt(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSquare:
|
||||
return r.math_Square(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathTan:
|
||||
return r.math_Tan(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathTrunc:
|
||||
return r.math_Trunc(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAtn2:
|
||||
return r.math_Atn2(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntAdd:
|
||||
return r.math_IntAdd(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitAnd:
|
||||
return r.math_IntBitAnd(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitLeftShift:
|
||||
return r.math_IntBitLeftShift(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitOr:
|
||||
return r.math_IntBitOr(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitRightShift:
|
||||
return r.math_IntBitRightShift(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitXor:
|
||||
return r.math_IntBitXor(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntDiv:
|
||||
return r.math_IntDiv(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntMod:
|
||||
return r.math_IntMod(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntMul:
|
||||
return r.math_IntMul(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntSub:
|
||||
return r.math_IntSub(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathPower:
|
||||
return r.math_Power(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathLog:
|
||||
return r.math_Log(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathNumberBin:
|
||||
return r.math_NumberBin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathPi:
|
||||
return r.math_Pi()
|
||||
case parsers.FunctionCallMathRand:
|
||||
return r.math_Rand()
|
||||
|
||||
case parsers.FunctionCallAggregateAvg:
|
||||
return r.aggregate_Avg(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateCount:
|
||||
return r.aggregate_Count(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateMax:
|
||||
return r.aggregate_Max(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateMin:
|
||||
return r.aggregate_Min(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateSum:
|
||||
return r.aggregate_Sum(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallIn:
|
||||
return r.misc_In(functionCall.Arguments)
|
||||
}
|
||||
|
||||
logger.Errorf("Unknown function call type: %v", functionCall.Type)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem) interface{} {
|
||||
value := r.tables[selectItem.Path[0]]
|
||||
|
||||
if len(selectItem.Path) > 1 {
|
||||
for _, pathSegment := range selectItem.Path[1:] {
|
||||
|
||||
switch nestedValue := value.(type) {
|
||||
case map[string]interface{}:
|
||||
value = nestedValue[pathSegment]
|
||||
case map[string]RowType:
|
||||
value = nestedValue[pathSegment]
|
||||
case []int, []string, []interface{}:
|
||||
slice := reflect.ValueOf(nestedValue)
|
||||
if arrayIndex, err := strconv.Atoi(pathSegment); err == nil && slice.Len() > arrayIndex {
|
||||
value = slice.Index(arrayIndex).Interface()
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
@@ -431,8 +657,9 @@ func compareValues(val1, val2 interface{}) int {
|
||||
}
|
||||
}
|
||||
|
||||
func deduplicate(slice []RowType) []RowType {
|
||||
var result []RowType
|
||||
func deduplicate[T RowType | interface{}](slice []T) []T {
|
||||
var result []T
|
||||
result = make([]T, 0)
|
||||
|
||||
for i := 0; i < len(slice); i++ {
|
||||
unique := true
|
||||
@@ -451,22 +678,12 @@ func deduplicate(slice []RowType) []RowType {
|
||||
return result
|
||||
}
|
||||
|
||||
func hasAggregateFunctions(selectItems []parsers.SelectItem) bool {
|
||||
if selectItems == nil {
|
||||
return false
|
||||
func copyMap[T RowType | []RowType](originalMap map[string]T) map[string]T {
|
||||
targetMap := make(map[string]T)
|
||||
|
||||
for k, v := range originalMap {
|
||||
targetMap[k] = v
|
||||
}
|
||||
|
||||
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
|
||||
return targetMap
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) misc_In(arguments []interface{}, row RowType) bool {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) misc_In(arguments []interface{}) bool {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
|
||||
for i := 1; i < len(arguments); i++ {
|
||||
compareValue := c.getFieldValue(arguments[i].(parsers.SelectItem), row)
|
||||
compareValue := r.resolveSelectItem(arguments[i].(parsers.SelectItem))
|
||||
if compareValues(value, compareValue) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func testQueryExecute(
|
||||
data []memoryexecutor.RowType,
|
||||
expectedData []memoryexecutor.RowType,
|
||||
) {
|
||||
result := memoryexecutor.Execute(query, data)
|
||||
result := memoryexecutor.ExecuteQuery(query, data)
|
||||
|
||||
if !reflect.DeepEqual(result, expectedData) {
|
||||
t.Errorf("execution result does not match expected data.\nExpected: %+v\nGot: %+v", expectedData, result)
|
||||
@@ -25,8 +25,20 @@ func Test_Execute(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "12345", "pk": 123, "_self": "self1", "_rid": "rid1", "_ts": 123456, "isCool": false},
|
||||
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{"id": "456", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{"id": "123", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{
|
||||
"id": "456", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-a"},
|
||||
{"name": "tag-b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": "123", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-b"},
|
||||
{"name": "tag-c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute SELECT with ORDER BY", func(t *testing.T) {
|
||||
@@ -124,4 +136,31 @@ func Test_Execute(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN selector", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "name"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "tag-a"},
|
||||
map[string]interface{}{"name": "tag-b"},
|
||||
map[string]interface{}{"name": "tag-b"},
|
||||
map[string]interface{}{"name": "tag-c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) strings_StringEquals(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_StringEquals(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
return strings.EqualFold(str1, str2)
|
||||
@@ -20,10 +20,10 @@ func (c memoryExecutorContext) strings_StringEquals(arguments []interface{}, row
|
||||
return str1 == str2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Contains(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_Contains(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -33,10 +33,10 @@ func (c memoryExecutorContext) strings_Contains(arguments []interface{}, row Row
|
||||
return strings.Contains(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_EndsWith(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_EndsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -46,10 +46,10 @@ func (c memoryExecutorContext) strings_EndsWith(arguments []interface{}, row Row
|
||||
return strings.HasSuffix(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_StartsWith(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_StartsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -59,12 +59,12 @@ func (c memoryExecutorContext) strings_StartsWith(arguments []interface{}, row R
|
||||
return strings.HasPrefix(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Concat(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Concat(arguments []interface{}) string {
|
||||
result := ""
|
||||
|
||||
for _, arg := range arguments {
|
||||
if selectItem, ok := arg.(parsers.SelectItem); ok {
|
||||
value := c.getFieldValue(selectItem, row)
|
||||
value := r.resolveSelectItem(selectItem)
|
||||
result += convertToString(value)
|
||||
}
|
||||
}
|
||||
@@ -72,13 +72,13 @@ func (c memoryExecutorContext) strings_Concat(arguments []interface{}, row RowTy
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_IndexOf(arguments []interface{}, row RowType) int {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
func (r rowContext) strings_IndexOf(arguments []interface{}) int {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
|
||||
start := 0
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
if startPos, ok := c.getFieldValue(arguments[2].(parsers.SelectItem), row).(int); ok {
|
||||
if startPos, ok := r.resolveSelectItem(arguments[2].(parsers.SelectItem)).(int); ok {
|
||||
start = startPos
|
||||
}
|
||||
}
|
||||
@@ -97,26 +97,26 @@ func (c memoryExecutorContext) strings_IndexOf(arguments []interface{}, row RowT
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_ToString(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_ToString(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return convertToString(value)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Upper(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_Upper(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return strings.ToUpper(convertToString(value))
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Lower(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_Lower(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return strings.ToLower(convertToString(value))
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Left(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Left(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
logger.Error("strings_Left - got parameters of wrong type")
|
||||
@@ -134,28 +134,28 @@ func (c memoryExecutorContext) strings_Left(arguments []interface{}, row RowType
|
||||
return str[:length]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Length(arguments []interface{}, row RowType) int {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Length(arguments []interface{}) int {
|
||||
str := r.parseString(arguments[0])
|
||||
return len(str)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_LTrim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_LTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimLeft(str, " ")
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Replace(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
oldStr := c.parseString(arguments[1], row)
|
||||
newStr := c.parseString(arguments[2], row)
|
||||
func (r rowContext) strings_Replace(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
oldStr := r.parseString(arguments[1])
|
||||
newStr := r.parseString(arguments[2])
|
||||
return strings.Replace(str, oldStr, newStr, -1)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Replicate(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Replicate(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var times int
|
||||
str := c.parseString(arguments[0], row)
|
||||
timesEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
timesEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if times, ok = timesEx.(int); !ok {
|
||||
logger.Error("strings_Replicate - got parameters of wrong type")
|
||||
@@ -173,8 +173,8 @@ func (c memoryExecutorContext) strings_Replicate(arguments []interface{}, row Ro
|
||||
return strings.Repeat(str, times)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Reverse(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Reverse(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
runes := []rune(str)
|
||||
|
||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||
@@ -184,11 +184,11 @@ func (c memoryExecutorContext) strings_Reverse(arguments []interface{}, row RowT
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Right(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Right(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
logger.Error("strings_Right - got parameters of wrong type")
|
||||
@@ -206,18 +206,18 @@ func (c memoryExecutorContext) strings_Right(arguments []interface{}, row RowTyp
|
||||
return str[len(str)-length:]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_RTrim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_RTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimRight(str, " ")
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Substring(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Substring(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var startPos int
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
startPosEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
startPosEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
lengthEx := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
|
||||
if startPos, ok = startPosEx.(int); !ok {
|
||||
logger.Error("strings_Substring - got start parameters of wrong type")
|
||||
@@ -240,16 +240,16 @@ func (c memoryExecutorContext) strings_Substring(arguments []interface{}, row Ro
|
||||
return str[startPos:endPos]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Trim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Trim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getBoolFlag(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) getBoolFlag(arguments []interface{}) bool {
|
||||
ignoreCase := false
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
ignoreCaseItem := arguments[2].(parsers.SelectItem)
|
||||
if value, ok := c.getFieldValue(ignoreCaseItem, row).(bool); ok {
|
||||
if value, ok := r.resolveSelectItem(ignoreCaseItem).(bool); ok {
|
||||
ignoreCase = value
|
||||
}
|
||||
}
|
||||
@@ -257,9 +257,9 @@ func (c memoryExecutorContext) getBoolFlag(arguments []interface{}, row RowType)
|
||||
return ignoreCase
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) parseString(argument interface{}, row RowType) string {
|
||||
func (r rowContext) parseString(argument interface{}) string {
|
||||
exItem := argument.(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
if str1, ok := ex.(string); ok {
|
||||
return str1
|
||||
}
|
||||
|
||||
155
query_executors/memory_executor/subquery_test.go
Normal file
155
query_executors/memory_executor/subquery_test.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_SubQuery(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "info": map[string]interface{}{"name": "row-1"}},
|
||||
map[string]interface{}{
|
||||
"id": "456",
|
||||
"info": map[string]interface{}{"name": "row-2"},
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-a"},
|
||||
{"name": "tag-b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": "789",
|
||||
"info": map[string]interface{}{"name": "row-3"},
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-b"},
|
||||
{"name": "tag-c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute FROM subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "row-1"},
|
||||
map[string]interface{}{"name": "row-2"},
|
||||
map[string]interface{}{"name": "row-3"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "cc",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "456", "name": "tag-a"},
|
||||
map[string]interface{}{"id": "456", "name": "tag-b"},
|
||||
map[string]interface{}{"id": "789", "name": "tag-b"},
|
||||
map[string]interface{}{"id": "789", "name": "tag-c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN EXISTS subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{Value: "hasTags"},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "hasTags",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
IsTopLevel: true,
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
Exists: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"hasTags"},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "456"},
|
||||
map[string]interface{}{"id": "789"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -6,32 +6,32 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsDefined(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsDefined(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
return ex != nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsArray(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsArray(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isArray := ex.([]interface{})
|
||||
return isArray
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsBool(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsBool(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isBool := ex.(bool)
|
||||
return isBool
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsFiniteNumber(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsFiniteNumber(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch num := ex.(type) {
|
||||
case int:
|
||||
@@ -43,41 +43,41 @@ func (c memoryExecutorContext) typeChecking_IsFiniteNumber(arguments []interface
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsInteger(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsInteger(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isInt := ex.(int)
|
||||
return isInt
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsNull(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsNull(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
return ex == nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsNumber(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsNumber(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isFloat := ex.(float64)
|
||||
_, isInt := ex.(int)
|
||||
return isFloat || isInt
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsObject(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsObject(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isObject := ex.(map[string]interface{})
|
||||
return isObject
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsPrimitive(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsPrimitive(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch ex.(type) {
|
||||
case bool, string, float64, int, nil:
|
||||
@@ -87,9 +87,9 @@ func (c memoryExecutorContext) typeChecking_IsPrimitive(arguments []interface{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsString(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsString(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isStr := ex.(string)
|
||||
return isStr
|
||||
|
||||
Reference in New Issue
Block a user