Skip to content

Commit

Permalink
Generates a partiql prelude for PLK (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell authored Jul 9, 2024
1 parent 916374a commit bb5cbbb
Show file tree
Hide file tree
Showing 49 changed files with 344 additions and 0 deletions.
19 changes: 19 additions & 0 deletions partiql-planner/src/main/resources/builtins/REAMDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
= Builtins

See https://github.com/orgs/partiql/discussions/64

== Syntax

```
{
name: <symbol>, // the routine name
type: op|fn|agg, // the routine type
description: '''
I am a multi-line string for generated documentation..
''',
impls: [
(fn name::type -> type), // start with op|fn|agg followed by name annotated arg types, then -> <return type>
....
]
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
name: cardinality,
type: fn,
description:'''
Placeholder for `cardinality` description
''',
properties: [],
impls: [
(fn container::bag -> int),
(fn container::list -> int),
(fn container::sexp -> int),
(fn container::struct -> int)
]
}
16 changes: 16 additions & 0 deletions partiql-planner/src/main/resources/builtins/other/fn_date_add.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
name: date_add,
type: fn,
description:'''
Placeholder for `date_add` description
''',
properties: [],
impls: [
(fn part::string interval::int datetime::date -> date),
(fn part::string interval::int datetime::time -> time),
(fn part::string interval::int datetime::timestamp -> timestamp),
(fn part::string interval::interval datetime::date -> date),
(fn part::string interval::interval datetime::time -> time),
(fn part::string interval::interval datetime::timestamp -> timestamp),
]
}
16 changes: 16 additions & 0 deletions partiql-planner/src/main/resources/builtins/other/fn_date_diff.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
name: date_diff,
type: fn,
description:'''
Placeholder for `date_diff` description
''',
properties: [],
impls: [
(fn part::string interval::int datetime::date -> date),
(fn part::string interval::int datetime::time -> time),
(fn part::string interval::int datetime::timestamp -> timestamp),
(fn part::string interval::interval datetime::date -> date),
(fn part::string interval::interval datetime::time -> time),
(fn part::string interval::interval datetime::timestamp -> timestamp),
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_any,
type: fn,
description:'''
Placeholder for `coll_any` description
''',
properties: [],
impls: [
(fn collection::bag -> bool),
(fn collection::list -> bool),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_avg,
type: fn,
description:'''
Placeholder for `coll_avg` description
''',
properties: [],
impls: [
(fn collection::bag -> dynamic),
(fn collection::list -> dynamic),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_count,
type: fn,
description:'''
Placeholder for `coll_count` description
''',
properties: [],
impls: [
(fn collection::bag -> dynamic),
(fn collection::list -> dynamic),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_every,
type: fn,
description:'''
Placeholder for `coll_every` description
''',
properties: [],
impls: [
(fn collection::bag -> bool),
(fn collection::list -> bool),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_max,
type: fn,
description:'''
Placeholder for `coll_max` description
''',
properties: [],
impls: [
(fn value::bag -> dynamic),
(fn value::list -> dynamic),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_min,
type: fn,
description:'''
Placeholder for `coll_min` description
''',
properties: [],
impls: [
(fn value::bag -> dynamic),
(fn value::list -> dynamic),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_some,
type: fn,
description:'''
Placeholder for `coll_some` description
''',
properties: [],
impls: [
(fn collection::bag -> bool),
(fn collection::list -> bool),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: coll_sum,
type: fn,
description:'''
Placeholder for `coll_sum` description
''',
properties: [],
impls: [
(fn value::bag -> dynamic),
(fn value::list -> dynamic)
]
}
11 changes: 11 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_any.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
name: any,
type: agg,
description:'''
Placeholder for `any` description
''',
properties: [],
impls: [
(agg value::bool -> bool),
]
}
18 changes: 18 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_avg.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
name: avg,
type: agg,
description:'''
Placeholder for `avg` description
''',
properties: [],
impls: [
(agg value::tinyint -> tinyint),
(agg value::smallint -> smallint),
(agg value::int -> int),
(agg value::bigint -> bigint),
(agg value::numeric -> numeric),
(agg value::decimal -> decimal),
(agg value::real -> real),
(agg value::double -> double),
]
}
12 changes: 12 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_count.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
name: count,
type: agg,
description:'''
Placeholder for `count` description
''',
properties: [],
impls: [
(agg -> int), // COUNT(*)
(agg value::dynamic -> int),
]
}
11 changes: 11 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_every.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
name: every,
type: agg,
description:'''
Placeholder for `every` description
''',
properties: [],
impls: [
(agg value::bool -> bool),
]
}
18 changes: 18 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_max.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
name: max,
type: agg,
description:'''
Placeholder for `max` description
''',
properties: [],
impls: [
(agg value::tinyint -> tinyint),
(agg value::smallint -> smallint),
(agg value::int -> int),
(agg value::bigint -> bigint),
(agg value::numeric -> numeric),
(agg value::decimal -> decimal),
(agg value::real -> real),
(agg value::double -> double),
]
}
18 changes: 18 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_min.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
name: min,
type: agg,
description:'''
Placeholder for `min` description
''',
properties: [],
impls: [
(agg value::tinyint -> tinyint),
(agg value::smallint -> smallint),
(agg value::int -> int),
(agg value::bigint -> bigint),
(agg value::numeric -> numeric),
(agg value::decimal -> decimal),
(agg value::real -> real),
(agg value::double -> double),
]
}
18 changes: 18 additions & 0 deletions partiql-planner/src/main/resources/builtins/sql99/agg_sum.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
name: sum,
type: agg,
description:'''
Placeholder for `sum` description
''',
properties: [],
impls: [
(agg value::tinyint -> numeric),
(agg value::smallint -> numeric),
(agg value::int -> numeric),
(agg value::bigint -> numeric),
(agg value::numeric -> numeric),
(agg value::decimal -> decimal),
(agg value::real -> double),
(agg value::double -> double),
]
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.partiql.spi.connector.sql

import com.amazon.ion.system.IonTextWriterBuilder
import com.amazon.ionelement.api.IonElement
import com.amazon.ionelement.api.SymbolElement
import com.amazon.ionelement.api.ionListOf
import com.amazon.ionelement.api.ionSexpOf
import com.amazon.ionelement.api.ionString
import com.amazon.ionelement.api.ionStructOf
import com.amazon.ionelement.api.ionSymbol
import org.junit.jupiter.api.Test
import org.partiql.spi.fn.FnExperimental
import org.partiql.spi.fn.FnSignature
import org.partiql.types.PType
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.outputStream

@OptIn(FnExperimental::class)
class SqlBuiltinsDump {

private val HOME = System.getProperty("user.home")
private val DEST = Paths.get(HOME, "Desktop", "builtins")

@Test
fun getBuiltins() {
Files.deleteIfExists(DEST)
Files.createDirectories(DEST)
val map = SqlBuiltins.builtins.groupBy(
keySelector = { it.signature.name },
valueTransform = { it.signature },
)
for ((name, signatures) in map) {
val file = Files.createFile(DEST.resolve("$name.ion"))
val fields = mutableListOf<Pair<String, IonElement>>()
val writer = IonTextWriterBuilder
.pretty()
.build(file.outputStream())
fields.add("name" to ionSymbol(name))
fields.add("type" to ionSymbol("fn"))
fields.add("description" to ionString("\nPlaceholder for $name description\n"))
fields.add("properties" to ionListOf())
fields.add("impls" to ionListOf(signatures.map { it.toIon() }))
val definition = ionStructOf(*fields.toTypedArray())
definition.writeTo(writer)
}
}

// private fun header(fn: FnSignature) = buildString {
// appendLine("/*")
// append(fn.sql())
// appendLine("*/")
// }

private fun FnSignature.toIon(): IonElement {
val elements = mutableListOf<IonElement>()
elements.add(ionSymbol("fn"))
for (p in parameters) {
val t = p.type.toIon()
elements.add(t.withAnnotations(p.name))
}
elements.add(ionSymbol("->"))
elements.add(returns.toIon())
return ionSexpOf(elements)
}

private fun PType.toIon(): SymbolElement {
val text = when (kind) {
PType.Kind.INT_ARBITRARY -> "numeric"
PType.Kind.DECIMAL_ARBITRARY -> "decimal"
PType.Kind.DOUBLE_PRECISION -> "double"
PType.Kind.ROW -> "struct"
else -> kind.name.lowercase()
}
return ionSymbol(text)
}
}

0 comments on commit bb5cbbb

Please sign in to comment.