This is an automated email from the ASF dual-hosted git repository.

libenchao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new b57f2d2cb0 [CALCITE-5905] Documentation for CREATE TYPE is incorrect
b57f2d2cb0 is described below

commit b57f2d2cb0fa1227ce17e4be872982c05f260249
Author: Mihai Budiu <[email protected]>
AuthorDate: Tue Sep 5 22:01:38 2023 -0700

    [CALCITE-5905] Documentation for CREATE TYPE is incorrect
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../java/org/apache/calcite/test/ServerTest.java   | 52 ++++++++++++++++++++++
 site/_docs/reference.md                            | 34 +++++++-------
 .../apache/calcite/test/schemata/hr/HrSchema.java  |  4 +-
 3 files changed, 71 insertions(+), 19 deletions(-)

diff --git a/server/src/test/java/org/apache/calcite/test/ServerTest.java 
b/server/src/test/java/org/apache/calcite/test/ServerTest.java
index cc5fb2a4f7..3c9180970c 100644
--- a/server/src/test/java/org/apache/calcite/test/ServerTest.java
+++ b/server/src/test/java/org/apache/calcite/test/ServerTest.java
@@ -39,6 +39,7 @@ import org.apache.calcite.sql.ddl.SqlTruncateTable;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
+import java.math.BigDecimal;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
@@ -144,6 +145,57 @@ class ServerTest {
     }
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5905";>[CALCITE-5905]
+   * Documentation for CREATE TYPE is incorrect</a>. */
+  @Test void testCreateTypeDocumentationExample() throws SQLException {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      boolean b = s.execute("CREATE TYPE address_typ AS (\n"
+          + "   street          VARCHAR(30),\n"
+          + "   city            VARCHAR(20),\n"
+          + "   state           CHAR(2),\n"
+          + "   postal_code     VARCHAR(6))");
+      assertThat(b, is(false));
+      b = s.execute("CREATE TYPE employee_typ AS (\n"
+          + "  employee_id       DECIMAL(6),\n"
+          + "  first_name        VARCHAR(20),\n"
+          + "  last_name         VARCHAR(25),\n"
+          + "  email             VARCHAR(25),\n"
+          + "  phone_number      VARCHAR(20),\n"
+          + "  hire_date         DATE,\n"
+          + "  job_id            VARCHAR(10),\n"
+          + "  salary            DECIMAL(8,2),\n"
+          + "  commission_pct    DECIMAL(2,2),\n"
+          + "  manager_id        DECIMAL(6),\n"
+          + "  department_id     DECIMAL(4),\n"
+          + "  address           address_typ)\n");
+      assertThat(b, is(false));
+      try (ResultSet r =
+               s.executeQuery("SELECT employee_typ(315, 'Francis', 'Logan', 
'FLOGAN',\n"
+                   + "    '555.777.2222', DATE '2004-05-01', 'SA_MAN', 11000, 
.15, 101, 110,\n"
+                   + "     address_typ('376 Mission', 'San Francisco', 'CA', 
'94222'))")) {
+        assertThat(r.next(), is(true));
+        Struct obj = r.getObject(1, Struct.class);
+        Object[] data = obj.getAttributes();
+        assertThat(data[0], is(315));
+        assertThat(data[1], is("Francis"));
+        assertThat(data[2], is("Logan"));
+        assertThat(data[3], is("FLOGAN"));
+        assertThat(data[4], is("555.777.2222"));
+        assertThat(data[5], is(java.sql.Date.valueOf("2004-05-01")));
+        assertThat(data[6], is("SA_MAN"));
+        assertThat(data[7], is(11000));
+        assertThat(data[8], is(new BigDecimal(".15")));
+        assertThat(data[9], is(101));
+        assertThat(data[10], is(110));
+        Struct address = (Struct) data[11];
+        assertArrayEquals(address.getAttributes(),
+            new Object[] { "376 Mission", "San Francisco", "CA", "94222" });
+      }
+    }
+  }
+
   @Test void testCreateType() throws Exception {
     try (Connection c = connect();
          Statement s = c.createStatement()) {
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 6cc2f03aa5..ccb79bc1f4 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -3503,24 +3503,24 @@ instantiated. Each object can hold different values.
 For example, we can declare types `address_typ` and `employee_typ`:
 
 {% highlight sql %}
-CREATE TYPE address_typ AS OBJECT (
-   street          VARCHAR2(30),
-   city            VARCHAR2(20),
+CREATE TYPE address_typ AS (
+   street          VARCHAR(30),
+   city            VARCHAR(20),
    state           CHAR(2),
-   postal_code     VARCHAR2(6));
-
-CREATE TYPE employee_typ AS OBJECT (
-  employee_id       NUMBER(6),
-  first_name        VARCHAR2(20),
-  last_name         VARCHAR2(25),
-  email             VARCHAR2(25),
-  phone_number      VARCHAR2(20),
+   postal_code     VARCHAR(6));
+
+CREATE TYPE employee_typ AS (
+  employee_id       DECIMAL(6),
+  first_name        VARCHAR(20),
+  last_name         VARCHAR(25),
+  email             VARCHAR(25),
+  phone_number      VARCHAR(20),
   hire_date         DATE,
-  job_id            VARCHAR2(10),
-  salary            NUMBER(8,2),
-  commission_pct    NUMBER(2,2),
-  manager_id        NUMBER(6),
-  department_id     NUMBER(4),
+  job_id            VARCHAR(10),
+  salary            DECIMAL(8,2),
+  commission_pct    DECIMAL(2,2),
+  manager_id        DECIMAL(6),
+  department_id     DECIMAL(4),
   address           address_typ);
 {% endhighlight %}
 
@@ -3528,6 +3528,6 @@ Using these types, you can instantiate objects as follows:
 
 {% highlight sql %}
 employee_typ(315, 'Francis', 'Logan', 'FLOGAN',
-    '555.777.2222', '01-MAY-04', 'SA_MAN', 11000, .15, 101, 110,
+    '555.777.2222', DATE '2004-05-01', 'SA_MAN', 11000, .15, 101, 110,
      address_typ('376 Mission', 'San Francisco', 'CA', '94222'))
 {% endhighlight %}
diff --git 
a/testkit/src/main/java/org/apache/calcite/test/schemata/hr/HrSchema.java 
b/testkit/src/main/java/org/apache/calcite/test/schemata/hr/HrSchema.java
index 82200c4c52..d1c2a65572 100644
--- a/testkit/src/main/java/org/apache/calcite/test/schemata/hr/HrSchema.java
+++ b/testkit/src/main/java/org/apache/calcite/test/schemata/hr/HrSchema.java
@@ -35,7 +35,7 @@ import java.util.Collections;
  * CREATE TABLE "emps" (
  *   "empid" INTEGER NOT NULL,
  *   "deptno" INTEGER NOT NULL,
- *   "name" VARCHAR2(10) NOT NULL,
+ *   "name" VARCHAR(10) NOT NULL,
  *   "salary" NUMBER(6, 2) NOT NULL,
  *   "commission" INTEGER);
  * INSERT INTO "emps" VALUES (100, 10, 'Bill', 10000, 1000);
@@ -45,7 +45,7 @@ import java.util.Collections;
  *
  * CREATE TABLE "depts" (
  *   "deptno" INTEGER NOT NULL,
- *   "name" VARCHAR2(10) NOT NULL,
+ *   "name" VARCHAR(10) NOT NULL,
  *   "employees" ARRAY OF "Employee",
  *   "location" "Location");
  * INSERT INTO "depts" VALUES (10, 'Sales', null, (-122, 38));

Reply via email to