Skip to content

Commit

Permalink
[Rel v0.2] Migrate Go SDK files
Browse files Browse the repository at this point in the history
  • Loading branch information
gkastrinis committed Sep 2, 2024
1 parent 991c342 commit a2b60f8
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 363 deletions.
2 changes: 1 addition & 1 deletion examples/query.rel
Original file line number Diff line number Diff line change
@@ -1 +1 @@
def output = x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}
def output(x, x2, x3, x4): {1; 2; 3; 4; 5}(x) and x2 = x^2 and x3 = x^3 and x4 = x^4
2 changes: 1 addition & 1 deletion examples/run-all
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ go run list_models/main.go -d $DATABASE -e $ENGINE
go run get_model/main.go -d $DATABASE -e $ENGINE -m stdlib

# exec
QUERY='x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}'
QUERY='def output(x, x2, x3, x4): {1; 2; 3; 4; 5}(x) and x2 = x^2 and x3 = x^3 and x4 = x^4'
go run execute/main.go -d $DATABASE -e $ENGINE -c "$QUERY"
go run execute/main.go -d $DATABASE -e $ENGINE -c "$QUERY" --readonly

Expand Down
15 changes: 8 additions & 7 deletions rai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,14 +1467,15 @@ func genSchemaConfig(b *strings.Builder, opts *CSVOptions) {
return
}
count := 0
b.WriteString("def config:schema = ")
b.WriteString("def config[:schema]: {")
for k, v := range schema {
if count > 0 {
b.WriteRune(';')
}
b.WriteString(fmt.Sprintf("\n :%s, \"%s\"", k, v))
b.WriteString(fmt.Sprintf("\n (:%s, \"%s\")", k, v))
count++
}
b.WriteRune('}')
b.WriteRune('\n')
}

Expand Down Expand Up @@ -1503,7 +1504,7 @@ func genLiteral(v interface{}) string {
// Generates a Rel syntax config def for the given option name and value.
func genSyntaxOption(b *strings.Builder, name string, value interface{}) {
lit := genLiteral(value)
def := fmt.Sprintf("def config:syntax:%s = %s\n", name, lit)
def := fmt.Sprintf("def config[:syntax, :%s]: %s\n", name, lit)
b.WriteString(def)
}

Expand Down Expand Up @@ -1531,8 +1532,8 @@ func genLoadCSV(relation string, opts *CSVOptions) string {
b := new(strings.Builder)
genSyntaxConfig(b, opts)
genSchemaConfig(b, opts)
b.WriteString("def config:data = data\n")
b.WriteString(fmt.Sprintf("def insert:%s = load_csv[config]", relation))
b.WriteString("def config[:data]: data\n")
b.WriteString(fmt.Sprintf("def insert[:%s]: load_csv[config]", relation))
return b.String()
}

Expand All @@ -1556,8 +1557,8 @@ func (c *Client) LoadJSON(
return nil, err
}
b := new(strings.Builder)
b.WriteString("def config:data = data\n")
b.WriteString(fmt.Sprintf("def insert:%s = load_json[config]", relation))
b.WriteString("def config[:data]: data\n")
b.WriteString(fmt.Sprintf("def insert[:%s]: load_json[config]", relation))
inputs := map[string]string{"data": string(data)}
return c.ExecuteV1(database, engine, b.String(), inputs, false)
}
Expand Down
42 changes: 21 additions & 21 deletions rai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestEngine(t *testing.T) {
func TestExecuteV1(t *testing.T) {
client := test.client

query := "x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}"
query := "def output(x, x2, x3, x4): {1; 2; 3; 4; 5}(x) and x2 = x^2 and x3 = x^3 and x4 = x^4"

rsp, err := client.ExecuteV1(test.databaseName, test.engineName, query, nil, true)
assert.Nil(t, err)
Expand All @@ -334,7 +334,7 @@ func TestExecuteV1(t *testing.T) {
func TestListTransactions(t *testing.T) {
client := test.client

query := "x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}"
query := "def output(x, x2, x3, x4): {1; 2; 3; 4; 5}(x) and x2 = x^2 and x3 = x^3 and x4 = x^4"
txn, err := client.Execute(test.databaseName, test.engineName, query, nil, true)
assert.Nil(t, err)

Expand All @@ -358,7 +358,7 @@ func TestListTransactions(t *testing.T) {
func TestListTransactionsByTag(t *testing.T) {
client := test.client

query := "x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}"
query := "def output(x, x2, x3, x4): {1; 2; 3; 4; 5}(x) and x2 = x^2 and x3 = x^3 and x4 = x^4"
tag := fmt.Sprintf("rai-sdk-go:%s", uuid.New().String())
txn, err := client.Execute(test.databaseName, test.engineName, query, nil, true, tag)
assert.Nil(t, err)
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestLoadCSV(t *testing.T) {
assert.Equal(t, 0, len(rsp.Problems))
}

rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output = sample_csv", nil, true)
rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output { sample_csv }", nil, true)
assert.Nil(t, err)
assert.NotNil(t, rsp)
if rsp != nil {
Expand Down Expand Up @@ -482,7 +482,7 @@ func TestLoadCSVNoHeader(t *testing.T) {
assert.Equal(t, 0, len(rsp.Problems))
}

rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output = sample_no_header", nil, true)
rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output { sample_no_header }", nil, true)
assert.Nil(t, err)
assert.NotNil(t, rsp)
if rsp != nil {
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestLoadCSVAltSyntax(t *testing.T) {
}

rsp, err = client.ExecuteV1(
test.databaseName, test.engineName, "def output = sample_alt_syntax", nil, true)
test.databaseName, test.engineName, "def output { sample_alt_syntax }", nil, true)
assert.Nil(t, err)
assert.NotNil(t, rsp)
if rsp != nil {
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestLoadCSVWithSchema(t *testing.T) {
assert.Equal(t, 0, len(rsp.Problems))
}

rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output = sample_with_schema", nil, true)
rsp, err = client.ExecuteV1(test.databaseName, test.engineName, "def output { sample_with_schema }", nil, true)
assert.Nil(t, err)
assert.NotNil(t, rsp)
if rsp != nil {
Expand Down Expand Up @@ -696,7 +696,7 @@ func TestLoadJSON(t *testing.T) {
}

rsp, err = client.ExecuteV1(
test.databaseName, test.engineName, "def output = sample_json", nil, true)
test.databaseName, test.engineName, "def output { sample_json }", nil, true)
assert.Nil(t, err)
assert.NotNil(t, rsp)
if rsp != nil {
Expand Down Expand Up @@ -738,7 +738,7 @@ func TestLoadJSON(t *testing.T) {
func TestModels(t *testing.T) {
client := test.client

const testModel = "def R = \"hello\", \"world\""
const testModel = "def R { \"hello\", \"world\" }"

r := strings.NewReader(testModel)
rsp, err := client.LoadModel(test.databaseName, test.engineName, "test_model", r)
Expand Down Expand Up @@ -935,7 +935,7 @@ func TestListEdb(t *testing.T) {
client := test.client

// simple edb: int64 values
query := "def insert:a = 1"
query := "def insert[:a]: 1"
rsp, err := client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
Expand All @@ -948,7 +948,7 @@ func TestListEdb(t *testing.T) {
assert.Equal(t, &EDB{Name: "a", Keys: []interface{}{}, Values: []interface{}{"Int64"}}, edb)

// simple edb: :x keys, int64 values
query = "def insert:b:x = 1"
query = "def insert[:b, :x]: 1"
rsp, err = client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
Expand All @@ -962,8 +962,8 @@ func TestListEdb(t *testing.T) {

// value type edb with only values
query = `
value type FooType = Int, Char
def insert:c = ^FooType[1, 'a']`
value type FooType {(Int, Char)}
def insert[:c]: ^FooType[1, 'a']`
rsp, err = client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
Expand All @@ -982,8 +982,8 @@ func TestListEdb(t *testing.T) {

// value type edb as value
query = `
value type FooType = Int, Char
def insert:d:x = ^FooType[1, 'a']`
value type FooType {(Int, Char)}
def insert[:d, :x]: ^FooType[1, 'a']`
rsp, err = client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
Expand All @@ -1002,9 +1002,9 @@ func TestListEdb(t *testing.T) {

// value type edb as key
query = `
value type FooType = Int, Char
def v = ^FooType[2, 'd']
def insert:e:v = #(v)`
value type FooType {(Int, Char)}
def v { ^FooType[2, 'd'] }
def insert[:e, :v]: ::std::mirror::lift[v]`
rsp, err = client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
Expand All @@ -1024,23 +1024,23 @@ func TestListEdb(t *testing.T) {

// cleanup
for _, edb := range []string{"a", "b", "c", "d", "e"} {
query = fmt.Sprintf("def delete:%s=%s", edb, edb)
query = fmt.Sprintf("def delete[:%s]: %s", edb, edb)
rsp, err = client.Execute(test.databaseName, test.engineName, query, nil, false)
assert.Nil(t, err)
assert.NotNil(t, rsp)
}
}

func TestTransactionAbort(t *testing.T) {
query := `ic test_ic { false }`
query := `ic test_ic() requires { false }`

rsp, err := test.client.Execute(test.databaseName, test.engineName, query, nil, true, o11yTag)
assert.Nil(t, err)
assert.Equal(t, "integrity constraint violation", rsp.Transaction.AbortReason)
}

func TestXRequestId(t *testing.T) {
query := `def output = 1 + 1`
query := `def output {1 + 1}`
inputs := make([]interface{}, 0)

tx := TransactionRequest{
Expand Down
2 changes: 1 addition & 1 deletion rai/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package rai
// relation data is partitioned by the resulting unique metadata signatures.
// For example:
//
// def output = 1, :foo; 2, :bar; 3, :baz
// def output {(1, :foo); (2, :bar); (3, :baz)}
//
// results in 3 partitions, each with a unique metadata signature, and in this
// example, a single column with a single row of data:
Expand Down
Loading

0 comments on commit a2b60f8

Please sign in to comment.