Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array intersects function #257

Merged
merged 7 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ npm test
You can generate tests from a specific version:

```shell
GROQTEST_SUITE_VERSION=v0.1.45 ./test/generate.sh
GROQTEST_SUITE_VERSION=v0.1.46 ./test/generate.sh
```

or from a file (as generated by the test suite):
Expand Down
26 changes: 26 additions & 0 deletions src/evaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {portableTextContent} from './pt'
import {Scope} from './scope'
import {evaluateScore} from './scoring'
import type {Executor} from './types'
import {isEqual} from './equality'

function hasReference(value: any, pathSet: Set<string>): boolean {
switch (getType(value)) {
Expand Down Expand Up @@ -392,6 +393,31 @@ array['unique'] = async function (args, scope, execute) {
}
array['unique'].arity = 1

array['intersects'] = async function (args, scope, execute) {
// Intersects returns true if the two arrays have at least one element in common. Only
// primitives are supported; non-primitives are ignored.
const arr1 = await execute(args[0], scope)
if (!arr1.isArray()) {
return NULL_VALUE
}

const arr2 = await execute(args[1], scope)
if (!arr2.isArray()) {
return NULL_VALUE
}

for await (const v1 of arr1) {
for await (const v2 of arr2) {
if (isEqual(v1, v2)) {
return TRUE_VALUE
}
}
}

return FALSE_VALUE
}
array['intersects'].arity = 2

const pt: FunctionSet = {}
pt['text'] = async function (args, scope, execute) {
const value = await execute(args[0], scope)
Expand Down
19 changes: 19 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
})
}

case 'array.intersects': {
const arg1 = walk({node: node.args[0], scope})
const arg2 = walk({node: node.args[1], scope})

return mapNode(arg1, scope, (arg1) =>
mapNode(arg2, scope, (arg2) => {
if (arg1.type !== 'array') {
return {type: 'null'}
}

if (arg2.type !== 'array') {
return {type: 'null'}
}

return {type: 'boolean'}
}),
)
}

case 'global.lower': {
const arg = walk({node: node.args[0], scope})

Expand Down
8 changes: 6 additions & 2 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const semver = require('semver')

const SUPPORTED_FEATURES = new Set(['portableText'])
// We implement GROQ-1.revision1. The final patch has to be there for it to be a valid SemVer.
const GROQ_VERSION = '1.1.0'
// E.g. GROQ-1.revision2 maps to 1.2.0
const GROQ_VERSION = '1.2.0'
const DISABLED_TESTS = [
'Filters / documents, nested 3', // very slow
'Parameters / Undefined',
Expand Down Expand Up @@ -167,7 +168,10 @@ process.stdin

if (entry._type === 'test') {
const supported = entry.features.every((f) => SUPPORTED_FEATURES.has(f))
if (!supported) return
if (!supported) {
process.stderr.write(`[warning] Skipping unsupported test: ${entry.name}\n`)
return
}

if (entry.version && !semver.satisfies(GROQ_VERSION, entry.version)) return

Expand Down
2 changes: 1 addition & 1 deletion test/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [[ "$GROQTEST_SUITE" != "" ]]; then
echo "Using test suite file: $GROQTEST_SUITE"
node "$DIR"/generate.js < "$GROQTEST_SUITE" >"$RESULT"
else
GROQTEST_SUITE_VERSION=${GROQTEST_SUITE_VERSION:-v0.1.45}
GROQTEST_SUITE_VERSION=${GROQTEST_SUITE_VERSION:-v0.1.46}
url=https://github.com/sanity-io/groq-test-suite/releases/download/$GROQTEST_SUITE_VERSION/suite.ndjson
echo "Getting test suite: $url"
curl -sfL "$url" | node "$DIR"/generate.js >"$RESULT"
Expand Down
30 changes: 30 additions & 0 deletions test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,36 @@ t.test('function: array::unique', (t) => {
t.end()
})

t.test('function: array::intersects', (t) => {
const query = `* {
"f1": array::intersects(true, true),
"f2": array::intersects([1, 2, true], [5, 1, false]),
}`
const ast = parse(query)
const res = typeEvaluate(ast, schemas)
t.strictSame(res, {
type: 'array',
of: {
type: 'object',
attributes: {
f1: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
f2: {
type: 'objectAttribute',
value: {
type: 'boolean',
},
},
},
},
})
t.end()
})

t.test('function: math::*', (t) => {
const query = `*[_type == "author"] {
"ages": math::min(ages),
Expand Down