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

Uses INT and BIGINT as default 32-bit and 64-bit integer names #38

Merged
merged 1 commit into from
May 28, 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
24 changes: 23 additions & 1 deletion src/main/kotlin/org/partiql/scribe/ScribeCompiler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.partiql.scribe

import org.partiql.ast.AstNode
import org.partiql.ast.Statement
import org.partiql.ast.Type
import org.partiql.ast.util.AstRewriter
import org.partiql.errors.Problem
import org.partiql.errors.ProblemCallback
import org.partiql.errors.ProblemSeverity
Expand Down Expand Up @@ -40,7 +43,16 @@ public class ScribeCompiler internal constructor(
session: PartiQLPlanner.Session,
): Scribe.Result<T> {
try {
val ast = parse(statement)
var ast = parse(statement)

// TODO: REMOVE ME
// PartiQL came from Ion SQL which uses the INT name for the unbounded integer.
// In SQL, the INT name must have some finite precision and most systems use a 32-bit integer.
// Scribe is built to interface with other systems, so we must change all occurrences of the INT
// type name with INT4. In short, all systems do INT = INT4 but PartiQL has INT4 != INT.
// >>>> ISSUE — https://github.com/partiql/partiql-lang-kotlin/issues/1471
ast = replaceIntWithInt4(ast)
Comment on lines +48 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice temporary work around.


val plan = plan(ast, session)
return scribe.compile(plan, target)
} catch (e: ScribeException) {
Expand Down Expand Up @@ -78,4 +90,14 @@ public class ScribeCompiler internal constructor(
@JvmStatic
public fun builder(): ScribeCompilerBuilder = ScribeCompilerBuilder()
}

/**
* Rewrite all occurrences of INT with INT4.
*/
private fun replaceIntWithInt4(ast: Statement): Statement {
val rewriter = object : AstRewriter<Unit>() {
override fun visitTypeInt(node: Type.Int, ctx: Unit): AstNode = Type.Int4()
}
return rewriter.visitStatement(ast, Unit) as Statement
}
}
14 changes: 8 additions & 6 deletions src/main/kotlin/org/partiql/scribe/sql/SqlCalls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.partiql.ast.exprUnary
import org.partiql.ast.identifierSymbol
import org.partiql.ast.typeAny
import org.partiql.ast.typeBag
import org.partiql.ast.typeBigint
import org.partiql.ast.typeBlob
import org.partiql.ast.typeBool
import org.partiql.ast.typeChar
Expand All @@ -33,6 +34,7 @@ import org.partiql.ast.typeList
import org.partiql.ast.typeMissing
import org.partiql.ast.typeNullType
import org.partiql.ast.typeSexp
import org.partiql.ast.typeSmallint
import org.partiql.ast.typeString
import org.partiql.ast.typeStruct
import org.partiql.ast.typeSymbol
Expand Down Expand Up @@ -287,9 +289,9 @@ public abstract class SqlCalls {
PartiQLValueType.ANY -> typeAny()
PartiQLValueType.BOOL -> typeBool()
PartiQLValueType.INT8 -> typeInt()
PartiQLValueType.INT16 -> typeInt2()
PartiQLValueType.INT32 -> typeInt4()
PartiQLValueType.INT64 -> typeInt8()
PartiQLValueType.INT16 -> typeSmallint()
PartiQLValueType.INT32 -> typeInt()
PartiQLValueType.INT64 -> typeBigint()
PartiQLValueType.INT -> typeInt()
PartiQLValueType.DECIMAL_ARBITRARY -> typeDecimal(null, null)
PartiQLValueType.DECIMAL -> typeDecimal(null, null)
Expand Down Expand Up @@ -327,9 +329,9 @@ public abstract class SqlCalls {
PartiQLValueType.ANY -> typeAny()
PartiQLValueType.BOOL -> typeBool()
PartiQLValueType.INT8 -> error("unsupported")
PartiQLValueType.INT16 -> typeInt2()
PartiQLValueType.INT32 -> typeInt4()
PartiQLValueType.INT64 -> typeInt8()
PartiQLValueType.INT16 -> typeSmallint()
PartiQLValueType.INT32 -> typeInt()
PartiQLValueType.INT64 -> typeBigint()
PartiQLValueType.INT -> typeInt()
PartiQLValueType.DECIMAL_ARBITRARY -> typeDecimal(null, null)
PartiQLValueType.DECIMAL -> typeDecimal(typeArg0?.toInt(), typeArg1?.toInt())
Expand Down
10 changes: 8 additions & 2 deletions src/test/resources/inputs/operators/cast.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
--#[cast-00]
CAST('1' AS INT4);
CAST('1' AS INT);

--#[cast-01]
SELECT CAST('foo' AS STRING) AS s FROM T;
CAST('1' AS INT4);

--#[cast-02]
CAST('1' AS INT8);

--#[cast-03]
CAST('1' AS BIGINT);
16 changes: 10 additions & 6 deletions src/test/resources/outputs/partiql/basics/simple.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,26 @@ NOT ("default"."T1" IS NULL);
NOT ("default"."T1" IS MISSING);

--#[expr-14]
"default"."T1" IS INT2;
"default"."T1" IS SMALLINT;

--#[expr-15]
NOT ("default"."T1" IS INT2);
NOT ("default"."T1" IS SMALLINT);

--#[expr-16]
"default"."T1" IS INT4;
-- TODO USE INT as the default INT4 name.
"default"."T1" IS INT;

--#[expr-17]
NOT ("default"."T1" IS INT4);
-- TODO USE INT as the default INT4 name.
NOT ("default"."T1" IS INT);

--#[expr-18]
"default"."T1" IS INT8;
-- TODO USE BIGINT as the default BIGINT/INT8 name.
"default"."T1" IS BIGINT;

--#[expr-19]
NOT ("default"."T1" IS INT8);
-- TODO USE BIGINT as the default BIGINT/INT8 name.
NOT ("default"."T1" IS BIGINT);

--#[expr-20]
"default"."T1" IS INT;
Expand Down
10 changes: 8 additions & 2 deletions src/test/resources/outputs/partiql/operators/cast.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
--#[cast-00]
CAST('1' AS INT4);
CAST('1' AS INT);

--#[cast-01]
SELECT CAST('foo' AS STRING) AS "s" FROM "default"."T" AS "T";
CAST('1' AS INT);

--#[cast-02]
CAST('1' AS BIGINT);

--#[cast-03]
CAST('1' AS BIGINT);
10 changes: 8 additions & 2 deletions src/test/resources/outputs/redshift/operators/cast.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
--#[cast-00]
CAST('1' AS INT4);
CAST('1' AS INT);

--#[cast-01]
SELECT CAST('foo' AS VARCHAR) AS "s" FROM "default"."T" AS "T";
CAST('1' AS INT);

--#[cast-02]
CAST('1' AS BIGINT);

--#[cast-03]
CAST('1' AS BIGINT);
14 changes: 10 additions & 4 deletions src/test/resources/outputs/spark/operators/cast.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
----#[cast-00]
--CAST('1' AS INT4);
-- #[cast-00]
-- SELECT CAST('1' AS INT);

--#[cast-01]
SELECT CAST('foo' AS STRING) AS `s` FROM `default`.`T` AS `T`;
-- #[cast-01]
-- SELECT CAST('1' AS INT);

-- #[cast-02]
-- SELECT CAST('1' AS BIGINT);

-- #[cast-03]
-- SELECT CAST('1' AS BIGINT);
12 changes: 9 additions & 3 deletions src/test/resources/outputs/trino/operators/cast.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
----#[cast-00]
--CAST('1' AS INT4);
--#[cast-00]
CAST('1' AS INT);

--#[cast-01]
SELECT CAST('foo' AS VARCHAR) AS "s" FROM "default"."T" AS "T";
CAST('1' AS INT);

--#[cast-02]
CAST('1' AS BIGINT);

--#[cast-03]
CAST('1' AS BIGINT);