Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Function to get a collection element by index: Handling zero index as argument

I have a custom member function that gets a varray collection element by index (as an experiment [link]).
CREATE TYPE BODY my_sdo_geom_type AS MEMBER FUNCTION GetOrdinates( self IN my_sdo_geom_type, idx IN NUMBER ) RETURN NUMBER IS BEGIN IF idx BETWEEN 1 AND shape.sdo_ordinates.COUNT THEN return shape.sdo_ordinates(idx); ELSIF -idx BETWEEN 1 AND shape.sdo_ordinates.COUNT THEN RETURN shape.sdo_ordinates(shape.sdo_ordinates.COUNT + 1 + idx); ELSE RETURN NULL; --Handles the case where the argument is zero, END IF; END; END; / insert into lines (my_sdo_geom_col) values (my_sdo_geom_type(sdo_geometry('linestring(10 20, 30 40, 50 60)'))); select (my_sdo_geom_col).GetOrdinates( 1) as first_ordinate, (my_sdo_geom_col).GetOrdinates(-1) as last_ordinate, (my_sdo_geom_col).GetOrdinates( 0) as zero_ordinate from lines FIRST_ORDINATE LAST_ORDINATE ZERO_ORDINATE -------------- ------------- ------------- 10 60 null
Full script: db<>fiddle
Source: https://stackoverflow.com/a/72884698/5576771
The member function accepts:
- Positive numbers. Example:
1
returns the first element. - Negative numbers. Example:
-1
returns the last element. 0
returns null.
That approach to handling a zero argument seems intuitive to me.
With that said, the null-approach is inconsistent with other functionality I've seen in Oracle functions.
For example, sdo_util.get_coordinate(shape, 0)
will return the last vertex, not null:
with data (shape) as ( select sdo_geometry('linestring(10 20, 30 40, 50 60)') from dual) select json_object(sdo_util.get_coordinate(shape, 0)) as vertex_zero from data --Returns the last vertex. VERTEX_ZERO ------------------------------------------------------------------------------------------------------------- {"SDO_GTYPE":2001,"SDO_SRID":null,"SDO_POINT":{"X":50,"Y":60,"Z":null},"SDO_ELEM_INFO":[],"SDO_ORDINATES":[]} -- ^^ ^^ -- The coordinates of the last vertex.
Question:
Is one approach more correct than the other? Should an argument of zero return null or the last element? Or something else, such as an error?
Thanks.
Best Answer
-
Personally, I would either raise an exception (ORA-06532: Subscript outside of limit) or return null for index = 0. Returning the last varray element makes zero sense to me, here. Mapping negatives as an offset from the end makes sense in iteration scenarios.
So I concur with your solution.
Answers
-
Personally, I would either raise an exception (ORA-06532: Subscript outside of limit) or return null for index = 0. Returning the last varray element makes zero sense to me, here. Mapping negatives as an offset from the end makes sense in iteration scenarios.
So I concur with your solution.