Uploaded image for project: 'Hive'
  1. Hive
  2. HIVE-179

SUBSTR function should work like other databases

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • None
    • 0.3.0
    • Query Processor
    • None
    • Incompatible change, Reviewed
    • SUBSTR position now starts at 1, not 0. A negative position counts backwards from the end of the string, starting at -1. Position 0 is treated as position 1.

    Description

      Positions start at 1, not 0. Negative positions start at the end of the string and count backwards.

      Oracle returns null for lengths less than 1 or non-existent substrings (any empty strings are null). MySQL and PostgreSQL return empty strings, never null. PostgreSQL errors for negative lengths. I suggest we follow the MySQL behavior.

      Oracle treats position 0 the same as 1. Perhaps we should too?

       SUBSTR('ABCDEFG',3,4): CDEF
      SUBSTR('ABCDEFG',-5,4): CDEF
         SUBSTR('ABCDEFG',3): CDEFG
        SUBSTR('ABCDEFG',-5): CDEFG
           SUBSTR('ABC',1,1): A
      
      MySQL:
           SUBSTR('ABC',0,1): <empty>
           SUBSTR('ABC',0,2): <empty>
           SUBSTR('ABC',1,0): <empty>
          SUBSTR('ABC',1,-1): <empty>
      
      Oracle:
           SUBSTR('ABC',0,1): A
           SUBSTR('ABC',0,2): AB
           SUBSTR('ABC',1,0): <null>
          SUBSTR('ABC',1,-1): <null>
      

      Attachments

        1. hive-substr.patch
          82 kB
          David Phillips

        Issue Links

          Activity

            People

              electrum David Phillips
              electrum David Phillips
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: