Skip to content
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
73 changes: 35 additions & 38 deletions cypher/models/pgsql/operators.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pgsql

import "slices"

type Operator string

func (s Operator) IsIn(others ...Operator) bool {
Expand All @@ -25,13 +27,7 @@ func (s Operator) NodeType() string {
}

func OperatorIsIn(operator Expression, matchers ...Expression) bool {
for _, matcher := range matchers {
if operator == matcher {
return true
}
}

return false
return slices.Contains(matchers, operator)
}

func OperatorIsBoolean(operator Expression) bool {
Expand Down Expand Up @@ -59,40 +55,41 @@ func OperatorIsComparator(operator Expression) bool {
return OperatorIsIn(operator,
OperatorEquals, OperatorNotEquals, OperatorGreaterThan, OperatorGreaterThanOrEqualTo, OperatorLessThan,
OperatorLessThanOrEqualTo, OperatorArrayOverlap, OperatorLike, OperatorILike, OperatorPGArrayOverlap,
OperatorRegexMatch, OperatorSimilarTo)
OperatorRegexMatch, OperatorSimilarTo, OperatorPGArrayLHSContainsRHS)
}

const (
UnsetOperator Operator = ""
OperatorUnion Operator = "union"
OperatorConcatenate Operator = "||"
OperatorArrayOverlap Operator = "&&"
OperatorEquals Operator = "="
OperatorNotEquals Operator = "!="
OperatorGreaterThan Operator = ">"
OperatorGreaterThanOrEqualTo Operator = ">="
OperatorLessThan Operator = "<"
OperatorLessThanOrEqualTo Operator = "<="
OperatorLike Operator = "like"
OperatorILike Operator = "ilike"
OperatorPGArrayOverlap Operator = "operator (pg_catalog.&&)"
OperatorAnd Operator = "and"
OperatorOr Operator = "or"
OperatorNot Operator = "not"
OperatorJSONBFieldExists Operator = "?"
OperatorJSONField Operator = "->"
OperatorJSONTextField Operator = "->>"
OperatorAdd Operator = "+"
OperatorSubtract Operator = "-"
OperatorMultiply Operator = "*"
OperatorDivide Operator = "/"
OperatorIn Operator = "in"
OperatorIs Operator = "is"
OperatorIsNot Operator = "is not"
OperatorSimilarTo Operator = "similar to"
OperatorRegexMatch Operator = "~"
OperatorAssignment Operator = "="
OperatorAdditionAssignment Operator = "+="
UnsetOperator Operator = ""
OperatorUnion Operator = "union"
OperatorConcatenate Operator = "||"
OperatorArrayOverlap Operator = "&&"
OperatorEquals Operator = "="
OperatorNotEquals Operator = "!="
OperatorGreaterThan Operator = ">"
OperatorGreaterThanOrEqualTo Operator = ">="
OperatorLessThan Operator = "<"
OperatorLessThanOrEqualTo Operator = "<="
OperatorLike Operator = "like"
OperatorILike Operator = "ilike"
OperatorPGArrayOverlap Operator = "operator (pg_catalog.&&)"
OperatorPGArrayLHSContainsRHS Operator = "operator (pg_catalog.@>)"
OperatorAnd Operator = "and"
OperatorOr Operator = "or"
OperatorNot Operator = "not"
OperatorJSONBFieldExists Operator = "?"
OperatorJSONField Operator = "->"
OperatorJSONTextField Operator = "->>"
OperatorAdd Operator = "+"
OperatorSubtract Operator = "-"
OperatorMultiply Operator = "*"
OperatorDivide Operator = "/"
OperatorIn Operator = "in"
OperatorIs Operator = "is"
OperatorIsNot Operator = "is not"
OperatorSimilarTo Operator = "similar to"
OperatorRegexMatch Operator = "~"
OperatorAssignment Operator = "="
OperatorAdditionAssignment Operator = "+="

OperatorCypherRegexMatch Operator = "=~"
OperatorCypherStartsWith Operator = "starts with"
Expand Down
6 changes: 2 additions & 4 deletions cypher/models/pgsql/pgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/specterops/dawgs/graph"
)

var (
ErrNoAvailableArrayDataType = errors.New("data type has no direct array representation")
)
var ErrNoAvailableArrayDataType = errors.New("data type has no direct array representation")

const (
StringLiteralNull = "null"
Expand Down Expand Up @@ -118,7 +116,7 @@ func (s DataType) IsKnown() bool {

func (s DataType) IsComparable(other DataType, operator Operator) bool {
switch operator {
case OperatorPGArrayOverlap, OperatorArrayOverlap:
case OperatorPGArrayOverlap, OperatorArrayOverlap, OperatorPGArrayLHSContainsRHS:
if !s.IsArrayType() || !other.IsArrayType() {
return false
}
Expand Down
14 changes: 14 additions & 0 deletions cypher/models/pgsql/pytypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ func TestDataType_Comparable(t *testing.T) {
Expected: false,
},

// Array types may use the "LHS contains RHS" operator but only if their base types match
{
LeftTypes: []DataType{IntArray},
Operators: []Operator{OperatorPGArrayLHSContainsRHS},
RightTypes: []DataType{IntArray},
Expected: true,
},
{
LeftTypes: []DataType{IntArray},
Operators: []Operator{OperatorPGArrayLHSContainsRHS},
RightTypes: []DataType{Int},
Expected: false,
},

// Catch all for any unsupported operator
{
LeftTypes: []DataType{Int},
Expand Down
18 changes: 17 additions & 1 deletion cypher/models/pgsql/test/translation_cases/delete.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
-- Copyright 2026 Specter Ops, Inc.
--
-- Licensed under the Apache License, Version 2.0
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- SPDX-License-Identifier: Apache-2.0

-- case: match (s:NodeKind1) detach delete s
with s0 as (select (n0.id, n0.kind_ids, n0.properties)::nodecomposite as n0 from node n0 where n0.kind_ids operator (pg_catalog.&&) array [1]::int2[]), s1 as (delete from node n1 using s0 where (s0.n0).id = n1.id) select 1;
with s0 as (select (n0.id, n0.kind_ids, n0.properties)::nodecomposite as n0 from node n0 where n0.kind_ids operator (pg_catalog.@>) array [1]::int2[]), s1 as (delete from node n1 using s0 where (s0.n0).id = n1.id) select 1;

-- case: match ()-[r:EdgeKind1]->() delete r
with s0 as (select (e0.id, e0.start_id, e0.end_id, e0.kind_id, e0.properties)::edgecomposite as e0, (n0.id, n0.kind_ids, n0.properties)::nodecomposite as n0, (n1.id, n1.kind_ids, n1.properties)::nodecomposite as n1 from edge e0 join node n0 on n0.id = e0.start_id join node n1 on n1.id = e0.end_id where e0.kind_id = any (array [3]::int2[])), s1 as (delete from edge e1 using s0 where (s0.e0).id = e1.id) select 1;
Expand Down
Loading